Allow agent to use few-shot learning.

This commit is contained in:
hchen 2023-10-19 12:24:00 -05:00
parent 5009b97327
commit bf9bb2b6d3
9 changed files with 31 additions and 13 deletions

View file

@ -41,7 +41,7 @@ public abstract class AgentHookBase : IAgentHook
return true;
}
public virtual bool OnSamplesLoaded(ref string samples)
public virtual bool OnSamplesLoaded(List<string> samples)
{
_agent.Samples = samples;
return true;

View file

@ -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.

View file

@ -27,7 +27,7 @@ public class Agent
/// Samples
/// </summary>
[JsonIgnore]
public string Samples { get; set; }
public List<string> Samples { get; set; }
/// <summary>
/// Functions
@ -109,6 +109,12 @@ public class Agent
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>(); ;

View file

@ -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();

View file

@ -42,10 +42,9 @@ public partial class AgentService
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);

View file

@ -56,6 +56,8 @@ public partial class ConversationService
var statistics = _services.GetRequiredService<ITokenStatistics>();
statistics.PrintStatistics();
routing.RefreshDialogs();
return true;
}

View file

@ -519,10 +519,12 @@ public class FileRepository : IBotSharpRepository
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);
}
@ -771,6 +773,14 @@ public class FileRepository : IBotSharpRepository
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>();

View file

@ -25,6 +25,11 @@ public partial class RoutingService : IRoutingService
}
}
public void RefreshDialogs()
{
_dialogs = null;
}
public RoutingService(IServiceProvider services,
RoutingSettings settings,
ILogger<RoutingService> logger,

View file

@ -23,16 +23,11 @@ public class ProviderHelper
}
}
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()))