Change agent load timing in InstructService.

This commit is contained in:
Haiping Chen 2023-10-30 22:47:14 -05:00
parent a18d1ea5aa
commit efeaaedf59
5 changed files with 26 additions and 16 deletions

View file

@ -42,14 +42,13 @@ Task OnHumanInterventionNeeded(RoleDialogModel message);
``` ```
More information about conversation hook please go to [Conversation Hook](../conversation/hook.md). More information about conversation hook please go to [Conversation Hook](../conversation/hook.md).
### Conversation State Hook
`IConversationHook` `IConversationHook`
```csharp ```csharp
Task OnStateLoaded(ConversationState state); Task OnStateLoaded(ConversationState state);
Task OnStateChanged(string name, string preValue, string currentValue); Task OnStateChanged(string name, string preValue, string currentValue);
``` ```
### Content Generating Hook ## Content Generating Hook
`IContentGeneratingHook` `IContentGeneratingHook`
Model content generating hook, it can be used for logging, metrics and tracing. Model content generating hook, it can be used for logging, metrics and tracing.
@ -60,3 +59,9 @@ Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations);
// After content generated. // After content generated.
Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats); Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats);
``` ```
`IInstructHook`
```csharp
Task BeforeCompletion(Agent agent, RoleDialogModel message);
Task AfterCompletion(Agent agent, InstructResult result);
```

View file

@ -5,6 +5,6 @@ namespace BotSharp.Abstraction.Instructs;
public interface IInstructHook public interface IInstructHook
{ {
string SelfId { get; } string SelfId { get; }
Task BeforeCompletion(RoleDialogModel message); Task BeforeCompletion(Agent agent, RoleDialogModel message);
Task AfterCompletion(InstructResult result); Task AfterCompletion(Agent agent, InstructResult result);
} }

View file

@ -5,12 +5,13 @@ namespace BotSharp.Abstraction.Instructs;
public class InstructHookBase : IInstructHook public class InstructHookBase : IInstructHook
{ {
public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!"); public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!");
public virtual async Task AfterCompletion(InstructResult result)
public virtual async Task BeforeCompletion(Agent agent, RoleDialogModel message)
{ {
return; return;
} }
public virtual async Task BeforeCompletion(RoleDialogModel message) public virtual async Task AfterCompletion(Agent agent, InstructResult result)
{ {
return; return;
} }

View file

@ -16,6 +16,9 @@ public partial class InstructService : IInstructService
public async Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null) public async Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null)
{ {
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
// Trigger before completion hooks // Trigger before completion hooks
var hooks = _services.GetServices<IInstructHook>(); var hooks = _services.GetServices<IInstructHook>();
foreach (var hook in hooks) foreach (var hook in hooks)
@ -25,7 +28,7 @@ public partial class InstructService : IInstructService
continue; continue;
} }
await hook.BeforeCompletion(message); await hook.BeforeCompletion(agent, message);
// Interrupted by hook // Interrupted by hook
if (message.StopCompletion) if (message.StopCompletion)
@ -39,8 +42,6 @@ public partial class InstructService : IInstructService
} }
// Render prompt // Render prompt
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
var prompt = string.IsNullOrEmpty(templateName) ? var prompt = string.IsNullOrEmpty(templateName) ?
agentService.RenderedInstruction(agent) : agentService.RenderedInstruction(agent) :
agentService.RenderedTemplate(agent, templateName); agentService.RenderedTemplate(agent, templateName);
@ -60,7 +61,7 @@ public partial class InstructService : IInstructService
continue; continue;
} }
await hook.AfterCompletion(response); await hook.AfterCompletion(agent, response);
} }
return response; return response;

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Users;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System.Security.Claims; using System.Security.Claims;
@ -15,11 +14,15 @@ public class UserIdentity : IUserIdentity
} }
public string Id => _claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value; public string Id
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
public string Email => _claims.First(x => x.Type == ClaimTypes.Email).Value; public string Email
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;
public string FirstName => _claims.First(x => x.Type == ClaimTypes.GivenName).Value; public string FirstName
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value;
public string LastName => _claims.First(x => x.Type == ClaimTypes.Surname).Value; public string LastName
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value;
} }