|
|
|
@ -1,5 +1,4 @@ |
|
|
|
using LINGYUN.Abp.AI; |
|
|
|
using LINGYUN.Abp.AI.Tools; |
|
|
|
using LINGYUN.Abp.AIManagement.Localization; |
|
|
|
using LINGYUN.Abp.AIManagement.Tokens; |
|
|
|
using Microsoft.Extensions.AI; |
|
|
|
@ -26,7 +25,6 @@ public class ConversationChangeNameHandler : |
|
|
|
private readonly IGuidGenerator _guidGenerator; |
|
|
|
private readonly IAbpDistributedLock _distributedLock; |
|
|
|
private readonly IChatClientFactory _chatClientFactory; |
|
|
|
private readonly IWorkspaceAIToolFinder _workspaceAIToolFinder; |
|
|
|
private readonly IStringLocalizer<AIManagementResource> _stringLocalizer; |
|
|
|
private readonly ITokenUsageRecordRepository _tokenUsageRecordRepository; |
|
|
|
private readonly IConversationRecordRepository _conversationRecordRepository; |
|
|
|
@ -37,7 +35,6 @@ public class ConversationChangeNameHandler : |
|
|
|
IGuidGenerator guidGenerator, |
|
|
|
IAbpDistributedLock distributedLock, |
|
|
|
IChatClientFactory chatClientFactory, |
|
|
|
IWorkspaceAIToolFinder workspaceAIToolFinder, |
|
|
|
IStringLocalizer<AIManagementResource> stringLocalizer, |
|
|
|
ITokenUsageRecordRepository tokenUsageRecordRepository, |
|
|
|
IConversationRecordRepository conversationRecordRepository, |
|
|
|
@ -47,7 +44,6 @@ public class ConversationChangeNameHandler : |
|
|
|
_guidGenerator = guidGenerator; |
|
|
|
_distributedLock = distributedLock; |
|
|
|
_chatClientFactory = chatClientFactory; |
|
|
|
_workspaceAIToolFinder = workspaceAIToolFinder; |
|
|
|
_stringLocalizer = stringLocalizer; |
|
|
|
_tokenUsageRecordRepository = tokenUsageRecordRepository; |
|
|
|
_conversationRecordRepository = conversationRecordRepository; |
|
|
|
@ -101,40 +97,44 @@ public class ConversationChangeNameHandler : |
|
|
|
var chatClient = await _chatClientFactory.CreateAsync(chatMessage.Workspace); |
|
|
|
var instructions = _stringLocalizer["DesignConversationNamePrompt", ConversationRecordConsts.MaxNameLength].Value; |
|
|
|
|
|
|
|
// 禁用AI工具
|
|
|
|
using (_workspaceAIToolFinder.DisableAITool()) |
|
|
|
{ |
|
|
|
var aiAgent = chatClient |
|
|
|
.CreateAIAgent( |
|
|
|
instructions: instructions, |
|
|
|
services: _serviceProvider); |
|
|
|
|
|
|
|
var agentRunRes = await aiAgent.RunAsync([ |
|
|
|
new ChatMessage(ChatRole.System, instructions), |
|
|
|
new ChatMessage(ChatRole.User, chatMessage.Content)]); |
|
|
|
|
|
|
|
conversation.SetName( |
|
|
|
agentRunRes.Text.Length > ConversationRecordConsts.MaxNameLength |
|
|
|
var aiAgent = chatClient |
|
|
|
.AsBuilder() |
|
|
|
.ConfigureOptions(options => |
|
|
|
{ |
|
|
|
// 禁用工具
|
|
|
|
options.Tools = []; |
|
|
|
}) |
|
|
|
.BuildAIAgent( |
|
|
|
instructions: instructions, |
|
|
|
services: _serviceProvider); |
|
|
|
|
|
|
|
var agentRunRes = await aiAgent.RunAsync([ |
|
|
|
new ChatMessage(ChatRole.System, instructions), |
|
|
|
new ChatMessage(ChatRole.User, chatMessage.Content)]); |
|
|
|
|
|
|
|
conversation.SetName( |
|
|
|
agentRunRes.Text.Length > ConversationRecordConsts.MaxNameLength |
|
|
|
? chatMessage.Content.Length > ConversationRecordConsts.MaxNameLength |
|
|
|
? chatMessage.Content[..ConversationRecordConsts.MaxNameLength] |
|
|
|
: agentRunRes.Text); |
|
|
|
: chatMessage.Content |
|
|
|
: agentRunRes.Text); |
|
|
|
|
|
|
|
await _conversationRecordRepository.UpdateAsync(conversation); |
|
|
|
await _conversationRecordRepository.UpdateAsync(conversation); |
|
|
|
|
|
|
|
if (agentRunRes.Usage != null) |
|
|
|
{ |
|
|
|
var tokenUsageRecord = new TokenUsageRecord( |
|
|
|
_guidGenerator.Create(), |
|
|
|
chatMessage.Id, |
|
|
|
conversation.Id, |
|
|
|
agentRunRes.Usage.InputTokenCount, |
|
|
|
agentRunRes.Usage.OutputTokenCount, |
|
|
|
agentRunRes.Usage.TotalTokenCount, |
|
|
|
agentRunRes.Usage.CachedInputTokenCount, |
|
|
|
agentRunRes.Usage.ReasoningTokenCount, |
|
|
|
chatMessage.TenantId); |
|
|
|
|
|
|
|
await _tokenUsageRecordRepository.InsertAsync(tokenUsageRecord); |
|
|
|
} |
|
|
|
if (agentRunRes.Usage != null) |
|
|
|
{ |
|
|
|
var tokenUsageRecord = new TokenUsageRecord( |
|
|
|
_guidGenerator.Create(), |
|
|
|
chatMessage.Id, |
|
|
|
conversation.Id, |
|
|
|
agentRunRes.Usage.InputTokenCount, |
|
|
|
agentRunRes.Usage.OutputTokenCount, |
|
|
|
agentRunRes.Usage.TotalTokenCount, |
|
|
|
agentRunRes.Usage.CachedInputTokenCount, |
|
|
|
agentRunRes.Usage.ReasoningTokenCount, |
|
|
|
chatMessage.TenantId); |
|
|
|
|
|
|
|
await _tokenUsageRecordRepository.InsertAsync(tokenUsageRecord); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|