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
2 changes: 0 additions & 2 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ updates:
versions: [ ">=5.0.0" ]
- dependency-name: "com.github.victools:jsonschema-module-jackson"
versions: [ ">=5.0.0" ]
- dependency-name: "org.springframework.ai:spring-ai-bom"
versions: [ ">=2.0.0" ]
groups:
production-minor-patch:
dependency-type: "production"
Expand Down
2 changes: 1 addition & 1 deletion docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### 🔧 Compatibility Notes

-
-[Orchestration] Spring AI support was upgraded to version `2.0.1`

### ✨ New Functionality

Expand Down
4 changes: 4 additions & 0 deletions foundation-models/openai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jackson</artifactId>
</dependency>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@
name -> {
final Function<String, Object> exec =
s -> function.apply(deserializeArgument(inputClass, s));
final var schema = GENERATOR.generateSchema(inputClass);
final var jackson3Schema = GENERATOR.generateSchema(inputClass);
final ObjectNode schema;
try {
schema = (ObjectNode) JACKSON.readTree(jackson3Schema.toString());
} catch (JsonProcessingException e) {
throw new IllegalStateException("Failed to parse generated JSON schema", e);
}
return new OpenAiTool(name, exec, schema, null, null);
};
}
Expand Down Expand Up @@ -145,7 +151,7 @@

private static SchemaGenerator createSchemaGenerator() {
final var module =
new JacksonModule(

Check warning on line 154 in foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiTool.java

View workflow job for this annotation

GitHub Actions / continuous-integration

com.github.victools.jsonschema.module.jackson.JacksonModule in com.github.victools.jsonschema.module.jackson has been deprecated and marked for removal
JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, JacksonOption.RESPECT_JSONPROPERTY_ORDER);
return new SchemaGenerator(
new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.sap.ai.sdk.foundationmodels.openai.spring;

import static org.springframework.ai.model.tool.ToolCallingChatOptions.isInternalToolExecutionEnabled;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -35,7 +33,7 @@
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.DefaultToolCallingManager;
import org.springframework.ai.model.tool.DefaultToolCallingChatOptions;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import reactor.core.publisher.Flux;

Expand All @@ -49,8 +47,10 @@ public class OpenAiChatModel implements ChatModel {
private final OpenAiClient client;

@Nonnull
private final DefaultToolCallingManager toolCallingManager =
DefaultToolCallingManager.builder().build();
@Override
public ChatOptions getOptions() {
return DefaultToolCallingChatOptions.builder().toolCallbacks(List.of()).build();
}

@Override
@Nonnull
Expand All @@ -66,18 +66,7 @@ public ChatResponse call(@Nonnull final Prompt prompt) {
}

val result = client.chatCompletion(request);
val response = new ChatResponse(toGenerations(result));

if (options != null && isInternalToolExecutionEnabled(options) && response.hasToolCalls()) {
val toolCalls =
response.getResult().getOutput().getToolCalls().stream().map(ToolCall::name).toList();
log.info("Executing {} tool call(s) - {}.", toolCalls.size(), toolCalls);
val toolExecutionResult = toolCallingManager.executeToolCalls(prompt, response);
// Send the tool execution result back to the model.
log.debug("Re-invoking model with tool execution results.");
return call(new Prompt(toolExecutionResult.conversationHistory(), options));
}
return response;
return new ChatResponse(toGenerations(result));
}

@Override
Expand Down Expand Up @@ -129,14 +118,15 @@ private static List<OpenAiMessage> extractMessages(final Prompt prompt) {

private static void addAssistantMessage(
final List<OpenAiMessage> result, final AssistantMessage message) {
if (message.getText() != null) {
result.add(OpenAiMessage.assistant(message.getText()));
final var toolCalls = message.getToolCalls();
if (toolCalls != null && !toolCalls.isEmpty()) {
final Function<ToolCall, OpenAiToolCall> callTranslate =
toolCall -> OpenAiToolCall.function(toolCall.id(), toolCall.name(), toolCall.arguments());
val calls = toolCalls.stream().map(callTranslate).toList();
result.add(OpenAiMessage.assistant(calls));
return;
}
final Function<ToolCall, OpenAiToolCall> callTranslate =
toolCall -> OpenAiToolCall.function(toolCall.id(), toolCall.name(), toolCall.arguments());
val calls = message.getToolCalls().stream().map(callTranslate).toList();
result.add(OpenAiMessage.assistant(calls));
Option.of(message.getText()).peek(t -> result.add(OpenAiMessage.assistant(t)));
}

private static void addToolMessages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ void testToolCallsWithoutExecution() throws IOException {
.withHeader("Content-Type", "application/json")
.withBodyFile("weatherToolResponse.json")));

var options = new DefaultToolCallingChatOptions();
options.setToolCallbacks(List.of(ToolCallbacks.from(new WeatherMethod())));
options.setInternalToolExecutionEnabled(false);
var options =
DefaultToolCallingChatOptions.builder()
.toolCallbacks(ToolCallbacks.from(new WeatherMethod()))
.build();
val prompt = new Prompt("What is the weather in Potsdam and in Toulouse?", options);
val result = client.call(prompt);

Expand Down Expand Up @@ -178,10 +179,16 @@ void testToolCallsWithExecution() throws IOException {
.withBodyFile("weatherToolResponse2.json")
.withHeader("Content-Type", "application/json")));

var options = new DefaultToolCallingChatOptions();
options.setToolCallbacks(List.of(ToolCallbacks.from(new WeatherMethod())));
val prompt = new Prompt("What is the weather in Potsdam and in Toulouse?", options);
val result = client.call(prompt);
var options =
DefaultToolCallingChatOptions.builder()
.toolCallbacks(ToolCallbacks.from(new WeatherMethod()))
.build();
val chatClient = ChatClient.builder(client).build();
val result =
chatClient
.prompt(new Prompt("What is the weather in Potsdam and in Toulouse?", options))
.call()
.chatResponse();

assertThat(result.getResult().getOutput().getText())
.isEqualTo("The current temperature in Potsdam is 30°C and in Toulouse 30°C.");
Expand Down
4 changes: 4 additions & 0 deletions orchestration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
<groupId>com.github.victools</groupId>
<artifactId>jsonschema-module-jackson</artifactId>
</dependency>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
@Nonnull
public static ResponseJsonSchema fromType(@Nonnull final Type classType) {
val module =
new JacksonModule(

Check warning on line 66 in orchestration/src/main/java/com/sap/ai/sdk/orchestration/ResponseJsonSchema.java

View workflow job for this annotation

GitHub Actions / continuous-integration

com.github.victools.jsonschema.module.jackson.JacksonModule in com.github.victools.jsonschema.module.jackson has been deprecated and marked for removal
JacksonOption.RESPECT_JSONPROPERTY_REQUIRED, JacksonOption.RESPECT_JSONPROPERTY_ORDER);
val generator =
new SchemaGenerator(
Expand All @@ -73,8 +73,12 @@
.with(module)
.build());
val jsonSchema = generator.generateSchema(classType);
val mapper = new ObjectMapper();
val schemaMap = mapper.convertValue(jsonSchema, new TypeReference<Map<String, Object>>() {});
final Map<String, Object> schemaMap;
try {
schemaMap = new ObjectMapper().readValue(jsonSchema.toString(), new TypeReference<>() {});
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
throw new IllegalStateException("Failed to parse generated JSON schema", e);
}
val schemaName = ((Class<?>) classType).getSimpleName() + "-Schema";
return new ResponseJsonSchema(schemaMap, schemaName, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.DefaultToolCallingManager;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import reactor.core.publisher.Flux;

/**
Expand All @@ -38,6 +40,8 @@
public class OrchestrationChatModel implements ChatModel {
@Nonnull private final OrchestrationClient client;

@Setter @Nullable private OrchestrationChatOptions defaultOptions;

@Nonnull
private final DefaultToolCallingManager toolCallingManager =
DefaultToolCallingManager.builder().build();
Expand All @@ -61,6 +65,15 @@ public OrchestrationChatModel(@Nonnull final OrchestrationClient client) {
this.client = client;
}

@Nonnull
@Override
public ChatOptions getOptions() {
if (defaultOptions != null) {
return defaultOptions;
}
return ChatModel.super.getOptions();
}

@Nonnull
@Override
public ChatResponse call(@Nonnull final Prompt prompt) {
Expand All @@ -71,7 +84,7 @@ public ChatResponse call(@Nonnull final Prompt prompt) {
new OrchestrationSpringChatResponse(
client.chatCompletion(orchestrationPrompt, options.getConfig()));

if (ToolCallingChatOptions.isInternalToolExecutionEnabled(prompt.getOptions())
if (!Boolean.FALSE.equals(options.isInternalToolExecutionEnabled())
&& response.hasToolCalls()) {

if (log.isDebugEnabled()) {
Expand All @@ -82,6 +95,13 @@ public ChatResponse call(@Nonnull final Prompt prompt) {

val toolExecutionResult = toolCallingManager.executeToolCalls(prompt, response);

if (toolExecutionResult.returnDirect()) {
log.debug("Returning tool execution result directly without re-invoking LLM.");
return new ChatResponse(
org.springframework.ai.model.tool.ToolExecutionResult.buildGenerations(
toolExecutionResult));
}

// Send the tool execution result back to the model.
log.debug("Re-invoking LLM with tool execution results.");
return call(new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()));
Expand Down
Loading