Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@

<ItemGroup>

<Content Include="appsettings.Development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoGen" Version="0.0.13" />
<PackageReference Include="AutoGen" Version="0.0.17" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.11.1-alpha" />
<PackageReference Include="Senparc.Weixin.Work" Version="3.21.1" />
<PackageReference Include="Senparc.Weixin.Work.Middleware" Version="1.2.1" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.16.2-alpha" />
<PackageReference Include="Senparc.Weixin.Work" Version="3.22.1" />
<PackageReference Include="Senparc.Weixin.Work.Middleware" Version="1.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.OpenAI" Version="1.10.0" />
<PackageReference Include="Microsoft.SemanticKernel.Planners.Handlebars" Version="1.10.0-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.10.0-alpha" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.OpenAI" Version="1.16.2" />
<PackageReference Include="Microsoft.SemanticKernel.Planners.Handlebars" Version="1.16.2-preview" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.16.2-alpha" />
<!--<PackageReference Include="Microsoft.SemanticKernel.Planning.SequentialPlanner" Version="0.24.230912.2-preview" />-->
<ProjectReference Include="..\..\src\Senparc.AI.Kernel\Senparc.AI.Kernel.csproj" />
<ProjectReference Include="..\..\src\Senparc.AI\Senparc.AI.csproj" />
Expand Down
67 changes: 36 additions & 31 deletions src/Senparc.AI.Agents/AgentUtility/PrintWechatMessageMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public PrintWechatMessageMiddleware(Action<IAgent, IMessage, string>? sendMessag
this._sendMessageAction = sendMessageAction;
}

public async Task<IMessage> InvokeAsync(MiddlewareContext context, IAgent agent, CancellationToken cancellationToken = default(CancellationToken))
public async Task<IMessage> InvokeAsync(MiddlewareContext context, IAgent agent, CancellationToken cancellationToken = default)
{
if (agent is IStreamingAgent agent2)
{
Expand Down Expand Up @@ -53,62 +53,67 @@ public PrintWechatMessageMiddleware(Action<IAgent, IMessage, string>? sendMessag
return obj;
}

public async IAsyncEnumerable<IStreamingMessage> InvokeAsync(MiddlewareContext context, IStreamingAgent agent, [EnumeratorCancellation] CancellationToken cancellationToken = default(CancellationToken))
public async IAsyncEnumerable<IMessage> InvokeAsync(MiddlewareContext context, IStreamingAgent agent, CancellationToken cancellationToken)
{
IMessage recentUpdate = null;
await foreach (IStreamingMessage item in agent.GenerateStreamingReplyAsync(context.Messages, context.Options, cancellationToken))
IMessage? recentUpdate = null;
await foreach (var message in agent.GenerateStreamingReplyAsync(context.Messages, context.Options, cancellationToken))
{
if (item is TextMessageUpdate textMessageUpdate)
if (message is TextMessageUpdate textMessageUpdate)
{
if (recentUpdate == null)
if (recentUpdate is null)
{
Console.WriteLine("from: " + textMessageUpdate.From);
// Print from: xxx
Console.WriteLine($"from: {textMessageUpdate.From}");
recentUpdate = new TextMessage(textMessageUpdate);
Console.Write(textMessageUpdate.Content);
yield return item;
continue;

yield return message;
}
else if (recentUpdate is TextMessage recentTextMessage)
{
// Print the content of the message
Console.Write(textMessageUpdate.Content);
recentTextMessage.Update(textMessageUpdate);

if (!(recentUpdate is TextMessage textMessage))
yield return recentTextMessage;
}
else
{
throw new InvalidOperationException("The recent update is not a TextMessage");
}

Console.Write(textMessageUpdate.Content);
textMessage.Update(textMessageUpdate);
yield return textMessage;
}
else if (item is ToolCallMessageUpdate update)
else if (message is ToolCallMessageUpdate toolCallUpdate)
{
if (recentUpdate == null)
if (recentUpdate is null)
{
recentUpdate = new ToolCallMessage(update);
yield return item;
continue;
recentUpdate = new ToolCallMessage(toolCallUpdate);

yield return message;
}
else if (recentUpdate is ToolCallMessage recentToolCallMessage)
{
recentToolCallMessage.Update(toolCallUpdate);

if (!(recentUpdate is ToolCallMessage toolCallMessage))
yield return message;
}
else
{
throw new InvalidOperationException("The recent update is not a ToolCallMessage");
}
}
else if (message is IMessage imessage)
{
recentUpdate = imessage;

toolCallMessage.Update(update);
yield return item;
yield return imessage;
}
else
{
if (!(item is IMessage message))
{
throw new InvalidOperationException("The message is not a valid message");
}

recentUpdate = message;
yield return message;
throw new InvalidOperationException("The message is not a valid message");
}
}

Console.WriteLine();
if (recentUpdate != null && !(recentUpdate is TextMessage))
if (recentUpdate is not null && recentUpdate is not TextMessage)
{
Console.WriteLine(recentUpdate.FormatMessage());
}
Expand Down
79 changes: 38 additions & 41 deletions src/Senparc.AI.Agents/Senparc.AI.Agents.csproj
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.1.4</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<AssemblyName>Senparc.AI.Agents</AssemblyName>
<RootNamespace>Senparc.AI.Agents</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
<Description>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>0.1.5</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<AssemblyName>Senparc.AI.Agents</AssemblyName>
<RootNamespace>Senparc.AI.Agents</RootNamespace>
<GeneratePackageOnBuild Condition=" '$(Configuration)' == 'Release' ">true</GeneratePackageOnBuild>
<Description>
Senparc.AI.Agents - AI Agents(智能体),支持 AutoGen,可配置 Agent 进行协作
</Description>
<Copyright>Senparc Copyright © 2004~2024</Copyright>
<PackageTags>
<Copyright>Senparc Copyright © 2004~2024</Copyright>
<PackageTags>
Senparc.AI.Agents,Agents,C#,AIGC,GenAI,AGI,LLM,SemanticKernel
</PackageTags>
<Authors>Jeffrey Su</Authors>
<Owners>Senparc</Owners>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<ProjectUrl>https://github.com/Senparc/Senparc.AI.Agents</ProjectUrl>
<Title>Senparc.AI.Agents.dll</Title>
<Summary>Senparc.AI 核心模块</Summary>
<PackageProjectUrl>https://github.com/Senparc/Senparc.AI.Agents</PackageProjectUrl>
<PackageIcon>icon.jpg</PackageIcon>
<PackageReleaseNotes>
<Authors>Jeffrey Su</Authors>
<Owners>Senparc</Owners>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<ProjectUrl>https://github.com/Senparc/Senparc.AI.Agents</ProjectUrl>
<Title>Senparc.AI.Agents.dll</Title>
<Summary>Senparc.AI 核心模块</Summary>
<PackageProjectUrl>https://github.com/Senparc/Senparc.AI.Agents</PackageProjectUrl>
<PackageIcon>icon.jpg</PackageIcon>
<PackageReleaseNotes>
v0.1.0 创世

</PackageReleaseNotes>
<RepositoryUrl>https://github.com/Senparc/Senparc.AI.Agents</RepositoryUrl>
<Configurations>Debug;Release;Test</Configurations>
<NoWarn>SKEXP0001;SKEXP0010;SKEXP0050;SKEXP0070</NoWarn>

</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.1|AnyCPU'">
<OutputPath>..\..\BuildOutPut\</OutputPath>
<DocumentationFile>..\..\BuildOutPut\netstandard2.1\Senparc.AI.Agents.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\icon.jpg" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoGen" Version="0.0.13" />
<ProjectReference Include="..\Senparc.AI.Kernel\Senparc.AI.Kernel.csproj" />
</ItemGroup>

</Project>
<RepositoryUrl>https://github.com/Senparc/Senparc.AI.Agents</RepositoryUrl>
<Configurations>Debug;Release;Test</Configurations>
<NoWarn>SKEXP0001;SKEXP0010;SKEXP0050;SKEXP0070</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.1|AnyCPU'">
<OutputPath>..\..\BuildOutPut\</OutputPath>
<DocumentationFile>..\..\BuildOutPut\netstandard2.1\Senparc.AI.Agents.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<None Include="..\icon.jpg" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoGen" Version="0.0.17" />
<ProjectReference Include="..\Senparc.AI.Kernel\Senparc.AI.Kernel.csproj" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions src/Senparc.AI.Kernel/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public static class Config
/// 当前配置
/// </summary>
public static SenparcAiSetting SenparcAiSetting
=> Senparc.AI.Config.SenparcAiSetting as SenparcAiSetting;
=> (SenparcAiSetting)Senparc.AI.Config.SenparcAiSetting;

static Config() {
static Config()
{
Senparc.AI.Config.SenparcAiSetting ??= new SenparcAiSetting();
}

Expand Down
1 change: 0 additions & 1 deletion src/Senparc.AI.Kernel/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.TextGeneration;
using Microsoft.SemanticKernel;
using OllamaSharp;

namespace Senparc.AI.Kernel
{
Expand Down
98 changes: 49 additions & 49 deletions src/Senparc.AI.Kernel/Ollama/OllamaChatCompletionService.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Services;
using Microsoft.SemanticKernel.TextGeneration;

namespace Senparc.AI.Kernel.Ollama
{
public class OllamaChatCompletionService : IChatCompletionService, IAIService, ITextGenerationService
{
private readonly OllamaSharp.OllamaApiClient _core;

Dictionary<string, object?> _attributes = new Dictionary<string, object?>();

public IReadOnlyDictionary<string, object?> Attributes => _attributes;

public OllamaChatCompletionService(string url, string modelId)
{
_core = new OllamaSharp.OllamaApiClient(url, modelId);

_attributes["model"] = modelId;
}


public Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}
//using System;
//using System.Collections.Generic;
//using System.Text;
//using System.Threading;
//using System.Threading.Tasks;
//using Microsoft.SemanticKernel;
//using Microsoft.SemanticKernel.ChatCompletion;
//using Microsoft.SemanticKernel.Services;
//using Microsoft.SemanticKernel.TextGeneration;

//namespace Senparc.AI.Kernel.Ollama
//{
// public class OllamaChatCompletionService : IChatCompletionService, IAIService, ITextGenerationService
// {
// private readonly OllamaSharp.OllamaApiClient _core;

// Dictionary<string, object?> _attributes = new Dictionary<string, object?>();

// public IReadOnlyDictionary<string, object?> Attributes => _attributes;

// public OllamaChatCompletionService(string url, string modelId)
// {
// _core = new OllamaSharp.OllamaApiClient(url, modelId);

// _attributes["model"] = modelId;
// }


// public Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
// {
// throw new NotImplementedException();
// }

// public IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
// {
// throw new NotImplementedException();
// }

// public IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
// {
// throw new NotImplementedException();
// }

// public Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Microsoft.SemanticKernel.Kernel? kernel = null, CancellationToken cancellationToken = default)
// {
// throw new NotImplementedException();
// }
// }
//}
Loading