feat: 新增tools接口
This commit is contained in:
@@ -1,17 +1,23 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using Dm.util;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using ModelContextProtocol;
|
||||||
|
using ModelContextProtocol.Server;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Serialization;
|
using Newtonsoft.Json.Serialization;
|
||||||
using OpenAI.Chat;
|
using OpenAI.Chat;
|
||||||
using Volo.Abp.Application.Services;
|
using Volo.Abp.Application.Services;
|
||||||
using Volo.Abp.Users;
|
using Volo.Abp.Users;
|
||||||
using Yi.Framework.AiHub.Application.Contracts.Dtos;
|
using Yi.Framework.AiHub.Application.Contracts.Dtos;
|
||||||
|
using Yi.Framework.AiHub.Domain;
|
||||||
using Yi.Framework.AiHub.Domain.Entities;
|
using Yi.Framework.AiHub.Domain.Entities;
|
||||||
using Yi.Framework.AiHub.Domain.Entities.Model;
|
using Yi.Framework.AiHub.Domain.Entities.Model;
|
||||||
using Yi.Framework.AiHub.Domain.Extensions;
|
using Yi.Framework.AiHub.Domain.Extensions;
|
||||||
@@ -175,4 +181,26 @@ public class AiChatService : ApplicationService
|
|||||||
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
||||||
CurrentUser.Id, null, null, cancellationToken);
|
CurrentUser.Id, null, null, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("ai-chat/tool")]
|
||||||
|
public string GetTool()
|
||||||
|
{
|
||||||
|
var toolClasses = typeof(YiFrameworkAiHubDomainModule).Assembly.GetTypes()
|
||||||
|
.Where(x => x.GetCustomAttribute<McpServerToolTypeAttribute>() is not null)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
List<McpServerTool> mcpTools = new List<McpServerTool>();
|
||||||
|
foreach (var toolClass in toolClasses)
|
||||||
|
{
|
||||||
|
var instance = LazyServiceProvider.GetRequiredService(toolClass);
|
||||||
|
var toolMethods = toolClass.GetMethods()
|
||||||
|
.Where(y => y.GetCustomAttribute<McpServerToolAttribute>() is not null).ToList();
|
||||||
|
foreach (var toolMethod in toolMethods)
|
||||||
|
{
|
||||||
|
mcpTools.add(McpServerTool.Create(toolMethod, instance));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var json = System.Text.Json.JsonSerializer.Serialize(mcpTools.Select(x=>x.ProtocolTool).ToList(),McpJsonUtilities.DefaultOptions);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
|
using System.Text.Json;
|
||||||
|
using ModelContextProtocol;
|
||||||
|
using ModelContextProtocol.Server;
|
||||||
using Yi.Framework.AiHub.Application.Contracts;
|
using Yi.Framework.AiHub.Application.Contracts;
|
||||||
using Yi.Framework.AiHub.Domain;
|
using Yi.Framework.AiHub.Domain;
|
||||||
using Yi.Framework.Ddd.Application;
|
using Yi.Framework.Ddd.Application;
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
|
using ModelContextProtocol.Server;
|
||||||
|
using Volo.Abp.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Mcp;
|
||||||
|
|
||||||
|
[McpServerToolType]
|
||||||
|
public class DeepThinkTool:ISingletonDependency
|
||||||
|
{
|
||||||
|
[McpServerTool, Description("进行深度思考")]
|
||||||
|
public void DeepThink()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
|
using ModelContextProtocol.Server;
|
||||||
|
using Volo.Abp.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Yi.Framework.AiHub.Domain.Mcp;
|
||||||
|
|
||||||
|
[McpServerToolType]
|
||||||
|
public class OnlineSearchTool:ISingletonDependency
|
||||||
|
{
|
||||||
|
[McpServerTool, Description("进行在线搜索")]
|
||||||
|
public void OnlineSearch()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AlipayEasySDK" Version="2.1.3" />
|
<PackageReference Include="AlipayEasySDK" Version="2.1.3" />
|
||||||
<PackageReference Include="Azure.AI.OpenAI" Version="2.2.0-beta.4" />
|
<PackageReference Include="Azure.AI.OpenAI" Version="2.2.0-beta.4" />
|
||||||
|
<PackageReference Include="ModelContextProtocol.Core" Version="0.5.0-preview.1" />
|
||||||
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
<PackageReference Include="Volo.Abp.Ddd.Domain" Version="$(AbpVersion)" />
|
||||||
<PackageReference Include="Volo.Abp.DistributedLocking" Version="$(AbpVersion)" />
|
<PackageReference Include="Volo.Abp.DistributedLocking" Version="$(AbpVersion)" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user