fix: 修复agent报错问题
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
namespace Yi.Framework.AiHub.Domain.Shared.Attributes;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
|
||||||
|
public class YiAgentToolAttribute:Attribute
|
||||||
|
{
|
||||||
|
public YiAgentToolAttribute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public YiAgentToolAttribute(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ using Yi.Framework.AiHub.Application.Contracts.Dtos.Chat;
|
|||||||
using Yi.Framework.AiHub.Domain.AiGateWay;
|
using Yi.Framework.AiHub.Domain.AiGateWay;
|
||||||
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||||||
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Attributes;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||||
@@ -49,18 +50,18 @@ public class ChatManager : DomainService
|
|||||||
_aiGateWayManager = aiGateWayManager;
|
_aiGateWayManager = aiGateWayManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// agent流式对话
|
/// agent流式对话
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="httpContext"></param>
|
/// <param name="httpContext"></param>
|
||||||
/// <param name="sessionId"></param>
|
/// <param name="sessionId"></param>
|
||||||
/// <param name="content"></param>
|
/// <param name="content"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <param name="tokenId"></param>
|
/// <param name="tokenId"></param>
|
||||||
/// <param name="modelId"></param>
|
/// <param name="modelId"></param>
|
||||||
/// <param name="userId"></param>
|
/// <param name="userId"></param>
|
||||||
/// <param name="tools"></param>
|
/// <param name="tools"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
public async Task AgentCompleteChatStreamAsync(HttpContext httpContext,
|
public async Task AgentCompleteChatStreamAsync(HttpContext httpContext,
|
||||||
Guid sessionId,
|
Guid sessionId,
|
||||||
string content,
|
string content,
|
||||||
@@ -135,7 +136,9 @@ public class ChatManager : DomainService
|
|||||||
var toolContents = GetTools();
|
var toolContents = GetTools();
|
||||||
var chatOptions = new ChatOptions()
|
var chatOptions = new ChatOptions()
|
||||||
{
|
{
|
||||||
Tools = toolContents.Where(x=>tools.Contains(x.Code)).Select(x => (AITool)x.Tool).ToList(),
|
Tools = toolContents
|
||||||
|
.Where(x => tools.Contains(x.Code))
|
||||||
|
.Select(x => (AITool)x.Tool).ToList(),
|
||||||
ToolMode = ChatToolMode.Auto
|
ToolMode = ChatToolMode.Auto
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -244,7 +247,7 @@ public class ChatManager : DomainService
|
|||||||
public List<(string Code, string Name, AIFunction Tool)> GetTools()
|
public List<(string Code, string Name, AIFunction Tool)> GetTools()
|
||||||
{
|
{
|
||||||
var toolClasses = typeof(YiFrameworkAiHubDomainModule).Assembly.GetTypes()
|
var toolClasses = typeof(YiFrameworkAiHubDomainModule).Assembly.GetTypes()
|
||||||
.Where(x => x.GetCustomAttribute<McpServerToolTypeAttribute>() is not null)
|
.Where(x => x.GetCustomAttribute<YiAgentToolAttribute>() is not null)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
List<(string Code, string Name, AIFunction Tool)> mcpTools = new();
|
List<(string Code, string Name, AIFunction Tool)> mcpTools = new();
|
||||||
@@ -252,14 +255,15 @@ public class ChatManager : DomainService
|
|||||||
{
|
{
|
||||||
var instance = LazyServiceProvider.GetRequiredService(toolClass);
|
var instance = LazyServiceProvider.GetRequiredService(toolClass);
|
||||||
var toolMethods = toolClass.GetMethods()
|
var toolMethods = toolClass.GetMethods()
|
||||||
.Where(y => y.GetCustomAttribute<McpServerToolAttribute>() is not null).ToList();
|
.Where(y => y.GetCustomAttribute<YiAgentToolAttribute>() is not null).ToList();
|
||||||
foreach (var toolMethod in toolMethods)
|
foreach (var toolMethod in toolMethods)
|
||||||
{
|
{
|
||||||
var display = toolMethod.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName;
|
var display = toolMethod.GetCustomAttribute<YiAgentToolAttribute>()?.Name;
|
||||||
var tool = AIFunctionFactory.Create(toolMethod, instance);
|
var tool = AIFunctionFactory.Create(toolMethod, instance);
|
||||||
mcpTools.add((tool.Name, display, tool));
|
mcpTools.add((tool.Name, display, tool));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mcpTools;
|
return mcpTools;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using ModelContextProtocol.Server;
|
using ModelContextProtocol.Server;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Volo.Abp.DependencyInjection;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Attributes;
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Domain.Mcp;
|
namespace Yi.Framework.AiHub.Domain.Mcp;
|
||||||
|
|
||||||
[McpServerToolType]
|
[YiAgentTool]
|
||||||
public class DeepThinkTool:ISingletonDependency
|
public class DeepThinkTool:ISingletonDependency
|
||||||
{
|
{
|
||||||
[McpServerTool, Description("进行深度思考"),DisplayName("深度思考")]
|
[YiAgentTool("深度思考"),DisplayName("DeepThink"),Description("进行深度思考")]
|
||||||
public void DeepThink()
|
public void DeepThink()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using ModelContextProtocol.Server;
|
using ModelContextProtocol.Server;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Volo.Abp.DependencyInjection;
|
||||||
|
using Yi.Framework.AiHub.Domain.Shared.Attributes;
|
||||||
|
|
||||||
namespace Yi.Framework.AiHub.Domain.Mcp;
|
namespace Yi.Framework.AiHub.Domain.Mcp;
|
||||||
|
|
||||||
[McpServerToolType]
|
[YiAgentTool]
|
||||||
public class OnlineSearchTool:ISingletonDependency
|
public class OnlineSearchTool:ISingletonDependency
|
||||||
{
|
{
|
||||||
[McpServerTool, Description("进行在线搜索"),DisplayName("在线搜索")]
|
[YiAgentTool("联网搜索"),DisplayName("OnlineSearch"), Description("进行在线搜索")]
|
||||||
public string OnlineSearch(string keyword)
|
public string OnlineSearch(string keyword)
|
||||||
{
|
{
|
||||||
return "奥德赛第一中学学生会会长是:郭老板";
|
return "奥德赛第一中学学生会会长是:郭老板";
|
||||||
|
|||||||
Reference in New Issue
Block a user