Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ The main documentation for the site is organized into the following sections:
:caption: Prompt Engineering

prompt/intro
prompt/template

.. _architecture-docs:

Expand Down
4 changes: 3 additions & 1 deletion docs/prompt/intro.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Prompt Engineering
# Prompt Engineering

LLM uses prompt as input, and the model produces different outputs according to the input.
3 changes: 3 additions & 0 deletions docs/prompt/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Template

We can define the prompt as a template, and the template can be changed according to variables, so that a instruction file can be used to generate a dynamic prompt.
46 changes: 0 additions & 46 deletions src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IAgentHook
bool OnAgentLoading(ref string id);


bool OnInstructionLoaded(ref string instruction);
bool OnInstructionLoaded(string template, Dictionary<string, object> dict);

bool OnFunctionsLoaded(ref string functions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class AgentSettings
/// </summary>
public string RouterId { get; set; }
public string DataDir { get; set; }
public string TemplateFormat { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace BotSharp.Abstraction.Conversations;
public interface IConversationStorage
{
void InitStorage(string conversationId);
void Append(string conversationId, RoleDialogModel dialog);
void Append(string conversationId, Agent agent, RoleDialogModel dialog);
List<RoleDialogModel> GetDialogs(string conversationId);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Agents.Enums;

namespace BotSharp.Abstraction.Conversations.Models;

public class RoleDialogModel
Expand Down Expand Up @@ -40,6 +42,13 @@ public RoleDialogModel(string role, string text)

public override string ToString()
{
return $"{Role}: {Content}";
if (Role == AgentRole.Function)
{
return $"{Role}: {FunctionName}";
}
else
{
return $"{Role}: {Content}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ namespace BotSharp.Abstraction.MLTasks;

public interface IChatCompletion
{
// string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived);
Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived);
Task<bool> GetChatCompletionsAsync(Agent agent,
List<RoleDialogModel> conversations,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting);

Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived);
}
76 changes: 76 additions & 0 deletions src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using Fluid;
using Microsoft.AspNetCore.Mvc;

namespace BotSharp.Core.Agents.Services;

public abstract class AgentHookBase : IAgentHook
{
protected Agent _agent;
public Agent Agent => _agent;
private static readonly FluidParser _parser = new FluidParser();

private readonly IServiceProvider _services;

public AgentHookBase(IServiceProvider services)
{
_services = services;
}

public void SetAget(Agent agent)
{
_agent = agent;
}

public virtual bool OnAgentLoading(ref string id)
{
return true;
}

public virtual bool OnInstructionLoaded(string template, Dictionary<string, object> dict)
{
if (_parser.TryParse(template, out var t, out var error))
{
PopulateStateTokens(dict);
var context = new TemplateContext(dict);
_agent.Instruction = t.Render(context);
return true;
}
else
{
return false;
}
}

private void PopulateStateTokens(Dictionary<string, object> dict)
{
var stateService = _services.GetRequiredService<IConversationStateService>();
var state = stateService.Load();
foreach (var t in state)
{
dict[t.Key] = t.Value;
}
}

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 void OnAgentLoaded(Agent agent)
{
}

public virtual bool OnAgentRouting(RoleDialogModel message, ref string id)
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public AgentRouter(IServiceProvider services,
public async Task<Agent> LoadCurrentAgent()
{
// Load current agent from state
var stateService = _services.GetRequiredService<IConversationStateService>();
var currentAgentId = stateService.GetState("agentId");
var state = _services.GetRequiredService<IConversationStateService>();
var currentAgentId = state.GetState("agentId");
if (string.IsNullOrEmpty(currentAgentId))
{
currentAgentId = _settings.RouterId;
Expand All @@ -30,7 +30,7 @@ public async Task<Agent> LoadCurrentAgent()
var agent = await agentService.LoadAgent(currentAgentId);

// Set agent and trigger state changed
stateService.SetState("agentId", currentAgentId);
state.SetState("agentId", currentAgentId);

return agent;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using Microsoft.Extensions.Logging;
using System.IO;

namespace BotSharp.Core.Agents.Services;
Expand All @@ -26,7 +25,7 @@ public async Task<Agent> GetAgent(string id)
var profile = query.FirstOrDefault();
var dir = GetAgentDataDir(id);

var instructionFile = Path.Combine(dir, "instruction.txt");
var instructionFile = Path.Combine(dir, $"instruction.{_settings.TemplateFormat}");
if (File.Exists(instructionFile))
{
profile.Instruction = File.ReadAllText(instructionFile);
Expand All @@ -36,7 +35,7 @@ public async Task<Agent> GetAgent(string id)
_logger.LogError($"Can't find instruction file from {instructionFile}");
}

var samplesFile = Path.Combine(dir, "samples.txt");
var samplesFile = Path.Combine(dir, $"samples.{_settings.TemplateFormat}");
if (File.Exists(samplesFile))
{
profile.Samples = File.ReadAllText(samplesFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public async Task<Agent> LoadAgent(string id)

if (!string.IsNullOrEmpty(agent.Instruction))
{
var instruction = agent.Instruction;
hook.OnInstructionLoaded(ref instruction);
hook.OnInstructionLoaded(agent.Instruction, new Dictionary<string, object>());
}

if (!string.IsNullOrEmpty(agent.Functions))
Expand Down
1 change: 1 addition & 0 deletions src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.15" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
<PackageReference Include="Fluid.Core" Version="2.4.0" />
<PackageReference Include="LLamaSharp" Version="0.4.2-preview" />
<PackageReference Include="PdfPig" Version="0.1.8" />
<PackageReference Include="TensorFlow.Keras" Version="0.11.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
var conv = _services.GetRequiredService<IConversationService>();

var response = new MessageResponseModel();
var stackMsg = new List<RoleDialogModel>();

await conv.SendMessage(agentId, conversationId,
new RoleDialogModel("user", input.Text),
async msg =>
response.Text = msg.Content,
stackMsg.Add(msg),
async fn
=> await Task.CompletedTask);

response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content));
return response;
}
}
Loading