2023-11-29 23:23:14 +00:00
|
|
|
using BotSharp.Abstraction.Loggers;
|
|
|
|
|
|
2023-08-19 13:25:47 +00:00
|
|
|
namespace BotSharp.Plugin.LLamaSharp.Providers;
|
2023-06-27 23:36:50 +00:00
|
|
|
|
|
|
|
|
public class TextCompletionProvider : ITextCompletion
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
2023-10-12 00:59:09 +00:00
|
|
|
private readonly ILogger _logger;
|
2023-09-18 08:35:02 +00:00
|
|
|
private readonly LlamaSharpSettings _settings;
|
2023-10-12 00:59:09 +00:00
|
|
|
private readonly ITokenStatistics _tokenStatistics;
|
2023-10-08 20:46:42 +00:00
|
|
|
private string _model;
|
|
|
|
|
public string Provider => "llama-sharp";
|
2023-06-27 23:36:50 +00:00
|
|
|
|
2023-09-18 08:35:02 +00:00
|
|
|
public TextCompletionProvider(IServiceProvider services,
|
2023-10-12 00:59:09 +00:00
|
|
|
ILogger<TextCompletionProvider> logger,
|
|
|
|
|
LlamaSharpSettings settings,
|
|
|
|
|
ITokenStatistics tokenStatistics)
|
2023-06-27 23:36:50 +00:00
|
|
|
{
|
|
|
|
|
_services = services;
|
2023-10-12 00:59:09 +00:00
|
|
|
_logger = logger;
|
2023-09-18 08:35:02 +00:00
|
|
|
_settings = settings;
|
2023-10-12 00:59:09 +00:00
|
|
|
_tokenStatistics = tokenStatistics;
|
2023-06-27 23:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
public async Task<string> GetCompletion(string text, string agentId, string messageId)
|
2023-06-27 23:36:50 +00:00
|
|
|
{
|
2023-10-16 20:07:07 +00:00
|
|
|
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
|
|
|
|
|
|
|
|
|
|
// Before chat completion hook
|
2023-10-30 16:48:18 +00:00
|
|
|
var agent = new Agent()
|
|
|
|
|
{
|
|
|
|
|
Id = agentId
|
|
|
|
|
};
|
|
|
|
|
var userMessage = new RoleDialogModel(AgentRole.User, text)
|
|
|
|
|
{
|
|
|
|
|
MessageId = messageId
|
|
|
|
|
};
|
2023-10-16 20:07:07 +00:00
|
|
|
Task.WaitAll(hooks.Select(hook =>
|
2023-10-30 16:48:18 +00:00
|
|
|
hook.BeforeGenerating(agent, new List<RoleDialogModel> { userMessage })).ToArray());
|
2023-10-16 20:07:07 +00:00
|
|
|
|
2023-06-27 23:36:50 +00:00
|
|
|
var llama = _services.GetRequiredService<LlamaAiModel>();
|
2023-10-12 00:59:09 +00:00
|
|
|
llama.LoadModel(_model);
|
2023-06-27 23:36:50 +00:00
|
|
|
|
2023-09-06 12:58:02 +00:00
|
|
|
var executor = new InstructExecutor(llama.Model.CreateContext(llama.Params));
|
2025-02-03 04:02:36 +00:00
|
|
|
var inferenceParams = new InferenceParams() { MaxTokens = 128 };
|
2023-06-27 23:36:50 +00:00
|
|
|
|
2023-10-12 00:59:09 +00:00
|
|
|
_tokenStatistics.StartTimer();
|
2023-10-16 20:07:07 +00:00
|
|
|
string completion = "";
|
2023-10-25 03:41:08 +00:00
|
|
|
await foreach (var response in executor.InferAsync(text, inferenceParams))
|
2023-06-27 23:36:50 +00:00
|
|
|
{
|
|
|
|
|
Console.Write(response);
|
2023-10-16 20:07:07 +00:00
|
|
|
completion += response;
|
2023-06-27 23:36:50 +00:00
|
|
|
}
|
2023-10-12 00:59:09 +00:00
|
|
|
_tokenStatistics.StopTimer();
|
2023-06-27 23:36:50 +00:00
|
|
|
|
2023-10-16 20:07:07 +00:00
|
|
|
// After chat completion hook
|
2023-10-30 16:48:18 +00:00
|
|
|
var responseMessage = new RoleDialogModel(AgentRole.Assistant, completion)
|
|
|
|
|
{
|
|
|
|
|
CurrentAgentId = agentId,
|
|
|
|
|
MessageId = messageId
|
|
|
|
|
};
|
2023-10-16 20:07:07 +00:00
|
|
|
Task.WaitAll(hooks.Select(hook =>
|
2023-10-30 16:48:18 +00:00
|
|
|
hook.AfterGenerated(responseMessage, new TokenStatsModel
|
2023-10-16 20:07:07 +00:00
|
|
|
{
|
|
|
|
|
Model = _model
|
|
|
|
|
})).ToArray());
|
|
|
|
|
|
|
|
|
|
return completion;
|
2023-06-27 23:36:50 +00:00
|
|
|
}
|
2023-10-08 20:46:42 +00:00
|
|
|
|
|
|
|
|
public void SetModelName(string model)
|
|
|
|
|
{
|
|
|
|
|
_model = model;
|
|
|
|
|
}
|
2023-06-27 23:36:50 +00:00
|
|
|
}
|