Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ private async Task<RoleDialogModel> GetChatCompletionsWithScopedState(
string stateValue)
{
var states = _services.GetRequiredService<IConversationStateService>();
states.SetState(stateKey, stateValue, source: StateSource.Application);
states.SetState(stateKey, stateValue, source: StateSource.Application, isNeedVersion: false);

try
{
return await completion.GetChatCompletions(agent, dialogs);
}
finally
{
states.RemoveState(stateKey);
states.SetState(stateKey, string.Empty, source: StateSource.Application, isNeedVersion: false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BotSharp.Abstraction.MessageHub.Models;
using BotSharp.Core.Infrastructures.Streams;
using BotSharp.Core.MessageHub;
using Microsoft.Extensions.Options;
using OpenAI.Chat;
using System.Net.Http;

Expand Down Expand Up @@ -405,11 +406,7 @@ private async Task<RoleDialogModel> InnerGetChatCompletionsStreamingAsync(Agent
}
}

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

if (!string.IsNullOrEmpty(agent.Knowledges))
{
Expand Down Expand Up @@ -706,5 +703,19 @@ private ChatImageDetailLevel ParseChatImageDetailLevel(string? level)

return imageLevel;
}

private void AddChatToolChoice(ChatCompletionOptions options)
{
if (options.Tools.IsNullOrEmpty())
{
return;
}

// Apply tool_choice only when tools are present; tool_choice is rejected by the API otherwise.
if (_state.GetState("tool_choice").IsEqualTo("required"))
{
options.ToolChoice = ChatToolChoice.CreateRequiredChoice();
}
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BotSharp.Abstraction.MessageHub.Models;
using BotSharp.Core.Infrastructures.Streams;
using BotSharp.Core.MessageHub;
using OpenAI.Chat;
using OpenAI.Responses;

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

AddBuiltInTools(options.Tools, settings);
AddResponseToolChoice(options);

if (!string.IsNullOrEmpty(agent.Knowledges))
{
Expand Down Expand Up @@ -751,5 +753,19 @@ private void AddBuiltInTools(IList<ResponseTool> tools, LlmModelSetting? modelSe
return null;
}
#endregion

private void AddResponseToolChoice(CreateResponseOptions options)
{
if (options.Tools.IsNullOrEmpty())
{
return;
}

// Apply tool_choice only when tools are present; tool_choice is rejected by the API otherwise.
if (_state.GetState("tool_choice").IsEqualTo("required"))
{
options.ToolChoice = ResponseToolChoice.CreateRequiredChoice();
}
}
#endregion
}
Loading