2023-10-09 22:28:17 +00:00
|
|
|
using BotSharp.Abstraction.Conversations;
|
|
|
|
|
using BotSharp.Plugin.GoogleAI.Settings;
|
|
|
|
|
using LLMSharp.Google.Palm;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2023-10-08 20:46:42 +00:00
|
|
|
namespace BotSharp.Plugin.GoogleAI.Providers;
|
|
|
|
|
|
|
|
|
|
public class TextCompletionProvider : ITextCompletion
|
|
|
|
|
{
|
|
|
|
|
public string Provider => "google-ai";
|
2023-10-09 22:28:17 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly GoogleAiSettings _settings;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ITokenStatistics _tokenStatistics;
|
2023-10-08 20:46:42 +00:00
|
|
|
private string _model;
|
|
|
|
|
|
2023-10-09 22:28:17 +00:00
|
|
|
public TextCompletionProvider(IServiceProvider services,
|
|
|
|
|
GoogleAiSettings settings,
|
|
|
|
|
ILogger<TextCompletionProvider> logger,
|
|
|
|
|
ITokenStatistics tokenStatistics)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_tokenStatistics = tokenStatistics;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetCompletion(string text)
|
2023-10-08 20:46:42 +00:00
|
|
|
{
|
2023-10-09 22:28:17 +00:00
|
|
|
var client = new GooglePalmClient(apiKey: _settings.PaLM.ApiKey);
|
|
|
|
|
_tokenStatistics.StartTimer();
|
|
|
|
|
var response = await client.GenerateTextAsync(text, null);
|
|
|
|
|
_tokenStatistics.StopTimer();
|
|
|
|
|
|
|
|
|
|
var message = response.Candidates.First();
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation(text);
|
|
|
|
|
|
|
|
|
|
return message.Output.Trim();
|
2023-10-08 20:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetModelName(string model)
|
|
|
|
|
{
|
|
|
|
|
_model = model;
|
|
|
|
|
}
|
|
|
|
|
}
|