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

74 lines
2.5 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-08-17 04:04:23 +00:00
public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
2023-07-21 20:15:09 +00:00
{
throw new NotImplementedException();
}
2023-08-18 04:27:07 +00:00
public async Task<bool> GetChatCompletionsAsync(Agent agent,
List<RoleDialogModel> conversations,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting)
2023-07-27 21:56:57 +00:00
{
2023-08-10 12:25:08 +00:00
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "User:")}")).Trim();
content += "\nBob: ";
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 = 256
};
string totalResponse = "";
var prompt = agent.Instruction + content;
await foreach (var response in executor.InferAsync(prompt, inferenceParams))
{
Console.Write(response);
totalResponse += response;
}
await onMessageReceived(new RoleDialogModel("assistant", totalResponse));
return true;
2023-07-27 21:56:57 +00:00
}
public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
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
}
return true;
2023-05-27 01:58:31 +00:00
}
}