Skip to content

Commit 8463454

Browse files
committed
docs: enhance Vertex AI Gemini documentation and code organization
- Add javadoc for VertexAiGeminiChatModel including features, examples, and cross-references - Document JsonSchemaConverter and VertexToolCallingManager classes and methods - Fix formatter annotation placement in VertexAiGeminiChatOptions Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
1 parent a307a2b commit 8463454

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/VertexAiGeminiChatModel.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,37 @@
9393
import org.springframework.util.StringUtils;
9494

9595
/**
96-
* Vertex AI Gemini Chat Model implementation.
96+
* Vertex AI Gemini Chat Model implementation that provides access to Google's Gemini
97+
* language models.
98+
*
99+
* <p>
100+
* Key features include:
101+
* <ul>
102+
* <li>Support for multiple Gemini model versions including Gemini Pro, Gemini 1.5 Pro,
103+
* Gemini 1.5/2.0 Flash variants</li>
104+
* <li>Tool/Function calling capabilities through {@link ToolCallingManager}</li>
105+
* <li>Streaming support via {@link #stream(Prompt)} method</li>
106+
* <li>Configurable safety settings through {@link VertexAiGeminiSafetySetting}</li>
107+
* <li>Support for system messages and multi-modal content (text and images)</li>
108+
* <li>Built-in retry mechanism and observability through Micrometer</li>
109+
* <li>Google Search Retrieval integration</li>
110+
* </ul>
111+
*
112+
* <p>
113+
* The model can be configured with various options including temperature, top-k, top-p
114+
* sampling, maximum output tokens, and candidate count through
115+
* {@link VertexAiGeminiChatOptions}.
116+
*
117+
* <p>
118+
* Use the {@link Builder} to create instances with custom configurations:
119+
*
120+
* <pre>{@code
121+
* VertexAiGeminiChatModel model = VertexAiGeminiChatModel.builder()
122+
* .vertexAI(vertexAI)
123+
* .defaultOptions(options)
124+
* .toolCallingManager(toolManager)
125+
* .build();
126+
* }</pre>
97127
*
98128
* @author Christian Tzolov
99129
* @author Grogdunn
@@ -104,6 +134,9 @@
104134
* @author Jihoon Kim
105135
* @author Alexandros Pappas
106136
* @since 0.8.1
137+
* @see VertexAiGeminiChatOptions
138+
* @see ToolCallingManager
139+
* @see ChatModel
107140
*/
108141
public class VertexAiGeminiChatModel extends AbstractToolCallSupport implements ChatModel, DisposableBean {
109142

@@ -203,6 +236,16 @@ public VertexAiGeminiChatModel(VertexAI vertexAI, VertexAiGeminiChatOptions opti
203236

204237
}
205238

239+
/**
240+
* Creates a new instance of VertexAiGeminiChatModel.
241+
* @param vertexAI the Vertex AI instance to use
242+
* @param defaultOptions the default options to use
243+
* @param toolCallingManager the tool calling manager to use. It is wrapped in a
244+
* {@link VertexToolCallingManager} to ensure compatibility with Vertex AI's OpenAPI
245+
* schema format.
246+
* @param retryTemplate the retry template to use
247+
* @param observationRegistry the observation registry to use
248+
*/
206249
public VertexAiGeminiChatModel(VertexAI vertexAI, VertexAiGeminiChatOptions defaultOptions,
207250
ToolCallingManager toolCallingManager, RetryTemplate retryTemplate,
208251
ObservationRegistry observationRegistry) {
@@ -221,6 +264,8 @@ public VertexAiGeminiChatModel(VertexAI vertexAI, VertexAiGeminiChatOptions defa
221264
this.retryTemplate = retryTemplate;
222265
this.observationRegistry = observationRegistry;
223266

267+
// Wrap the provided tool calling manager in a VertexToolCallingManager to ensure
268+
// compatibility with Vertex AI's OpenAPI schema format.
224269
if (toolCallingManager instanceof VertexToolCallingManager) {
225270
this.toolCallingManager = toolCallingManager;
226271
}

models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/VertexAiGeminiChatOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,12 @@ public class VertexAiGeminiChatOptions implements ToolCallingChatOptions {
126126

127127
@JsonIgnore
128128
private List<VertexAiGeminiSafetySetting> safetySettings = new ArrayList<>();
129+
// @formatter:on
129130

130131
public static Builder builder() {
131132
return new Builder();
132133
}
133134

134-
// @formatter:on
135-
136135
public static VertexAiGeminiChatOptions fromOptions(VertexAiGeminiChatOptions fromOptions) {
137136
VertexAiGeminiChatOptions options = new VertexAiGeminiChatOptions();
138137
options.setStopSequences(fromOptions.getStopSequences());

models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/schema/JsonSchemaConverter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ private JsonSchemaConverter() {
3535
// Prevent instantiation
3636
}
3737

38+
/**
39+
* Parses a JSON string into an ObjectNode.
40+
* @param jsonString The JSON string to parse
41+
* @return ObjectNode containing the parsed JSON
42+
* @throws RuntimeException if the JSON string cannot be parsed
43+
*/
3844
public static ObjectNode fromJson(String jsonString) {
3945
try {
4046
return (ObjectNode) JsonParser.getObjectMapper().readTree(jsonString);

models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/schema/VertexToolCallingManager.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,44 @@
2626
import org.springframework.ai.model.tool.ToolExecutionResult;
2727
import org.springframework.ai.tool.definition.ToolDefinition;
2828
import org.springframework.ai.util.json.schema.JsonSchemaGenerator;
29+
import org.springframework.util.Assert;
2930

3031
/**
32+
* Implementation of {@link ToolCallingManager} specifically designed for Vertex AI
33+
* Gemini. This manager adapts tool definitions to be compatible with Vertex AI's OpenAPI
34+
* schema format by converting JSON schemas and ensuring proper type value upper-casing.
35+
*
36+
* <p>
37+
* It delegates the actual tool execution to another {@link ToolCallingManager} while
38+
* handling the necessary schema conversions for Vertex AI compatibility.
39+
*
3140
* @author Christian Tzolov
3241
* @since 1.0.0
3342
*/
34-
3543
public class VertexToolCallingManager implements ToolCallingManager {
3644

45+
/**
46+
* The underlying tool calling manager that handles actual tool execution.
47+
*/
3748
private final ToolCallingManager delegateToolCallingManager;
3849

50+
/**
51+
* Creates a new instance of VertexToolCallingManager.
52+
* @param delegateToolCallingManager the underlying tool calling manager that handles
53+
* actual tool execution
54+
*/
3955
public VertexToolCallingManager(ToolCallingManager delegateToolCallingManager) {
56+
Assert.notNull(delegateToolCallingManager, "Delegate tool calling manager must not be null");
4057
this.delegateToolCallingManager = delegateToolCallingManager;
4158
}
4259

60+
/**
61+
* Resolves tool definitions and converts their input schemas to be compatible with
62+
* Vertex AI's OpenAPI format. This includes converting JSON schemas to OpenAPI format
63+
* and ensuring proper type value casing.
64+
* @param chatOptions the options containing tool preferences and configurations
65+
* @return a list of tool definitions with Vertex AI compatible schemas
66+
*/
4367
@Override
4468
public List<ToolDefinition> resolveToolDefinitions(ToolCallingChatOptions chatOptions) {
4569

@@ -58,6 +82,12 @@ public List<ToolDefinition> resolveToolDefinitions(ToolCallingChatOptions chatOp
5882
}).toList();
5983
}
6084

85+
/**
86+
* Executes tool calls by delegating to the underlying tool calling manager.
87+
* @param prompt the original prompt that triggered the tool calls
88+
* @param chatResponse the chat response containing the tool calls to execute
89+
* @return the result of executing the tool calls
90+
*/
6191
@Override
6292
public ToolExecutionResult executeToolCalls(Prompt prompt, ChatResponse chatResponse) {
6393
return this.delegateToolCallingManager.executeToolCalls(prompt, chatResponse);

0 commit comments

Comments
 (0)