2023-06-17 02:42:35 +00:00
|
|
|
using Azure.AI.OpenAI;
|
2023-10-28 20:59:26 +00:00
|
|
|
using BotSharp.Abstraction.Agents;
|
2023-08-18 04:27:07 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
2023-09-14 01:41:51 +00:00
|
|
|
using BotSharp.Abstraction.Conversations;
|
2023-06-27 18:31:13 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
2023-11-29 23:23:14 +00:00
|
|
|
using BotSharp.Abstraction.Loggers;
|
2023-06-19 18:32:49 +00:00
|
|
|
using BotSharp.Abstraction.MLTasks;
|
|
|
|
|
using BotSharp.Plugin.AzureOpenAI.Settings;
|
2023-08-20 20:33:35 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
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-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
|
|
|
{
|
2024-03-25 23:14:14 +00:00
|
|
|
protected readonly AzureOpenAiSettings _settings;
|
|
|
|
|
protected readonly IServiceProvider _services;
|
|
|
|
|
protected readonly ILogger _logger;
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2024-03-25 23:14:14 +00:00
|
|
|
protected string _model;
|
|
|
|
|
|
|
|
|
|
public virtual string Provider => "azure-openai";
|
2023-09-09 15:37:38 +00:00
|
|
|
|
2023-08-20 20:33:35 +00:00
|
|
|
public ChatCompletionProvider(AzureOpenAiSettings settings,
|
|
|
|
|
ILogger<ChatCompletionProvider> logger,
|
2023-10-16 20:07:07 +00:00
|
|
|
IServiceProvider services)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
|
|
|
|
_settings = settings;
|
2023-08-07 17:48:09 +00:00
|
|
|
_logger = logger;
|
2023-08-20 20:33:35 +00:00
|
|
|
_services = services;
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-14 04:48:26 +00:00
|
|
|
public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
|
2023-09-27 12:51:15 +00:00
|
|
|
{
|
2023-11-29 23:23:14 +00:00
|
|
|
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
|
2023-10-16 20:07:07 +00:00
|
|
|
|
|
|
|
|
// Before chat completion hook
|
2023-12-01 17:31:45 +00:00
|
|
|
foreach (var hook in contentHooks)
|
|
|
|
|
{
|
2024-01-14 04:48:26 +00:00
|
|
|
await hook.BeforeGenerating(agent, conversations);
|
2023-12-01 17:31:45 +00:00
|
|
|
}
|
2023-10-16 20:07:07 +00:00
|
|
|
|
2024-03-25 23:14:14 +00:00
|
|
|
var client = ProviderHelper.GetClient(Provider, _model, _services);
|
2023-10-30 16:48:18 +00:00
|
|
|
var (prompt, chatCompletionsOptions) = PrepareOptions(agent, conversations);
|
2023-11-13 17:19:25 +00:00
|
|
|
chatCompletionsOptions.DeploymentName = _model;
|
|
|
|
|
var response = client.GetChatCompletions(chatCompletionsOptions);
|
2023-09-27 12:51:15 +00:00
|
|
|
var choice = response.Value.Choices[0];
|
|
|
|
|
var message = choice.Message;
|
|
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
var responseMessage = new RoleDialogModel(AgentRole.Assistant, message.Content)
|
2023-09-27 12:51:15 +00:00
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
CurrentAgentId = agent.Id,
|
|
|
|
|
MessageId = conversations.Last().MessageId
|
2023-10-16 20:07:07 +00:00
|
|
|
};
|
2023-09-27 12:51:15 +00:00
|
|
|
|
|
|
|
|
if (choice.FinishReason == CompletionsFinishReason.FunctionCall)
|
|
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
responseMessage = new RoleDialogModel(AgentRole.Function, message.Content)
|
2023-09-27 12:51:15 +00:00
|
|
|
{
|
|
|
|
|
CurrentAgentId = agent.Id,
|
2023-10-30 16:48:18 +00:00
|
|
|
MessageId = conversations.Last().MessageId,
|
2023-09-27 12:51:15 +00:00
|
|
|
FunctionName = message.FunctionCall.Name,
|
|
|
|
|
FunctionArgs = message.FunctionCall.Arguments
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Somethings LLM will generate a function name with agent name.
|
2023-10-30 16:48:18 +00:00
|
|
|
if (!string.IsNullOrEmpty(responseMessage.FunctionName))
|
2023-09-27 12:51:15 +00:00
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
responseMessage.FunctionName = responseMessage.FunctionName.Split('.').Last();
|
2023-09-27 12:51:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-16 20:07:07 +00:00
|
|
|
|
|
|
|
|
// After chat completion hook
|
2023-12-01 17:31:45 +00:00
|
|
|
foreach(var hook in contentHooks)
|
|
|
|
|
{
|
2024-01-14 04:48:26 +00:00
|
|
|
await hook.AfterGenerated(responseMessage, new TokenStatsModel
|
2023-09-27 12:51:15 +00:00
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
Prompt = prompt,
|
2023-12-13 18:12:25 +00:00
|
|
|
Provider = Provider,
|
2023-10-16 20:07:07 +00:00
|
|
|
Model = _model,
|
|
|
|
|
PromptCount = response.Value.Usage.PromptTokens,
|
|
|
|
|
CompletionCount = response.Value.Usage.CompletionTokens
|
2024-01-14 04:48:26 +00:00
|
|
|
});
|
2023-12-01 17:31:45 +00:00
|
|
|
}
|
2023-09-27 12:51:15 +00:00
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
return responseMessage;
|
2023-09-27 12:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-18 04:27:07 +00:00
|
|
|
public async Task<bool> GetChatCompletionsAsync(Agent agent,
|
|
|
|
|
List<RoleDialogModel> conversations,
|
|
|
|
|
Func<RoleDialogModel, Task> onMessageReceived,
|
|
|
|
|
Func<RoleDialogModel, Task> onFunctionExecuting)
|
2023-07-27 21:56:57 +00:00
|
|
|
{
|
2023-10-16 20:07:07 +00:00
|
|
|
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
|
|
|
|
|
|
|
|
|
|
// Before chat completion hook
|
2023-12-01 17:31:45 +00:00
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.BeforeGenerating(agent, conversations);
|
|
|
|
|
}
|
2023-10-16 20:07:07 +00:00
|
|
|
|
2024-03-25 23:14:14 +00:00
|
|
|
var client = ProviderHelper.GetClient(Provider, _model, _services);
|
2023-10-30 16:48:18 +00:00
|
|
|
var (prompt, chatCompletionsOptions) = PrepareOptions(agent, conversations);
|
2023-07-27 21:56:57 +00:00
|
|
|
|
2023-11-13 17:19:25 +00:00
|
|
|
chatCompletionsOptions.DeploymentName = _model;
|
|
|
|
|
var response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
|
2023-07-27 21:56:57 +00:00
|
|
|
var choice = response.Value.Choices[0];
|
|
|
|
|
var message = choice.Message;
|
|
|
|
|
|
2023-10-16 20:07:07 +00:00
|
|
|
var msg = new RoleDialogModel(AgentRole.Assistant, message.Content)
|
2023-09-26 03:04:04 +00:00
|
|
|
{
|
2023-10-16 20:07:07 +00:00
|
|
|
CurrentAgentId = agent.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// After chat completion hook
|
2023-12-01 17:31:45 +00:00
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
|
|
|
|
await hook.AfterGenerated(msg, new TokenStatsModel
|
2023-10-16 20:07:07 +00:00
|
|
|
{
|
2023-10-30 16:48:18 +00:00
|
|
|
Prompt = prompt,
|
2023-12-13 18:12:25 +00:00
|
|
|
Provider = Provider,
|
2023-10-16 20:07:07 +00:00
|
|
|
Model = _model,
|
|
|
|
|
PromptCount = response.Value.Usage.PromptTokens,
|
|
|
|
|
CompletionCount = response.Value.Usage.CompletionTokens
|
2023-12-01 17:31:45 +00:00
|
|
|
});
|
|
|
|
|
}
|
2023-09-04 02:21:53 +00:00
|
|
|
|
2023-07-27 21:56:57 +00:00
|
|
|
if (choice.FinishReason == CompletionsFinishReason.FunctionCall)
|
|
|
|
|
{
|
2023-10-09 22:28:17 +00:00
|
|
|
_logger.LogInformation($"[{agent.Name}]: {message.FunctionCall.Name}({message.FunctionCall.Arguments})");
|
2023-07-28 04:27:12 +00:00
|
|
|
|
2023-08-18 04:27:07 +00:00
|
|
|
var funcContextIn = new RoleDialogModel(AgentRole.Function, message.Content)
|
|
|
|
|
{
|
|
|
|
|
CurrentAgentId = agent.Id,
|
|
|
|
|
FunctionName = message.FunctionCall.Name,
|
2023-09-14 01:41:51 +00:00
|
|
|
FunctionArgs = message.FunctionCall.Arguments
|
2023-08-18 04:27:07 +00:00
|
|
|
};
|
2023-07-27 21:56:57 +00:00
|
|
|
|
2023-09-08 22:05:55 +00:00
|
|
|
// Somethings LLM will generate a function name with agent name.
|
|
|
|
|
if (!string.IsNullOrEmpty(funcContextIn.FunctionName))
|
|
|
|
|
{
|
|
|
|
|
funcContextIn.FunctionName = funcContextIn.FunctionName.Split('.').Last();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 04:27:07 +00:00
|
|
|
// Execute functions
|
|
|
|
|
await onFunctionExecuting(funcContextIn);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Text response received
|
|
|
|
|
await onMessageReceived(msg);
|
2023-08-10 22:17:55 +00:00
|
|
|
}
|
2023-07-28 04:27:12 +00:00
|
|
|
|
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
|
|
|
{
|
2024-03-25 23:14:14 +00:00
|
|
|
var client = ProviderHelper.GetClient(Provider, _model, _services);
|
2023-10-30 16:48:18 +00:00
|
|
|
var (prompt, chatCompletionsOptions) = PrepareOptions(agent, conversations);
|
2023-11-13 17:19:25 +00:00
|
|
|
chatCompletionsOptions.DeploymentName = _model;
|
|
|
|
|
var response = await client.GetChatCompletionsStreamingAsync(chatCompletionsOptions);
|
2023-06-17 02:42:35 +00:00
|
|
|
|
|
|
|
|
string output = "";
|
2023-11-13 17:19:25 +00:00
|
|
|
await foreach (var choice in response)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-07-27 15:07:39 +00:00
|
|
|
if (choice.FinishReason == CompletionsFinishReason.FunctionCall)
|
|
|
|
|
{
|
2023-11-13 17:19:25 +00:00
|
|
|
Console.Write(choice.FunctionArgumentsUpdate);
|
2023-07-27 21:56:57 +00:00
|
|
|
|
2023-11-13 17:19:25 +00:00
|
|
|
await onMessageReceived(new RoleDialogModel(ChatRole.Assistant.ToString(), choice.FunctionArgumentsUpdate));
|
2023-07-27 21:56:57 +00:00
|
|
|
continue;
|
2023-07-27 15:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-13 17:19:25 +00:00
|
|
|
if (choice.ContentUpdate == null)
|
|
|
|
|
continue;
|
|
|
|
|
Console.Write(choice.ContentUpdate);
|
2023-08-07 17:48:09 +00:00
|
|
|
|
2023-11-13 17:19:25 +00:00
|
|
|
_logger.LogInformation(choice.ContentUpdate);
|
2023-08-07 17:48:09 +00:00
|
|
|
|
2023-11-13 17:19:25 +00:00
|
|
|
await onMessageReceived(new RoleDialogModel(choice.Role.ToString(), choice.ContentUpdate));
|
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-08-17 04:04:23 +00:00
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
protected (string, ChatCompletionsOptions) PrepareOptions(Agent agent, List<RoleDialogModel> conversations)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-10-28 20:59:26 +00:00
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
|
2023-06-29 23:14:57 +00:00
|
|
|
var chatCompletionsOptions = new ChatCompletionsOptions();
|
2023-08-17 04:04:23 +00:00
|
|
|
|
2023-06-29 23:14:57 +00:00
|
|
|
if (!string.IsNullOrEmpty(agent.Instruction))
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-10-28 20:59:26 +00:00
|
|
|
var instruction = agentService.RenderedInstruction(agent);
|
2023-12-18 20:14:14 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatRequestSystemMessage(instruction));
|
2023-06-29 23:14:57 +00:00
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
|
2023-06-29 23:14:57 +00:00
|
|
|
if (!string.IsNullOrEmpty(agent.Knowledges))
|
|
|
|
|
{
|
2023-12-18 20:14:14 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatRequestSystemMessage(agent.Knowledges));
|
2023-06-29 23:14:57 +00:00
|
|
|
}
|
2023-07-21 01:55:40 +00:00
|
|
|
|
2023-10-09 22:28:17 +00:00
|
|
|
var samples = ProviderHelper.GetChatSamples(agent.Samples);
|
2023-07-21 01:55:40 +00:00
|
|
|
foreach (var message in samples)
|
2023-06-17 02:42:35 +00:00
|
|
|
{
|
2023-12-18 20:14:14 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(message.Role == AgentRole.User ?
|
|
|
|
|
new ChatRequestUserMessage(message.Content) :
|
|
|
|
|
new ChatRequestAssistantMessage(message.Content));
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-27 20:49:44 +00:00
|
|
|
foreach (var function in agent.Functions)
|
2023-07-26 21:05:30 +00:00
|
|
|
{
|
2024-03-31 19:28:16 +00:00
|
|
|
if (agentService.RenderFunction(agent, function))
|
2023-07-26 21:05:30 +00:00
|
|
|
{
|
2024-04-09 20:52:21 +00:00
|
|
|
var property = agentService.RenderFunctionProperty(agent, function);
|
2024-03-31 19:28:16 +00:00
|
|
|
chatCompletionsOptions.Functions.Add(new FunctionDefinition
|
|
|
|
|
{
|
|
|
|
|
Name = function.Name,
|
|
|
|
|
Description = function.Description,
|
2024-04-09 20:52:21 +00:00
|
|
|
Parameters = BinaryData.FromObjectAsJson(property)
|
2024-03-31 19:28:16 +00:00
|
|
|
});
|
|
|
|
|
}
|
2023-07-26 21:05:30 +00:00
|
|
|
}
|
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)
|
|
|
|
|
{
|
2024-02-22 12:48:10 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatRequestAssistantMessage(string.Empty)
|
2024-02-21 04:31:09 +00:00
|
|
|
{
|
2024-02-22 12:48:10 +00:00
|
|
|
FunctionCall = new FunctionCall(message.FunctionName, message.FunctionArgs ?? String.Empty),
|
|
|
|
|
});
|
2024-02-22 12:22:26 +00:00
|
|
|
|
2023-12-18 20:14:14 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatRequestFunctionMessage(message.FunctionName, message.Content));
|
|
|
|
|
}
|
|
|
|
|
else if (message.Role == ChatRole.User)
|
|
|
|
|
{
|
2024-03-13 15:01:09 +00:00
|
|
|
var userMessage = new ChatRequestUserMessage(message.Content)
|
2024-01-24 23:47:57 +00:00
|
|
|
{
|
|
|
|
|
// To display Planner name in log
|
2024-03-08 13:19:13 +00:00
|
|
|
Name = message.FunctionName,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(message.ImageUrl))
|
|
|
|
|
{
|
|
|
|
|
var uri = new Uri(message.ImageUrl);
|
|
|
|
|
userMessage.MultimodalContentItems.Add(
|
|
|
|
|
new ChatMessageImageContentItem(uri, ChatMessageImageDetailLevel.Low));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chatCompletionsOptions.Messages.Add(userMessage);
|
2023-07-28 15:54:29 +00:00
|
|
|
}
|
2023-12-18 20:14:14 +00:00
|
|
|
else if (message.Role == ChatRole.Assistant)
|
2023-07-28 15:54:29 +00:00
|
|
|
{
|
2023-12-18 20:14:14 +00:00
|
|
|
chatCompletionsOptions.Messages.Add(new ChatRequestAssistantMessage(message.Content));
|
2023-07-28 15:54:29 +00:00
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-17 04:04:23 +00:00
|
|
|
// 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
|
2023-09-14 01:41:51 +00:00
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
2024-03-07 18:27:29 +00:00
|
|
|
var temperature = float.Parse(state.GetState("temperature", "0.0"));
|
|
|
|
|
var samplingFactor = float.Parse(state.GetState("sampling_factor", "0.0"));
|
2023-09-14 01:41:51 +00:00
|
|
|
chatCompletionsOptions.Temperature = temperature;
|
|
|
|
|
chatCompletionsOptions.NucleusSamplingFactor = samplingFactor;
|
2024-03-20 15:44:52 +00:00
|
|
|
chatCompletionsOptions.MaxTokens = int.Parse(state.GetState("max_tokens", "1024"));
|
2023-09-23 21:33:05 +00:00
|
|
|
// chatCompletionsOptions.FrequencyPenalty = 0;
|
|
|
|
|
// chatCompletionsOptions.PresencePenalty = 0;
|
2023-08-17 04:04:23 +00:00
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
var prompt = GetPrompt(chatCompletionsOptions);
|
|
|
|
|
|
|
|
|
|
return (prompt, chatCompletionsOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetPrompt(ChatCompletionsOptions chatCompletionsOptions)
|
|
|
|
|
{
|
|
|
|
|
var prompt = string.Empty;
|
|
|
|
|
|
|
|
|
|
if (chatCompletionsOptions.Messages.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// System instruction
|
|
|
|
|
var verbose = string.Join("\r\n", chatCompletionsOptions.Messages
|
2023-12-18 20:14:14 +00:00
|
|
|
.Where(x => x.Role == AgentRole.System)
|
|
|
|
|
.Select(x => x as ChatRequestSystemMessage).Select(x =>
|
2023-10-30 16:48:18 +00:00
|
|
|
{
|
2024-01-24 23:47:57 +00:00
|
|
|
if (!string.IsNullOrEmpty(x.Name))
|
|
|
|
|
{
|
|
|
|
|
// To display Agent name in log
|
|
|
|
|
return $"[{x.Name}]: {x.Content}";
|
|
|
|
|
}
|
2023-10-30 16:48:18 +00:00
|
|
|
return $"{x.Role}: {x.Content}";
|
|
|
|
|
}));
|
2023-11-01 01:48:12 +00:00
|
|
|
prompt += $"{verbose}\r\n";
|
2023-10-30 16:48:18 +00:00
|
|
|
|
|
|
|
|
verbose = string.Join("\r\n", chatCompletionsOptions.Messages
|
|
|
|
|
.Where(x => x.Role != AgentRole.System).Select(x =>
|
2023-10-20 03:47:14 +00:00
|
|
|
{
|
2023-12-18 20:14:14 +00:00
|
|
|
if (x.Role == ChatRole.Function)
|
|
|
|
|
{
|
|
|
|
|
var m = x as ChatRequestFunctionMessage;
|
2024-02-21 04:31:09 +00:00
|
|
|
return $"{m.Role}: {m.Content}";
|
2023-12-18 20:14:14 +00:00
|
|
|
}
|
|
|
|
|
else if (x.Role == ChatRole.User)
|
|
|
|
|
{
|
|
|
|
|
var m = x as ChatRequestUserMessage;
|
2024-04-24 21:45:18 +00:00
|
|
|
return !string.IsNullOrEmpty(m.Name) && m.Name != "route_to_agent" ?
|
2024-01-29 18:08:49 +00:00
|
|
|
$"{m.Name}: {m.Content}" :
|
|
|
|
|
$"{m.Role}: {m.Content}";
|
2023-12-18 20:14:14 +00:00
|
|
|
}
|
|
|
|
|
else if (x.Role == ChatRole.Assistant)
|
|
|
|
|
{
|
|
|
|
|
var m = x as ChatRequestAssistantMessage;
|
2024-02-21 04:31:09 +00:00
|
|
|
return m.FunctionCall != null ?
|
|
|
|
|
$"{m.Role}: Call function {m.FunctionCall.Name}({m.FunctionCall.Arguments})" :
|
|
|
|
|
$"{m.Role}: {m.Content}";
|
2023-12-18 20:14:14 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException("Not found role");
|
|
|
|
|
}
|
2023-10-20 03:47:14 +00:00
|
|
|
}));
|
2023-11-01 01:48:12 +00:00
|
|
|
prompt += $"\r\n{verbose}\r\n";
|
2023-10-30 16:48:18 +00:00
|
|
|
}
|
2023-09-28 03:31:58 +00:00
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
if (chatCompletionsOptions.Functions.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var functions = string.Join("\r\n", chatCompletionsOptions.Functions.Select(x =>
|
2023-09-28 03:31:58 +00:00
|
|
|
{
|
2023-11-01 21:30:39 +00:00
|
|
|
return $"\r\n{x.Name}: {x.Description}\r\n{x.Parameters}";
|
2023-10-30 16:48:18 +00:00
|
|
|
}));
|
|
|
|
|
prompt += $"\r\n[FUNCTIONS]\r\n{functions}\r\n";
|
2023-08-20 20:33:35 +00:00
|
|
|
}
|
|
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
return prompt;
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|
2023-09-19 16:29:25 +00:00
|
|
|
|
|
|
|
|
public void SetModelName(string model)
|
|
|
|
|
{
|
|
|
|
|
_model = model;
|
|
|
|
|
}
|
2023-06-17 02:42:35 +00:00
|
|
|
}
|