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

content-types.mddocs/

Content Types

Multi-modal content structures supporting text, binary data (images), file references, function calls, and function responses. Content is organized into parts with role-based messaging compatible with Gemini's API format.

Capabilities

Content

The primary container for multi-modal messages in chat conversations. Each Content instance has a role (user, model, system) and consists of one or more parts.

/**
 * Represents content with a role and multiple parts.
 *
 * @param role The message role: "user", "model", or "system"
 * @param parts List of content parts (text, images, function calls, etc.)
 */
public record Content(String role, List<Part> parts) {
    /**
     * Creates content with a single part.
     *
     * @param part The content part
     * @return Content with the specified part
     */
    public static Content ofPart(Part part);
}

Content Part

Individual components within a Content message. A part can be text, an inline image, a file reference, a function call, or a function response.

/**
 * Individual part of content supporting multiple types.
 *
 * @param text Plain text content
 * @param functionCall Function call from the model
 * @param functionResponse Function response from the application
 * @param fileData Reference to a file (e.g., in Google Cloud Storage)
 * @param inlineData Inline binary data (e.g., base64-encoded image)
 * @param thoughtSignature Signature for thinking/reasoning parts
 */
public record Part(
    String text,
    FunctionCall functionCall,
    FunctionResponse functionResponse,
    FileData fileData,
    Blob inlineData,
    String thoughtSignature
) {
    /**
     * Creates a text part.
     *
     * @param text The text content
     * @return Part containing the text
     */
    public static Part ofText(String text);

    /**
     * Creates a function call part.
     *
     * @param functionCall The function call
     * @param thoughtSignature Optional signature for reasoning
     * @return Part containing the function call
     */
    public static Part ofFunctionCall(FunctionCall functionCall, String thoughtSignature);

    /**
     * Creates a function response part.
     *
     * @param functionResponse The function response
     * @return Part containing the function response
     */
    public static Part ofFunctionResponse(FunctionResponse functionResponse);

    /**
     * Creates a file data part.
     *
     * @param fileData The file reference
     * @return Part containing the file reference
     */
    public static Part ofFileData(FileData fileData);

    /**
     * Creates an inline data part.
     *
     * @param inlineData The binary data
     * @return Part containing the inline data
     */
    public static Part ofInlineData(Blob inlineData);
}

Blob

Represents inline binary data with MIME type information, typically used for images embedded directly in requests.

/**
 * Binary data with MIME type (e.g., base64-encoded images).
 *
 * @param mimeType The MIME type (e.g., "image/png", "image/jpeg")
 * @param data Base64-encoded binary data
 */
public record Blob(String mimeType, String data);

FileData

Represents a reference to a file stored externally, such as in Google Cloud Storage or Gemini's file API.

/**
 * Reference to an external file with MIME type.
 *
 * @param mimeType The MIME type (e.g., "image/png", "application/pdf")
 * @param fileUri The URI to the file (e.g., "gs://bucket/file.png")
 */
public record FileData(String mimeType, String fileUri);

Usage Examples

Text-Only Content

// Create a simple text message from the user
Content userMessage = new Content(
    "user",
    List.of(Content.Part.ofText("What is the capital of France?"))
);

// Using the ofPart convenience method
Content modelResponse = Content.ofPart(
    Content.Part.ofText("The capital of France is Paris.")
);

Multi-Modal Content with Image (Inline)

// Load and encode an image
byte[] imageBytes = Files.readAllBytes(Path.of("diagram.png"));
String base64Image = Base64.getEncoder().encodeToString(imageBytes);

// Create content with text and inline image
Content multiModalMessage = new Content(
    "user",
    List.of(
        Content.Part.ofText("What does this diagram show?"),
        Content.Part.ofInlineData(new Blob("image/png", base64Image))
    )
);

Multi-Modal Content with Image (File Reference)

// Reference a file uploaded to Gemini or stored in GCS
FileData imageFile = new FileData(
    "image/jpeg",
    "gs://my-bucket/photos/landscape.jpg"
);

Content fileMessage = new Content(
    "user",
    List.of(
        Content.Part.ofText("Describe this landscape photo."),
        Content.Part.ofFileData(imageFile)
    )
);

Function Call Content

// Model's function call response
FunctionCall call = new FunctionCall(
    "get_weather",
    Map.of(
        "location", "New York",
        "unit", "celsius"
    )
);

Content functionCallMessage = Content.ofPart(
    Content.Part.ofFunctionCall(call, null)
);

Function Response Content

// Application's function execution result
FunctionResponse.Response response = new FunctionResponse.Response(
    "get_weather",
    Map.of(
        "temperature", 22,
        "condition", "sunny",
        "humidity", 65
    )
);

FunctionResponse functionResponse = new FunctionResponse("get_weather", response);

Content functionResponseMessage = Content.ofPart(
    Content.Part.ofFunctionResponse(functionResponse)
);

Complex Multi-Part Content

// Combine multiple content types in a single message
Content complexMessage = new Content(
    "user",
    List.of(
        Content.Part.ofText("Here are the images from our meeting:"),
        Content.Part.ofInlineData(new Blob("image/png", base64Image1)),
        Content.Part.ofInlineData(new Blob("image/png", base64Image2)),
        Content.Part.ofText("Please analyze both images and compare them.")
    )
);

Building a Complete Conversation

List<Content> conversation = new ArrayList<>();

// User's initial message
conversation.add(new Content(
    "user",
    List.of(Content.Part.ofText("I need help analyzing this chart."))
));

// User's follow-up with image
byte[] chartBytes = Files.readAllBytes(Path.of("chart.png"));
String base64Chart = Base64.getEncoder().encodeToString(chartBytes);
conversation.add(new Content(
    "user",
    List.of(Content.Part.ofInlineData(new Blob("image/png", base64Chart)))
));

// Model's response
conversation.add(Content.ofPart(
    Content.Part.ofText("This chart shows quarterly revenue trends...")
));

// Use in a request
GenerateContentRequest request = new GenerateContentRequest(
    conversation,
    null,  // systemInstruction
    null,  // tools
    null   // generationConfig
);

Content Roles

  • "user": Messages from the end user or application
  • "model": Messages generated by the AI model
  • "system": System-level instructions (typically in SystemInstruction, not Content)

Supported MIME Types

Images

  • image/png
  • image/jpeg
  • image/webp
  • image/heic
  • image/heif

Documents

  • application/pdf
  • text/plain
  • text/html

Audio/Video

  • audio/wav
  • audio/mp3
  • video/mp4
  • video/mpeg

(Note: Specific model support varies. Check Gemini model capabilities for supported media types.)

Best Practices

  1. Use FileData for Large Files: For files larger than a few MB, upload to GCS or Gemini's file API and use FileData instead of inline Blob
  2. Base64 Encoding: Always Base64-encode binary data for Blob
  3. MIME Type Accuracy: Provide accurate MIME types for optimal processing
  4. Part Ordering: Order parts logically (typically text description before or after related media)
  5. Single Purpose Parts: Each Part should contain only one type of content (text XOR functionCall XOR inlineData, etc.)

Integration Points

Content types integrate with:

  • GenerateContentRequest: Used for chat message history
  • ContentMapper: Converts LangChain4j messages to Content
  • Function Calling: Function calls and responses are Part subtypes
  • Multi-modal Models: Enable vision, audio, and document understanding capabilities

Install with Tessl CLI

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

docs

chat-models.md

configuration.md

content-types.md

embedding-models.md

function-calling.md

index.md

requests-responses.md

utilities.md

tile.json