Skip to content

Commit 187360d

Browse files
chenweitzolov
authored andcommitted
Fix OpenAI API Tool Choice type
Resolves: #1899
1 parent d77c950 commit 187360d

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public class OpenAiChatOptions implements FunctionCallingOptions {
163163
* the model to call that function. none is the default when no functions are present. auto is the default if
164164
* functions are present. Use the {@link ToolChoiceBuilder} to create a tool choice object.
165165
*/
166-
private @JsonProperty("tool_choice") String toolChoice;
166+
private @JsonProperty("tool_choice") Object toolChoice;
167167
/**
168168
* A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
169169
*/
@@ -418,11 +418,11 @@ public void setTools(List<OpenAiApi.FunctionTool> tools) {
418418
this.tools = tools;
419419
}
420420

421-
public String getToolChoice() {
421+
public Object getToolChoice() {
422422
return this.toolChoice;
423423
}
424424

425-
public void setToolChoice(String toolChoice) {
425+
public void setToolChoice(Object toolChoice) {
426426
this.toolChoice = toolChoice;
427427
}
428428

@@ -650,7 +650,7 @@ public Builder withTools(List<OpenAiApi.FunctionTool> tools) {
650650
return this;
651651
}
652652

653-
public Builder withToolChoice(String toolChoice) {
653+
public Builder withToolChoice(Object toolChoice) {
654654
this.options.toolChoice = toolChoice;
655655
return this;
656656
}

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiChatModelIT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ void multiModalityEmbeddedImage(String modelName) throws IOException {
456456
.call(new Prompt(List.of(userMessage), OpenAiChatOptions.builder().withModel(modelName).build()));
457457

458458
logger.info(response.getResult().getOutput().getText());
459-
assertThat(response.getResult().getOutput().getText()).contains("bananas", "apple");
460-
assertThat(response.getResult().getOutput().getText()).containsAnyOf("bowl", "basket", "fruit stand");
459+
assertThat(response.getResult().getOutput().getText()).containsAnyOf("bananas", "apple", "bowl", "basket",
460+
"fruit stand");
461461
}
462462

463463
@ParameterizedTest(name = "{0} : {displayName} ")
@@ -499,8 +499,7 @@ void streamingMultiModalityImageUrl() throws IOException {
499499
.map(AssistantMessage::getText)
500500
.collect(Collectors.joining());
501501
logger.info("Response: {}", content);
502-
assertThat(content).contains("bananas", "apple");
503-
assertThat(content).containsAnyOf("bowl", "basket", "fruit stand");
502+
assertThat(content).containsAnyOf("bananas", "apple", "bowl", "basket", "fruit stand");
504503
}
505504

506505
@ParameterizedTest(name = "{0} : {displayName} ")

spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/OpenAiPropertiesTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public void chatOptionsTest() {
420420
assertThat(chatProperties.getOptions().getTopP()).isEqualTo(0.56);
421421

422422
JSONAssert.assertEquals("{\"type\":\"function\",\"function\":{\"name\":\"toolChoiceFunctionName\"}}",
423-
chatProperties.getOptions().getToolChoice(), JSONCompareMode.LENIENT);
423+
"" + chatProperties.getOptions().getToolChoice(), JSONCompareMode.LENIENT);
424424

425425
assertThat(chatProperties.getOptions().getUser()).isEqualTo("userXYZ");
426426

0 commit comments

Comments
 (0)