2024-08-07 21:56:57 +00:00
using BotSharp.Abstraction.Files.Utilities ;
2024-06-26 03:38:01 +00:00
using OpenAI.Chat ;
2024-09-29 01:01:16 +00:00
using System.ClientModel ;
2023-06-17 02:42:35 +00:00
2024-06-27 17:49:51 +00:00
namespace BotSharp.Plugin.AzureOpenAI.Providers.Chat ;
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 ;
2024-07-01 02:14:02 +00:00
protected readonly ILogger < ChatCompletionProvider > _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
2024-07-10 02:17:36 +00:00
public ChatCompletionProvider (
AzureOpenAiSettings settings ,
2023-08-20 20:33:35 +00:00
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 ) ;
2024-06-26 03:38:01 +00:00
var chatClient = client . GetChatClient ( _model ) ;
var ( prompt , messages , options ) = PrepareOptions ( agent , conversations ) ;
2023-09-27 12:51:15 +00:00
2024-09-29 01:01:16 +00:00
ChatCompletion value = default ;
2024-06-26 03:38:01 +00:00
RoleDialogModel responseMessage ;
2024-09-29 01:01:16 +00:00
try
2023-09-27 12:51:15 +00:00
{
2024-09-29 01:01:16 +00:00
var response = chatClient . CompleteChat ( messages , options ) ;
value = response . Value ;
var reason = value . FinishReason ;
var content = value . Content ;
var text = content . FirstOrDefault ( ) ? . Text ? ? string . Empty ;
2024-10-02 18:00:23 +00:00
if ( reason = = ChatFinishReason . FunctionCall | | reason = = ChatFinishReason . ToolCalls )
2023-09-27 12:51:15 +00:00
{
2024-10-02 18:00:23 +00:00
var toolCall = value . ToolCalls . FirstOrDefault ( ) ;
2024-09-29 01:01:16 +00:00
responseMessage = new RoleDialogModel ( AgentRole . Function , text )
{
CurrentAgentId = agent . Id ,
MessageId = conversations . LastOrDefault ( ) ? . MessageId ? ? string . Empty ,
2024-10-02 18:00:23 +00:00
FunctionName = toolCall ? . FunctionName ,
FunctionArgs = toolCall ? . FunctionArguments ? . ToString ( )
2024-09-29 01:01:16 +00:00
} ;
2023-09-27 12:51:15 +00:00
2024-09-29 01:01:16 +00:00
// Somethings LLM will generate a function name with agent name.
if ( ! string . IsNullOrEmpty ( responseMessage . FunctionName ) )
{
responseMessage . FunctionName = responseMessage . FunctionName . Split ( '.' ) . Last ( ) ;
}
}
else
2023-09-27 12:51:15 +00:00
{
2024-09-29 01:01:16 +00:00
responseMessage = new RoleDialogModel ( AgentRole . Assistant , text )
{
CurrentAgentId = agent . Id ,
MessageId = conversations . LastOrDefault ( ) ? . MessageId ? ? string . Empty ,
} ;
2023-09-27 12:51:15 +00:00
}
}
2024-09-29 01:01:16 +00:00
catch ( ClientResultException ex )
2024-05-02 22:07:12 +00:00
{
2024-09-29 01:01:16 +00:00
_logger . LogError ( ex , ex . Message ) ;
responseMessage = new RoleDialogModel ( AgentRole . Assistant , "The response was filtered due to the prompt triggering our content management policy. Please modify your prompt and retry." )
2024-05-02 22:07:12 +00:00
{
CurrentAgentId = agent . Id ,
2024-09-03 20:41:12 +00:00
MessageId = conversations . LastOrDefault ( ) ? . MessageId ? ? string . Empty ,
2024-05-02 22:07:12 +00:00
} ;
}
2024-09-29 01:01:16 +00:00
catch ( Exception ex )
2024-05-02 22:07:12 +00:00
{
2024-09-29 01:01:16 +00:00
_logger . LogError ( ex , ex . Message ) ;
responseMessage = new RoleDialogModel ( AgentRole . Assistant , ex . Message )
2024-05-02 22:07:12 +00:00
{
CurrentAgentId = agent . Id ,
2024-09-03 20:41:12 +00:00
MessageId = conversations . LastOrDefault ( ) ? . MessageId ? ? string . Empty ,
2024-05-02 22:07:12 +00:00
} ;
}
2023-10-16 20:07:07 +00:00
// After chat completion hook
2024-06-27 17:49:51 +00:00
foreach ( var hook in contentHooks )
2023-12-01 17:31:45 +00:00
{
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 ,
2024-10-02 18:00:23 +00:00
PromptCount = value ? . Usage ? . InputTokenCount ? ? 0 ,
CompletionCount = value ? . Usage ? . OutputTokenCount ? ? 0
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
}
2024-06-27 17:49:51 +00:00
public async Task < bool > GetChatCompletionsAsync ( Agent agent ,
List < RoleDialogModel > conversations ,
2023-08-18 04:27:07 +00:00
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 ) ;
2024-06-26 03:38:01 +00:00
var chatClient = client . GetChatClient ( _model ) ;
var ( prompt , messages , options ) = PrepareOptions ( agent , conversations ) ;
2023-07-27 21:56:57 +00:00
2024-06-26 03:38:01 +00:00
var response = await chatClient . CompleteChatAsync ( messages , options ) ;
var value = response . Value ;
var reason = value . FinishReason ;
var content = value . Content ;
2024-06-26 05:01:05 +00:00
var text = content . FirstOrDefault ( ) ? . Text ? ? string . Empty ;
2023-07-27 21:56:57 +00:00
2024-06-26 05:01:05 +00:00
var msg = new RoleDialogModel ( AgentRole . Assistant , text )
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 ,
2024-10-02 18:00:23 +00:00
PromptCount = response . Value ? . Usage ? . InputTokenCount ? ? 0 ,
CompletionCount = response . Value ? . Usage ? . OutputTokenCount ? ? 0
2023-12-01 17:31:45 +00:00
} ) ;
}
2023-09-04 02:21:53 +00:00
2024-10-02 18:00:23 +00:00
if ( reason = = ChatFinishReason . FunctionCall | | reason = = ChatFinishReason . ToolCalls )
2023-07-27 21:56:57 +00:00
{
2024-10-02 18:00:23 +00:00
var toolCall = value . ToolCalls ? . FirstOrDefault ( ) ;
_logger . LogInformation ( $"[{agent.Name}]: {toolCall?.FunctionName}({toolCall?.FunctionArguments})" ) ;
2023-07-28 04:27:12 +00:00
2024-06-26 05:01:05 +00:00
var funcContextIn = new RoleDialogModel ( AgentRole . Function , text )
2023-08-18 04:27:07 +00:00
{
CurrentAgentId = agent . Id ,
2024-10-02 18:00:23 +00:00
FunctionName = toolCall ? . FunctionName ,
FunctionArgs = toolCall ? . FunctionArguments ? . ToString ( )
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 ) ;
2024-06-26 03:38:01 +00:00
var chatClient = client . GetChatClient ( _model ) ;
var ( prompt , messages , options ) = PrepareOptions ( agent , conversations ) ;
var response = chatClient . CompleteChatStreamingAsync ( messages , options ) ;
2023-06-17 02:42:35 +00:00
2023-11-13 17:19:25 +00:00
await foreach ( var choice in response )
2023-06-17 02:42:35 +00:00
{
2024-10-02 18:00:23 +00:00
if ( choice . FinishReason = = ChatFinishReason . FunctionCall | | choice . FinishReason = = ChatFinishReason . ToolCalls )
2023-07-27 15:07:39 +00:00
{
2024-10-02 18:00:23 +00:00
var update = choice . ToolCallUpdates ? . FirstOrDefault ( ) ? . FunctionArgumentsUpdate ? . ToString ( ) ? ? string . Empty ;
Console . Write ( update ) ;
2024-06-27 17:49:51 +00:00
2024-10-02 18:00:23 +00:00
await onMessageReceived ( new RoleDialogModel ( AgentRole . Assistant , update ) ) ;
2023-07-27 21:56:57 +00:00
continue ;
2023-07-27 15:07:39 +00:00
}
2024-06-26 03:38:01 +00:00
if ( choice . ContentUpdate . IsNullOrEmpty ( ) ) continue ;
2023-08-07 17:48:09 +00:00
2024-06-26 05:01:05 +00:00
_logger . LogInformation ( choice . ContentUpdate [ 0 ] ? . Text ) ;
2023-08-07 17:48:09 +00:00
2024-10-02 18:00:23 +00:00
await onMessageReceived ( new RoleDialogModel ( choice . Role ? . ToString ( ) ? ? ChatMessageRole . Assistant . ToString ( ) , choice . ContentUpdate [ 0 ] ? . Text ? ? string . Empty ) ) ;
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
}
2024-06-26 03:38:01 +00:00
protected ( string , IEnumerable < ChatMessage > , ChatCompletionOptions ) 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 > ( ) ;
2024-05-13 21:53:41 +00:00
var state = _services . GetRequiredService < IConversationStateService > ( ) ;
2024-08-08 18:34:50 +00:00
var fileStorage = _services . GetRequiredService < IFileStorageService > ( ) ;
2024-05-13 21:53:41 +00:00
var settingsService = _services . GetRequiredService < ILlmProviderService > ( ) ;
var settings = settingsService . GetSetting ( Provider , _model ) ;
2024-05-14 16:51:39 +00:00
var allowMultiModal = settings ! = null & & settings . MultiModal ;
2024-05-13 21:53:41 +00:00
2024-06-26 03:38:01 +00:00
var messages = new List < ChatMessage > ( ) ;
var temperature = float . Parse ( state . GetState ( "temperature" , "0.0" ) ) ;
var maxTokens = int . Parse ( state . GetState ( "max_tokens" , "1024" ) ) ;
var options = new ChatCompletionOptions ( )
{
Temperature = temperature ,
2024-10-02 18:00:23 +00:00
MaxOutputTokenCount = maxTokens
2024-06-26 03:38:01 +00:00
} ;
foreach ( var function in agent . Functions )
{
if ( ! agentService . RenderFunction ( agent , function ) ) continue ;
var property = agentService . RenderFunctionProperty ( agent , function ) ;
options . Tools . Add ( ChatTool . CreateFunctionTool (
functionName : function . Name ,
functionDescription : function . Description ,
functionParameters : BinaryData . FromObjectAsJson ( property ) ) ) ;
}
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 ) ;
2024-06-26 03:38:01 +00:00
messages . Add ( new SystemChatMessage ( 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 ) )
{
2024-06-26 03:38:01 +00:00
messages . Add ( new SystemChatMessage ( 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 ) ;
2024-06-26 03:38:01 +00:00
foreach ( var sample in samples )
2023-06-17 02:42:35 +00:00
{
2024-06-26 03:38:01 +00:00
messages . Add ( sample . Role = = AgentRole . User ? new UserChatMessage ( sample . Content ) : new AssistantChatMessage ( sample . Content ) ) ;
2023-07-26 21:05:30 +00:00
}
2023-07-28 15:54:29 +00:00
2024-10-02 18:00:23 +00:00
var filteredMessages = conversations . Select ( x = > x ) . ToList ( ) ;
var firstUserMsgIdx = filteredMessages . FindIndex ( x = > x . Role = = AgentRole . User ) ;
if ( firstUserMsgIdx > 0 )
{
filteredMessages = filteredMessages . Where ( ( _ , idx ) = > idx > = firstUserMsgIdx ) . ToList ( ) ;
}
foreach ( var message in filteredMessages )
2023-06-17 02:42:35 +00:00
{
2024-06-26 03:38:01 +00:00
if ( message . Role = = AgentRole . Function )
2023-07-28 15:54:29 +00:00
{
2024-10-02 18:00:23 +00:00
messages . Add ( new AssistantChatMessage ( new List < ChatToolCall >
2024-02-21 04:31:09 +00:00
{
2024-10-02 18:00:23 +00:00
ChatToolCall . CreateFunctionToolCall ( message . FunctionName , message . FunctionName , BinaryData . FromString ( message . FunctionArgs ? ? string . Empty ) )
} ) ) ;
2024-02-22 12:22:26 +00:00
2024-10-02 18:00:23 +00:00
messages . Add ( new ToolChatMessage ( message . FunctionName , message . Content ) ) ;
2023-12-18 20:14:14 +00:00
}
2024-06-26 03:38:01 +00:00
else if ( message . Role = = AgentRole . User )
2023-12-18 20:14:14 +00:00
{
2024-05-11 03:04:59 +00:00
var text = ! string . IsNullOrWhiteSpace ( message . Payload ) ? message . Payload : message . Content ;
2024-10-02 18:00:23 +00:00
var textPart = ChatMessageContentPart . CreateTextPart ( text ) ;
2024-07-11 16:30:52 +00:00
var contentParts = new List < ChatMessageContentPart > { textPart } ;
2024-03-08 13:19:13 +00:00
2024-07-11 16:30:52 +00:00
if ( allowMultiModal & & ! message . Files . IsNullOrEmpty ( ) )
2024-03-08 13:19:13 +00:00
{
2024-07-11 16:30:52 +00:00
foreach ( var file in message . Files )
2024-05-15 19:34:57 +00:00
{
2024-08-08 18:34:50 +00:00
if ( ! string . IsNullOrEmpty ( file . FileData ) )
2024-05-14 16:51:39 +00:00
{
2024-08-07 21:56:57 +00:00
var ( contentType , bytes ) = FileUtility . GetFileInfoFromData ( file . FileData ) ;
2024-10-02 18:00:23 +00:00
var contentPart = ChatMessageContentPart . CreateImagePart ( BinaryData . FromBytes ( bytes ) , contentType , ChatImageDetailLevel . Low ) ;
2024-07-11 16:30:52 +00:00
contentParts . Add ( contentPart ) ;
}
else if ( ! string . IsNullOrEmpty ( file . FileStorageUrl ) )
{
2024-08-07 21:56:57 +00:00
var contentType = FileUtility . GetFileContentType ( file . FileStorageUrl ) ;
2024-08-08 18:34:50 +00:00
var bytes = fileStorage . GetFileBytes ( file . FileStorageUrl ) ;
2024-10-02 18:00:23 +00:00
var contentPart = ChatMessageContentPart . CreateImagePart ( BinaryData . FromBytes ( bytes ) , contentType , ChatImageDetailLevel . Low ) ;
2024-08-08 18:34:50 +00:00
contentParts . Add ( contentPart ) ;
}
else if ( ! string . IsNullOrEmpty ( file . FileUrl ) )
{
var uri = new Uri ( file . FileUrl ) ;
2024-10-02 18:00:23 +00:00
var contentPart = ChatMessageContentPart . CreateImagePart ( uri , ChatImageDetailLevel . Low ) ;
2024-07-11 16:30:52 +00:00
contentParts . Add ( contentPart ) ;
2024-05-14 16:51:39 +00:00
}
}
2024-05-15 19:34:57 +00:00
}
2024-07-11 16:30:52 +00:00
messages . Add ( new UserChatMessage ( contentParts ) { ParticipantName = message . FunctionName } ) ;
2023-07-28 15:54:29 +00:00
}
2024-06-26 03:38:01 +00:00
else if ( message . Role = = AgentRole . Assistant )
2023-07-28 15:54:29 +00:00
{
2024-10-22 12:04:40 +00:00
messages . Add ( new AssistantChatMessage ( message . Content ) ) ;
2023-07-28 15:54:29 +00:00
}
2023-06-17 02:42:35 +00:00
}
2024-06-26 03:38:01 +00:00
var prompt = GetPrompt ( messages , options ) ;
return ( prompt , messages , options ) ;
2023-10-30 16:48:18 +00:00
}
2024-06-26 03:38:01 +00:00
private string GetPrompt ( IEnumerable < ChatMessage > messages , ChatCompletionOptions options )
2023-10-30 16:48:18 +00:00
{
var prompt = string . Empty ;
2024-06-26 03:38:01 +00:00
if ( ! messages . IsNullOrEmpty ( ) )
2023-10-30 16:48:18 +00:00
{
// System instruction
2024-06-26 03:38:01 +00:00
var verbose = string . Join ( "\r\n" , messages
. Select ( x = > x as SystemChatMessage )
. Where ( x = > x ! = null )
. Select ( x = >
2023-10-30 16:48:18 +00:00
{
2024-06-26 03:38:01 +00:00
if ( ! string . IsNullOrEmpty ( x . ParticipantName ) )
2024-01-24 23:47:57 +00:00
{
// To display Agent name in log
2024-06-26 05:01:05 +00:00
return $"[{x.ParticipantName}]: {x.Content.FirstOrDefault()?.Text ?? string.Empty}" ;
2024-01-24 23:47:57 +00:00
}
2024-06-26 05:01:05 +00:00
return $"{AgentRole.System}: {x.Content.FirstOrDefault()?.Text ?? string.Empty}" ;
2023-10-30 16:48:18 +00:00
} ) ) ;
2023-11-01 01:48:12 +00:00
prompt + = $"{verbose}\r\n" ;
2023-10-30 16:48:18 +00:00
2024-05-05 22:18:34 +00:00
prompt + = "\r\n[CONVERSATION]" ;
2024-06-26 03:38:01 +00:00
verbose = string . Join ( "\r\n" , messages
2024-06-27 17:49:51 +00:00
. Where ( x = > x as SystemChatMessage = = null )
2024-06-26 03:38:01 +00:00
. Select ( x = >
2023-10-20 03:47:14 +00:00
{
2024-10-02 18:00:23 +00:00
var fnMessage = x as ToolChatMessage ;
2024-06-26 03:38:01 +00:00
if ( fnMessage ! = null )
2023-12-18 20:14:14 +00:00
{
2024-06-26 05:01:05 +00:00
return $"{AgentRole.Function}: {fnMessage.Content.FirstOrDefault()?.Text ?? string.Empty}" ;
2023-12-18 20:14:14 +00:00
}
2024-06-26 03:38:01 +00:00
var userMessage = x as UserChatMessage ;
if ( userMessage ! = null )
2023-12-18 20:14:14 +00:00
{
2024-06-26 05:01:05 +00:00
var content = x . Content . FirstOrDefault ( ) ? . Text ? ? string . Empty ;
2024-06-26 03:38:01 +00:00
return ! string . IsNullOrEmpty ( userMessage . ParticipantName ) & & userMessage . ParticipantName ! = "route_to_agent" ?
$"{userMessage.ParticipantName}: {content}" :
$"{AgentRole.User}: {content}" ;
2023-12-18 20:14:14 +00:00
}
2024-06-26 03:38:01 +00:00
var assistMessage = x as AssistantChatMessage ;
if ( assistMessage ! = null )
2023-12-18 20:14:14 +00:00
{
2024-10-02 18:00:23 +00:00
var toolCall = assistMessage . ToolCalls ? . FirstOrDefault ( ) ;
return toolCall ! = null ?
$"{AgentRole.Assistant}: Call function {toolCall?.FunctionName}({toolCall?.FunctionArguments})" :
2024-06-26 05:01:05 +00:00
$"{AgentRole.Assistant}: {assistMessage.Content.FirstOrDefault()?.Text ?? string.Empty}" ;
2023-12-18 20:14:14 +00:00
}
2024-06-26 03:38:01 +00:00
return string . Empty ;
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
2024-06-26 03:38:01 +00:00
if ( ! options . Tools . IsNullOrEmpty ( ) )
2023-10-30 16:48:18 +00:00
{
2024-06-26 03:38:01 +00:00
var functions = string . Join ( "\r\n" , options . Tools . Select ( fn = >
2023-09-28 03:31:58 +00:00
{
2024-06-26 03:38:01 +00:00
return $"\r\n{fn.FunctionName}: {fn.FunctionDescription}\r\n{fn.FunctionParameters}" ;
2023-10-30 16:48:18 +00:00
} ) ) ;
2024-05-05 22:18:34 +00:00
prompt + = $"\r\n[FUNCTIONS]{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
}