feat: 新增功能
- ChatManager:
- 引入 System.Text.Json,用于将 agent thread 序列化与反序列化(示例:thread.Serialize(...) -> JsonSerializer.Deserialize -> agent.DeserializeThread)。
- 增加示例:创建 OpenAIClient、初始化 agent、运行流式响应并处理更新。
- 小幅格式和空行调整。
- AiChatService:
- 为 Agent 发送接口 PostAgentSendAsync 增加注释与路由标记 HttpPost("ai-chat/agent/send")。
注意:提交中出现了硬编码的 API Key,请尽快改为从配置或机密管理中读取以防泄露。
This commit is contained in:
@@ -148,6 +148,9 @@ public class AiChatService : ApplicationService
|
|||||||
// 使用 ChatManager
|
// 使用 ChatManager
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Agent 发送消息
|
||||||
|
/// </summary>
|
||||||
[HttpPost("ai-chat/agent/send")]
|
[HttpPost("ai-chat/agent/send")]
|
||||||
public async Task PostAgentSendAsync()
|
public async Task PostAgentSendAsync()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.ClientModel;
|
using System.ClientModel;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text.Json;
|
||||||
using Dm.util;
|
using Dm.util;
|
||||||
using Microsoft.Agents.AI;
|
using Microsoft.Agents.AI;
|
||||||
using Microsoft.Extensions.AI;
|
using Microsoft.Extensions.AI;
|
||||||
@@ -15,6 +16,7 @@ public class ChatManager : DomainService
|
|||||||
{
|
{
|
||||||
private readonly AiGateWayManager _aiGateWayManager;
|
private readonly AiGateWayManager _aiGateWayManager;
|
||||||
private readonly ILoggerFactory _loggerFactory;
|
private readonly ILoggerFactory _loggerFactory;
|
||||||
|
|
||||||
public ChatManager(AiGateWayManager aiGateWayManager, ILoggerFactory loggerFactory)
|
public ChatManager(AiGateWayManager aiGateWayManager, ILoggerFactory loggerFactory)
|
||||||
{
|
{
|
||||||
_aiGateWayManager = aiGateWayManager;
|
_aiGateWayManager = aiGateWayManager;
|
||||||
@@ -26,15 +28,16 @@ public class ChatManager : DomainService
|
|||||||
//token可以用户传进来
|
//token可以用户传进来
|
||||||
// HttpClient.DefaultProxy = new WebProxy("127.0.0.1:8888");
|
// HttpClient.DefaultProxy = new WebProxy("127.0.0.1:8888");
|
||||||
var modelId = "gpt-5.2-chat";
|
var modelId = "gpt-5.2-chat";
|
||||||
var client = new OpenAIClient(new ApiKeyCredential("xxx"), new OpenAIClientOptions
|
var client = new OpenAIClient(new ApiKeyCredential("xxx"),
|
||||||
{
|
new OpenAIClientOptions
|
||||||
Endpoint = new Uri("https://yxai.chat/v1"),
|
{
|
||||||
});
|
Endpoint = new Uri("https://yxai.chat/v1"),
|
||||||
|
});
|
||||||
var agent = client.GetChatClient(modelId)
|
var agent = client.GetChatClient(modelId)
|
||||||
.AsIChatClient() // Converts a native OpenAI SDK ChatClient into a Microsoft.Extensions.AI.IChatClient
|
.AsIChatClient() // Converts a native OpenAI SDK ChatClient into a Microsoft.Extensions.AI.IChatClient
|
||||||
.CreateAIAgent("你是一个专业的网页ai助手") ;
|
.CreateAIAgent("你是一个专业的网页ai助手");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var thread = agent.GetNewThread();
|
var thread = agent.GetNewThread();
|
||||||
|
|
||||||
var tools = GetTools();
|
var tools = GetTools();
|
||||||
@@ -43,7 +46,7 @@ public class ChatManager : DomainService
|
|||||||
Tools = tools.Select(x => (AITool)x).ToList(),
|
Tools = tools.Select(x => (AITool)x).ToList(),
|
||||||
ToolMode = ChatToolMode.Auto
|
ToolMode = ChatToolMode.Auto
|
||||||
};
|
};
|
||||||
|
|
||||||
await foreach (var update in agent.RunStreamingAsync("联网搜索一下,奥德赛第一中学学生会会长是谁", thread,
|
await foreach (var update in agent.RunStreamingAsync("联网搜索一下,奥德赛第一中学学生会会长是谁", thread,
|
||||||
new ChatClientAgentRunOptions(chatOptions)))
|
new ChatClientAgentRunOptions(chatOptions)))
|
||||||
{
|
{
|
||||||
@@ -71,6 +74,11 @@ public class ChatManager : DomainService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
string serializedJson = thread.Serialize(JsonSerializerOptions.Web).GetRawText();
|
||||||
|
JsonElement reloaded = JsonSerializer.Deserialize<JsonElement>(serializedJson, JsonSerializerOptions.Web);
|
||||||
|
var newThread = agent.DeserializeThread(reloaded, JsonSerializerOptions.Web);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user