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
40 changes: 40 additions & 0 deletions Samples/Senaprc.AI.Samples.Agents/AgentKeys.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoGen.Core;

namespace Senaprc.AI.Agents
{
public static class AgentKeys
{
public static string AgentKey1 = null;
public static string AgentKey2 = null;
public static string AgentKey3 = null;

public static Action<IAgent, IMessage, string> SendWechatMessage => async (agent, replyObject, message) =>
{
string key = null;
switch (agent.Name)
{
case "行政主管":
key = AgentKeys.AgentKey1;
break;
case "产品经理":
key = AgentKeys.AgentKey2;
break;
case "项目经理":
key = AgentKeys.AgentKey3;
break;
default:
break;
}

if (key != null)
{
await Senparc.Weixin.Work.AdvancedAPIs.Webhook.WebhookApi.SendTextAsync(key, message);
}
};
}
}
186 changes: 186 additions & 0 deletions Samples/Senaprc.AI.Samples.Agents/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// See https://aka.ms/new-console-template for more information

using AutoGen;
using AutoGen.Core;
using AutoGen.Mistral;
using AutoGen.SemanticKernel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.Web;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using Senaprc.AI.Agents;
using Senaprc.AI.Agents.AgentExtensions;
using Senaprc.AI.Agents.AgentUtility;
using Senparc.AI;
using Senparc.AI.Entities;
using Senparc.AI.Kernel;
using Senparc.AI.Kernel.Handlers;
using Senparc.AI.Samples.Agents;
using Senparc.CO2NET;
using Senparc.CO2NET.RegisterServices;


var configBuilder = new ConfigurationBuilder();
var appsettingsJsonFileName = SampleHelper.GetAppSettingsFile();
configBuilder.AddJsonFile(appsettingsJsonFileName, false, false);

Console.WriteLine("完成 appsettings.json 添加");
var config = configBuilder.Build();

#region 初始化 Senparc 基座

var senparcSetting = new SenparcSetting();
config.GetSection("SenparcSetting").Bind(senparcSetting);

var services = new ServiceCollection();

services.AddSenparcGlobalServices(config)
.AddSenparcAI(config);


Console.WriteLine("完成 ServiceCollection 和 ConfigurationBuilder 初始化");
#endregion

#region 初始化模型配置

var setting = (SenparcAiSetting)Senparc.AI.Config.SenparcAiSetting;//也可以留空,将自动获取

var _semanticAiHandler = new SemanticAiHandler(setting);

var parameter = new PromptConfigParameter()
{
MaxTokens = 2000,
Temperature = 0.7,
TopP = 0.5,
};

var iWantToRunConfig = _semanticAiHandler.IWantTo(setting)
.ConfigModel(ConfigModel.Chat, "JeffreySu");


var bingSearchAPIKey = config.GetSection("BingSearchAPIKey").Value;

var bingSearch = new BingConnector(bingSearchAPIKey);
var webSearchPlugin = new WebSearchEnginePlugin(bingSearch);
iWantToRunConfig.Kernel.Plugins.AddFromObject(webSearchPlugin);

var iWantToRun = iWantToRunConfig.BuildKernel();
var kernel = iWantToRun.Kernel;

#endregion

#region 企业微信用的 AgentKey

AgentKeys.AgentKey1 = config.GetSection("AgentKey1").Value;
AgentKeys.AgentKey2 = config.GetSection("AgentKey2").Value;
AgentKeys.AgentKey3 = config.GetSection("AgentKey3").Value;

#endregion

// Create the Administrtor
var administrator = new SemanticKernelAgent(
kernel: kernel,
name: "行政主管",
systemMessage: """
你是行政主管,你拥有制定合同、确认项目在公司层面的风险等责任。你正在处理一个项目需求的梳理,并且需要答复客户可行性,以及最终可能的合同条款。
你可以向你的下属提问,以获得你想要的答案,并按照要求总结后进行回复。

请注意:每一项决策都必须至少通过产品经理、项目经理的反馈和确认,才能最终给出最终的回复。

这些是你的下属:
- 产品经理:产品经理负责产品的功能设计和产品的功能规划。
- 项目经理: 项目经理负责所有项目的开发任务的安排和功能可行性的评估。
""")
.RegisterTextMessageConnector()
.RegisterCustomPrintMessage(new PrintWechatMessageMiddleware(AgentKeys.SendWechatMessage));

// Create the Product Manager
var productManager = new SemanticKernelAgent(
kernel: kernel,
name: "产品经理",
systemMessage: """
你是产品经理,你向行政主管负责,并且负责回答他的所有问题。

为了确保你有最新的信息,你可以使用网络搜索插件在回答问题之前在网上搜索信息,也可以根据你的经验,按照要求回答问题,并作出相对应的规划和决策。
""")
.RegisterTextMessageConnector()
.RegisterCustomPrintMessage(new PrintWechatMessageMiddleware(AgentKeys.SendWechatMessage));

// Create the Project Manager
var projectManager = new SemanticKernelAgent(
kernel: kernel,
name: "项目经理",
systemMessage: """
你是项目经理,你向行政主管负责,并且负责回答他的所有问题。
产品经理可能会告诉你项目的规划方案,此时你需要对其进行评估,并安排对应的开发任务。

为了确保你有最新的信息,你可以使用网络搜索插件在回答问题之前在网上搜索信息,也可以根据你的经验,按照要求回答问题,并作出相对应的规划和决策。

当安排项目开发任务时,你的任务安排中需要包含这些人员的名字具体的任务安排,任务需要细分到具体的开发内容而不仅仅是功能点
""")
.RegisterTextMessageConnector()
.RegisterCustomPrintMessage(new PrintWechatMessageMiddleware(AgentKeys.SendWechatMessage));


// Create the hearing member
var hearingMember = new UserProxyAgent(name: "BA");

// Create the group admin
var admin = new SemanticKernelAgent(
kernel: kernel,
name: "admin",
systemMessage: "你是群管理员.")
.RegisterTextMessageConnector();

// Create the AI team
// define the transition among group members
// we only allow the following transitions:
// hearingMember -> 行政
// 行政 -> 项目经理
// 行政 -> 产品经理
// 项目经理 -> 行政
// 产品经理 -> 行政
// 行政 -> hearingMember

#region 旧方法

//var hearingMember2Administrator = Transition.Create(hearingMember, administrator);

//var admin2ProjectManager = Transition.Create(administrator, projectManager);
//var admin2ProductManager = Transition.Create(administrator, productManager);
//var projectManager2Administrator = Transition.Create(projectManager, administrator);
//var productManager2Administrator = Transition.Create(productManager, administrator);

//var administrator2HearingMember = Transition.Create(administrator, hearingMember);

//var graph = new Graph([hearingMember2Administrator,
// admin2ProjectManager,
// admin2ProductManager,
// projectManager2Administrator,
// productManager2Administrator,
// administrator2HearingMember]);

//var aiTeam = new GroupChat(
// members: graphConnect.Agents.Values,
// admin: admin,
// workflow: graphConnect.Graph);

#endregion

var graphConnector = GraphBuilder.Start()
.ConnectFrom(hearingMember).TwoWay(administrator)
.ConnectFrom(administrator).TwoWay(projectManager).AlsoTwoWay(productManager)
.Finish();

var aiTeam = graphConnector.CreateAiTeam(admin);

// start the chat
// generate a greeting message to hearing member from Administrator
var greetingMessage = await administrator.SendAsync("你好,如果已经就绪,请告诉我们“已就位”,并和 BA 打个招呼");

await administrator.SendMessageToGroupAsync(
groupChat: aiTeam,
chatHistory: [greetingMessage],
maxRound: 20);

35 changes: 35 additions & 0 deletions Samples/Senaprc.AI.Samples.Agents/SampleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Senparc.AI.Samples.Agents
{
public static class SampleHelper
{
//public static string Default_Chat_ModeName = "gpt-35-turbo";//"chatglm2";//"gpt-35-turbo";//gpt-4-1106
//public static string Default_TextCompletion_ModeName = "text-davinci-003";//gpt-4-1106
//public static string Default_TextEmbedding_ModeName = "text-embedding-ada-002";
//public static string Default_TextCompletion_ModeName = "chatglm2";
//public static string Default_TextEmbedding_ModeName = "chatglm2";

/// <summary>
/// Get AppSettings file name.
/// </summary>
/// <returns></returns>
public static string GetAppSettingsFile()
{
if (File.Exists("appsettings.Development.json"))
{
Console.WriteLine("use appsettings.Development.json");
return "appsettings.Development.json";
}

Console.WriteLine("use appsettings.json");

return "appsettings.json";
}

}
}
45 changes: 45 additions & 0 deletions Samples/Senaprc.AI.Samples.Agents/Senaprc.AI.Samples.Agents.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<NoWarn>SKEXP0050;</NoWarn>
</PropertyGroup>

<ItemGroup>
<Compile Remove="bak\**" />
<EmbeddedResource Remove="bak\**" />
<None Remove="bak\**" />
</ItemGroup>

<ItemGroup>
<None Remove="appsettings.Development.json" />
<None Remove="appsettings.json" />
</ItemGroup>

<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="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" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Senparc.AI.Agents\Senparc.AI.Agents.csproj" />
<ProjectReference Include="..\..\src\Senparc.AI.Kernel\Senparc.AI.Kernel.csproj" />
<ProjectReference Include="..\..\src\Senparc.AI\Senparc.AI.csproj" />
</ItemGroup>

</Project>
Loading