2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
|
|
|
|
using BotSharp.Abstraction.MLTasks;
|
2023-05-27 01:58:31 +00:00
|
|
|
using LLama;
|
2023-06-27 23:36:50 +00:00
|
|
|
using LLama.Common;
|
2023-05-27 01:58:31 +00:00
|
|
|
|
2023-06-26 23:08:24 +00:00
|
|
|
namespace BotSharp.Core.Plugins.LLamaSharp;
|
2023-05-27 01:58:31 +00:00
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public class ChatCompletionProvider : IChatCompletion
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
2023-06-27 23:36:50 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
public ChatCompletionProvider(IServiceProvider services)
|
2023-06-27 19:17:53 +00:00
|
|
|
{
|
2023-06-27 23:36:50 +00:00
|
|
|
_services = services;
|
2023-05-29 01:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public Task<string> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations)
|
|
|
|
|
{
|
|
|
|
|
string totalResponse = "";
|
2023-06-27 19:17:53 +00:00
|
|
|
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Text.Replace("user:", "")}")).Trim();
|
|
|
|
|
content += "\nassistant: ";
|
2023-06-27 23:36:50 +00:00
|
|
|
|
|
|
|
|
var llama = _services.GetRequiredService<LlamaAiModel>();
|
|
|
|
|
llama.LoadModel();
|
|
|
|
|
var executor = new StatelessExecutor(llama.Model);
|
|
|
|
|
var inferenceParams = new InferenceParams() { Temperature = 1.0f, AntiPrompts = new List<string> { "user:" }, MaxTokens = 64 };
|
|
|
|
|
|
|
|
|
|
foreach (var response in executor.Infer(agent.Instruction, inferenceParams))
|
2023-05-29 01:06:05 +00:00
|
|
|
{
|
2023-06-27 19:17:53 +00:00
|
|
|
Console.Write(response);
|
|
|
|
|
totalResponse += response;
|
2023-05-29 01:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 19:17:53 +00:00
|
|
|
return Task.FromResult(totalResponse.Trim());
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
|
|
|
|
}
|