2023-05-27 01:58:31 +00:00
|
|
|
using LLama;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
2023-06-26 23:08:24 +00:00
|
|
|
namespace BotSharp.Core.Plugins.LLamaSharp;
|
2023-05-27 01:58:31 +00:00
|
|
|
|
2023-06-23 04:43:00 +00:00
|
|
|
public class ChatCompletionProvider : IChatServiceZone
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
|
|
|
|
private readonly IChatModel _model;
|
|
|
|
|
private readonly LlamaSharpSettings _settings;
|
|
|
|
|
|
2023-06-07 01:08:25 +00:00
|
|
|
public ChatCompletionProvider(LlamaSharpSettings settings)
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
|
|
|
|
_settings = settings;
|
2023-06-07 00:59:32 +00:00
|
|
|
_model = new LLamaModel(new LLamaParams(model: _settings.ModelPath,
|
|
|
|
|
n_ctx: _settings.MaxContextLength,
|
|
|
|
|
interactive: _settings.Interactive,
|
|
|
|
|
repeat_penalty: _settings.RepeatPenalty,
|
2023-05-29 01:06:05 +00:00
|
|
|
verbose_prompt: _settings.VerbosePrompt,
|
|
|
|
|
n_gpu_layers: _settings.NumberOfGpuLayer));
|
2023-05-27 01:58:31 +00:00
|
|
|
|
2023-05-29 01:06:05 +00:00
|
|
|
var prompt = GetInstruction();
|
|
|
|
|
_model.InitChatPrompt(prompt, "UTF-8");
|
|
|
|
|
_model.InitChatAntiprompt(new string[] { "user:" });
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-23 04:43:00 +00:00
|
|
|
public int Priority => 100;
|
|
|
|
|
|
2023-05-29 01:06:05 +00:00
|
|
|
public async Task GetChatCompletionsAsync(List<RoleDialogModel> conversations,
|
|
|
|
|
Func<string, Task> onChunkReceived)
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
|
|
|
|
string totalResponse = "";
|
2023-06-03 17:01:38 +00:00
|
|
|
var prompt = GetInstruction();
|
2023-06-23 04:43:00 +00:00
|
|
|
var content = string.Join("\n ", conversations.Select(x => $"{x.Role}: {x.Text.Replace("user:", "")}")).Trim();
|
2023-06-03 17:01:50 +00:00
|
|
|
content += "\n assistant: ";
|
2023-06-03 17:01:38 +00:00
|
|
|
foreach (var response in _model.Chat(content, prompt, "UTF-8"))
|
2023-05-27 01:58:31 +00:00
|
|
|
{
|
2023-05-28 16:30:27 +00:00
|
|
|
Console.Write(response);
|
2023-05-27 01:58:31 +00:00
|
|
|
totalResponse += response;
|
2023-05-28 16:30:27 +00:00
|
|
|
await onChunkReceived(response);
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
2023-05-28 16:30:27 +00:00
|
|
|
|
|
|
|
|
Console.WriteLine();
|
2023-05-29 01:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<RoleDialogModel> GetChatSamples()
|
|
|
|
|
{
|
|
|
|
|
var samples = new List<RoleDialogModel>();
|
|
|
|
|
if (!string.IsNullOrEmpty(_settings.ChatSampleFile))
|
|
|
|
|
{
|
|
|
|
|
var lines = File.ReadAllLines(_settings.ChatSampleFile);
|
|
|
|
|
for (int i = 0; i < lines.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var line = lines[i];
|
|
|
|
|
var role = line.Substring(0, line.IndexOf(' ') - 1);
|
|
|
|
|
var content = line.Substring(line.IndexOf(' ') + 1);
|
|
|
|
|
|
|
|
|
|
samples.Add(new RoleDialogModel
|
|
|
|
|
{
|
|
|
|
|
Role = role,
|
2023-06-23 04:43:00 +00:00
|
|
|
Text = content
|
2023-05-29 01:06:05 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return samples;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetInstruction()
|
|
|
|
|
{
|
|
|
|
|
var instruction = "";
|
|
|
|
|
if (!string.IsNullOrEmpty(_settings.InstructionFile))
|
|
|
|
|
{
|
|
|
|
|
instruction = File.ReadAllText(_settings.InstructionFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instruction += "\n";
|
|
|
|
|
foreach (var message in GetChatSamples())
|
|
|
|
|
{
|
2023-06-23 04:43:00 +00:00
|
|
|
instruction += $"\n{message.Role}: {message.Text}";
|
2023-05-29 01:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return instruction;
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
2023-06-13 03:27:31 +00:00
|
|
|
|
|
|
|
|
public async Task Serving(ContentContainer content)
|
|
|
|
|
{
|
2023-06-19 16:33:27 +00:00
|
|
|
string output = "";
|
|
|
|
|
var prompt = GetInstruction();
|
2023-06-23 04:43:00 +00:00
|
|
|
var conversations = string.Join("\n ", content.Conversations.Select(x => $"{x.Role}: {x.Text.Replace("user:", "")}")).Trim();
|
2023-06-19 16:33:27 +00:00
|
|
|
conversations += "\n assistant: ";
|
|
|
|
|
foreach (var response in _model.Chat(conversations, prompt, "UTF-8"))
|
|
|
|
|
{
|
|
|
|
|
Console.Write(response);
|
|
|
|
|
output += response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine();
|
2023-06-13 03:27:31 +00:00
|
|
|
}
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|