Class GoogleGenAiStreamingChatModel.Builder

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

public static class GoogleGenAiStreamingChatModel.Builder extends Object
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • client

      public GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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

      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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.Builder modelName(String modelName)
      Sets the model to use for streaming chat completions.

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

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

      Sets the HTTP request timeout for calls to the Google GenAI API.
      Parameters:
      timeout - the request timeout
      Returns:
      this
    • temperature

      public GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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
    • safetySettings

      public GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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

      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
    • executor

      Executor used to drive the blocking ResponseStream iteration off the calling thread. If not set, a shared default executor from DefaultExecutorProvider is used.

      Strongly recommended: supply an executor managed by your application (Spring/Quarkus task executor, virtual-thread executor, bounded pool, etc.). The default executor is unbounded and not tied to any application lifecycle, so it offers no back-pressure or graceful shutdown.

    • defaultRequestParameters

      public GoogleGenAiStreamingChatModel.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
    • cachedContent

      public GoogleGenAiStreamingChatModel.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
    • vertexSearchDatastore

      public GoogleGenAiStreamingChatModel.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

      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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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
    • logRequests

      public GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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 GoogleGenAiStreamingChatModel.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