293 lines
9.9 KiB
C#
293 lines
9.9 KiB
C#
using Azure;
|
|
using Azure.AI.OpenAI;
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
using BotSharp.Abstraction.Conversations.Models;
|
|
using BotSharp.Abstraction.Functions.Models;
|
|
using BotSharp.Abstraction.MLTasks;
|
|
using BotSharp.Plugin.AzureOpenAI.Settings;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BotSharp.Plugin.AzureOpenAI.Providers;
|
|
|
|
public class ChatCompletionProvider : IChatCompletion
|
|
{
|
|
private readonly AzureOpenAiSettings _settings;
|
|
private readonly ILogger _logger;
|
|
|
|
public ChatCompletionProvider(AzureOpenAiSettings settings, ILogger<ChatCompletionProvider> logger)
|
|
{
|
|
_settings = settings;
|
|
_logger = logger;
|
|
}
|
|
|
|
private OpenAIClient GetClient()
|
|
{
|
|
var client = new OpenAIClient(new Uri(_settings.Endpoint), new AzureKeyCredential(_settings.ApiKey));
|
|
return client;
|
|
}
|
|
|
|
/*public string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
|
|
{
|
|
var client = GetClient();
|
|
var chatCompletionsOptions = PrepareOptions(agent, conversations);
|
|
|
|
var response = client.GetChatCompletions(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
|
var choice = response.Value.Choices[0];
|
|
var message = choice.Message;
|
|
|
|
if (choice.FinishReason == CompletionsFinishReason.FunctionCall)
|
|
{
|
|
response = HandleFunctionCall(message,
|
|
onMessageReceived,
|
|
chatCompletionsOptions).Result;
|
|
}
|
|
|
|
choice = response.Value.Choices[0];
|
|
message = choice.Message;
|
|
|
|
_logger.LogInformation(message.Content);
|
|
|
|
if (!string.IsNullOrEmpty(message.Content))
|
|
{
|
|
onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), message.Content))
|
|
.Wait();
|
|
}
|
|
|
|
return message.Content.Trim();
|
|
}*/
|
|
|
|
public List<RoleDialogModel> GetChatSamples(string sampleText)
|
|
{
|
|
var samples = new List<RoleDialogModel>();
|
|
if (string.IsNullOrEmpty(sampleText))
|
|
{
|
|
return samples;
|
|
}
|
|
|
|
var lines = sampleText.Split('\n');
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i];
|
|
if (string.IsNullOrEmpty(line.Trim()))
|
|
{
|
|
continue;
|
|
}
|
|
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));
|
|
}
|
|
|
|
return samples;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public async Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
|
|
{
|
|
var client = GetClient();
|
|
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)
|
|
{
|
|
response = await HandleFunctionCall(agent,
|
|
message,
|
|
onMessageReceived,
|
|
chatCompletionsOptions);
|
|
}
|
|
|
|
if (response != null)
|
|
{
|
|
choice = response.Value.Choices[0];
|
|
message = choice.Message;
|
|
|
|
_logger.LogInformation(message.Content);
|
|
|
|
if (!string.IsNullOrEmpty(message.Content))
|
|
{
|
|
var msgByLlm = new RoleDialogModel(ChatRole.Assistant.ToString(), message.Content);
|
|
await onMessageReceived(msgByLlm);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> GetChatCompletionsStreamingAsync(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.GetChatCompletionsStreamingAsync(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
|
using StreamingChatCompletions streaming = response.Value;
|
|
|
|
string output = "";
|
|
await foreach (var choice in streaming.GetChoicesStreaming())
|
|
{
|
|
if (choice.FinishReason == CompletionsFinishReason.FunctionCall)
|
|
{
|
|
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;
|
|
}
|
|
|
|
await foreach (var message in choice.GetMessageStreaming())
|
|
{
|
|
if (message.Content == null)
|
|
continue;
|
|
Console.Write(message.Content);
|
|
output += message.Content;
|
|
|
|
_logger.LogInformation(message.Content);
|
|
|
|
await onMessageReceived(new RoleDialogModel(message.Role.ToString(), message.Content));
|
|
}
|
|
|
|
output = "";
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private async Task<Response<ChatCompletions>> HandleFunctionCall(Agent agent,
|
|
ChatMessage message,
|
|
Func<RoleDialogModel, Task> onMessageReceived,
|
|
ChatCompletionsOptions chatCompletionsOptions)
|
|
{
|
|
Response<ChatCompletions> response = default;
|
|
|
|
if (message.FunctionCall == null || message.FunctionCall.Arguments == null)
|
|
{
|
|
return response;
|
|
}
|
|
|
|
_logger.LogInformation($"{message.FunctionCall.Name}: {message.FunctionCall.Arguments}");
|
|
var funcContextIn = new RoleDialogModel(ChatRole.Function.ToString(), message.Content)
|
|
{
|
|
CurrentAgentId = agent.Id,
|
|
FunctionName = message.FunctionCall.Name,
|
|
FunctionArgs = message.FunctionCall.Arguments
|
|
};
|
|
|
|
// Execute functions
|
|
await onMessageReceived(funcContextIn);
|
|
|
|
if (funcContextIn.StopPropagate)
|
|
{
|
|
return response;
|
|
}
|
|
|
|
if (funcContextIn.IsConversationEnd)
|
|
{
|
|
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), funcContextIn.Content)
|
|
{
|
|
IsConversationEnd = true
|
|
});
|
|
return response;
|
|
}
|
|
|
|
// After function is executed, pass the result to LLM
|
|
if (funcContextIn.ExecutionResult != null)
|
|
{
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.Function, funcContextIn.ExecutionResult)
|
|
{
|
|
Name = funcContextIn.FunctionName
|
|
});
|
|
var client = GetClient();
|
|
response = client.GetChatCompletions(_settings.DeploymentModel.ChatCompletionModel, chatCompletionsOptions);
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
private ChatCompletionsOptions PrepareOptions(Agent agent, List<RoleDialogModel> conversations)
|
|
{
|
|
var chatCompletionsOptions = new ChatCompletionsOptions();
|
|
|
|
if (!string.IsNullOrEmpty(agent.Instruction))
|
|
{
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.System, agent.Instruction));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
|
{
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(ChatRole.System, agent.Knowledges));
|
|
}
|
|
|
|
var samples = GetChatSamples(agent.Samples);
|
|
foreach (var message in samples)
|
|
{
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
|
}
|
|
|
|
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)
|
|
});
|
|
}
|
|
|
|
foreach (var message in conversations)
|
|
{
|
|
if (message.Role == ChatRole.Function)
|
|
{
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content)
|
|
{
|
|
Name = message.FunctionName
|
|
});
|
|
}
|
|
else
|
|
{
|
|
chatCompletionsOptions.Messages.Add(new ChatMessage(message.Role, message.Content));
|
|
}
|
|
}
|
|
|
|
// https://community.openai.com/t/cheat-sheet-mastering-temperature-and-top-p-in-chatgpt-api-a-few-tips-and-tricks-on-controlling-the-creativity-deterministic-output-of-prompt-responses/172683
|
|
chatCompletionsOptions.Temperature = 0.5f;
|
|
chatCompletionsOptions.NucleusSamplingFactor = 0.5f;
|
|
|
|
_logger.LogInformation(string.Join("\n", chatCompletionsOptions.Messages.Select(x => $"{x.Role}: {x.Content}")));
|
|
return chatCompletionsOptions;
|
|
}
|
|
}
|