Skip to content

Commit 90392d0

Browse files
authored
Merge pull request #1296 from SciSharp/revert-1295-bugfix/append-system-instruction
Revert "append system prompt"
2 parents 89eb73e + 9c402bc commit 90392d0

2 files changed

Lines changed: 66 additions & 73 deletions

File tree

src/Plugins/BotSharp.Plugin.OpenAI/Providers/Chat/ChatCompletionProvider.cs

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,42 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
367367
// Prepare instruction and functions
368368
var renderData = agentService.CollectRenderData(agent);
369369
var (instruction, functions) = agentService.PrepareInstructionAndFunctions(agent, renderData);
370+
if (!string.IsNullOrWhiteSpace(instruction))
371+
{
372+
renderedInstructions.Add(instruction);
373+
messages.Add(new SystemChatMessage(instruction));
374+
}
375+
376+
// Render functions
377+
if (options.WebSearchOptions == null)
378+
{
379+
foreach (var function in functions)
380+
{
381+
if (!agentService.RenderFunction(agent, function, renderData))
382+
{
383+
continue;
384+
}
385+
386+
var property = agentService.RenderFunctionProperty(agent, function, renderData);
387+
388+
options.Tools.Add(ChatTool.CreateFunctionTool(
389+
functionName: function.Name,
390+
functionDescription: function.Description,
391+
functionParameters: BinaryData.FromObjectAsJson(property)));
392+
}
393+
}
394+
395+
if (!string.IsNullOrEmpty(agent.Knowledges))
396+
{
397+
messages.Add(new SystemChatMessage(agent.Knowledges));
398+
}
399+
400+
var samples = ProviderHelper.GetChatSamples(agent.Samples);
401+
foreach (var sample in samples)
402+
{
403+
messages.Add(sample.Role == AgentRole.User ? new UserChatMessage(sample.Content) : new AssistantChatMessage(sample.Content));
404+
}
370405

371-
// Build messages
372406
var filteredMessages = conversations.Select(x => x).ToList();
373407
var firstUserMsgIdx = filteredMessages.FindIndex(x => x.Role == AgentRole.User);
374408
if (firstUserMsgIdx > 0)
@@ -419,43 +453,6 @@ public async Task<RoleDialogModel> GetChatCompletionsStreamingAsync(Agent agent,
419453
}
420454
}
421455

422-
// Build system messages
423-
if (!string.IsNullOrWhiteSpace(instruction))
424-
{
425-
renderedInstructions.Add(instruction);
426-
messages.Add(new SystemChatMessage(instruction));
427-
}
428-
429-
if (!string.IsNullOrEmpty(agent.Knowledges))
430-
{
431-
messages.Add(new SystemChatMessage(agent.Knowledges));
432-
}
433-
434-
var samples = ProviderHelper.GetChatSamples(agent.Samples);
435-
foreach (var sample in samples)
436-
{
437-
messages.Add(sample.Role == AgentRole.User ? new UserChatMessage(sample.Content) : new AssistantChatMessage(sample.Content));
438-
}
439-
440-
// Render functions
441-
if (options.WebSearchOptions == null)
442-
{
443-
foreach (var function in functions)
444-
{
445-
if (!agentService.RenderFunction(agent, function, renderData))
446-
{
447-
continue;
448-
}
449-
450-
var property = agentService.RenderFunctionProperty(agent, function, renderData);
451-
452-
options.Tools.Add(ChatTool.CreateFunctionTool(
453-
functionName: function.Name,
454-
functionDescription: function.Description,
455-
functionParameters: BinaryData.FromObjectAsJson(property)));
456-
}
457-
}
458-
459456
var prompt = GetPrompt(messages, options);
460457
return (prompt, messages, options);
461458
}

src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,37 @@ private async Task<RoleDialogModel> OnUserAudioTranscriptionCompleted(RealtimeHu
575575
// Prepare instruction and functions
576576
var renderData = agentService.CollectRenderData(agent);
577577
var (instruction, functions) = agentService.PrepareInstructionAndFunctions(agent, renderData);
578-
579-
// Build messages
578+
if (!string.IsNullOrWhiteSpace(instruction))
579+
{
580+
messages.Add(new SystemChatMessage(instruction));
581+
}
582+
583+
foreach (var function in functions)
584+
{
585+
if (!agentService.RenderFunction(agent, function, renderData))
586+
{
587+
continue;
588+
}
589+
590+
var property = agentService.RenderFunctionProperty(agent, function, renderData);
591+
592+
options.Tools.Add(ChatTool.CreateFunctionTool(
593+
functionName: function.Name,
594+
functionDescription: function.Description,
595+
functionParameters: BinaryData.FromObjectAsJson(property)));
596+
}
597+
598+
if (!string.IsNullOrEmpty(agent.Knowledges))
599+
{
600+
messages.Add(new SystemChatMessage(agent.Knowledges));
601+
}
602+
603+
var samples = ProviderHelper.GetChatSamples(agent.Samples);
604+
foreach (var sample in samples)
605+
{
606+
messages.Add(sample.Role == AgentRole.User ? new UserChatMessage(sample.Content) : new AssistantChatMessage(sample.Content));
607+
}
608+
580609
var filteredMessages = conversations.Select(x => x).ToList();
581610
var firstUserMsgIdx = filteredMessages.FindIndex(x => x.Role == AgentRole.User);
582611
if (firstUserMsgIdx > 0)
@@ -605,39 +634,6 @@ private async Task<RoleDialogModel> OnUserAudioTranscriptionCompleted(RealtimeHu
605634
}
606635
}
607636

608-
// Build system messages
609-
if (!string.IsNullOrWhiteSpace(instruction))
610-
{
611-
messages.Add(new SystemChatMessage(instruction));
612-
}
613-
614-
if (!string.IsNullOrEmpty(agent.Knowledges))
615-
{
616-
messages.Add(new SystemChatMessage(agent.Knowledges));
617-
}
618-
619-
var samples = ProviderHelper.GetChatSamples(agent.Samples);
620-
foreach (var sample in samples)
621-
{
622-
messages.Add(sample.Role == AgentRole.User ? new UserChatMessage(sample.Content) : new AssistantChatMessage(sample.Content));
623-
}
624-
625-
// Build functions
626-
foreach (var function in functions)
627-
{
628-
if (!agentService.RenderFunction(agent, function, renderData))
629-
{
630-
continue;
631-
}
632-
633-
var property = agentService.RenderFunctionProperty(agent, function, renderData);
634-
635-
options.Tools.Add(ChatTool.CreateFunctionTool(
636-
functionName: function.Name,
637-
functionDescription: function.Description,
638-
functionParameters: BinaryData.FromObjectAsJson(property)));
639-
}
640-
641637
var prompt = GetPrompt(messages, options);
642638
return (prompt, messages, options);
643639
}

0 commit comments

Comments
 (0)