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).
### Conversation State Hook
`IConversationHook`
```csharp
Task OnStateLoaded(ConversationState state);
Task OnStateChanged(string name, string preValue, string currentValue);
```
### Content Generating Hook
## Content Generating Hook
`IContentGeneratingHook`
Model content generating hook, it can be used for logging, metrics and tracing.
@ -59,4 +58,10 @@ Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations);
// After content generated.
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
{
string SelfId { get; }
Task BeforeCompletion(RoleDialogModel message);
Task AfterCompletion(InstructResult result);
Task BeforeCompletion(Agent agent, RoleDialogModel message);
Task AfterCompletion(Agent agent, InstructResult result);
}

View file

@ -5,12 +5,13 @@ namespace BotSharp.Abstraction.Instructs;
public class InstructHookBase : IInstructHook
{
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;
}
public virtual async Task BeforeCompletion(RoleDialogModel message)
public virtual async Task AfterCompletion(Agent agent, InstructResult result)
{
return;
}

View file

@ -16,6 +16,9 @@ public partial class InstructService : IInstructService
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
var hooks = _services.GetServices<IInstructHook>();
foreach (var hook in hooks)
@ -25,7 +28,7 @@ public partial class InstructService : IInstructService
continue;
}
await hook.BeforeCompletion(message);
await hook.BeforeCompletion(agent, message);
// Interrupted by hook
if (message.StopCompletion)
@ -39,9 +42,7 @@ public partial class InstructService : IInstructService
}
// 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.RenderedTemplate(agent, templateName);
@ -60,7 +61,7 @@ public partial class InstructService : IInstructService
continue;
}
await hook.AfterCompletion(response);
await hook.AfterCompletion(agent, response);
}
return response;

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Users;
using Microsoft.AspNetCore.Http;
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;
}