Class GoogleGenAiChatModel

java.lang.Object
org.springframework.ai.google.genai.GoogleGenAiChatModel
All Implemented Interfaces:
org.springframework.ai.chat.model.ChatModel, org.springframework.ai.chat.model.StreamingChatModel, org.springframework.ai.model.Model<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>, org.springframework.ai.model.StreamingModel<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>, org.springframework.beans.factory.DisposableBean

public class GoogleGenAiChatModel extends Object implements org.springframework.ai.chat.model.ChatModel, org.springframework.beans.factory.DisposableBean
Google GenAI Chat Model implementation that provides access to Google's Gemini language models.

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.

Use the GoogleGenAiChatModel.Builder to create instances with custom configurations:


 GoogleGenAiChatModel model = GoogleGenAiChatModel.builder()
 		.genAiClient(genAiClient)
 		.defaultOptions(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
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
     
    static enum 
     
    static enum 
     
    static final record 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    GoogleGenAiChatModel(com.google.genai.Client genAiClient, GoogleGenAiChatOptions defaultOptions, org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, org.springframework.retry.support.RetryTemplate retryTemplate, io.micrometer.observation.ObservationRegistry observationRegistry)
    Creates a new instance of GoogleGenAiChatModel.
    GoogleGenAiChatModel(com.google.genai.Client genAiClient, GoogleGenAiChatOptions defaultOptions, org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, org.springframework.retry.support.RetryTemplate retryTemplate, io.micrometer.observation.ObservationRegistry observationRegistry, org.springframework.ai.model.tool.ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate)
    Creates a new instance of GoogleGenAiChatModel.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    org.springframework.ai.chat.model.ChatResponse
    call(org.springframework.ai.chat.prompt.Prompt prompt)
     
    void
     
    Gets the cached content service for managing cached content.
    org.springframework.ai.chat.prompt.ChatOptions
     
    reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse>
    internalStream(org.springframework.ai.chat.prompt.Prompt prompt, org.springframework.ai.chat.model.ChatResponse previousChatResponse)
     
    protected List<org.springframework.ai.chat.model.Generation>
    responseCandidateToGeneration(com.google.genai.types.Candidate candidate)
     
    void
    setObservationConvention(org.springframework.ai.chat.observation.ChatModelObservationConvention observationConvention)
    Use the provided convention for reporting observation data
    reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse>
    stream(org.springframework.ai.chat.prompt.Prompt prompt)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.ai.chat.model.ChatModel

    call, call

    Methods inherited from interface org.springframework.ai.chat.model.StreamingChatModel

    stream, stream
  • Constructor Details

    • GoogleGenAiChatModel

      public GoogleGenAiChatModel(com.google.genai.Client genAiClient, GoogleGenAiChatOptions defaultOptions, org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, org.springframework.retry.support.RetryTemplate retryTemplate, io.micrometer.observation.ObservationRegistry observationRegistry)
      Creates a new instance of GoogleGenAiChatModel.
      Parameters:
      genAiClient - the GenAI Client instance to use
      defaultOptions - the default options to use
      toolCallingManager - the tool calling manager to use. It is wrapped in a GoogleGenAiToolCallingManager to ensure compatibility with Vertex AI's OpenAPI schema format.
      retryTemplate - the retry template to use
      observationRegistry - the observation registry to use
    • GoogleGenAiChatModel

      public GoogleGenAiChatModel(com.google.genai.Client genAiClient, GoogleGenAiChatOptions defaultOptions, org.springframework.ai.model.tool.ToolCallingManager toolCallingManager, org.springframework.retry.support.RetryTemplate retryTemplate, io.micrometer.observation.ObservationRegistry observationRegistry, org.springframework.ai.model.tool.ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate)
      Creates a new instance of GoogleGenAiChatModel.
      Parameters:
      genAiClient - the GenAI Client instance to use
      defaultOptions - the default options to use
      toolCallingManager - the tool calling manager to use. It is wrapped in a GoogleGenAiToolCallingManager to ensure compatibility with Vertex AI's OpenAPI schema format.
      retryTemplate - the retry template to use
      observationRegistry - the observation registry to use
      toolExecutionEligibilityPredicate - the tool execution eligibility predicate
  • Method Details

    • call

      public org.springframework.ai.chat.model.ChatResponse call(org.springframework.ai.chat.prompt.Prompt prompt)
      Specified by:
      call in interface org.springframework.ai.chat.model.ChatModel
      Specified by:
      call in interface org.springframework.ai.model.Model<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>
    • stream

      public reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> stream(org.springframework.ai.chat.prompt.Prompt prompt)
      Specified by:
      stream in interface org.springframework.ai.chat.model.ChatModel
      Specified by:
      stream in interface org.springframework.ai.chat.model.StreamingChatModel
      Specified by:
      stream in interface org.springframework.ai.model.StreamingModel<org.springframework.ai.chat.prompt.Prompt,org.springframework.ai.chat.model.ChatResponse>
    • internalStream

      public reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> internalStream(org.springframework.ai.chat.prompt.Prompt prompt, org.springframework.ai.chat.model.ChatResponse previousChatResponse)
    • responseCandidateToGeneration

      protected List<org.springframework.ai.chat.model.Generation> responseCandidateToGeneration(com.google.genai.types.Candidate candidate)
    • getDefaultOptions

      public org.springframework.ai.chat.prompt.ChatOptions getDefaultOptions()
      Specified by:
      getDefaultOptions in interface org.springframework.ai.chat.model.ChatModel
    • getCachedContentService

      public GoogleGenAiCachedContentService getCachedContentService()
      Gets the cached content service for managing cached content.
      Returns:
      the cached content service
    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
      Throws:
      Exception
    • setObservationConvention

      public void setObservationConvention(org.springframework.ai.chat.observation.ChatModelObservationConvention observationConvention)
      Use the provided convention for reporting observation data
      Parameters:
      observationConvention - The provided convention
    • builder

      public static GoogleGenAiChatModel.Builder builder()