2023-06-17 02:42:35 +00:00
|
|
|
using Azure.AI.OpenAI;
|
|
|
|
|
using BotSharp.Abstraction.MLTasks;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-06-19 18:32:49 +00:00
|
|
|
using BotSharp.Plugin.AzureOpenAI.Settings;
|
2023-08-07 10:21:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-10-09 22:28:17 +00:00
|
|
|
using BotSharp.Abstraction.Conversations;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
2023-10-13 20:08:59 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-06-17 13:32:39 +00:00
|
|
|
namespace BotSharp.Plugin.AzureOpenAI.Providers;
|
2023-06-17 02:42:35 +00:00
|
|
|
|
|
|
|
|
public class TextCompletionProvider : ITextCompletion
|
|
|
|
|
{
|
2023-10-09 22:28:17 +00:00
|
|
|
private readonly IServiceProvider _services;
|
2023-06-17 02:42:35 +00:00
|
|
|
private readonly AzureOpenAiSettings _settings;
|
2023-08-07 10:21:31 +00:00
|
|
|
private readonly ILogger _logger;
|
2023-10-09 22:28:17 +00:00
|
|
|
private readonly ITokenStatistics _tokenStatistics;
|
2023-10-08 20:46:42 +00:00
|
|
|
private string _model;
|
|
|
|
|
public string Provider => "azure-openai";
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-10-09 22:28:17 +00:00
|
|
|
public TextCompletionProvider(IServiceProvider services,
|
|
|
|
|
AzureOpenAiSettings settings,
|
|
|
|
|
ILogger<TextCompletionProvider> logger,
|
|
|
|
|
ITokenStatistics tokenStatistics)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-10-09 22:28:17 +00:00
|
|
|
_services = services;
|
2023-06-17 02:42:35 +00:00
|
|
|
_settings = settings;
|
2023-08-07 10:21:31 +00:00
|
|
|
_logger = logger;
|
2023-10-09 22:28:17 +00:00
|
|
|
_tokenStatistics = tokenStatistics;
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetCompletion(string text)
|
|
|
|
|
{
|
2023-10-09 22:28:17 +00:00
|
|
|
var (client, _) = ProviderHelper.GetClient(_model, _settings);
|
|
|
|
|
|
2023-06-17 02:42:35 +00:00
|
|
|
var completionsOptions = new CompletionsOptions()
|
|
|
|
|
{
|
|
|
|
|
Prompts =
|
|
|
|
|
{
|
|
|
|
|
text
|
|
|
|
|
},
|
2023-10-13 20:08:59 +00:00
|
|
|
MaxTokens = 256,
|
2023-06-17 02:42:35 +00:00
|
|
|
};
|
2023-10-13 20:08:59 +00:00
|
|
|
completionsOptions.StopSequences.Add($"{AgentRole.Assistant}:");
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-10-09 22:28:17 +00:00
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
var temperature = float.Parse(state.GetState("temperature", "0.5"));
|
|
|
|
|
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.5"));
|
|
|
|
|
completionsOptions.Temperature = temperature;
|
|
|
|
|
completionsOptions.NucleusSamplingFactor = samplingFactor;
|
|
|
|
|
|
|
|
|
|
_tokenStatistics.StartTimer();
|
2023-06-17 02:42:35 +00:00
|
|
|
var response = await client.GetCompletionsAsync(
|
2023-06-19 18:32:49 +00:00
|
|
|
deploymentOrModelName: _settings.DeploymentModel.TextCompletionModel,
|
2023-06-17 02:42:35 +00:00
|
|
|
completionsOptions);
|
2023-10-09 22:28:17 +00:00
|
|
|
_tokenStatistics.StopTimer();
|
|
|
|
|
|
|
|
|
|
_tokenStatistics.AddToken(new TokenStatsModel
|
|
|
|
|
{
|
|
|
|
|
Model = _model,
|
|
|
|
|
PromptCount = response.Value.Usage.PromptTokens,
|
|
|
|
|
CompletionCount = response.Value.Usage.CompletionTokens,
|
|
|
|
|
PromptCost = 0.0015f,
|
|
|
|
|
CompletionCost = 0.002f
|
|
|
|
|
});
|
2023-06-17 02:42:35 +00:00
|
|
|
|
|
|
|
|
// OpenAI
|
|
|
|
|
var completion = "";
|
|
|
|
|
foreach (var t in response.Value.Choices)
|
|
|
|
|
{
|
|
|
|
|
completion += t.Text;
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-09 22:28:17 +00:00
|
|
|
_logger.LogInformation(text);
|
2023-08-07 10:21:31 +00:00
|
|
|
|
2023-06-27 19:17:53 +00:00
|
|
|
return completion.Trim();
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 20:46:42 +00:00
|
|
|
public void SetModelName(string model)
|
|
|
|
|
{
|
|
|
|
|
_model = model;
|
|
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|