fix: 修复agent报错问题

This commit is contained in:
ccnetcore
2025-12-24 22:51:18 +08:00
parent ee4cb20eef
commit 7495dc86a0
4 changed files with 42 additions and 20 deletions

View File

@@ -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; }
}

View File

@@ -16,6 +16,7 @@ using Yi.Framework.AiHub.Application.Contracts.Dtos.Chat;
using Yi.Framework.AiHub.Domain.AiGateWay;
using Yi.Framework.AiHub.Domain.Entities.Chat;
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.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
@@ -49,18 +50,18 @@ public class ChatManager : DomainService
_aiGateWayManager = aiGateWayManager;
}
/// <summary>
/// agent流式对话
/// </summary>
/// <param name="httpContext"></param>
/// <param name="sessionId"></param>
/// <param name="content"></param>
/// <param name="token"></param>
/// <param name="tokenId"></param>
/// <param name="modelId"></param>
/// <param name="userId"></param>
/// <param name="tools"></param>
/// <param name="cancellationToken"></param>
/// <summary>
/// agent流式对话
/// </summary>
/// <param name="httpContext"></param>
/// <param name="sessionId"></param>
/// <param name="content"></param>
/// <param name="token"></param>
/// <param name="tokenId"></param>
/// <param name="modelId"></param>
/// <param name="userId"></param>
/// <param name="tools"></param>
/// <param name="cancellationToken"></param>
public async Task AgentCompleteChatStreamAsync(HttpContext httpContext,
Guid sessionId,
string content,
@@ -135,7 +136,9 @@ public class ChatManager : DomainService
var toolContents = GetTools();
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
};
@@ -244,7 +247,7 @@ public class ChatManager : DomainService
public List<(string Code, string Name, AIFunction Tool)> GetTools()
{
var toolClasses = typeof(YiFrameworkAiHubDomainModule).Assembly.GetTypes()
.Where(x => x.GetCustomAttribute<McpServerToolTypeAttribute>() is not null)
.Where(x => x.GetCustomAttribute<YiAgentToolAttribute>() is not null)
.ToList();
List<(string Code, string Name, AIFunction Tool)> mcpTools = new();
@@ -252,14 +255,15 @@ public class ChatManager : DomainService
{
var instance = LazyServiceProvider.GetRequiredService(toolClass);
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)
{
var display = toolMethod.GetCustomAttribute<DisplayNameAttribute>()?.DisplayName;
var display = toolMethod.GetCustomAttribute<YiAgentToolAttribute>()?.Name;
var tool = AIFunctionFactory.Create(toolMethod, instance);
mcpTools.add((tool.Name, display, tool));
}
}
return mcpTools;
}

View File

@@ -1,13 +1,14 @@
using System.ComponentModel;
using ModelContextProtocol.Server;
using Volo.Abp.DependencyInjection;
using Yi.Framework.AiHub.Domain.Shared.Attributes;
namespace Yi.Framework.AiHub.Domain.Mcp;
[McpServerToolType]
[YiAgentTool]
public class DeepThinkTool:ISingletonDependency
{
[McpServerTool, Description("进行深度思考"),DisplayName("深度思考")]
[YiAgentTool("深度思考"),DisplayName("DeepThink"),Description("进行深度思考")]
public void DeepThink()
{

View File

@@ -1,13 +1,14 @@
using System.ComponentModel;
using ModelContextProtocol.Server;
using Volo.Abp.DependencyInjection;
using Yi.Framework.AiHub.Domain.Shared.Attributes;
namespace Yi.Framework.AiHub.Domain.Mcp;
[McpServerToolType]
[YiAgentTool]
public class OnlineSearchTool:ISingletonDependency
{
[McpServerTool, Description("进行在线搜索"),DisplayName("在线搜索")]
[YiAgentTool("联网搜索"),DisplayName("OnlineSearch"), Description("进行在线搜索")]
public string OnlineSearch(string keyword)
{
return "奥德赛第一中学学生会会长是:郭老板";