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

38 lines
1,023 B
C#
Raw Normal View History

2023-06-27 23:36:50 +00:00
using BotSharp.Abstraction.MLTasks;
2023-08-19 13:25:47 +00:00
using BotSharp.Core.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;
public TextCompletionProvider(IServiceProvider services)
{
_services = services;
}
public Task<string> GetCompletion(string text)
{
var llama = _services.GetRequiredService<LlamaAiModel>();
llama.LoadModel();
var executor = new InstructExecutor(llama.Model);
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);
}
}