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

100 lines
3.1 KiB
C#
Raw Normal View History

2023-06-13 03:27:31 +00:00
using BotSharp.Abstraction.Infrastructures.ContentTransfers;
2023-05-28 16:30:27 +00:00
using BotSharp.Abstraction.Models;
2023-06-19 16:33:27 +00:00
using BotSharp.Core.Repository.Collections;
2023-05-27 01:58:31 +00:00
using LLama;
using System.IO;
2023-06-11 23:46:02 +00:00
namespace BotSharp.Plugins.LLamaSharp;
2023-05-27 01:58:31 +00:00
2023-06-17 02:42:35 +00:00
public class ChatCompletionProvider : IServiceZone
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-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-03 17:01:50 +00:00
var content = string.Join("\n ", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
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,
Content = content
});
}
}
return samples;
}
public string GetInstruction()
{
var instruction = "";
if (!string.IsNullOrEmpty(_settings.InstructionFile))
{
instruction = File.ReadAllText(_settings.InstructionFile);
}
instruction += "\n";
foreach (var message in GetChatSamples())
{
instruction += $"\n{message.Role}: {message.Content}";
}
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();
var conversations = string.Join("\n ", content.Conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
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
}