From 3f19c4a6e5aa562a003d1f1d64a4eb0254a90e78 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Thu, 1 Aug 2024 17:11:05 +0200 Subject: [PATCH] Add OpenAI paralel funciton call option Resolves #1143 --- .../ai/openai/OpenAiChatOptions.java | 27 +++++++++++++++++++ .../ROOT/pages/api/chat/openai-chat.adoc | 1 + 2 files changed, 28 insertions(+) diff --git a/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java b/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java index e5738e41f..b7067f9d7 100644 --- a/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java +++ b/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java @@ -141,6 +141,11 @@ public class OpenAiChatOptions implements FunctionCallingOptions, ChatOptions { * A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. */ private @JsonProperty("user") String user; + /** + * Whether to enable parallel function calling during tool use. + * Defaults to true. + */ + private @JsonProperty("parallel_tool_calls") Boolean parallelToolCalls; /** * OpenAI Tool Function Callbacks to register with the ChatModel. @@ -272,6 +277,11 @@ public class OpenAiChatOptions implements FunctionCallingOptions, ChatOptions { return this; } + public Builder withParallelToolCalls(Boolean parallelToolCalls) { + this.options.parallelToolCalls = parallelToolCalls; + return this; + } + public Builder withFunctionCallbacks(List functionCallbacks) { this.options.functionCallbacks = functionCallbacks; return this; @@ -441,6 +451,14 @@ public class OpenAiChatOptions implements FunctionCallingOptions, ChatOptions { this.user = user; } + public Boolean getParallelToolCalls() { + return this.parallelToolCalls; + } + + public void setParallelToolCalls(Boolean parallelToolCalls) { + this.parallelToolCalls = parallelToolCalls; + } + @Override public List getFunctionCallbacks() { return this.functionCallbacks; @@ -481,6 +499,7 @@ public class OpenAiChatOptions implements FunctionCallingOptions, ChatOptions { result = prime * result + ((tools == null) ? 0 : tools.hashCode()); result = prime * result + ((toolChoice == null) ? 0 : toolChoice.hashCode()); result = prime * result + ((user == null) ? 0 : user.hashCode()); + result = prime * result + ((parallelToolCalls == null) ? 0 : parallelToolCalls.hashCode()); return result; } @@ -595,6 +614,13 @@ public class OpenAiChatOptions implements FunctionCallingOptions, ChatOptions { } else if (!this.user.equals(other.user)) return false; + else if (this.parallelToolCalls == null) { + if (other.parallelToolCalls != null) + return false; + } + else if (!this.parallelToolCalls.equals(other.parallelToolCalls)) + return false; + return true; } @@ -633,6 +659,7 @@ public class OpenAiChatOptions implements FunctionCallingOptions, ChatOptions { .withTools(fromOptions.getTools()) .withToolChoice(fromOptions.getToolChoice()) .withUser(fromOptions.getUser()) + .withParallelToolCalls(fromOptions.getParallelToolCalls()) .withFunctionCallbacks(fromOptions.getFunctionCallbacks()) .withFunctions(fromOptions.getFunctions()) .build(); diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc index 351f79abb..4ff7debcb 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc @@ -107,6 +107,7 @@ The prefix `spring.ai.openai.chat` is the property prefix that lets you configur | spring.ai.openai.chat.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | - | spring.ai.openai.chat.options.functions | List of functions, identified by their names, to enable for function calling in a single prompt requests. Functions with those names must exist in the functionCallbacks registry. | - | spring.ai.openai.chat.options.stream-usage | (For streaming only) Set to add an additional chunk with token usage statistics for the entire request. The `choices` field for this chunk is an empty array and all other chunks will also include a usage field, but with a null value. | false +| spring.ai.openai.chat.options.parallel-tool-calls | Whether to enable link:https://platform.openai.com/docs/guides/function-calling/parallel-function-calling[parallel function calling] during tool use. | true |==== NOTE: You can override the common `spring.ai.openai.base-url` and `spring.ai.openai.api-key` for the `ChatModel` and `EmbeddingModel` implementations.