using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Conversations.Settings; using BotSharp.Plugin.HuggingFace.Services; using BotSharp.Plugin.HuggingFace.Settings; using Microsoft.Extensions.Logging; namespace BotSharp.Plugin.HuggingFace.Providers; public class ChatCompletionProvider : IChatCompletion { public string Provider => "huggingface"; private readonly IServiceProvider _services; private readonly HuggingFaceSettings _settings; private readonly ILogger _logger; private string _model; public ChatCompletionProvider(IServiceProvider services, HuggingFaceSettings settings, ILogger logger) { _services = services; _settings = settings; _logger = logger; } public async Task GetChatCompletionsAsync(Agent agent, List conversations, Func onMessageReceived, Func onFunctionExecuting) { var content = string.Join("\r\n", conversations.Select(x => $"{AgentRole.System}: {x.Content}")).Trim(); content += $"\r\n{AgentRole.Assistant}: "; var prompt = agent.Instruction + "\r\n" + content; var convSetting = _services.GetRequiredService(); if (convSetting.ShowVerboseLog) { _logger.LogInformation(prompt); } var api = _services.GetRequiredService(); var space = _model.Split('/')[0]; var model = _model.Split("/")[1]; var response = await api.Post(space, model, new InferenceInput { Inputs = prompt }); var falcon = JsonSerializer.Deserialize>(response); var message = falcon[0].GeneratedText.Trim(); _logger.LogInformation($"[{agent.Name}] {AgentRole.Assistant}: {message}"); var msg = new RoleDialogModel(AgentRole.Assistant, message) { CurrentAgentId = agent.Id }; // Text response received await onMessageReceived(msg); return true; } public async Task GetChatCompletionsStreamingAsync(Agent agent, List conversations, Func onMessageReceived) { return true; } public void SetModelName(string model) { _model = model; } public RoleDialogModel GetChatCompletions(Agent agent, List conversations) { var content = string.Join("\r\n", conversations.Select(x => $"{AgentRole.System}: {x.Content}")).Trim(); content += $"\r\n{AgentRole.Assistant}: "; var prompt = agent.Instruction + "\r\n" + content; var convSetting = _services.GetRequiredService(); if (convSetting.ShowVerboseLog) { _logger.LogInformation(prompt); } var api = _services.GetRequiredService(); var space = _model.Split('/')[0]; var model = _model.Split("/")[1]; var response = api.Post(space, model, new InferenceInput { Inputs = prompt }).Result; var falcon = JsonSerializer.Deserialize>(response); var message = falcon[0].GeneratedText.Trim(); _logger.LogInformation($"[{agent.Name}] {AgentRole.Assistant}: {message}"); var msg = new RoleDialogModel(AgentRole.Assistant, message) { CurrentAgentId = agent.Id }; return msg; } }