BotSharp/src/Plugins/BotSharp.Plugin.LLamaSharp/Providers/ChatCompletionProvider.cs

168 lines
5.3 KiB
C#
Raw Normal View History

2023-08-19 13:25:47 +00:00
namespace BotSharp.Plugin.LLamaSharp.Providers;
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;
private readonly ILogger _logger;
2023-09-18 08:35:02 +00:00
private readonly LlamaSharpSettings _settings;
private string _model;
public ChatCompletionProvider(IServiceProvider services,
2023-09-18 08:35:02 +00:00
ILogger<ChatCompletionProvider> logger,
2023-10-16 20:07:07 +00:00
LlamaSharpSettings settings)
2023-06-27 19:17:53 +00:00
{
2023-06-27 23:36:50 +00:00
_services = services;
_logger = logger;
2023-09-18 08:35:02 +00:00
_settings = settings;
2023-05-29 01:06:05 +00:00
}
2023-09-18 08:35:02 +00:00
public string Provider => "llama-sharp";
2023-07-21 20:15:09 +00:00
public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
2023-10-16 20:07:07 +00:00
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
// Before chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.BeforeGenerating(agent, conversations)).ToArray());
var content = string.Join("\r\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim();
content += $"\r\n{AgentRole.Assistant}: ";
var llama = _services.GetRequiredService<LlamaAiModel>();
2023-10-12 00:59:09 +00:00
llama.LoadModel(_model);
var executor = llama.GetStatelessExecutor();
var inferenceParams = new InferenceParams()
{
Temperature = 0.1f,
AntiPrompts = new List<string> { $"{AgentRole.User}:", "[/INST]" },
MaxTokens = 64
};
string totalResponse = "";
var prompt = agent.Instruction + "\r\n" + content;
var convSetting = _services.GetRequiredService<ConversationSetting>();
if (convSetting.ShowVerboseLog)
{
_logger.LogInformation(prompt);
}
foreach (var response in executor.Infer(prompt, inferenceParams))
{
Console.Write(response);
totalResponse += response;
}
foreach (var anti in inferenceParams.AntiPrompts)
{
totalResponse = totalResponse.Replace(anti, "").Trim();
}
var msg = new RoleDialogModel(AgentRole.Assistant, totalResponse)
{
CurrentAgentId = agent.Id
};
2023-10-16 20:07:07 +00:00
// After chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.AfterGenerated(msg, new TokenStatsModel
{
Model = _model
})).ToArray());
return msg;
}
2023-08-19 13:25:47 +00:00
public async Task<bool> GetChatCompletionsAsync(Agent agent,
List<RoleDialogModel> conversations,
2023-08-18 04:27:07 +00:00
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting)
2023-07-27 21:56:57 +00:00
{
2023-09-18 08:35:02 +00:00
var content = string.Join("\r\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim();
content += $"\r\n{AgentRole.Assistant}: ";
var state = _services.GetRequiredService<IConversationStateService>();
var model = state.GetState("model", _settings.DefaultModel);
2023-08-10 12:25:08 +00:00
var llama = _services.GetRequiredService<LlamaAiModel>();
2023-09-18 08:35:02 +00:00
llama.LoadModel(model);
var executor = llama.GetStatelessExecutor();
2023-08-10 12:25:08 +00:00
var inferenceParams = new InferenceParams()
{
Temperature = 0.1f,
AntiPrompts = new List<string> { $"{AgentRole.User}:", "[/INST]" },
MaxTokens = 64
2023-08-10 12:25:08 +00:00
};
string totalResponse = "";
2023-09-18 08:35:02 +00:00
var prompt = agent.Instruction + "\r\n" + content;
var convSetting = _services.GetRequiredService<ConversationSetting>();
if (convSetting.ShowVerboseLog)
{
_logger.LogInformation(prompt);
}
foreach (var response in executor.Infer(prompt, inferenceParams))
2023-08-10 12:25:08 +00:00
{
Console.Write(response);
totalResponse += response;
}
foreach (var anti in inferenceParams.AntiPrompts)
{
totalResponse = totalResponse.Replace(anti, "").Trim();
}
2023-09-18 08:35:02 +00:00
var msg = new RoleDialogModel(AgentRole.Assistant, totalResponse)
{
CurrentAgentId = agent.Id
};
// Text response received
await onMessageReceived(msg);
2023-08-10 12:25:08 +00:00
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-09-18 08:35:02 +00:00
var content = string.Join("\r\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim();
content += $"\r\n{AgentRole.Assistant}: ";
var state = _services.GetRequiredService<IConversationStateService>();
var model = state.GetState("model", "llama-2-7b-chat.Q8_0");
2023-06-27 23:36:50 +00:00
var llama = _services.GetRequiredService<LlamaAiModel>();
2023-09-18 08:35:02 +00:00
llama.LoadModel(model);
var executor = new StatelessExecutor(llama.Model, llama.Params);
var inferenceParams = new InferenceParams() { Temperature = 1.0f, AntiPrompts = new List<string> { $"{AgentRole.User}:" }, MaxTokens = 64 };
var convSetting = _services.GetRequiredService<ConversationSetting>();
if (convSetting.ShowVerboseLog)
{
_logger.LogInformation(agent.Instruction);
}
2023-06-27 23:36:50 +00:00
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
}
public void SetModelName(string model)
{
_model = model;
}
2023-05-27 01:58:31 +00:00
}