Change agent load timing in InstructService.
This commit is contained in:
parent
a18d1ea5aa
commit
efeaaedf59
|
|
@ -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.
|
||||||
|
|
@ -59,4 +58,10 @@ 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);
|
||||||
```
|
```
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,9 +42,7 @@ public partial class InstructService : IInstructService
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render prompt
|
// Render prompt
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var prompt = string.IsNullOrEmpty(templateName) ?
|
||||||
Agent agent = await agentService.LoadAgent(agentId);
|
|
||||||
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;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue