Fix OpenAI API Tool Choice configuraiton options

- Related to the  https://platform.openai.com/docs/api-reference/chat/create#chat-create-tool_choice
 - It seems that when a cuntion type is set explicitely: {"type": "function", "function": {"name": "my_function"}}
 the parallel calling is not working anymore!

 Resolves #551
This commit is contained in:
Christian Tzolov
2024-04-07 00:01:32 +02:00
parent 4e473aaff1
commit 1196ef2f71
2 changed files with 7 additions and 14 deletions

View File

@@ -319,7 +319,7 @@ public class OpenAiApi {
@JsonProperty("temperature") Float temperature,
@JsonProperty("top_p") Float topP,
@JsonProperty("tools") List<FunctionTool> tools,
@JsonProperty("tool_choice") String toolChoice,
@JsonProperty("tool_choice") Object toolChoice,
@JsonProperty("user") String user) {
/**
@@ -360,7 +360,7 @@ public class OpenAiApi {
* @param toolChoice Controls which (if any) function is called by the model.
*/
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model,
List<FunctionTool> tools, String toolChoice) {
List<FunctionTool> tools, Object toolChoice) {
this(messages, model, null, null, null, null, null, null, null,
null, null, null, false, 0.8f, null,
tools, toolChoice, null);
@@ -396,8 +396,8 @@ public class OpenAiApi {
/**
* Specifying a particular function forces the model to call that function.
*/
public static String FUNCTION(String functionName) {
return ModelOptionsUtils.toJsonString(Map.of("type", "function", "function", Map.of("name", functionName)));
public static Object FUNCTION(String functionName) {
return Map.of("type", "function", "function", Map.of("name", functionName));
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage.Role;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage.ToolCall;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest.ToolChoiceBuilder;
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest;
import org.springframework.ai.openai.api.OpenAiApi.FunctionTool.Type;
import org.springframework.http.ResponseEntity;
@@ -89,19 +90,11 @@ public class OpenAiApiToolFunctionCallIT {
}
""")));
// Or you can use the
// ModelOptionsUtils.getJsonSchema(FakeWeatherService.Request.class))) to
// auto-generate the JSON schema like:
// var functionTool = new OpenAiApi.FunctionTool(Type.FUNCTION, new
// OpenAiApi.FunctionTool.Function(
// "Get the weather in location. Return temperature in 30°F or 30°C format.",
// "getCurrentWeather",
// ModelOptionsUtils.getJsonSchema(FakeWeatherService.Request.class)));
List<ChatCompletionMessage> messages = new ArrayList<>(List.of(message));
ChatCompletionRequest chatCompletionRequest = new ChatCompletionRequest(messages, "gpt-4-turbo-preview",
List.of(functionTool), null);
List.of(functionTool), ToolChoiceBuilder.AUTO);
// List.of(functionTool), ToolChoiceBuilder.FUNCTION("getCurrentWeather"));
ResponseEntity<ChatCompletion> chatCompletion = completionApi.chatCompletionEntity(chatCompletionRequest);