From ed96ff0fb9f32e478dc4a2f49c332cce6c069207 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 4 Dec 2023 18:12:57 -0600 Subject: [PATCH] Fix load agent error for InstructMode --- .../BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs | 5 +++++ src/Infrastructure/BotSharp.Logger/Hooks/VerboseLogHook.cs | 4 ++-- .../BotSharp.OpenAPI/Controllers/InstructModeController.cs | 5 +---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index 1b7a6e99..ab40b134 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -8,6 +8,11 @@ public partial class AgentService [MemoryCache(10 * 60, perInstanceCache: true)] public async Task LoadAgent(string id) { + if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString()) + { + return null; + } + var hooks = _services.GetServices(); // Before agent is loaded. diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/VerboseLogHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/VerboseLogHook.cs index 00b68d4d..119e1bba 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/VerboseLogHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/VerboseLogHook.cs @@ -38,8 +38,8 @@ public class VerboseLogHook : IContentGeneratingHook var agent = await agentService.LoadAgent(message.CurrentAgentId); var log = message.Role == AgentRole.Function ? - $"[[msg_id: {message.MessageId}] [{agent.Name}]: {message.FunctionName}({message.FunctionArgs})" : - $"[[msg_id: {message.MessageId}] [{agent.Name}]: {message.Content}"; + $"[[msg_id: {message.MessageId}] [{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" : + $"[[msg_id: {message.MessageId}] [{agent?.Name}]: {message.Content}"; _logger.LogInformation(tokenStats.Prompt); _logger.LogInformation(log); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs index 40de5287..fb047973 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/InstructModeController.cs @@ -1,6 +1,3 @@ -using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.ApiAdapters; -using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Instructs.Models; using BotSharp.Core.Infrastructures; @@ -48,6 +45,6 @@ public class InstructModeController : ControllerBase, IApiAdapter .SetState("model", input.Model); var textCompletion = CompletionProvider.GetTextCompletion(_services); - return await textCompletion.GetCompletion(input.Text, Guid.Empty.ToString(), Guid.Empty.ToString()); + return await textCompletion.GetCompletion(input.Text, Guid.Empty.ToString(), Guid.NewGuid().ToString()); } }