BotSharp/src/Plugins/Platforms/BotSharp.Platform.Native/Handlers/ChatCompletionHandler.cs

91 lines
2.7 KiB
C#
Raw Normal View History

2023-05-27 01:58:31 +00:00
using BotSharp.Abstraction;
2023-05-28 16:30:27 +00:00
using BotSharp.Abstraction.Models;
2023-06-03 14:57:08 +00:00
using BotSharp.Platform.Native.Settings;
2023-05-27 01:58:31 +00:00
using LLama;
using System;
2023-05-28 16:30:27 +00:00
using System.Collections.Generic;
2023-05-27 01:58:31 +00:00
using System.IO;
2023-05-29 01:06:05 +00:00
using System.Linq;
using System.Runtime;
2023-05-27 01:58:31 +00:00
using System.Text;
using System.Threading.Tasks;
2023-06-03 14:57:08 +00:00
namespace BotSharp.Platform.Native.Handlers;
2023-05-27 01:58:31 +00:00
public class ChatCompletionHandler : IChatCompletionHandler
{
private readonly IChatModel _model;
private readonly LlamaSharpSettings _settings;
public ChatCompletionHandler(LlamaSharpSettings settings)
{
_settings = settings;
_model = new LLamaModel(new LLamaParams(model: _settings.ModelPath,
2023-05-29 01:06:05 +00:00
n_ctx: _settings.MaxContextLength,
interactive: _settings.Interactive,
repeat_penalty: _settings.RepeatPenalty,
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();
var content = string.Join(". ", conversations.Select(x => $"{x.Role}: {x.Content.Replace("user:", "")}")).Trim();
content += ". assistant: ";
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
}
}