Skip to content
Closed
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 @@ -380,7 +380,7 @@ else if (message instanceof AssistantMessage assistantMessage) {
if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) {
toolCalls = assistantMessage.getToolCalls().stream().map(toolCall -> {
var function = new ChatCompletionFunction(toolCall.name(), toolCall.arguments());
return new ToolCall(toolCall.id(), toolCall.type(), function);
return new ToolCall(toolCall.id(), toolCall.type(), function, null);
}).toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,11 @@ public enum Role {
* @param type The type of tool call the output is required for. For now, this is
* always function.
* @param function The function definition.
* @param index The index of the tool call in the list of tool calls.
*/
@JsonInclude(Include.NON_NULL)
public record ToolCall(@JsonProperty("id") String id, @JsonProperty("type") String type,
@JsonProperty("function") ChatCompletionFunction function) {
@JsonProperty("function") ChatCompletionFunction function, @JsonProperty("index") Integer index) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package org.springframework.ai.mistralai.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wildcard imports are not allowed.
Will try to fix it while reviewing.


import org.springframework.ai.mistralai.api.MistralAiApi.ChatCompletionChunk;
import org.springframework.ai.mistralai.api.MistralAiApi.ChatCompletionChunk.ChunkChoice;
Expand Down Expand Up @@ -74,16 +71,16 @@ private ChunkChoice merge(ChunkChoice previous, ChunkChoice current) {
Optional<String> id = current.delta()
.toolCalls()
.stream()
.filter(tool -> tool.id() != null)
.map(tool -> tool.id())
.map(ToolCall::id)
.filter(Objects::nonNull)
.findFirst();
if (!id.isPresent()) {
if (id.isEmpty()) {
var newId = UUID.randomUUID().toString();

var toolCallsWithID = current.delta()
.toolCalls()
.stream()
.map(toolCall -> new ToolCall(newId, "function", toolCall.function()))
.map(toolCall -> new ToolCall(newId, "function", toolCall.function(), toolCall.index()))
.toList();

var role = current.delta().role() != null ? current.delta().role() : Role.ASSISTANT;
Expand Down Expand Up @@ -151,7 +148,8 @@ private ToolCall merge(ToolCall previous, ToolCall current) {
String id = (current.id() != null ? current.id() : previous.id());
String type = (current.type() != null ? current.type() : previous.type());
ChatCompletionFunction function = merge(previous.function(), current.function());
return new ToolCall(id, type, function);
Integer index = (current.index() != null ? current.index() : previous.index());
return new ToolCall(id, type, function, index);
}

private ChatCompletionFunction merge(ChatCompletionFunction previous, ChatCompletionFunction current) {
Expand Down
Loading