2023-06-17 02:42:35 +00:00
|
|
|
using Azure;
|
|
|
|
|
using Azure.AI.OpenAI;
|
2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
2023-07-30 18:22:07 +00:00
|
|
|
using BotSharp.Abstraction.Functions.Models;
|
2023-06-19 18:32:49 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks;
|
|
|
|
|
using BotSharp.Plugin.AzureOpenAI.Settings;
|
2023-08-07 17:48:09 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-06-17 02:42:35 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-08-07 22:37:32 +00:00
|
|
|
using System.Linq;
|
2023-07-26 21:05:30 +00:00
|
|
|
using System.Text.Json;
|
2023-06-17 02:42:35 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-06-17 13:32:39 +00:00
|
|
|
namespace BotSharp.Plugin.AzureOpenAI.Providers;
|
2023-06-19 18:32:49 +00:00
|
|
|
|
|
|
|
|
public class ChatCompletionProvider : IChatCompletion
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
|
|
|
|
private readonly AzureOpenAiSettings _settings;
|
2023-08-07 17:48:09 +00:00
|
|
|
private readonly ILogger _logger;
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-08-07 17:48:09 +00:00
|
|
|
public ChatCompletionProvider(AzureOpenAiSettings settings, ILogger<ChatCompletionProvider> logger)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
|
|
|
|
_settings = settings;
|
2023-08-07 17:48:09 +00:00
|
|
|
_logger = logger;
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 20:15:09 +00:00
|
|
|
public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
|
|
|
|
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
|
2023-07-21 20:15:09 +00:00
|
|
|
var chatCompletionsOptions = PrepareOptions(agent, conversations);
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-07-21 20:15:09 +00:00
|
|
|
var response = client.GetChatCompletions(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-07-21 20:15:09 +00:00
|
|
|
string output = "";
|
|
|
|
|
foreach (var choice in response.Value.Choices)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-07-21 20:15:09 +00:00
|
|
|
var message = choice.Message;
|
|
|
|
|
if (message.Content == null)
|
|
|
|
|
continue;
|
|
|
|
|
Console.Write(message.Content);
|
|
|
|
|
output += message.Content;
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-07 17:48:09 +00:00
|
|
|
_logger.LogInformation(output);
|
|
|
|
|
|
2023-07-21 20:15:09 +00:00
|
|
|
return output.Trim();
|
|
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public List<RoleDialogModel> GetChatSamples(string sampleText)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
|
|
|
|
var samples = new List<RoleDialogModel>();
|
2023-07-21 01:55:40 +00:00
|
|
|
if (string.IsNullOrEmpty(sampleText))
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-07-21 01:55:40 +00:00
|
|
|
return samples;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lines = sampleText.Split('\n');
|
|
|
|
|
for (int i = 0; i < lines.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var line = lines[i];
|
|
|
|
|
if (string.IsNullOrEmpty(line.Trim()))
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-07-21 01:55:40 +00:00
|
|
|
continue;
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
2023-07-21 01:55:40 +00:00
|
|
|
var role = line.Substring(0, line.IndexOf(' ') - 1).Trim();
|
|
|
|
|
var content = line.Substring(line.IndexOf(' ') + 1).Trim();
|
|
|
|
|
|
|
|
|
|
// comments
|
|
|
|
|
if (role == "##")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
samples.Add(new RoleDialogModel(role, content));
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
2023-07-21 01:55:40 +00:00
|
|
|
|
2023-06-17 02:42:35 +00:00
|
|
|
return samples;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 21:05:30 +00:00
|
|
|
public List<FunctionDef> GetFunctions(string functionsJson)
|
|
|
|
|
{
|
|
|
|
|
var functions = new List<FunctionDef>();
|
|
|
|
|
if (!string.IsNullOrEmpty(functionsJson))
|
|
|
|
|
{
|
|
|
|
|
functions = JsonSerializer.Deserialize<List<FunctionDef>>(functionsJson, new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
|
|
|
|
AllowTrailingCommas = true
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return functions;
|
|
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-07-27 21:56:57 +00:00
|
|
|
public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
|
|
|
|
|
{
|
|
|
|
|
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
|
|
|
|
|
var chatCompletionsOptions = PrepareOptions(agent, conversations);
|
|
|
|
|
|
|
|
|
|
var response = await client.GetChatCompletionsAsync(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
|
|
|
|
var choice = response.Value.Choices[0];
|
|
|
|
|
var message = choice.Message;
|
|
|
|
|
|
|
|
|
|
if (choice.FinishReason == CompletionsFinishReason.FunctionCall)
|
|
|
|
|
{
|
|
|
|
|
if (message.FunctionCall == null || message.FunctionCall.Arguments == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-08-09 21:49:55 +00:00
|
|
|
_logger.LogInformation($"{message.FunctionCall.Name}: {message.FunctionCall.Arguments}");
|
2023-07-27 21:56:57 +00:00
|
|
|
var funcContextIn = new RoleDialogModel(ChatRole.Function.ToString(), message.FunctionCall.Arguments)
|
|
|
|
|
{
|
2023-07-28 04:27:12 +00:00
|
|
|
FunctionName = message.FunctionCall.Name
|
2023-07-27 21:56:57 +00:00
|
|
|
};
|
2023-07-28 04:27:12 +00:00
|
|
|
|
2023-07-30 18:22:07 +00:00
|
|
|
// Execute functions
|
2023-07-27 21:56:57 +00:00
|
|
|
await onMessageReceived(funcContextIn);
|
|
|
|
|
|
|
|
|
|
// After function is executed, pass the result to LLM
|
2023-08-07 22:37:32 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.Function, funcContextIn.ExecutionResult)
|
2023-07-28 04:27:12 +00:00
|
|
|
{
|
|
|
|
|
Name = funcContextIn.FunctionName
|
|
|
|
|
});
|
|
|
|
|
response = client.GetChatCompletions(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
2023-07-27 21:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-28 04:27:12 +00:00
|
|
|
choice = response.Value.Choices[0];
|
|
|
|
|
message = choice.Message;
|
2023-08-07 17:48:09 +00:00
|
|
|
|
|
|
|
|
_logger.LogInformation(message.Content);
|
|
|
|
|
|
2023-07-28 04:27:12 +00:00
|
|
|
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), message.Content));
|
|
|
|
|
|
2023-07-27 21:56:57 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
public async Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
|
|
|
|
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
|
2023-06-27 18:31:13 +00:00
|
|
|
var chatCompletionsOptions = PrepareOptions(agent, conversations);
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-06-19 18:32:49 +00:00
|
|
|
var response = await client.GetChatCompletionsStreamingAsync(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
2023-06-17 02:42:35 +00:00
|
|
|
using StreamingChatCompletions streaming = response.Value;
|
|
|
|
|
|
|
|
|
|
string output = "";
|
|
|
|
|
await foreach (var choice in streaming.GetChoicesStreaming())
|
|
|
|
|
{
|
2023-07-27 15:07:39 +00:00
|
|
|
if (choice.FinishReason == CompletionsFinishReason.FunctionCall)
|
|
|
|
|
{
|
2023-07-27 21:56:57 +00:00
|
|
|
var args = "";
|
|
|
|
|
await foreach (var message in choice.GetMessageStreaming())
|
|
|
|
|
{
|
|
|
|
|
if (message.FunctionCall == null || message.FunctionCall.Arguments == null)
|
|
|
|
|
continue;
|
|
|
|
|
Console.Write(message.FunctionCall.Arguments);
|
|
|
|
|
args += message.FunctionCall.Arguments;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), args));
|
|
|
|
|
continue;
|
2023-07-27 15:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-17 02:42:35 +00:00
|
|
|
await foreach (var message in choice.GetMessageStreaming())
|
|
|
|
|
{
|
|
|
|
|
if (message.Content == null)
|
|
|
|
|
continue;
|
|
|
|
|
Console.Write(message.Content);
|
|
|
|
|
output += message.Content;
|
2023-08-07 17:48:09 +00:00
|
|
|
|
|
|
|
|
_logger.LogInformation(message.Content);
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
await onMessageReceived(new RoleDialogModel(message.Role.ToString(), message.Content));
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
2023-07-27 15:07:39 +00:00
|
|
|
|
|
|
|
|
output = "";
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-27 15:07:39 +00:00
|
|
|
return true;
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
private ChatCompletionsOptions PrepareOptions(Agent agent, List<RoleDialogModel> conversations)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-06-29 23:14:57 +00:00
|
|
|
var chatCompletionsOptions = new ChatCompletionsOptions();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(agent.Instruction))
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-06-29 23:14:57 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.System, agent.Instruction));
|
|
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-06-29 23:14:57 +00:00
|
|
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
|
|
|
|
{
|
|
|
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.System, agent.Knowledges));
|
|
|
|
|
}
|
2023-07-21 01:55:40 +00:00
|
|
|
|
|
|
|
|
var samples = GetChatSamples(agent.Samples);
|
|
|
|
|
foreach (var message in samples)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-07-21 20:15:09 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-26 21:05:30 +00:00
|
|
|
var functions = GetFunctions(agent.Functions);
|
|
|
|
|
foreach (var function in functions)
|
|
|
|
|
{
|
|
|
|
|
chatCompletionsOptions.Functions.Add(new FunctionDefinition
|
|
|
|
|
{
|
|
|
|
|
Name = function.Name,
|
|
|
|
|
Description = function.Description,
|
|
|
|
|
Parameters = BinaryData.FromObjectAsJson(function.Parameters)
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-07-28 15:54:29 +00:00
|
|
|
|
2023-06-17 02:42:35 +00:00
|
|
|
foreach (var message in conversations)
|
|
|
|
|
{
|
2023-07-28 15:54:29 +00:00
|
|
|
if (message.Role == ChatRole.Function)
|
|
|
|
|
{
|
|
|
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)
|
|
|
|
|
{
|
2023-08-07 22:37:32 +00:00
|
|
|
Name = message.FunctionName
|
2023-07-28 15:54:29 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
|
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-07 22:37:32 +00:00
|
|
|
_logger.LogInformation(string.Join("\n", chatCompletionsOptions.Messages.Select(x => $"{x.Role}: {x.Content}")));
|
2023-06-17 02:42:35 +00:00
|
|
|
return chatCompletionsOptions;
|
|
|
|
|
}
|
|
|
|
|
}
|