Skip to content

Commit f401464

Browse files
authored
Merge pull request #1349 from iceljc/master
refine tool choice
2 parents dba92a2 + 29c11bc commit f401464

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/Infrastructure/BotSharp.Core/Routing/Reasoning/OneStepForwardReasoner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ private async Task<RoleDialogModel> GetChatCompletionsWithScopedState(
121121
string stateValue)
122122
{
123123
var states = _services.GetRequiredService<IConversationStateService>();
124-
states.SetState(stateKey, stateValue, source: StateSource.Application);
124+
states.SetState(stateKey, stateValue, source: StateSource.Application, isNeedVersion: false);
125125

126126
try
127127
{
128128
return await completion.GetChatCompletions(agent, dialogs);
129129
}
130130
finally
131131
{
132-
states.RemoveState(stateKey);
132+
states.SetState(stateKey, string.Empty, source: StateSource.Application, isNeedVersion: false);
133133
}
134134
}
135135

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BotSharp.Abstraction.MessageHub.Models;
33
using BotSharp.Core.Infrastructures.Streams;
44
using BotSharp.Core.MessageHub;
5+
using Microsoft.Extensions.Options;
56
using OpenAI.Chat;
67
using System.Net.Http;
78

@@ -405,11 +406,7 @@ private async Task<RoleDialogModel> InnerGetChatCompletionsStreamingAsync(Agent
405406
}
406407
}
407408

408-
// Apply tool_choice only when tools are present; tool_choice is rejected by the API otherwise.
409-
if (!options.Tools.IsNullOrEmpty() && _state.GetState("tool_choice") == "required")
410-
{
411-
options.ToolChoice = ChatToolChoice.CreateRequiredChoice();
412-
}
409+
AddChatToolChoice(options);
413410

414411
if (!string.IsNullOrEmpty(agent.Knowledges))
415412
{
@@ -706,5 +703,19 @@ private ChatImageDetailLevel ParseChatImageDetailLevel(string? level)
706703

707704
return imageLevel;
708705
}
706+
707+
private void AddChatToolChoice(ChatCompletionOptions options)
708+
{
709+
if (options.Tools.IsNullOrEmpty())
710+
{
711+
return;
712+
}
713+
714+
// Apply tool_choice only when tools are present; tool_choice is rejected by the API otherwise.
715+
if (_state.GetState("tool_choice").IsEqualTo("required"))
716+
{
717+
options.ToolChoice = ChatToolChoice.CreateRequiredChoice();
718+
}
719+
}
709720
#endregion
710721
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BotSharp.Abstraction.MessageHub.Models;
33
using BotSharp.Core.Infrastructures.Streams;
44
using BotSharp.Core.MessageHub;
5+
using OpenAI.Chat;
56
using OpenAI.Responses;
67

78
namespace BotSharp.Plugin.OpenAI.Providers.Chat;
@@ -471,6 +472,7 @@ private async Task<RoleDialogModel> InnerCreateResponseStreamingAsync(Agent agen
471472
}
472473

473474
AddBuiltInTools(options.Tools, settings);
475+
AddResponseToolChoice(options);
474476

475477
if (!string.IsNullOrEmpty(agent.Knowledges))
476478
{
@@ -751,5 +753,19 @@ private void AddBuiltInTools(IList<ResponseTool> tools, LlmModelSetting? modelSe
751753
return null;
752754
}
753755
#endregion
756+
757+
private void AddResponseToolChoice(CreateResponseOptions options)
758+
{
759+
if (options.Tools.IsNullOrEmpty())
760+
{
761+
return;
762+
}
763+
764+
// Apply tool_choice only when tools are present; tool_choice is rejected by the API otherwise.
765+
if (_state.GetState("tool_choice").IsEqualTo("required"))
766+
{
767+
options.ToolChoice = ResponseToolChoice.CreateRequiredChoice();
768+
}
769+
}
754770
#endregion
755771
}

0 commit comments

Comments
 (0)