Support liquid template.
This commit is contained in:
parent
15fb98ce8d
commit
f825438632
|
|
@ -1,46 +0,0 @@
|
|||
using BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
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 void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual bool OnAgentRouting(RoleDialogModel message, ref string id)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -7,4 +7,5 @@ public class AgentSettings
|
|||
/// </summary>
|
||||
public string RouterId { get; set; }
|
||||
public string DataDir { get; set; }
|
||||
public string TemplateFormat { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.IO;
|
||||
|
||||
namespace BotSharp.Core.Agents.Services;
|
||||
|
|
@ -26,7 +25,7 @@ public partial class AgentService
|
|||
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);
|
||||
|
|
@ -36,7 +35,7 @@ public partial class AgentService
|
|||
_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);
|
||||
|
|
|
|||
|
|
@ -23,8 +23,7 @@ public partial class AgentService
|
|||
|
||||
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))
|
||||
|
|
|
|||
|
|
@ -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" />
|
||||
|
|
|
|||
|
|
@ -26,26 +26,18 @@ public class fastTextEmbeddingProvider : ITextEmbedding
|
|||
public fastTextEmbeddingProvider(fastTextSetting settings)
|
||||
{
|
||||
_settings = settings;
|
||||
_fastText = new FastTextWrapper();
|
||||
|
||||
if (!File.Exists(settings.ModelPath))
|
||||
{
|
||||
throw new FileNotFoundException($"Can't load pre-trained word vectors from {settings.ModelPath}.\n Try to download from https://fasttext.cc/docs/en/english-vectors.html.");
|
||||
}
|
||||
}
|
||||
|
||||
public float[] GetVector(string text)
|
||||
{
|
||||
if (!_fastText.IsModelReady())
|
||||
{
|
||||
_fastText.LoadModel(_settings.ModelPath);
|
||||
}
|
||||
|
||||
LoadModel();
|
||||
return _fastText.GetSentenceVector(text);
|
||||
}
|
||||
|
||||
public List<float[]> GetVectors(List<string> texts)
|
||||
{
|
||||
LoadModel();
|
||||
var vectors = new List<float[]>();
|
||||
for (int i = 0; i < texts.Count; i++)
|
||||
{
|
||||
|
|
@ -53,4 +45,22 @@ public class fastTextEmbeddingProvider : ITextEmbedding
|
|||
}
|
||||
return vectors;
|
||||
}
|
||||
|
||||
private void LoadModel()
|
||||
{
|
||||
if (_fastText == null)
|
||||
{
|
||||
if (!File.Exists(_settings.ModelPath))
|
||||
{
|
||||
throw new FileNotFoundException($"Can't load pre-trained word vectors from {_settings.ModelPath}.\n Try to download from https://fasttext.cc/docs/en/english-vectors.html.");
|
||||
}
|
||||
|
||||
_fastText = new FastTextWrapper();
|
||||
|
||||
if (!_fastText.IsModelReady())
|
||||
{
|
||||
_fastText.LoadModel(_settings.ModelPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@
|
|||
},
|
||||
|
||||
"Agent": {
|
||||
"DataDir": "agents"
|
||||
"RouterId": "",
|
||||
"DataDir": "agents",
|
||||
"TemplateFormat": "liquid"
|
||||
},
|
||||
|
||||
"Conversation": {
|
||||
|
|
|
|||
Loading…
Reference in a new issue