Skip to content

Allow agent to use few-shot learning. #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public virtual bool OnFunctionsLoaded(List<FunctionDef> functions)
return true;
}

public virtual bool OnSamplesLoaded(ref string samples)
public virtual bool OnSamplesLoaded(List<string> samples)
{
_agent.Samples = samples;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IAgentHook

bool OnFunctionsLoaded(List<FunctionDef> functions);

bool OnSamplesLoaded(ref string samples);
bool OnSamplesLoaded(List<string> samples);

/// <summary>
/// Triggered when agent is loaded completely.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Agent
/// Samples
/// </summary>
[JsonIgnore]
public string Samples { get; set; }
public List<string> Samples { get; set; }

/// <summary>
/// Functions
Expand Down Expand Up @@ -109,6 +109,12 @@ public Agent SetFunctions(List<FunctionDef> functions)
return this;
}

public Agent SetSamples(List<string> samples)
{
Samples = samples ?? new List<string>();
return this;
}

public Agent SetResponses(List<AgentResponse> responses)
{
Responses = responses ?? new List<AgentResponse>(); ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Routing;
public interface IRoutingService
{
List<RoleDialogModel> Dialogs { get; }
void RefreshDialogs();
Task<FunctionCallFromLlm> GetNextInstruction();
Task<RoleDialogModel> InvokeAgent(string agentId);
Task<RoleDialogModel> InstructLoop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ public async Task<Agent> LoadAgent(string id)
hook.OnFunctionsLoaded(agent.Functions);
}

if (!string.IsNullOrEmpty(agent.Samples))
if (agent.Samples != null)
{
var samples = agent.Samples;
hook.OnSamplesLoaded(ref samples);
hook.OnSamplesLoaded(agent.Samples);
}

hook.OnAgentLoaded(agent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ await routing.InstructLoop() :
var statistics = _services.GetRequiredService<ITokenStatistics>();
statistics.PrintStatistics();

routing.RefreshDialogs();

return true;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,12 @@ public List<string> GetAgentResponses(string agentId, string prefix, string inte

var instruction = FetchInstruction(dir);
var functions = FetchFunctions(dir);
var samples = FetchSamples(dir);
var templates = FetchTemplates(dir);
var responses = FetchResponses(dir);
return record.SetInstruction(instruction)
.SetFunctions(functions)
.SetSamples(samples)
.SetTemplates(templates)
.SetResponses(responses);
}
Expand Down Expand Up @@ -771,6 +773,14 @@ private List<FunctionDef> FetchFunctions(string fileDir)
return functions;
}

private List<string> FetchSamples(string fileDir)
{
var file = Path.Combine(fileDir, "samples.txt");
if (!File.Exists(file)) return new List<string>();

return File.ReadAllLines(file).ToList();
}

private List<AgentTemplate> FetchTemplates(string fileDir)
{
var templates = new List<AgentTemplate>();
Expand Down
5 changes: 5 additions & 0 deletions src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public List<RoleDialogModel> Dialogs {
}
}

public void RefreshDialogs()
{
_dialogs = null;
}

public RoutingService(IServiceProvider services,
RoutingSettings settings,
ILogger<RoutingService> logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ public static (OpenAIClient, string) GetClient(string model, AzureOpenAiSettings
}
}

public static List<RoleDialogModel> GetChatSamples(string sampleText)
public static List<RoleDialogModel> GetChatSamples(List<string> lines)
{
var samples = new List<RoleDialogModel>();
if (string.IsNullOrEmpty(sampleText))
{
return samples;
}

var lines = sampleText.Split('\n');
for (int i = 0; i < lines.Length; i++)
for (int i = 0; i < lines.Count; i++)
{
var line = lines[i];
if (string.IsNullOrEmpty(line.Trim()))
Expand Down