-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from hchen2020/master
Add Agent hook.
- Loading branch information
Showing
14 changed files
with
273 additions
and
71 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace BotSharp.Abstraction.Agents; | ||
|
||
public abstract class AgentHookBase : IAgentHook | ||
{ | ||
protected Agent _agent; | ||
public Agent Agent => _agent; | ||
|
||
public void SetAget(Agent agent) | ||
{ | ||
_agent = agent; | ||
} | ||
|
||
public virtual bool OnAgentLoading(ref string id) | ||
{ | ||
return true; | ||
} | ||
|
||
public virtual bool OnInstructionLoaded(ref string instruction) | ||
{ | ||
_agent.Instruction = instruction; | ||
return true; | ||
} | ||
|
||
public virtual bool OnFunctionsLoaded(ref string functions) | ||
{ | ||
_agent.Functions = functions; | ||
return true; | ||
} | ||
|
||
public virtual bool OnSamplesLoaded(ref string samples) | ||
{ | ||
_agent.Samples = samples; | ||
return true; | ||
} | ||
|
||
public virtual Agent OnAgentLoaded() | ||
{ | ||
return _agent; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentRole.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace BotSharp.Abstraction.Agents.Enums; | ||
|
||
public class AgentRole | ||
{ | ||
public const string System = "system"; | ||
public const string Assistant = "assistant"; | ||
public const string User = "user"; | ||
public const string Function = "function"; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace BotSharp.Abstraction.Agents; | ||
|
||
public interface IAgentHook | ||
{ | ||
Agent Agent { get; } | ||
void SetAget(Agent agent); | ||
|
||
/// <summary> | ||
/// Triggered before loading, you can change the returned id to switch agent. | ||
/// </summary> | ||
/// <param name="id">Agent Id</param> | ||
/// <returns></returns> | ||
bool OnAgentLoading(ref string id); | ||
|
||
|
||
bool OnInstructionLoaded(ref string instruction); | ||
|
||
bool OnFunctionsLoaded(ref string functions); | ||
|
||
bool OnSamplesLoaded(ref string samples); | ||
|
||
/// <summary> | ||
/// Triggered when agent is loaded completely. | ||
/// </summary> | ||
/// <param name="agent"></param> | ||
/// <returns></returns> | ||
Agent OnAgentLoaded(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationCompletionHook.cs
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using BotSharp.Abstraction.Conversations.Models; | ||
using BotSharp.Abstraction.MLTasks; | ||
|
||
namespace BotSharp.Abstraction.Conversations; | ||
|
||
public interface IConversationHook | ||
{ | ||
Agent Agent { get; } | ||
IConversationHook SetAgent(Agent agent); | ||
|
||
Conversation Conversation { get; } | ||
IConversationHook SetConversation(Conversation conversation); | ||
|
||
List<RoleDialogModel> Dialogs { get; } | ||
/// <summary> | ||
/// Triggered when dialog history is loaded | ||
/// </summary> | ||
/// <param name="dialogs"></param> | ||
/// <returns></returns> | ||
Task OnDialogsLoaded(List<RoleDialogModel> dialogs); | ||
|
||
Task OnStateLoaded(ConversationState state); | ||
Task OnStateChanged(string name, string preValue, string currentValue); | ||
|
||
Task BeforeCompletion(); | ||
Task OnFunctionExecuting(RoleDialogModel message); | ||
Task OnFunctionExecuted(RoleDialogModel message); | ||
Task AfterCompletion(RoleDialogModel message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using BotSharp.Abstraction.Agents.Models; | ||
|
||
namespace BotSharp.Core.Agents.Services; | ||
|
||
public partial class AgentService | ||
{ | ||
public async Task<Agent> LoadAgent(string id) | ||
{ | ||
var hooks = _services.GetServices<IAgentHook>(); | ||
|
||
// Before agent is loaded. | ||
foreach (var hook in hooks) | ||
{ | ||
hook.OnAgentLoading(ref id); | ||
} | ||
|
||
var agent = await GetAgent(id); | ||
|
||
// After agent is loaded | ||
foreach (var hook in hooks) | ||
{ | ||
hook.SetAget(agent); | ||
|
||
if (!string.IsNullOrEmpty(agent.Instruction)) | ||
{ | ||
var instruction = agent.Instruction; | ||
hook.OnInstructionLoaded(ref instruction); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(agent.Functions)) | ||
{ | ||
var functions = agent.Functions; | ||
hook.OnFunctionsLoaded(ref functions); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(agent.Samples)) | ||
{ | ||
var samples = agent.Samples; | ||
hook.OnSamplesLoaded(ref samples); | ||
} | ||
|
||
hook.OnAgentLoaded(); | ||
} | ||
|
||
return agent; | ||
} | ||
} |
Oops, something went wrong.