Class GeminiChatModel

java.lang.Object
io.agentscope.core.model.ChatModelBase
io.agentscope.extensions.model.gemini.GeminiChatModel
All Implemented Interfaces:
io.agentscope.core.model.Model

public class GeminiChatModel extends io.agentscope.core.model.ChatModelBase
Gemini Chat Model implementation using the official Google GenAI Java SDK.

This implementation provides complete integration with Gemini's Content Generation API, including tool calling and multi-agent conversation support.

Supported Features:

  • Text generation with streaming and non-streaming modes
  • Tool/function calling support
  • Multi-agent conversation with history merging
  • Vision capabilities (images, audio, video)
  • Thinking mode (extended reasoning)
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder for creating GeminiChatModel instances.
  • Constructor Summary

    Constructors
    Constructor
    Description
    GeminiChatModel(String apiKey, String modelName, boolean streamEnabled, String project, String location, Boolean vertexAI, com.google.genai.types.HttpOptions httpOptions, com.google.auth.oauth2.GoogleCredentials credentials, com.google.genai.types.ClientOptions clientOptions, io.agentscope.core.model.GenerateOptions defaultOptions, io.agentscope.core.formatter.Formatter<com.google.genai.types.Content,com.google.genai.types.GenerateContentResponse,com.google.genai.types.GenerateContentConfig.Builder> formatter)
    Creates a new Gemini chat model instance using the default endpoint configuration.
    GeminiChatModel(String apiKey, String baseUrl, String modelName, boolean streamEnabled, String project, String location, Boolean vertexAI, com.google.genai.types.HttpOptions httpOptions, com.google.auth.oauth2.GoogleCredentials credentials, com.google.genai.types.ClientOptions clientOptions, io.agentscope.core.model.GenerateOptions defaultOptions, io.agentscope.core.formatter.Formatter<com.google.genai.types.Content,com.google.genai.types.GenerateContentResponse,com.google.genai.types.GenerateContentConfig.Builder> formatter)
    Creates a new Gemini chat model instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a new builder for GeminiChatModel.
    void
    Close the Gemini client.
    protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse>
    doStream(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options)
    Stream chat completion responses from Gemini's API.
    protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse>
    doStream0(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options)
     
     

    Methods inherited from class io.agentscope.core.model.ChatModelBase

    getContextWindowSize, setContextWindowSize, setNativeStructuredOutput, setNativeStructuredOutputWithTools, stream, supportsNativeStructuredOutput, supportsNativeStructuredOutputWithTools

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • GeminiChatModel

      public GeminiChatModel(String apiKey, String baseUrl, String modelName, boolean streamEnabled, String project, String location, Boolean vertexAI, com.google.genai.types.HttpOptions httpOptions, com.google.auth.oauth2.GoogleCredentials credentials, com.google.genai.types.ClientOptions clientOptions, io.agentscope.core.model.GenerateOptions defaultOptions, io.agentscope.core.formatter.Formatter<com.google.genai.types.Content,com.google.genai.types.GenerateContentResponse,com.google.genai.types.GenerateContentConfig.Builder> formatter)
      Creates a new Gemini chat model instance.
      Parameters:
      apiKey - the API key for authentication (for Gemini API)
      baseUrl - the custom base URL for Gemini API (null for default)
      modelName - the model name to use (e.g., "gemini-2.0-flash", "gemini-1.5-pro")
      streamEnabled - whether streaming should be enabled
      project - the Google Cloud project ID (for Vertex AI)
      location - the Google Cloud location (for Vertex AI, e.g., "us-central1")
      vertexAI - whether to use Vertex AI APIs (null for auto-detection)
      httpOptions - HTTP options for the client
      credentials - Google credentials (for Vertex AI)
      clientOptions - client options for the API client
      defaultOptions - default generation options
      formatter - the message formatter to use (null for default Gemini formatter)
    • GeminiChatModel

      public GeminiChatModel(String apiKey, String modelName, boolean streamEnabled, String project, String location, Boolean vertexAI, com.google.genai.types.HttpOptions httpOptions, com.google.auth.oauth2.GoogleCredentials credentials, com.google.genai.types.ClientOptions clientOptions, io.agentscope.core.model.GenerateOptions defaultOptions, io.agentscope.core.formatter.Formatter<com.google.genai.types.Content,com.google.genai.types.GenerateContentResponse,com.google.genai.types.GenerateContentConfig.Builder> formatter)
      Creates a new Gemini chat model instance using the default endpoint configuration.

      This overload passes null for baseUrl, allowing the SDK to use its default endpoint behavior for either Gemini API or Vertex AI, depending on the provided configuration.

      Parameters:
      apiKey - the API key for authentication (for Gemini API)
      modelName - the model name to use (e.g., "gemini-2.0-flash", "gemini-1.5-pro")
      streamEnabled - whether streaming should be enabled
      project - the Google Cloud project ID (for Vertex AI)
      location - the Google Cloud location (for Vertex AI, e.g., "us-central1")
      vertexAI - whether to use Vertex AI APIs (null for auto-detection)
      httpOptions - HTTP options for the client
      credentials - Google credentials (for Vertex AI)
      clientOptions - client options for the API client
      defaultOptions - default generation options
      formatter - the message formatter to use (null for default Gemini formatter)
  • Method Details

    • doStream

      protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse> doStream(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options)
      Stream chat completion responses from Gemini's API.

      This method internally handles message formatting using the configured formatter. When streaming is enabled, it returns incremental responses as they arrive. When streaming is disabled, it returns a single complete response.

      Specified by:
      doStream in class io.agentscope.core.model.ChatModelBase
      Parameters:
      messages - AgentScope messages to send to the model
      tools - Optional list of tool schemas (null or empty if no tools)
      options - Optional generation options (null to use defaults)
      Returns:
      Flux stream of chat responses
    • doStream0

      protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse> doStream0(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options)
    • getModelName

      public String getModelName()
    • close

      public void close()
      Close the Gemini client.
    • builder

      public static GeminiChatModel.Builder builder()
      Creates a new builder for GeminiChatModel.
      Returns:
      a new Builder instance