2023-09-06 12:58:02 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
2023-09-06 12:58:02 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Settings;
|
2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks;
|
2023-08-19 13:27:23 +00:00
|
|
|
using BotSharp.Plugins.LLamaSharp;
|
2023-05-27 01:58:31 +00:00
|
|
|
using LLama;
|
2023-06-27 23:36:50 +00:00
|
|
|
using LLama.Common;
|
2023-08-19 13:25:47 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-09-06 12:58:02 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-08-19 13:25:47 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-05-27 01:58:31 +00:00
|
|
|
|
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;
|
2023-09-06 12:58:02 +00:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public ChatCompletionProvider(IServiceProvider services,
|
|
|
|
|
ILogger<ChatCompletionProvider> logger)
|
2023-06-27 19:17:53 +00:00
|
|
|
{
|
2023-06-27 23:36:50 +00:00
|
|
|
_services = services;
|
2023-09-06 12:58:02 +00:00
|
|
|
_logger = logger;
|
2023-05-29 01:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-09 15:37:38 +00:00
|
|
|
public string ModelName => "llama-2";
|
|
|
|
|
|
2023-07-21 20:15:09 +00:00
|
|
|
|
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-06 12:58:02 +00:00
|
|
|
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim();
|
|
|
|
|
content += $"\n{AgentRole.Assistant}: ";
|
2023-08-10 12:25:08 +00:00
|
|
|
|
|
|
|
|
var llama = _services.GetRequiredService<LlamaAiModel>();
|
|
|
|
|
llama.LoadModel();
|
2023-09-06 12:58:02 +00:00
|
|
|
var executor = llama.GetStatelessExecutor();
|
|
|
|
|
|
2023-08-10 12:25:08 +00:00
|
|
|
var inferenceParams = new InferenceParams()
|
|
|
|
|
{
|
|
|
|
|
Temperature = 1.0f,
|
2023-09-06 12:58:02 +00:00
|
|
|
AntiPrompts = new List<string> { $"{AgentRole.User}:", "\n", "?" },
|
2023-08-10 12:25:08 +00:00
|
|
|
MaxTokens = 256
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string totalResponse = "";
|
|
|
|
|
|
|
|
|
|
var prompt = agent.Instruction + content;
|
2023-09-06 12:58:02 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 12:58:02 +00:00
|
|
|
foreach (var anti in inferenceParams.AntiPrompts)
|
|
|
|
|
{
|
|
|
|
|
totalResponse = totalResponse.Replace(anti, "").Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await onMessageReceived(new RoleDialogModel(AgentRole.Assistant, totalResponse));
|
2023-08-10 12:25:08 +00:00
|
|
|
|
|
|
|
|
return true;
|
2023-07-27 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 15:07:39 +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-06 12:58:02 +00:00
|
|
|
var content = string.Join("\n", conversations.Select(x => $"{x.Role}: {x.Content}")).Trim();
|
|
|
|
|
content += $"\n{AgentRole.Assistant}: ";
|
2023-06-27 23:36:50 +00:00
|
|
|
|
|
|
|
|
var llama = _services.GetRequiredService<LlamaAiModel>();
|
|
|
|
|
llama.LoadModel();
|
2023-09-06 12:58:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
return true;
|
2023-05-27 01:58:31 +00:00
|
|
|
}
|
|
|
|
|
}
|