Class GoogleGenAiChatModel
- All Implemented Interfaces:
ChatModel,StreamingChatModel,Model<Prompt,,ChatResponse> StreamingModel<Prompt,,ChatResponse> org.springframework.beans.factory.DisposableBean
Key features include:
- Support for multiple Gemini model versions including Gemini Pro, Gemini 1.5 Pro, Gemini 1.5/2.0 Flash variants
- Tool/Function calling capabilities through
ToolCallingManager - Streaming support via
stream(Prompt)method - Configurable safety settings through
GoogleGenAiSafetySetting - Support for system messages and multi-modal content (text and images)
- Built-in retry mechanism and observability through Micrometer
- Google Search Retrieval integration
The model can be configured with various options including temperature, top-k, top-p
sampling, maximum output tokens, and candidate count through
GoogleGenAiChatOptions.
A response candidate is one Generation whose parts mirror the Gemini parts in order, each with its thought signature as an
OpaquePayload, so that the message replays to Gemini as it was produced and the
model keeps its reasoning context across tool-call rounds. See
responseCandidateToGeneration(Candidate) for the mapping.
Use the GoogleGenAiChatModel.Builder to create instances with custom configurations:
GoogleGenAiChatModel model = GoogleGenAiChatModel.builder()
.genAiClient(genAiClient)
.options(options)
.toolCallingManager(toolManager)
.build();
- Since:
- 0.8.1
- Author:
- Christian Tzolov, Grogdunn, luocongqiu, Chris Turchin, Mark Pollack, Soby Chacko, Jihoon Kim, Alexandros Pappas, Ilayaperumal Gopinathan, Dan Dobrin, Thomas Vitale, Sebastien Deleuze, Dimitar Proynov
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic enumstatic enumstatic final record -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringTheOpaquePayload.provider()of the parts produced by this model.static final StringTheOpaquePayload.kind()of a Gemini thought signature; the payload data is the Base64 encoded signature, replayed unmodified on the part that carried it. -
Constructor Summary
ConstructorsConstructorDescriptionGoogleGenAiChatModel(com.google.genai.Client genAiClient, GoogleGenAiChatOptions options, ToolCallingManager toolCallingManager, org.springframework.core.retry.RetryTemplate retryTemplate, io.micrometer.observation.ObservationRegistry observationRegistry) Creates a new instance of GoogleGenAiChatModel. -
Method Summary
Modifier and TypeMethodDescriptionstatic GoogleGenAiChatModel.Builderbuilder()Executes a method call to the AI model.voiddestroy()@Nullable GoogleGenAiCachedContentServiceGets the cached content service for managing cached content.Gets the chat options for this model.protected List<Generation>responseCandidateToGeneration(com.google.genai.types.Candidate candidate) Converts a response candidate to a single generation whose message holds the Gemini parts, in order, asMessageParts: thought text asReasoningPart, answer text asTextPart, function calls asToolCallPart, inline data asMediaPart, and anything else (server-side tool calls and responses, executable code, code execution results, file data) asUnknownPart, kept verbatim for replay.voidsetObservationConvention(ChatModelObservationConvention observationConvention) Use the provided convention for reporting observation datareactor.core.publisher.Flux<ChatResponse>Executes a method call to the AI model.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.chat.model.ChatModel
call, call, getDefaultOptionsMethods inherited from interface org.springframework.ai.chat.model.StreamingChatModel
stream, stream
-
Field Details
-
GOOGLE_PROVIDER
TheOpaquePayload.provider()of the parts produced by this model.- Since:
- 2.1.0
- See Also:
-
PAYLOAD_THOUGHT_SIGNATURE
TheOpaquePayload.kind()of a Gemini thought signature; the payload data is the Base64 encoded signature, replayed unmodified on the part that carried it.- Since:
- 2.1.0
- See Also:
-
-
Constructor Details
-
GoogleGenAiChatModel
public GoogleGenAiChatModel(com.google.genai.Client genAiClient, GoogleGenAiChatOptions options, ToolCallingManager toolCallingManager, org.springframework.core.retry.RetryTemplate retryTemplate, io.micrometer.observation.ObservationRegistry observationRegistry) Creates a new instance of GoogleGenAiChatModel.- Parameters:
genAiClient- the GenAI Client instance to useoptions- the default options to usetoolCallingManager- the tool calling manager to use. It is wrapped in aGoogleGenAiToolCallingManagerto ensure compatibility with Vertex AI's OpenAPI schema format.retryTemplate- the retry template to useobservationRegistry- the observation registry to use
-
-
Method Details
-
call
Description copied from interface:ModelExecutes a method call to the AI model. -
stream
Description copied from interface:StreamingModelExecutes a method call to the AI model.- Specified by:
streamin interfaceChatModel- Specified by:
streamin interfaceStreamingChatModel- Specified by:
streamin interfaceStreamingModel<Prompt,ChatResponse> - Parameters:
prompt- the request object to be sent to the AI model- Returns:
- the streaming response from the AI model
-
responseCandidateToGeneration
protected List<Generation> responseCandidateToGeneration(com.google.genai.types.Candidate candidate) Converts a response candidate to a single generation whose message holds the Gemini parts, in order, asMessageParts: thought text asReasoningPart, answer text asTextPart, function calls asToolCallPart, inline data asMediaPart, and anything else (server-side tool calls and responses, executable code, code execution results, file data) asUnknownPart, kept verbatim for replay. A thought signature becomes theOpaquePayloadof the part that carried it.The streaming path maps every chunk through this method too, so an override applies to both
call(Prompt)andstream(Prompt). When streaming, the parts of the returned message are stamped with a content block index; a message of anAssistantMessagesubclass is passed through as returned instead, since its parts cannot be restamped without losing its type, and is aggregated the way messages without indexed parts are.- Parameters:
candidate- the response candidate- Returns:
- a single-element list holding the generation
-
getOptions
Description copied from interface:ChatModelGets the chat options for this model.- Specified by:
getOptionsin interfaceChatModel- Returns:
- the chat options
- Since:
- 2.0.0
-
getCachedContentService
Gets the cached content service for managing cached content.- Returns:
- the cached content service
-
destroy
- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean- Throws:
Exception
-
setObservationConvention
Use the provided convention for reporting observation data- Parameters:
observationConvention- The provided convention
-
builder
-