BotSharp/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/ChatCompletionProvider.cs

42 lines
1.4 KiB
C#
Raw Normal View History

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-07-21 20:15:09 +00:00
public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
throw new NotImplementedException();
}
public Task<string> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations)
2023-06-27 18:31:13 +00:00
{
string totalResponse = "";
2023-07-21 20:15:09 +00:00
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
2023-06-27 19:17:53 +00:00
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
}
}