Browse Source

feat: The ConversationId is not mandatory.

pull/1421/head
colin 2 months ago
parent
commit
f6726548cd
  1. 2
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentFactory.cs
  2. 6
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs
  3. 2
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/IUserMessageStore.cs
  4. 18
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/InMemoryUserMessageStore.cs
  5. 47
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/UserMessage.cs
  6. 1
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/OpenAIChatClientProvider.cs

2
aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentFactory.cs

@ -80,7 +80,7 @@ public class AgentFactory : IAgentFactory, IScopedDependency
Tools = tools,
},
Name = workspace?.Name,
Description = description
Description = description,
};
var aiAgent = chatClient.CreateAIAgent(clientAgentOptions)

6
aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs

@ -2,6 +2,7 @@
using LINGYUN.Abp.AI.Models;
using LINGYUN.Abp.AI.Tokens;
using Microsoft.Extensions.AI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -49,12 +50,15 @@ public class AgentService : IAgentService, IScopedDependency
{
var messages = new List<ChatMessage>();
var historyMessages = await _userMessageStore.GetHistoryMessagesAsync(message.ChatId);
if (!message.ConversationId.IsNullOrWhiteSpace())
{
var historyMessages = await _userMessageStore.GetHistoryMessagesAsync(message.ConversationId);
foreach (var chatMessage in historyMessages)
{
messages.Add(new ChatMessage(ChatRole.System, chatMessage.Content));
}
}
messages.Add(new ChatMessage(ChatRole.User, message.Content));

2
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/IUserMessageStore.cs

@ -7,5 +7,5 @@ public interface IUserMessageStore
{
Task<string> SaveMessageAsync(UserMessage message);
Task<IEnumerable<UserMessage>> GetHistoryMessagesAsync(string chatId);
Task<IEnumerable<UserMessage>> GetHistoryMessagesAsync(string conversationId);
}

18
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/InMemoryUserMessageStore.cs

@ -14,9 +14,9 @@ public class InMemoryUserMessageStore : IUserMessageStore
{
private static readonly ConcurrentDictionary<string, List<UserMessage>> _userMessageCache = new ConcurrentDictionary<string, List<UserMessage>>();
public Task<IEnumerable<UserMessage>> GetHistoryMessagesAsync(string chatId)
public Task<IEnumerable<UserMessage>> GetHistoryMessagesAsync(string conversationId)
{
if (_userMessageCache.TryGetValue(chatId, out var messages))
if (_userMessageCache.TryGetValue(conversationId, out var messages))
{
return Task.FromResult(messages.Take(5));
}
@ -26,15 +26,21 @@ public class InMemoryUserMessageStore : IUserMessageStore
public Task<string> SaveMessageAsync(UserMessage message)
{
if (_userMessageCache.ContainsKey(message.ChatId))
var messageId = message.Id;
if (messageId.IsNullOrWhiteSpace())
{
_userMessageCache[message.ChatId].Add(message);
messageId = Guid.NewGuid().ToString();
message.WithMessageId(messageId);
}
if (_userMessageCache.ContainsKey(messageId))
{
_userMessageCache[messageId].Add(message);
}
else
{
_userMessageCache[message.ChatId] = new List<UserMessage>() { message };
_userMessageCache[messageId] = new List<UserMessage>() { message };
}
return Task.FromResult(message.Id);
return Task.FromResult(messageId);
}
}

47
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/UserMessage.cs

@ -3,24 +3,59 @@
namespace LINGYUN.Abp.AI.Models;
public class UserMessage
{
public string Id { get; }
public string ChatId { get; }
/// <summary>
/// 消息内容
/// </summary>
public string Content { get; }
/// <summary>
/// 工作区
/// </summary>
public string Workspace { get; }
/// <summary>
/// 消息Id
/// </summary>
/// <remarks>
/// 在持久化设施处更新
/// </remarks>
public string? Id { get; private set; }
/// <summary>
/// 对话Id
/// </summary>
/// <remarks>
/// 用于从客户端存储中持久化和检索聊天历史的唯一标识符,如果未指定则与AI对话时无上下文关联
/// </remarks>
public string? ConversationId { get; private set; }
/// <summary>
/// AI回复消息
/// </summary>
public string ReplyMessage { get; private set; }
/// <summary>
/// 媒体附件
/// </summary>
/// <remarks>
/// 暂未实现
/// </remarks>
public MediaMessage[]? Medias { get; private set; }
public UserMessage(
string workspace,
string id,
string chatId,
string content)
{
Workspace = workspace;
Id = id;
ChatId = chatId;
Content = content;
}
public UserMessage WithMessageId(string id)
{
Id = id;
return this;
}
public UserMessage WithConversationId(string conversationId)
{
ConversationId = conversationId;
return this;
}
public UserMessage WithMedia(MediaMessage media)
{
Medias ??= [];

1
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/OpenAIChatClientProvider.cs

@ -38,7 +38,6 @@ public class OpenAIChatClientProvider : ChatClientProvider
.UseOpenTelemetry()
.UseFunctionInvocation()
.UseDistributedCache()
.UseChatReducer()
.Build(ServiceProvider);
return Task.FromResult(chatClient);

Loading…
Cancel
Save