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: 1 addition & 1 deletion contrib/sarvam-ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>com.google.adk</groupId>
<artifactId>google-adk-parent</artifactId>
<version>1.2.0</version><!-- {x-version-update:google-adk:current} -->
<version>0.9.1-SNAPSHOT</version><!-- {x-version-update:google-adk:current} -->
<relativePath>../../pom.xml</relativePath>
</parent>

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

package com.google.adk.models.sarvamai;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.adk.models.BaseLlm;
Expand All @@ -24,13 +25,19 @@
import com.google.adk.models.LlmResponse;
import com.google.adk.models.sarvamai.chat.ChatRequest;
import com.google.adk.models.sarvamai.chat.ChatResponse;
import com.google.adk.models.sarvamai.chat.ChatToolCall;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.genai.types.Content;
import com.google.genai.types.FunctionCall;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.BackpressureStrategy;
import io.reactivex.rxjava3.core.Flowable;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
Expand Down Expand Up @@ -233,12 +240,48 @@ private LlmResponse toLlmResponse(ChatResponse chatResponse) {
}
var choice = chatResponse.getChoices().get(0);
var effectiveMsg = choice.effectiveMessage();
if (effectiveMsg == null || effectiveMsg.getContent() == null) {
throw new SarvamAiException("No content in response choice");
if (effectiveMsg == null) {
throw new SarvamAiException("No message in response choice");
}

Content content =
Content.builder().role("model").parts(Part.fromText(effectiveMsg.getContent())).build();
// Handle tool_calls in response (model requesting function execution)
if (effectiveMsg.getToolCalls() != null && !effectiveMsg.getToolCalls().isEmpty()) {
List<Part> parts = new ArrayList<>();
for (ChatToolCall tc : effectiveMsg.getToolCalls()) {
if (tc.getFunction() == null || tc.getFunction().getName() == null) {
continue;
}
String argsStr = tc.getFunction().getArguments();
if (argsStr == null) {
argsStr = "{}";
}
Map<String, Object> args;
try {
args = objectMapper.readValue(argsStr, new TypeReference<Map<String, Object>>() {});
} catch (Exception e) {
args = Map.of();
}
FunctionCall fc =
FunctionCall.builder()
.id(tc.getId())
.name(tc.getFunction().getName())
.args(args)
.build();
parts.add(Part.builder().functionCall(fc).build());
}
if (parts.isEmpty()) {
throw new SarvamAiException("Tool calls in response but no valid function call found");
}
Content content = Content.builder().role("model").parts(ImmutableList.copyOf(parts)).build();
return LlmResponse.builder().content(content).build();
}

// Handle text content
String textContent = effectiveMsg.getContent();
if (textContent == null) {
textContent = "";
}
Content content = Content.builder().role("model").parts(Part.fromText(textContent)).build();
return LlmResponse.builder().content(content).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

/**
* A message in the Sarvam AI chat completion API (request or response).
Expand All @@ -38,6 +39,12 @@ public final class ChatMessage {
@JsonProperty("reasoning_content")
private String reasoningContent;

@JsonProperty("tool_calls")
private List<ChatToolCall> toolCalls;

@JsonProperty("tool_call_id")
private String toolCallId;

public ChatMessage() {}

public ChatMessage(String role, String content) {
Expand Down Expand Up @@ -68,4 +75,20 @@ public String getReasoningContent() {
public void setReasoningContent(String reasoningContent) {
this.reasoningContent = reasoningContent;
}

public List<ChatToolCall> getToolCalls() {
return toolCalls;
}

public void setToolCalls(List<ChatToolCall> toolCalls) {
this.toolCalls = toolCalls;
}

public String getToolCallId() {
return toolCallId;
}

public void setToolCallId(String toolCallId) {
this.toolCallId = toolCallId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.adk.models.LlmRequest;
import com.google.adk.models.sarvamai.SarvamAiConfig;
import com.google.common.collect.ImmutableList;
import com.google.genai.types.Content;
import com.google.genai.types.FunctionDeclaration;
import com.google.genai.types.Part;
import com.google.genai.types.Schema;
import com.google.genai.types.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Request body for the Sarvam AI chat completions endpoint. Constructed from the ADK {@link
Expand Down Expand Up @@ -73,6 +80,15 @@ public final class ChatRequest {
@JsonProperty("stop")
private Object stop;

@JsonProperty("tools")
private List<Map<String, Object>> tools;

@JsonProperty("tool_choice")
private String toolChoice;

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String IDENTIFIER_REGEX = "[^a-zA-Z0-9_\\.-]";

public ChatRequest() {}

/**
Expand All @@ -95,18 +111,68 @@ public static ChatRequest fromLlmRequest(
if ("model".equals(role)) {
role = "assistant";
}
StringBuilder textBuilder = new StringBuilder();
content
.parts()
.ifPresent(
parts -> {
for (Part part : parts) {
part.text().ifPresent(textBuilder::append);
}
});
if (textBuilder.length() > 0) {
request.messages.add(new ChatMessage(role, textBuilder.toString()));
List<Part> parts = content.parts().orElse(ImmutableList.of());
if (parts.isEmpty()) {
continue;
}
Part firstPart = parts.get(0);

if (firstPart.functionResponse().isPresent()) {
var fr = firstPart.functionResponse().get();
ChatMessage toolMsg = new ChatMessage();
toolMsg.setRole("tool");
toolMsg.setToolCallId(fr.id().orElse("call_" + fr.name().orElse("unknown")));
toolMsg.setContent(
fr.response()
.map(
r -> {
try {
return OBJECT_MAPPER.writeValueAsString(r);
} catch (Exception e) {
return "{}";
}
})
.orElse("{}"));
request.messages.add(toolMsg);
} else if (firstPart.functionCall().isPresent()) {
var fc = firstPart.functionCall().get();
ChatMessage assistantMsg = new ChatMessage();
assistantMsg.setRole("assistant");
assistantMsg.setContent(null);
ChatToolCall tc = new ChatToolCall();
tc.setId(fc.id().orElse("call_" + fc.name().orElse("unknown")));
tc.setType("function");
ChatToolCall.ChatToolCallFunction tcf = new ChatToolCall.ChatToolCallFunction();
tcf.setName(fc.name().orElse(""));
tcf.setArguments(
fc.args()
.map(
args -> {
try {
return OBJECT_MAPPER.writeValueAsString(args);
} catch (Exception e) {
return "{}";
}
})
.orElse("{}"));
tc.setFunction(tcf);
assistantMsg.setToolCalls(List.of(tc));
request.messages.add(assistantMsg);
} else {
StringBuilder textBuilder = new StringBuilder();
for (Part part : parts) {
part.text().ifPresent(textBuilder::append);
}
if (textBuilder.length() > 0) {
request.messages.add(
new ChatMessage(role.equals("model") ? "assistant" : role, textBuilder.toString()));
}
}
}

if (!llmRequest.tools().isEmpty()) {
request.tools = buildTools(llmRequest);
request.toolChoice = "auto";
}

config.temperature().ifPresent(v -> request.temperature = v);
Expand All @@ -120,6 +186,96 @@ public static ChatRequest fromLlmRequest(
return request;
}

private static List<Map<String, Object>> buildTools(LlmRequest llmRequest) {
List<Map<String, Object>> toolsList = new ArrayList<>();
llmRequest
.tools()
.forEach(
(name, baseTool) -> {
Optional<FunctionDeclaration> declOpt = baseTool.declaration();
if (declOpt.isEmpty()) {
return;
}
FunctionDeclaration decl = declOpt.get();
Map<String, Object> funcMap = new java.util.HashMap<>();
funcMap.put("name", cleanForIdentifier(decl.name().orElse("")));
funcMap.put("description", cleanForIdentifier(decl.description().orElse("")));

decl.parameters()
.ifPresent(
paramsSchema -> {
Map<String, Object> paramsMap = new java.util.HashMap<>();
paramsMap.put("type", "object");
paramsSchema
.properties()
.ifPresent(
props -> {
Map<String, Object> propsMap = new java.util.HashMap<>();
props.forEach(
(key, schema) -> {
Map<String, Object> schemaMap = schemaToMap(schema);
normalizeTypeStrings(schemaMap);
propsMap.put(key, schemaMap);
});
paramsMap.put("properties", propsMap);
});
paramsSchema.required().ifPresent(r -> paramsMap.put("required", r));
funcMap.put("parameters", paramsMap);
});

Map<String, Object> toolWrapper = new java.util.HashMap<>();
toolWrapper.put("type", "function");
toolWrapper.put("function", funcMap);
toolsList.add(toolWrapper);
});
return toolsList;
}

/** Manually convert Schema to Map to avoid Jackson Optional serialization issues. */
private static Map<String, Object> schemaToMap(Schema schema) {
Map<String, Object> map = new java.util.HashMap<>();
schema.type().ifPresent(t -> map.put("type", schemaTypeToString(t)));
schema.description().ifPresent(d -> map.put("description", d));
schema
.properties()
.ifPresent(
props -> {
Map<String, Object> propsMap = new java.util.HashMap<>();
props.forEach((k, v) -> propsMap.put(k, schemaToMap(v)));
map.put("properties", propsMap);
});
schema.required().ifPresent(r -> map.put("required", r));
schema.items().ifPresent(i -> map.put("items", schemaToMap(i)));
return map;
}

private static String schemaTypeToString(Type type) {
return switch (type.knownEnum()) {
case STRING -> "string";
case NUMBER -> "number";
case INTEGER -> "integer";
case BOOLEAN -> "boolean";
case ARRAY -> "array";
case OBJECT -> "object";
default -> "string";
};
}

private static String cleanForIdentifier(String input) {
return input == null ? "" : input.replaceAll(IDENTIFIER_REGEX, "");
}

@SuppressWarnings("unchecked")
private static void normalizeTypeStrings(Map<String, Object> valueDict) {
if (valueDict == null) return;
if (valueDict.containsKey("type") && valueDict.get("type") instanceof String) {
valueDict.put("type", ((String) valueDict.get("type")).toLowerCase());
}
if (valueDict.containsKey("items") && valueDict.get("items") instanceof Map) {
normalizeTypeStrings((Map<String, Object>) valueDict.get("items"));
}
}

public String getModel() {
return model;
}
Expand Down Expand Up @@ -151,4 +307,12 @@ public String getReasoningEffort() {
public Boolean getWikiGrounding() {
return wikiGrounding;
}

public List<Map<String, Object>> getTools() {
return tools;
}

public String getToolChoice() {
return toolChoice;
}
}
Loading