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; return true;
} }
public virtual bool OnSamplesLoaded(ref string samples) public virtual bool OnSamplesLoaded(List<string> samples)
{ {
_agent.Samples = samples; _agent.Samples = samples;
return true; return true;

View file

@ -19,7 +19,7 @@ public interface IAgentHook
bool OnFunctionsLoaded(List<FunctionDef> functions); bool OnFunctionsLoaded(List<FunctionDef> functions);
bool OnSamplesLoaded(ref string samples); bool OnSamplesLoaded(List<string> samples);
/// <summary> /// <summary>
/// Triggered when agent is loaded completely. /// Triggered when agent is loaded completely.

View file

@ -27,7 +27,7 @@ public class Agent
/// Samples /// Samples
/// </summary> /// </summary>
[JsonIgnore] [JsonIgnore]
public string Samples { get; set; } public List<string> Samples { get; set; }
/// <summary> /// <summary>
/// Functions /// Functions
@ -109,6 +109,12 @@ public class Agent
return this; return this;
} }
public Agent SetSamples(List<string> samples)
{
Samples = samples ?? new List<string>();
return this;
}
public Agent SetResponses(List<AgentResponse> responses) public Agent SetResponses(List<AgentResponse> responses)
{ {
Responses = responses ?? new List<AgentResponse>(); ; Responses = responses ?? new List<AgentResponse>(); ;

View file

@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Routing;
public interface IRoutingService public interface IRoutingService
{ {
List<RoleDialogModel> Dialogs { get; } List<RoleDialogModel> Dialogs { get; }
void RefreshDialogs();
Task<FunctionCallFromLlm> GetNextInstruction(); Task<FunctionCallFromLlm> GetNextInstruction();
Task<RoleDialogModel> InvokeAgent(string agentId); Task<RoleDialogModel> InvokeAgent(string agentId);
Task<RoleDialogModel> InstructLoop(); Task<RoleDialogModel> InstructLoop();

View file

@ -42,10 +42,9 @@ public partial class AgentService
hook.OnFunctionsLoaded(agent.Functions); hook.OnFunctionsLoaded(agent.Functions);
} }
if (!string.IsNullOrEmpty(agent.Samples)) if (agent.Samples != null)
{ {
var samples = agent.Samples; hook.OnSamplesLoaded(agent.Samples);
hook.OnSamplesLoaded(ref samples);
} }
hook.OnAgentLoaded(agent); hook.OnAgentLoaded(agent);

View file

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

View file

@ -519,10 +519,12 @@ public class FileRepository : IBotSharpRepository
var instruction = FetchInstruction(dir); var instruction = FetchInstruction(dir);
var functions = FetchFunctions(dir); var functions = FetchFunctions(dir);
var samples = FetchSamples(dir);
var templates = FetchTemplates(dir); var templates = FetchTemplates(dir);
var responses = FetchResponses(dir); var responses = FetchResponses(dir);
return record.SetInstruction(instruction) return record.SetInstruction(instruction)
.SetFunctions(functions) .SetFunctions(functions)
.SetSamples(samples)
.SetTemplates(templates) .SetTemplates(templates)
.SetResponses(responses); .SetResponses(responses);
} }
@ -771,6 +773,14 @@ public class FileRepository : IBotSharpRepository
return functions; 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) private List<AgentTemplate> FetchTemplates(string fileDir)
{ {
var templates = new List<AgentTemplate>(); 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, public RoutingService(IServiceProvider services,
RoutingSettings settings, RoutingSettings settings,
ILogger<RoutingService> logger, 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>(); var samples = new List<RoleDialogModel>();
if (string.IsNullOrEmpty(sampleText))
{
return samples;
}
var lines = sampleText.Split('\n'); for (int i = 0; i < lines.Count; i++)
for (int i = 0; i < lines.Length; i++)
{ {
var line = lines[i]; var line = lines[i];
if (string.IsNullOrEmpty(line.Trim())) if (string.IsNullOrEmpty(line.Trim()))