Skip to content

鲸舰AI智能体

概述

由于微软官方的AIAgent仅支持.net8.0以上版本,故鲸舰封装一个系统智能体(使用微软内置改造),可通过与系统智能体对话,要求智能体做对应的系统操作。

支持操作:

  • 内置了一个系统智能体,默认情况下可使用该智能体(自定义智能体:后续根据情况添加)。
  • 在每个功能包中,可增加该功能包的特有插件工具,并在OnMounted中挂载插件。

不支持:

  • 暂时不支持流式输出。
  • 暂不支持从Configuration获取配置。
  • 仅测试了deppeseek,其他模型暂未测试。

快速开始

C#
// 注册智能体可用的工具类
public override void OnMounted(IApplication application, IServiceProvider serviceProvider)
{
    // 获取智能体工厂
    var factory = serviceProvider.GetRequiredService<IAIAgentFactory>();
    var builder =  factory.CreateAgentBuilder();
    
    // 加入AI工具(每个功能包均可加入自己功能包的工具,比如套餐工具,商品工具等)
    builder.AddPlugins(serviceProvider,typeof(MathPlugin));
    
    factory.SetDefaultAgent(builder.Build());
}

// 用agent进行对话
var session = agent.NewSession();
var result = await agent.ChatAsync(session.SessionId, input);

定义工具类

在功能包根目录,创建AIPlugins文件夹, 定义对应的插件。

基础参数插件

C#
[AIPlugin(nameof(MathPlugin), "用于各种数学计算")]
public class MathPlugin
{
    [AITool("multiply", "计算两个数的乘积")]
    public double Multiply(
        [Description("第一个数字")] double a,
        [Description("第二个数字")] double b)
    {
        return a * b;
    }

    [AITool("add", "计算两个数的和'")]
    public double Add(
        [Description("第一个数字")] double a,
        [Description("第二个数字")] double b)
    {
        return a + b;
    }
}

复杂参数插件

c#
[AIPlugin("PersonInfoPlugin", "记录与查询人员档案信息")]
public class ObjectParamPlugin
{
    [AIToolMethod("Log", "记录人员档案")]
    public void Log(
        [AIToolParameter("人员信息")] Person person,
        [AIToolParameter("记录日期")] DateTime date)
    {
        Console.WriteLine(person?.ToJsonString() + "|" + date);
    }
}

public class Person
{
    [AIToolParameter("姓名")]
    public string Name { get; set; }

    [AIToolParameter("年龄")]
    public int Age { get; set; }
}

两种配置方式

方式一

json
{
  "AIAgent": {
    "ModelId": "deepseek-chat",
    "ApiKey": "sk-******",
    "Endpoint": "https://api.deepseek.com",
    "AgentName": "系统运营助手",
    "AgentInstruction": "你是鲸舰ERP系统的助手,可以使用可用的函数来帮助用户。不要回答你不知道的事情。"
  }
}

方式二

C#
public override void OnMounted(IApplication application, IServiceProvider serviceProvider)
{
    // 获取智能体工厂
    var factory = serviceProvider.GetRequiredService<IAIAgentFactory>();
    var builder =  factory.CreateAgentBuilder();
    
    // 该设置,仅在AI功能包里传一次,其他功能包不需要
    builder.SetOptions(new AIOptions()
            {
                ModelId = "deepseek-chat",
                ApiKey = "sk-xxxxxx", // 当前支持deepseek,其他未测试
                Endpoint = "https://api.deepseek.com",
                AgentName = "系统运营助手",
                AgentInstruction = "你是鲸舰ERP系统的助手,可以使用可用的函数来帮助用户。不要回答你不知道的事情。",
            });
            
    // 其他代码......
}

广州宝点数字化科技