Browse Source

feat: Adding the API key without proper verification

pull/1421/head
colin 1 week ago
parent
commit
409154366b
  1. 40
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs
  2. 35
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/System/Collections/Generic/IAsyncEnumerableErrorHandlingExtenssions.cs
  3. 4
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/AbpAIErrorCodes.cs
  4. 1
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/en.json
  5. 1
      aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/zh-Hans.json

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

@ -2,10 +2,14 @@
using LINGYUN.Abp.AI.Localization;
using LINGYUN.Abp.AI.Models;
using LINGYUN.Abp.AI.Tokens;
using LINGYUN.Abp.AI.Workspaces;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.ClientModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -19,6 +23,8 @@ using AIChatMessage = Microsoft.Extensions.AI.ChatMessage;
namespace LINGYUN.Abp.AI.Agent;
public class AgentService : IAgentService, IScopedDependency
{
public ILogger<AgentService> Logger { protected get; set; }
private readonly IClock _clock;
private readonly IGuidGenerator _guidGenerator;
private readonly IAgentFactory _agentFactory;
@ -42,6 +48,8 @@ public class AgentService : IAgentService, IScopedDependency
_chatMessageStore = chatMessageStore;
_conversationStore = conversationStore;
_localizerResource = localizerResource;
Logger = NullLogger<AgentService>.Instance;
}
public async virtual IAsyncEnumerable<string> SendMessageAsync(Models.ChatMessage message)
@ -52,9 +60,29 @@ public class AgentService : IAgentService, IScopedDependency
var messages = await BuildChatMessages(message);
var agent = await _agentFactory.CreateAsync(message.Workspace);
AIAgent agent = default!;
try
{
agent = await _agentFactory.CreateAsync(message.Workspace);
}
catch (ArgumentException ae)
{
if (ae.ParamName == nameof(WorkspaceDefinition.ApiKey))
{
throw new BusinessException(AbpAIErrorCodes.MissingOrInvalidApiKey, innerException: ae);
}
throw;
}
var agentRunRes = agent.RunStreamingAsync(messages);
var agentRunRes = agent.RunStreamingAsync(messages)
.WithErrorHandling((ex) =>
{
if (ex is ClientResultException cre && cre.Status == 401)
{
throw new BusinessException(AbpAIErrorCodes.MissingOrInvalidApiKey, innerException: ex);
}
});
var tokenUsageInfo = new TokenUsageInfo(message.Workspace, conversationId);
var agentMessageBuilder = new StringBuilder();
@ -70,10 +98,10 @@ public class AgentService : IAgentService, IScopedDependency
tokenUsageInfo.WithMessageId(messageId);
#if DEBUG
Console.WriteLine();
Console.WriteLine(tokenUsageInfo);
#endif
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug("TokenUsageInfo: {TokenUsageInfo}", tokenUsageInfo);
}
await StoreTokenUsageInfo(tokenUsageInfo);
}

35
aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/System/Collections/Generic/IAsyncEnumerableErrorHandlingExtenssions.cs

@ -0,0 +1,35 @@
using System.Runtime.CompilerServices;
using System.Threading;
namespace System.Collections.Generic;
internal static class IAsyncEnumerableErrorHandlingExtenssions
{
public static async IAsyncEnumerable<T> WithErrorHandling<T>(
this IAsyncEnumerable<T> source,
Action<Exception> onError,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var enumerator = source.GetAsyncEnumerator(cancellationToken);
await using (enumerator)
{
while (true)
{
try
{
if (!await enumerator.MoveNextAsync())
{
break;
}
}
catch (Exception ex)
{
onError(ex);
yield break;
}
yield return enumerator.Current;
}
}
}
}

4
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/AbpAIErrorCodes.cs

@ -7,6 +7,10 @@ public static class AbpAIErrorCodes
/// </summary>
public const string WorkspaceIsNotEnabled = Namespace + ":110001";
/// <summary>
/// ApiKey缺失或无效!
/// </summary>
public const string MissingOrInvalidApiKey = Namespace + ":110002";
/// <summary>
/// 对话已过期, 请重新创建会话!
/// </summary>
public const string ConversationHasExpired = Namespace + ":110101";

1
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/en.json

@ -2,6 +2,7 @@
"culture": "en",
"texts": {
"Abp.AI:110001": "Workspace is not enabled: {Workspace}!",
"Abp.AI:110002": "Missing or invalid API key!",
"Abp.AI:110101": "The conversation has expired. Please create a new one!",
"NewConversation": "New Conversation"
}

1
aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/zh-Hans.json

@ -2,6 +2,7 @@
"culture": "zh-Hans",
"texts": {
"Abp.AI:110001": "工作区不可用: {Workspace}!",
"Abp.AI:110002": "ApiKey缺失或无效!",
"Abp.AI:110101": "对话已过期, 请重新创建会话!",
"NewConversation": "新对话"
}

Loading…
Cancel
Save