Skip to content

Commit

Permalink
Merge pull request #101 from hchen2020/master
Browse files Browse the repository at this point in the history
Add Agent hook.
  • Loading branch information
Oceania2018 committed Aug 14, 2023
2 parents 3f7b2c3 + e57676e commit 893acaa
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 71 deletions.
40 changes: 40 additions & 0 deletions src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs
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;
}
}
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 src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ public interface IAgentService
{
Task<Agent> CreateAgent(Agent agent);
Task<List<Agent>> GetAgents();

/// <summary>
/// Load agent configurations and triggher hooks
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<Agent> LoadAgent(string id);

Task<Agent> GetAgent(string id);
Task<bool> DeleteAgent(string id);
Task UpdateAgent(Agent agent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.MLTasks;

namespace BotSharp.Abstraction.Conversations;

public abstract class ConversationCompletionHookBase : IConversationCompletionHook
public abstract class ConversationHookBase : IConversationHook
{
protected Agent _agent;
public Agent Agent => _agent;
Expand All @@ -14,44 +13,39 @@ public abstract class ConversationCompletionHookBase : IConversationCompletionHo
protected List<RoleDialogModel> _dialogs;
public List<RoleDialogModel> Dialogs => _dialogs;

protected IChatCompletion _chatCompletion;
public IChatCompletion ChatCompletion => _chatCompletion;

public IConversationCompletionHook SetAgent(Agent agent)
public IConversationHook SetAgent(Agent agent)
{
_agent = agent;
return this;
}

public IConversationCompletionHook SetConversation(Conversation conversation)
public IConversationHook SetConversation(Conversation conversation)
{
_conversation = conversation;
return this;
}

public IConversationCompletionHook SetDialogs(List<RoleDialogModel> dialogs)
public virtual Task OnStateLoaded(ConversationState state)
{
_dialogs = dialogs;
return this;
return Task.CompletedTask;
}

public IConversationCompletionHook SetChatCompletion(IChatCompletion chatCompletion)
public virtual Task OnStateChanged(string name, string preValue, string currentValue)
{
_chatCompletion = chatCompletion;
return this;
return Task.CompletedTask;
}

public virtual Task OnStateLoaded(ConversationState state, Action<Agent>? onAgentSwitched = null)
public virtual Task BeforeCompletion()
{
return Task.CompletedTask;
}

public virtual Task BeforeCompletion()
public virtual Task OnFunctionExecuting(RoleDialogModel message)
{
return Task.CompletedTask;
}

public virtual Task OnFunctionExecuting(string name, string args)
public virtual Task OnFunctionExecuted(RoleDialogModel message)
{
return Task.CompletedTask;
}
Expand All @@ -60,4 +54,10 @@ public virtual Task AfterCompletion(RoleDialogModel message)
{
return Task.CompletedTask;
}

public virtual Task OnDialogsLoaded(List<RoleDialogModel> dialogs)
{
_dialogs = dialogs;
return Task.CompletedTask;
}
}

This file was deleted.

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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ namespace BotSharp.Abstraction.Conversations;
/// </summary>
public interface IConversationStateService
{
ConversationState Load(string conversationId);
void SetConversation(string conversationId);
ConversationState Load();
string GetState(string name);
void SetState(string name, string value);
void Save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public class RoleDialogModel
/// </summary>
public string? ExecutionResult { get; set; }

/// <summary>
/// When function callback has been executed, system will pass result to LLM again,
/// Set this property to True to stop calling LLM.
/// </summary>
public bool StopSubsequentInteraction { get;set; }

/// <summary>
/// Channel name
/// </summary>
public string Channel { get; set; }

public RoleDialogModel(string role, string text)
{
Role = role;
Expand Down
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;
}
}
Loading

0 comments on commit 893acaa

Please sign in to comment.