CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-quarkiverse-langchain4j--quarkus-langchain4j-gemini-common

Common shared infrastructure for integrating Google Gemini AI models with Quarkus applications through the LangChain4j framework, providing base chat model functionality, schema mapping, and embedding model support.

Overview
Eval results
Files

configuration.mddocs/

Configuration

Configuration classes for controlling content generation behavior, including temperature, token limits, structured output schemas, thinking/reasoning budgets, and JSON schema definitions.

Capabilities

Generation Config

Controls all aspects of content generation, including sampling parameters, output constraints, response format, and thinking configuration.

/**
 * Configuration for content generation parameters.
 * Use the builder pattern to construct instances.
 */
public class GenerationConfig {
    /**
     * Creates a new builder for GenerationConfig.
     *
     * @return Builder instance
     */
    public static Builder builder();

    /**
     * Gets the temperature parameter.
     *
     * @return Temperature value (0.0-1.0), or null if not set
     */
    public Double getTemperature();

    /**
     * Gets the maximum output tokens limit.
     *
     * @return Maximum tokens, or null if not set
     */
    public Integer getMaxOutputTokens();

    /**
     * Gets the top-K sampling parameter.
     *
     * @return Top-K value, or null if not set
     */
    public Integer getTopK();

    /**
     * Gets the top-P (nucleus) sampling parameter.
     *
     * @return Top-P value (0.0-1.0), or null if not set
     */
    public Double getTopP();

    /**
     * Gets the response MIME type for structured output.
     *
     * @return MIME type (e.g., "application/json"), or null if not set
     */
    public String getResponseMimeType();

    /**
     * Gets the response schema for structured output.
     *
     * @return Schema object, or null if not set
     */
    public Schema getResponseSchema();

    /**
     * Gets the raw JSON schema for structured output.
     *
     * @return Map representation of JSON schema, or null if not set
     */
    public Map<String, Object> getResponseJsonSchema();

    /**
     * Gets the stop sequences.
     *
     * @return List of stop sequences, or null if not set
     */
    public List<String> getStopSequences();

    /**
     * Gets the thinking configuration.
     *
     * @return ThinkingConfig, or null if not set
     */
    public ThinkingConfig getThinkingConfig();

    /**
     * Builder for constructing GenerationConfig instances.
     */
    public static final class Builder {
        /**
         * Sets the temperature parameter.
         *
         * @param temperature Randomness control (0.0 = deterministic, 1.0 = maximum randomness)
         * @return This builder
         */
        public Builder temperature(Double temperature);

        /**
         * Sets the maximum output tokens.
         *
         * @param maxOutputTokens Maximum number of tokens in the response
         * @return This builder
         */
        public Builder maxOutputTokens(Integer maxOutputTokens);

        /**
         * Sets the top-K parameter.
         *
         * @param topK Number of highest probability tokens to consider
         * @return This builder
         */
        public Builder topK(Integer topK);

        /**
         * Sets the top-P parameter.
         *
         * @param topP Nucleus sampling threshold (0.0-1.0)
         * @return This builder
         */
        public Builder topP(Double topP);

        /**
         * Sets the response MIME type.
         *
         * @param responseMimeType MIME type (e.g., "application/json")
         * @return This builder
         */
        public Builder responseMimeType(String responseMimeType);

        /**
         * Sets the response schema using Schema object.
         *
         * @param schema Schema object defining the response structure
         * @return This builder
         */
        public Builder responseSchema(Schema schema);

        /**
         * Sets the response schema using raw JSON schema map.
         *
         * @param responseJsonSchema Map representation of JSON schema
         * @return This builder
         */
        public Builder responseJsonSchema(Map<String, Object> responseJsonSchema);

        /**
         * Sets the stop sequences.
         *
         * @param stopSequences List of strings that stop generation
         * @return This builder
         */
        public Builder stopSequences(List<String> stopSequences);

        /**
         * Sets the thinking configuration.
         *
         * @param thinkingConfig Configuration for reasoning capabilities
         * @return This builder
         */
        public Builder thinkingConfig(ThinkingConfig thinkingConfig);

        /**
         * Builds the GenerationConfig instance.
         *
         * @return Configured GenerationConfig
         */
        public GenerationConfig build();
    }
}

Thinking Config

Configuration for Gemini's thinking and reasoning capabilities, available in Gemini 2.0+ models.

/**
 * Configuration for thinking and reasoning capabilities.
 */
public class ThinkingConfig {
    /**
     * Creates a thinking configuration.
     *
     * @param thinkingBudget Token budget allocated for internal reasoning
     * @param includeThoughts Whether to include reasoning process in the response
     */
    public ThinkingConfig(Long thinkingBudget, Boolean includeThoughts);

    /**
     * Gets the thinking budget.
     *
     * @return Token budget for reasoning, or null if not set
     */
    public Long getThinkingBudget();

    /**
     * Gets whether thoughts are included.
     *
     * @return true if thoughts should be included, false otherwise, or null if not set
     */
    public Boolean getIncludeThoughts();
}

Schema

Represents a JSON schema for defining structured output formats. Schemas can be nested and support all standard JSON Schema types.

/**
 * JSON schema for structured data validation and generation.
 * Use the builder pattern to construct complex schemas.
 */
public class Schema {
    /**
     * Creates a new builder for Schema.
     *
     * @return Builder instance
     */
    public static Builder builder();

    /**
     * Gets the schema type.
     *
     * @return Type enum value, or null if not set
     */
    public Type getType();

    /**
     * Gets the format specifier.
     *
     * @return Format string (e.g., "date-time"), or null if not set
     */
    public String getFormat();

    /**
     * Gets the description.
     *
     * @return Human-readable description, or null if not set
     */
    public String getDescription();

    /**
     * Gets whether the field is nullable.
     *
     * @return true if nullable, false otherwise, or null if not set
     */
    public Boolean getNullable();

    /**
     * Gets the enumeration values.
     *
     * @return List of allowed values, or null if not set
     */
    public List<String> getEnumeration();

    /**
     * Gets the maximum items for arrays.
     *
     * @return Maximum items as string, or null if not set
     */
    public String getMaxItems();

    /**
     * Gets the object properties.
     *
     * @return Map of property names to their schemas, or null if not set
     */
    public Map<String, Schema> getProperties();

    /**
     * Gets the required property names.
     *
     * @return List of required property names, or null if not set
     */
    public List<String> getRequired();

    /**
     * Gets the schema for array items.
     *
     * @return Schema for items, or null if not set
     */
    public Schema getItems();

    /**
     * Checks if this is effectively an empty object schema.
     *
     * @return true if empty object, false otherwise
     */
    @JsonIgnore
    public boolean isEffectiveEmptyObject();

    /**
     * Builder for constructing Schema instances.
     */
    public static class Builder {
        /**
         * Sets the schema type.
         *
         * @param type Type enum (STRING, NUMBER, INTEGER, BOOLEAN, ARRAY, OBJECT)
         * @return This builder
         */
        public Builder type(Type type);

        /**
         * Sets the format specifier.
         *
         * @param format Format string for validation hints
         * @return This builder
         */
        public Builder format(String format);

        /**
         * Sets the description.
         *
         * @param description Human-readable description
         * @return This builder
         */
        public Builder description(String description);

        /**
         * Sets whether the field is nullable.
         *
         * @param nullable true to allow null values
         * @return This builder
         */
        public Builder nullable(Boolean nullable);

        /**
         * Sets the enumeration values.
         *
         * @param enumeration List of allowed values
         * @return This builder
         */
        public Builder enumeration(List<String> enumeration);

        /**
         * Sets the maximum items for arrays.
         *
         * @param maxItems Maximum number of items as string
         * @return This builder
         */
        public Builder maxItems(String maxItems);

        /**
         * Sets the object properties.
         *
         * @param properties Map of property names to schemas
         * @return This builder
         */
        public Builder properties(Map<String, Schema> properties);

        /**
         * Sets the required property names.
         *
         * @param required List of required property names
         * @return This builder
         */
        public Builder required(List<String> required);

        /**
         * Sets the schema for array items.
         *
         * @param items Schema for array items
         * @return This builder
         */
        public Builder items(Schema items);

        /**
         * Builds the Schema instance.
         *
         * @return Configured Schema
         */
        public Schema build();
    }
}

Type Enum

Enumeration of JSON schema data types.

/**
 * JSON schema data types.
 */
public enum Type {
    STRING,
    NUMBER,
    INTEGER,
    BOOLEAN,
    ARRAY,
    OBJECT;

    /**
     * Returns lowercase string representation.
     *
     * @return Type name in lowercase
     */
    @Override
    public String toString();
}

Usage Examples

Basic Generation Config

// Simple configuration with temperature and token limit
GenerationConfig config = GenerationConfig.builder()
    .temperature(0.7)
    .maxOutputTokens(1024)
    .build();

GenerateContentRequest request = new GenerateContentRequest(
    contents,
    null,
    null,
    config
);

Advanced Sampling Configuration

// Fine-tuned sampling parameters
GenerationConfig config = GenerationConfig.builder()
    .temperature(0.8)        // Higher randomness
    .topK(40)                // Consider top 40 tokens
    .topP(0.95)              // Nucleus sampling at 95%
    .maxOutputTokens(2048)
    .stopSequences(List.of("\n\n", "END"))
    .build();

Structured JSON Output

// Define the expected response structure
Schema personSchema = Schema.builder()
    .type(Type.OBJECT)
    .properties(Map.of(
        "name", Schema.builder()
            .type(Type.STRING)
            .description("Person's full name")
            .build(),
        "age", Schema.builder()
            .type(Type.INTEGER)
            .description("Person's age in years")
            .build(),
        "email", Schema.builder()
            .type(Type.STRING)
            .format("email")
            .description("Email address")
            .build()
    ))
    .required(List.of("name", "age"))
    .build();

GenerationConfig config = GenerationConfig.builder()
    .responseMimeType("application/json")
    .responseSchema(personSchema)
    .build();

// Response will be valid JSON matching the schema

Complex Nested Schema

// Array of objects schema
Schema itemSchema = Schema.builder()
    .type(Type.OBJECT)
    .properties(Map.of(
        "product", Schema.builder()
            .type(Type.STRING)
            .build(),
        "quantity", Schema.builder()
            .type(Type.INTEGER)
            .build(),
        "price", Schema.builder()
            .type(Type.NUMBER)
            .build()
    ))
    .required(List.of("product", "quantity", "price"))
    .build();

Schema orderSchema = Schema.builder()
    .type(Type.OBJECT)
    .properties(Map.of(
        "orderId", Schema.builder()
            .type(Type.STRING)
            .build(),
        "items", Schema.builder()
            .type(Type.ARRAY)
            .items(itemSchema)
            .build(),
        "total", Schema.builder()
            .type(Type.NUMBER)
            .build()
    ))
    .required(List.of("orderId", "items", "total"))
    .build();

GenerationConfig config = GenerationConfig.builder()
    .responseMimeType("application/json")
    .responseSchema(orderSchema)
    .build();

Enum Constraints

// Schema with enum values
Schema statusSchema = Schema.builder()
    .type(Type.OBJECT)
    .properties(Map.of(
        "status", Schema.builder()
            .type(Type.STRING)
            .enumeration(List.of("pending", "approved", "rejected"))
            .description("Application status")
            .build(),
        "priority", Schema.builder()
            .type(Type.STRING)
            .enumeration(List.of("low", "medium", "high", "urgent"))
            .build()
    ))
    .required(List.of("status"))
    .build();

GenerationConfig config = GenerationConfig.builder()
    .responseMimeType("application/json")
    .responseSchema(statusSchema)
    .build();

Thinking Configuration

// Enable reasoning capabilities (Gemini 2.0+)
ThinkingConfig thinkingConfig = new ThinkingConfig(
    2000L,    // thinkingBudget: 2000 tokens for reasoning
    true      // includeThoughts: include reasoning in response
);

GenerationConfig config = GenerationConfig.builder()
    .temperature(0.7)
    .maxOutputTokens(4096)
    .thinkingConfig(thinkingConfig)
    .build();

// Response will include the model's reasoning process
GenerateContentRequest request = new GenerateContentRequest(
    List.of(Content.ofPart(
        Content.Part.ofText("Solve this complex math problem: ...")
    )),
    null,
    null,
    config
);

GenerateContentResponse response = chatModel.generateContext(request);

// Extract the reasoning
String thoughts = GenerateContentResponseHandler.getThoughts(response);
String answer = GenerateContentResponseHandler.getText(response);

System.out.println("Model's reasoning:\n" + thoughts);
System.out.println("\nFinal answer:\n" + answer);

Raw JSON Schema

// Using raw JSON schema map
Map<String, Object> rawSchema = Map.of(
    "type", "object",
    "properties", Map.of(
        "title", Map.of("type", "string"),
        "summary", Map.of("type", "string"),
        "tags", Map.of(
            "type", "array",
            "items", Map.of("type", "string")
        )
    ),
    "required", List.of("title", "summary")
);

GenerationConfig config = GenerationConfig.builder()
    .responseMimeType("application/json")
    .responseJsonSchema(rawSchema)
    .build();

Combining Multiple Options

// Comprehensive configuration
GenerationConfig config = GenerationConfig.builder()
    .temperature(0.9)
    .maxOutputTokens(3000)
    .topK(50)
    .topP(0.95)
    .stopSequences(List.of("---END---"))
    .responseMimeType("application/json")
    .responseSchema(mySchema)
    .thinkingConfig(new ThinkingConfig(1500L, true))
    .build();

Configuration Guidelines

Temperature

  • 0.0: Deterministic, consistent outputs (best for structured tasks)
  • 0.3-0.5: Slightly creative (good for technical content)
  • 0.7-0.9: Creative (good for stories, brainstorming)
  • 1.0: Maximum randomness (experimental)

Token Limits

  • Consider your use case: summaries need fewer tokens, articles need more
  • Leave buffer for context: if max model limit is 8192, use ~6000-7000 for output
  • Monitor usage metadata to optimize limits

Top-K and Top-P

  • Top-K: Limits vocabulary size. Lower = more focused, higher = more diverse
  • Top-P: Limits cumulative probability. 0.9-0.95 is a good balance
  • Can use both together or independently
  • Set to null to disable

Structured Output

  • Always set responseMimeType to "application/json" when using schemas
  • Make schemas specific: clear property names and descriptions
  • Mark required fields appropriately
  • Test schemas with simple examples first

Thinking Configuration

  • Available only in Gemini 2.0+ models
  • thinkingBudget: Higher values allow more complex reasoning
  • includeThoughts: Set true to debug reasoning, false for production
  • Increases token usage significantly

Stop Sequences

  • Use to control output boundaries
  • Maximum of 5 stop sequences
  • Useful for structured formats and templates

Integration with Schema Mapper

The SchemaMapper utility converts LangChain4j JsonSchema to Gemini Schema:

// Convert LangChain4j schema
JsonSchema langchain4jSchema = JsonSchema.from(...);
Schema geminiSchema = SchemaMapper.fromJsonSchemaToSchema(langchain4jSchema);

GenerationConfig config = GenerationConfig.builder()
    .responseSchema(geminiSchema)
    .responseMimeType("application/json")
    .build();

Best Practices

  1. Start Simple: Begin with basic temperature and token settings, add complexity as needed
  2. Test Schemas: Validate JSON schemas with small examples before production use
  3. Monitor Usage: Track token consumption, especially with thinking enabled
  4. Schema Descriptions: Provide clear descriptions in schemas to guide the model
  5. Required Fields: Be conservative with required fields; make optional when possible
  6. Error Handling: Handle cases where model doesn't follow schema perfectly
  7. Version Awareness: Check model version for feature support (e.g., thinking in 2.0+)

Install with Tessl CLI

npx tessl i tessl/maven-io-quarkiverse-langchain4j--quarkus-langchain4j-gemini-common@1.7.0

docs

chat-models.md

configuration.md

content-types.md

embedding-models.md

function-calling.md

index.md

requests-responses.md

utilities.md

tile.json