Files
Yi.Framework/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/ChatManager.cs
chenchun dfc143379f fix: 调整 OpenAI 客户端配置并更新在线搜索返回值
- ChatManager.cs
  - 添加/调整相关 using 引用,修正 modelId 为 "gpt-5.2",并更新 agent 创建方式以匹配当前 SDK/服务端使用。
  - 保留代理示例注释(HttpClient.DefaultProxy)。
- OnlineSearchTool.cs
  - 将占位返回值 "xxx" 替换为示例查询结果 "奥德赛第一中学学生会会长是:郭老板"。

简短修正以确保与服务端模型命名及功能返回一致。
2025-12-23 17:40:00 +08:00

109 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.ClientModel;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Reflection;
using System.Text.Json;
using Dm.util;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using OpenAI;
using OpenAI.Chat;
using OpenAI.Responses;
using Volo.Abp.Domain.Services;
namespace Yi.Framework.AiHub.Domain.Managers;
public class ChatManager : DomainService
{
private readonly AiGateWayManager _aiGateWayManager;
private readonly ILoggerFactory _loggerFactory;
public ChatManager(AiGateWayManager aiGateWayManager, ILoggerFactory loggerFactory)
{
_aiGateWayManager = aiGateWayManager;
_loggerFactory = loggerFactory;
}
public async Task CompleteChatStreamAsync()
{
//token可以用户传进来
// HttpClient.DefaultProxy = new WebProxy("127.0.0.1:8888");
var modelId = "gpt-5.2";
var client = new OpenAIClient(new ApiKeyCredential("xxx"),
new OpenAIClientOptions
{
Endpoint = new Uri("https://yxai.chat/v1"),
});
var agent = client.GetChatClient(modelId)
.CreateAIAgent("你是一个专业的网页ai助手");
var thread = agent.GetNewThread();
var tools = GetTools();
var chatOptions = new ChatOptions()
{
Tools = tools.Select(x => (AITool)x).ToList(),
ToolMode = ChatToolMode.Auto
};
await foreach (var update in agent.RunStreamingAsync("联网搜索一下,奥德赛第一中学学生会会长是谁", thread,
new ChatClientAgentRunOptions(chatOptions)))
{
// 检查每个更新中的内容
foreach (var content in update.Contents)
{
switch (content)
{
case FunctionCallContent functionCall:
Console.WriteLine();
Console.WriteLine(
$"🔧 工具调用开始: {functionCall.CallId},{functionCall.Name},{functionCall.Arguments}");
break;
case FunctionResultContent functionResult:
Console.WriteLine();
Console.WriteLine($"✅ 工具调用完成: {functionResult.CallId}{functionResult.Result}");
break;
case TextContent textContent:
Console.Write($"{textContent.Text}");
break;
case UsageContent usageContent:
Console.WriteLine();
Console.WriteLine($"✅ 用量统计: {usageContent.Details.TotalTokenCount}");
break;
}
}
}
string serializedJson = thread.Serialize(JsonSerializerOptions.Web).GetRawText();
JsonElement reloaded = JsonSerializer.Deserialize<JsonElement>(serializedJson, JsonSerializerOptions.Web);
var newThread = agent.DeserializeThread(reloaded, JsonSerializerOptions.Web);
}
private List<AIFunction> GetTools()
{
var toolClasses = typeof(YiFrameworkAiHubDomainModule).Assembly.GetTypes()
.Where(x => x.GetCustomAttribute<McpServerToolTypeAttribute>() is not null)
.ToList();
List<AIFunction> mcpTools = new List<AIFunction>();
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(AIFunctionFactory.Create(toolMethod, instance));
}
}
return mcpTools;
}
}