Class GoogleGenAiChatModel.Builder

java.lang.Object
dev.langchain4j.model.google.genai.GoogleGenAiChatModel.Builder
Enclosing class:
GoogleGenAiChatModel

public static class GoogleGenAiChatModel.Builder extends Object
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • client

      public GoogleGenAiChatModel.Builder client(com.google.genai.Client client)
      Sets a pre-configured Google GenAI Client.

      Use this when you need full control over client configuration. When set, apiKey(String), googleCredentials(GoogleCredentials), projectId(String), location(String), and timeout(Duration) are ignored.

      Parameters:
      client - the pre-configured client
      Returns:
      this
    • googleCredentials

      public GoogleGenAiChatModel.Builder googleCredentials(com.google.auth.oauth2.GoogleCredentials credentials)
      Sets the Google OAuth2 credentials used to authenticate requests via Vertex AI.

      Use this instead of apiKey(String) when authenticating with a service account or Application Default Credentials (ADC). Requires projectId(String) and location(String) to be set.

      Parameters:
      credentials - the Google OAuth2 credentials
      Returns:
      this
    • apiKey

      public GoogleGenAiChatModel.Builder apiKey(String apiKey)
      Sets the Google AI Gemini API key used to authenticate requests.

      Use this for the Google AI Studio API. For Vertex AI, use googleCredentials(GoogleCredentials). Alternatively, set the GOOGLE_API_KEY environment variable.

      Parameters:
      apiKey - the API key
      Returns:
      this
    • projectId

      public GoogleGenAiChatModel.Builder projectId(String projectId)
      Sets the Google Cloud project ID for Vertex AI access.

      Required when using googleCredentials(GoogleCredentials) for Vertex AI.

      Parameters:
      projectId - the GCP project ID
      Returns:
      this
    • location

      public GoogleGenAiChatModel.Builder location(String location)
      Sets the Google Cloud region/location for Vertex AI access (e.g. "us-central1").

      Required when using googleCredentials(GoogleCredentials) for Vertex AI.

      Parameters:
      location - the GCP region
      Returns:
      this
    • modelName

      public GoogleGenAiChatModel.Builder modelName(String modelName)
      Sets the model to use for chat completions.

      Examples: "gemini-2.0-flash", "gemini-2.5-pro".

      Parameters:
      modelName - the model name
      Returns:
      this
    • timeout

      public GoogleGenAiChatModel.Builder timeout(Duration timeout)
      Sets the HTTP request timeout for calls to the Google GenAI API.
      Parameters:
      timeout - the request timeout
      Returns:
      this
    • temperature

      public GoogleGenAiChatModel.Builder temperature(Double temperature)
      Sets the sampling temperature in the range [0.0, 2.0]. Higher values produce more creative output; lower values produce more deterministic output.
      Parameters:
      temperature - the sampling temperature
      Returns:
      this
    • topP

      Sets the nucleus sampling probability (top-p). Only the tokens whose cumulative probability exceeds this threshold are considered.
      Parameters:
      topP - the nucleus sampling threshold
      Returns:
      this
    • topK

      Sets the top-K sampling value. Only the topK most-likely next tokens are considered at each step.
      Parameters:
      topK - the number of top tokens to sample from
      Returns:
      this
    • frequencyPenalty

      public GoogleGenAiChatModel.Builder frequencyPenalty(Double frequencyPenalty)
      Sets the frequency penalty, which reduces the likelihood of repeating tokens proportionally to how often they have appeared in the response so far.
      Parameters:
      frequencyPenalty - the frequency penalty
      Returns:
      this
    • presencePenalty

      public GoogleGenAiChatModel.Builder presencePenalty(Double presencePenalty)
      Sets the presence penalty, which reduces the likelihood of repeating any token that has already appeared in the response, regardless of frequency.
      Parameters:
      presencePenalty - the presence penalty
      Returns:
      this
    • maxOutputTokens

      public GoogleGenAiChatModel.Builder maxOutputTokens(Integer maxOutputTokens)
      Sets the maximum number of tokens to generate in the response.
      Parameters:
      maxOutputTokens - the maximum number of output tokens
      Returns:
      this
    • thinkingBudget

      public GoogleGenAiChatModel.Builder thinkingBudget(Integer thinkingBudget)
      The thinking budget to use. This is a legacy parameter. For Gemini 3.x models, use thinkingLevel(String) instead.
    • thinkingLevel

      public GoogleGenAiChatModel.Builder thinkingLevel(String thinkingLevel)
      The thinking level to use. This is the recommended parameter for Gemini 3.x models. Allowed values are "MINIMAL", "LOW", "MEDIUM", "HIGH". Note that this cannot be used together with thinkingBudget(Integer).
    • seed

      Sets the random seed for deterministic output. Requests with the same seed and parameters should produce the same response.
      Parameters:
      seed - the random seed
      Returns:
      this
    • stopSequences

      public GoogleGenAiChatModel.Builder stopSequences(List<String> stopSequences)
      Sets sequences that, when generated, will cause the model to stop generating further tokens.
      Parameters:
      stopSequences - the list of stop sequences
      Returns:
      this
    • maxRetries

      public GoogleGenAiChatModel.Builder maxRetries(Integer maxRetries)
      Sets the number of times to retry a request on transient errors (e.g. rate limits, server errors).

      Defaults to 2.

      Parameters:
      maxRetries - the maximum number of retry attempts
      Returns:
      this
    • safetySettings

      public GoogleGenAiChatModel.Builder safetySettings(List<com.google.genai.types.SafetySetting> safetySettings)
      Sets safety content filter settings to block harmful content. See the safety settings docs.
      Parameters:
      safetySettings - the list of safety settings
      Returns:
      this
    • responseFormat

      public GoogleGenAiChatModel.Builder responseFormat(ResponseFormat responseFormat)
      Sets the response format, enabling structured output such as JSON mode or JSON Schema.
      Parameters:
      responseFormat - the desired response format
      Returns:
      this
    • enableGoogleSearch

      public GoogleGenAiChatModel.Builder enableGoogleSearch(boolean googleSearch)
      Enables the Google Search grounding tool, allowing the model to retrieve real-time information from the web. See the Google Search tool docs.
      Parameters:
      googleSearch - whether to enable Google Search
      Returns:
      this
    • enableGoogleMaps

      public GoogleGenAiChatModel.Builder enableGoogleMaps(boolean googleMaps)
      Enables the Google Maps grounding tool, allowing the model to retrieve location-based information. See the Google Maps tool docs.
      Parameters:
      googleMaps - whether to enable Google Maps
      Returns:
      this
    • enableUrlContext

      public GoogleGenAiChatModel.Builder enableUrlContext(boolean urlContext)
      Enables the URL context tool, allowing the model to read and reason about content at provided URLs. See the URL context tool docs.
      Parameters:
      urlContext - whether to enable URL context
      Returns:
      this
    • allowedFunctionNames

      public GoogleGenAiChatModel.Builder allowedFunctionNames(List<String> allowedFunctionNames)
      Restricts function calling to only the specified function names. When set, the model can only call functions whose names appear in this list.
      Parameters:
      allowedFunctionNames - the list of allowed function names
      Returns:
      this
    • listeners

      public GoogleGenAiChatModel.Builder listeners(List<ChatModelListener> listeners)
      Sets the list of ChatModelListeners to be notified on each request and response. Useful for logging, metrics, and observability integrations.
      Parameters:
      listeners - the chat model listeners
      Returns:
      this
    • defaultRequestParameters

      public GoogleGenAiChatModel.Builder defaultRequestParameters(ChatRequestParameters defaultRequestParameters)
      Sets default ChatRequestParameters that are merged into every request. Individual request parameters take precedence over these defaults.
      Parameters:
      defaultRequestParameters - the default request parameters
      Returns:
      this
    • vertexSearchDatastore

      public GoogleGenAiChatModel.Builder vertexSearchDatastore(String vertexSearchDatastore)
      Sets the Vertex AI Search datastore to use for grounding the model's responses with enterprise data.
      Parameters:
      vertexSearchDatastore - the Vertex AI Search datastore resource name
      Returns:
      this
    • labels

      public GoogleGenAiChatModel.Builder labels(Map<String,String> labels)
      Sets labels (key-value metadata) attached to the request. Useful for cost attribution and billing tracking in Google Cloud.
      Parameters:
      labels - a map of label keys to values
      Returns:
      this
    • apiEndpoint

      public GoogleGenAiChatModel.Builder apiEndpoint(String apiEndpoint)
      Overrides the default API endpoint.

      Useful for pointing at a regional endpoint or a compatible proxy.

      Parameters:
      apiEndpoint - the custom API endpoint URL
      Returns:
      this
    • customHeaders

      public GoogleGenAiChatModel.Builder customHeaders(Map<String,String> customHeaders)
      Sets extra HTTP headers to include in every request to the Google GenAI API.
      Parameters:
      customHeaders - a map of header names to values
      Returns:
      this
    • cachedContent

      public GoogleGenAiChatModel.Builder cachedContent(String cachedContent)
      Sets the name of a previously created cached content resource to use with this model. Using cached content can reduce latency and cost for repeated prompts. See the context caching docs.
      Parameters:
      cachedContent - the cached content resource name
      Returns:
      this
    • logRequests

      public GoogleGenAiChatModel.Builder logRequests(Boolean logRequests)
      Enables debug logging of request details sent to the Google GenAI API.
      Parameters:
      logRequests - whether to log requests
      Returns:
      this
    • logResponses

      public GoogleGenAiChatModel.Builder logResponses(Boolean logResponses)
      Enables debug logging of response details received from the Google GenAI API.
      Parameters:
      logResponses - whether to log responses
      Returns:
      this
    • logRequestsAndResponses

      public GoogleGenAiChatModel.Builder logRequestsAndResponses(Boolean logRequestsAndResponses)
      Enables debug logging of both request and response details. Equivalent to calling both logRequests(Boolean) and logResponses(Boolean).
      Parameters:
      logRequestsAndResponses - whether to log requests and responses
      Returns:
      this
    • build

      public GoogleGenAiChatModel build()