Skip to content
Open
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 @@ -120,11 +120,14 @@ public OpenAiChatCompletionResponse chatCompletionToolExecution(
final var messages = new ArrayList<OpenAiMessage>();
messages.add(OpenAiMessage.user("What's the weather in %s in %s?".formatted(location, unit)));

record WeatherRequest(String location, WeatherMethod.Unit unit) {}

// 1. Define the function
final List<OpenAiTool> tools =
List.of(
OpenAiTool.forFunction(WeatherMethod::getCurrentWeather)
.withArgument(WeatherMethod.Request.class)
OpenAiTool.forFunction(
(WeatherRequest r) -> WeatherMethod.getCurrentWeather(r.location(), r.unit()))
.withArgument(WeatherRequest.class)
.withName("weather")
.withDescription("Get the weather for the given location"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ public OpenAiChatCompletionOutput chatCompletionImage(@Nonnull final String link
public OpenAiChatCompletionOutput chatCompletionToolExecution(
@Nonnull final String location, @Nonnull final String unit) {

record WeatherRequest(String location, WeatherMethod.Unit unit) {}

// 1. Define the function
final Map<String, Object> schemaMap = generateSchema(WeatherMethod.Request.class);
final Map<String, Object> schemaMap = generateSchema(WeatherRequest.class);
final var function =
new OpenAiChatCompletionFunction()
.setName("weather")
Expand All @@ -133,9 +135,10 @@ public OpenAiChatCompletionOutput chatCompletionToolExecution(
// 2. Optionally, execute the function.
final OpenAiChatToolCall toolCall =
response.getChoices().get(0).getMessage().getToolCalls().get(0);
final WeatherMethod.Request arguments =
parseJson(toolCall.getFunction().getArguments(), WeatherMethod.Request.class);
final WeatherMethod.Response currentWeather = WeatherMethod.getCurrentWeather(arguments);
final WeatherRequest arguments =
parseJson(toolCall.getFunction().getArguments(), WeatherRequest.class);
final WeatherMethod.Response currentWeather =
WeatherMethod.getCurrentWeather(arguments.location(), arguments.unit());

final OpenAiChatMessage.OpenAiChatAssistantMessage assistantMessage =
response.getChoices().get(0).getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ enum Unit {
F
}

/**
* Request for the weather
*
* @param location the city
* @param unit the unit of temperature
*/
record Request(String location, Unit unit) {}

/**
* Response for the weather
*
Expand All @@ -35,8 +27,10 @@ record Response(double temp, Unit unit) {}
@Nonnull
@SuppressWarnings("unused")
@Tool(description = "Get the weather in location")
static Response getCurrentWeather(@ToolParam @Nonnull final Request request) {
final int temperature = request.location.hashCode() % 30;
return new Response(temperature, request.unit);
static Response getCurrentWeather(
@ToolParam(description = "the city") @Nonnull final String location,
@ToolParam(description = "the unit of temperature") @Nonnull final Unit unit) {
final int temperature = location.hashCode() % 30;
return new Response(temperature, unit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void testToolCallingWithoutExecution() {
assertThat(toolCall1.name()).isEqualTo("getCurrentWeather");
assertThat(toolCall2.name()).isEqualTo("getCurrentWeather");
assertThat(toolCall1.arguments())
.matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Potsdam[^\"]*\", \"unit\": \"C\"}}");
.matches("\\{\"arg0\": \"[^\"]*Potsdam[^\"]*\", \"arg1\": \"C\"}");
assertThat(toolCall2.arguments())
.matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Toulouse[^\"]*\", \"unit\": \"C\"}}");
.matches("\\{\"arg0\": \"[^\"]*Toulouse[^\"]*\", \"arg1\": \"C\"}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ void testToolCallingWithoutExecution() {
assertThat(toolCall1.name()).isEqualTo("getCurrentWeather");
assertThat(toolCall2.name()).isEqualTo("getCurrentWeather");
assertThat(toolCall1.arguments())
.matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Potsdam[^\"]*\", \"unit\": \"C\"}}");
.matches("\\{\"arg0\": \"[^\"]*Potsdam[^\"]*\", \"arg1\": \"C\"}");
assertThat(toolCall2.arguments())
.matches("\\{\"arg0\": \\{\"location\": \"[^\"]*Toulouse[^\"]*\", \"unit\": \"C\"}}");
.matches("\\{\"arg0\": \"[^\"]*Toulouse[^\"]*\", \"arg1\": \"C\"}");
}

@Test
Expand Down