Skip to content

Issue 3890 Tool call results returned via ChatResponse lack identification metadata #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -113,6 +113,12 @@ public class OllamaChatModel implements ChatModel {

private static final String METADATA_EVAL_DURATION = "eval-duration";

private static final String METADATA_SOURCE = "source";

private static final String SOURCE_MODEL = "model";

private static final String SOURCE_TOOL = "tool";

private static final ChatModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultChatModelObservationConvention();

private static final ToolCallingManager DEFAULT_TOOL_CALLING_MANAGER = ToolCallingManager.builder().build();
Expand Down Expand Up @@ -264,6 +270,7 @@ private ChatResponse internalCall(Prompt prompt, ChatResponse previousChatRespon
if (ollamaResponse.promptEvalCount() != null && ollamaResponse.evalCount() != null) {
generationMetadata = ChatGenerationMetadata.builder()
.finishReason(ollamaResponse.doneReason())
.metadata(METADATA_SOURCE, SOURCE_MODEL)
.build();
}

Expand Down Expand Up @@ -340,7 +347,10 @@ private Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCh

ChatGenerationMetadata generationMetadata = ChatGenerationMetadata.NULL;
if (chunk.promptEvalCount() != null && chunk.evalCount() != null) {
generationMetadata = ChatGenerationMetadata.builder().finishReason(chunk.doneReason()).build();
generationMetadata = ChatGenerationMetadata.builder()
.finishReason(chunk.doneReason())
.metadata(METADATA_SOURCE, SOURCE_MODEL)
.build();
}

var generator = new Generation(assistantMessage, generationMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,23 @@ void chatMemoryWithTools() {
assertThat(newResponse.getResult().getOutput().getText()).contains("6").contains("8");
}

@Test
void shouldProvideSourceMetadataInRealResponses() {
String userMessageContent = "Hello, can you respond with a simple greeting?";
ChatResponse response = this.chatModel.call(new Prompt(userMessageContent));

assertThat(response).isNotNull();
assertThat(response.getResults()).hasSize(1);

Generation generation = response.getResult();
assertThat(generation).isNotNull();
assertThat(generation.getOutput().getText()).isNotEmpty();

// Verify that source metadata is present and set to "model"
String source = (String) generation.getMetadata().get("source");
assertThat(source).isEqualTo("model");
}

static class MathTools {

@Tool(description = "Multiply the two numbers")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,32 @@ void buildChatResponseMetadataAggregationWithNonEmptyMetadataButEmptyEval() {

}

@Test
void shouldAddSourceMetadataForModelResponses() {
// Create a mock OllamaApi.ChatResponse with eval counts to trigger metadata
// creation
Long evalDuration = 1000L;
Integer evalCount = 101;
Integer promptEvalCount = 808;
Long promptEvalDuration = 8L;
Long loadDuration = 100L;
Long totalDuration = 2000L;

OllamaApi.ChatResponse response = new OllamaApi.ChatResponse("model", Instant.now(),
new OllamaApi.Message(OllamaApi.Message.Role.ASSISTANT, "Test response", null, null), "stop", true,
totalDuration, loadDuration, promptEvalCount, promptEvalDuration, evalCount, evalDuration);

ChatResponseMetadata metadata = OllamaChatModel.from(response, null);

// Verify that basic metadata fields are present
assertEquals(Duration.ofNanos(evalDuration), metadata.get("eval-duration"));
assertEquals(evalCount, metadata.get("eval-count"));

// Test that source metadata is NOT added by the from() method (this is handled in
// Generation creation)
// The from() method only creates response-level metadata, not generation-level
// metadata
assertNull(metadata.get("source"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public interface ToolExecutionResult {

String METADATA_TOOL_NAME = "toolName";

String METADATA_SOURCE = "source";

String SOURCE_TOOL = "tool";

/**
* The history of messages exchanged during the conversation, including the tool
* execution result.
Expand Down Expand Up @@ -75,6 +79,7 @@ static List<Generation> buildGenerations(ToolExecutionResult toolExecutionResult
ChatGenerationMetadata.builder()
.metadata(METADATA_TOOL_ID, response.id())
.metadata(METADATA_TOOL_NAME, response.name())
.metadata(METADATA_SOURCE, SOURCE_TOOL)
.finishReason(FINISH_REASON)
.build());
generations.add(generation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,29 @@ void whenMultipleToolCallsThenMultipleGenerations() {
assertThat(generations.get(1).getMetadata().getFinishReason()).isEqualTo(ToolExecutionResult.FINISH_REASON);
}

@Test
void shouldAddSourceMetadataForToolResponses() {
var toolExecutionResult = ToolExecutionResult.builder()
.conversationHistory(List.of(new AssistantMessage("Hello, how can I help you?"),
new UserMessage("I would like to know the weather in London"),
new AssistantMessage("Call the weather tool"),
new ToolResponseMessage(List.of(new ToolResponseMessage.ToolResponse("42", "weather",
"The weather in London is 20 degrees Celsius")))))
.build();

var generations = ToolExecutionResult.buildGenerations(toolExecutionResult);

assertThat(generations).hasSize(1);
var generation = generations.get(0);

// Verify existing metadata fields are still present
assertThat((String) generation.getMetadata().get(ToolExecutionResult.METADATA_TOOL_NAME)).isEqualTo("weather");
assertThat((String) generation.getMetadata().get(ToolExecutionResult.METADATA_TOOL_ID)).isEqualTo("42");
assertThat(generation.getMetadata().getFinishReason()).isEqualTo(ToolExecutionResult.FINISH_REASON);

// Verify new source metadata is added
assertThat((String) generation.getMetadata().get(ToolExecutionResult.METADATA_SOURCE))
.isEqualTo(ToolExecutionResult.SOURCE_TOOL);
}

}
Loading