BotSharp/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/TextCompletionProvider.cs

46 lines
1.4 KiB
C#
Raw Normal View History

2023-09-18 08:35:02 +00:00
using BotSharp.Abstraction.Conversations;
2023-06-27 23:36:50 +00:00
using BotSharp.Abstraction.MLTasks;
2023-09-18 08:35:02 +00:00
using BotSharp.Plugin.LLamaSharp.Settings;
2023-08-19 13:27:23 +00:00
using BotSharp.Plugins.LLamaSharp;
2023-06-27 23:36:50 +00:00
using LLama;
using LLama.Common;
2023-08-19 13:25:47 +00:00
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
2023-06-27 23:36:50 +00:00
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-09-18 08:35:02 +00:00
private readonly LlamaSharpSettings _settings;
2023-06-27 23:36:50 +00:00
2023-09-18 08:35:02 +00:00
public TextCompletionProvider(IServiceProvider services,
LlamaSharpSettings settings)
2023-06-27 23:36:50 +00:00
{
_services = services;
2023-09-18 08:35:02 +00:00
_settings = settings;
2023-06-27 23:36:50 +00:00
}
public Task<string> GetCompletion(string text)
{
2023-09-18 08:35:02 +00:00
var state = _services.GetRequiredService<IConversationStateService>();
var model = state.GetState("model", _settings.DefaultModel);
2023-06-27 23:36:50 +00:00
var llama = _services.GetRequiredService<LlamaAiModel>();
2023-09-18 08:35:02 +00:00
llama.LoadModel(model);
2023-06-27 23:36:50 +00:00
var executor = new InstructExecutor(llama.Model.CreateContext(llama.Params));
2023-06-27 23:36:50 +00:00
var inferenceParams = new InferenceParams() { Temperature = 0.5f, MaxTokens = 128 };
string totalResponse = "";
foreach (var response in executor.Infer(text, inferenceParams))
{
Console.Write(response);
totalResponse += response;
}
return Task.FromResult(totalResponse);
}
}