LangChain4j Embeddings
Since Camel 4.5
Only producer is supported
The LangChain4j embeddings component provides support for compute embeddings using LangChain4j embeddings.
URI format
langchain4j-embeddings:embeddingId[?options]
Where embeddingId can be any string to uniquely identify the endpoint
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The LangChain4j Embeddings component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The configuration. | LangChain4jEmbeddingsConfiguration | ||
Autowired The EmbeddingModel engine to use. Either this or a provider is required. | EmbeddingModel | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
The URL of the provider’s API (http://localhost:11434 for a local Ollama), when the model is created from the provider. The provider’s default when not set. | String | ||
The name of the model at the provider (qwen2.5, gpt-4o-mini, …), when the model is created from the provider. | String | ||
The LangChain4j provider of the embedding model, to create the model from the options here (modelName, baseUrl, apiKey, temperature, timeout, and provider-specific model. properties) instead of a EmbeddingModel bean. The LangChain4j module of the provider (dev.langchain4j:langchain4j-ollama, …) must be on the classpath; Camel JBang downloads it. Ignored when a EmbeddingModel is configured. For a provider not listed, set customProvider instead. Enum values:
| String | ||
The sampling temperature of the model, when the model is created from the provider. | Double | ||
The request timeout of the model (30s, 2m), when the model is created from the provider. | Duration | ||
The fully qualified class name of the LangChain4j model class of a provider that is not listed in provider (dev.langchain4j.model.jlama.JlamaChatModel), created from the options here through its builder() as a listed provider is. Set either provider or customProvider. | String | ||
Provider-specific properties of the model, set on the model’s builder as they are (model.numPredict=512 for Ollama, model.maxTokens=1024 for OpenAI), when the model is created from the provider. This is a multi-value option with prefix: model. | Map | ||
The API key or access token of the provider, when the model is created from the provider. | String |
Endpoint Options
The LangChain4j Embeddings endpoint is configured using URI syntax:
langchain4j-embeddings:embeddingId
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Autowired The EmbeddingModel engine to use. Either this or a provider is required. | EmbeddingModel | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The URL of the provider’s API (http://localhost:11434 for a local Ollama), when the model is created from the provider. The provider’s default when not set. | String | ||
The name of the model at the provider (qwen2.5, gpt-4o-mini, …), when the model is created from the provider. | String | ||
The LangChain4j provider of the embedding model, to create the model from the options here (modelName, baseUrl, apiKey, temperature, timeout, and provider-specific model. properties) instead of a EmbeddingModel bean. The LangChain4j module of the provider (dev.langchain4j:langchain4j-ollama, …) must be on the classpath; Camel JBang downloads it. Ignored when a EmbeddingModel is configured. For a provider not listed, set customProvider instead. Enum values:
| String | ||
The sampling temperature of the model, when the model is created from the provider. | Double | ||
The request timeout of the model (30s, 2m), when the model is created from the provider. | Duration | ||
The fully qualified class name of the LangChain4j model class of a provider that is not listed in provider (dev.langchain4j.model.jlama.JlamaChatModel), created from the options here through its builder() as a listed provider is. Set either provider or customProvider. | String | ||
Provider-specific properties of the model, set on the model’s builder as they are (model.numPredict=512 for Ollama, model.maxTokens=1024 for OpenAI), when the model is created from the provider. This is a multi-value option with prefix: model. | Map | ||
The API key or access token of the provider, when the model is created from the provider. | String |
Message Headers
The LangChain4j Embeddings component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelLangChain4jEmbeddingsFinishReason (producer) Constant: | The Finish Reason. Enum values:
| FinishReason | |
CamelLangChain4jEmbeddingsInputTokenCount (producer) Constant: | The Input Token Count. | int | |
CamelLangChain4jEmbeddingsOutputTokenCount (producer) Constant: | The Output Token Count. | int | |
CamelLangChain4jEmbeddingsTotalTokenCount (producer) Constant: | The Total Token Count. | int | |
CamelLangChain4jEmbeddingsRequestModel (producer) Constant: | The request model name. | String | |
CamelLangChain4jEmbeddingsResponseModel (producer) Constant: | The response model name. | String | |
CamelLangChain4jEmbeddingsEmbedding (producer) Constant: | Embedding representation of a text. | Embedding | |
CamelLangChain4jEmbeddingsEmbeddings (producer) Constant: | List of embeddings from a batch embedAll operation. | List | |
CamelLangChain4jEmbeddingsVector (producer) Constant: | A dense vector embedding of a text. | float[] | |
CamelLangChain4jEmbeddingsTextSegment (producer) Constant: | A TextSegment representation of the vector embedding input text. | TextSegment | |
CamelLangChain4jEmbeddingsTextSegments (producer) Constant: | List of text segments from a batch embedAll operation. | List |
Usage
Configuring the model by provider
The embedding model can be declared by its provider and the options every provider has, without a bean of the provider’s model class (since Camel 4.23). In application.properties, for every endpoint of the component:
camel.component.langchain4j-embeddings.provider = ollama
camel.component.langchain4j-embeddings.model-name = nomic-embed-text
camel.component.langchain4j-embeddings.base-url = http://localhost:11434 Or on the endpoint, where model. prefixes a provider-specific option of the model’s builder:
- route:
from:
uri: direct:start
steps:
- to:
uri: langchain4j-embeddings:documents
parameters:
provider: openai
apiKey: "{{openai.api.key}}"
modelName: text-embedding-3-small
timeout: 30s The provider is one of ollama, openai, anthropic, azure-openai, mistral, gemini, vertex-ai, github, hugging-face, bedrock; a provider not in the list is set as customProvider, the fully qualified class name of its LangChain4j model class (any class with a builder(), such as dev.langchain4j.model.jlama.JlamaChatModel). The LangChain4j module of the provider (dev.langchain4j:langchain4j-ollama, dev.langchain4j:langchain4j-open-ai, …) must be on the classpath; Camel JBang downloads it for the listed providers. Ollama, LocalAI, vLLM, LM Studio and the other servers that speak the OpenAI API can also be used through provider: openai with their baseUrl, which needs no module of their own. The common options are modelName, baseUrl, apiKey, temperature and timeout, set under the name the provider’s builder uses for them (apiKey is the access token of Hugging Face, modelName the deployment name of Azure OpenAI); an option the provider does not have (an apiKey for Ollama), or a model. property its builder does not have, is an error naming what the builder accepts.
Endpoints with the same provider options share one model instance. The provider is ignored when a model bean is configured (embeddingModel).
Using Embedding Models
The Camel LangChain4j embeddings component provides support for generating embeddings using various embedding models supported by LangChain4j.
Integrating with specific Embedding Model
Using LangChain4j Spring Boot Starters (Recommended for Spring Boot)
When using Camel with Spring Boot, you can leverage LangChain4j’s Spring Boot starters for automatic configuration of embedding models.
Add the dependency for LangChain4j OpenAI Spring Boot starter:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
<version>1.10.0</version>
<!-- use the same version as your LangChain4j version -->
</dependency> Configure the OpenAI Embedding Model in application.properties or application.yml:
langchain4j.open-ai.embedding-model.api-key=${OPENAI_API_KEY}
langchain4j.open-ai.embedding-model.model-name=text-embedding-ada-002 langchain4j:
open-ai:
embedding-model:
api-key: ${OPENAI_API_KEY}
model-name: text-embedding-ada-002 The EmbeddingModel bean will be automatically configured and available in the Spring context. Use it in your Camel routes:
-
Java
-
YAML
-
XML
from("direct:embeddings")
.to("langchain4j-embeddings:test?embeddingModel=#embeddingModel"); - route:
from:
uri: direct:embeddings
steps:
- to:
uri: langchain4j-embeddings:test
parameters:
embeddingModel: "#embeddingModel" <route>
<from uri="direct:embeddings"/>
<to uri="langchain4j-embeddings:test?embeddingModel=#embeddingModel"/>
</route> | LangChain4j Spring Boot starters provide auto-configuration for various embedding model providers including:
For a complete list of available starters and their configuration options, refer to the LangChain4j Spring Boot Integration documentation. |
Manual Configuration (Alternative)
Alternatively, you can manually initialize the Embedding Model and add it to the Camel Registry:
Add the dependency for LangChain4j OpenAI support:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.10.0</version>
<!-- use the same version as your LangChain4j version -->
</dependency> Initialize the OpenAI Embedding Model:
EmbeddingModel embeddingModel = OpenAiEmbeddingModel.builder()
.apiKey(openApiKey)
.modelName("text-embedding-ada-002")
.build();
context.getRegistry().bind("myEmbeddingModel", embeddingModel); Use the model in the Camel LangChain4j Embeddings Producer:
-
Java
-
YAML
from("direct:embeddings")
.to("langchain4j-embeddings:test?embeddingModel=#myEmbeddingModel"); - route:
from:
uri: direct:embeddings
steps:
- to:
uri: langchain4j-embeddings:test
parameters:
embeddingModel: "#myEmbeddingModel" Integration with Vector Stores
The LangChain4j Embeddings component works seamlessly with vector databases for RAG (Retrieval-Augmented Generation) workflows.
Using with Qdrant
This example shows how to embed text and store it in Qdrant vector database:
-
Java
-
YAML
from("direct:store")
.to("langchain4j-embeddings:embed")
.setHeader(QdrantHeaders.ACTION).constant(QdrantAction.UPSERT)
.setHeader(QdrantHeaders.POINT_ID).constant(1)
.transformDataType(new DataType("qdrant:embeddings"))
.to("qdrant:myCollection"); - route:
from:
uri: direct:store
steps:
- to:
uri: langchain4j-embeddings:embed
- setHeader:
name: CamelQdrantAction
constant: UPSERT
- setHeader:
name: CamelQdrantPointId
constant: 1
- transformDataType:
toType: "qdrant:embeddings"
- to:
uri: qdrant:myCollection Similarity Search for RAG
Retrieve relevant content using similarity search:
-
Java
-
YAML
from("direct:search")
.to("langchain4j-embeddings:embed")
.transformDataType(new DataType("qdrant:embeddings"))
.setHeader(QdrantHeaders.ACTION, constant(QdrantAction.SIMILARITY_SEARCH))
.setHeader(QdrantHeaders.INCLUDE_PAYLOAD, constant(true))
.to("qdrant:myCollection")
.transformDataType(new DataType("qdrant:rag")); - route:
from:
uri: direct:search
steps:
- to:
uri: langchain4j-embeddings:embed
- transformDataType:
toType: "qdrant:embeddings"
- setHeader:
name: CamelQdrantAction
constant: SIMILARITY_SEARCH
- setHeader:
name: CamelQdrantWithPayload
constant: true
- to:
uri: qdrant:myCollection
- transformDataType:
toType: "qdrant:rag" Using with PGVector
This example shows how to embed text and store it in PostgreSQL with pgvector:
-
Java
-
YAML
from("direct:store")
.to("langchain4j-embeddings:embed")
.setHeader(PgVectorHeaders.ACTION).constant(PgVectorAction.UPSERT)
.transformDataType(new DataType("pgvector:embeddings"))
.to("pgvector:myCollection"); - route:
from:
uri: direct:store
steps:
- to:
uri: langchain4j-embeddings:embed
- setHeader:
name: CamelPgVectorAction
constant: UPSERT
- transformDataType:
toType: "pgvector:embeddings"
- to:
uri: pgvector:myCollection Similarity search with PGVector and RAG:
-
Java
-
YAML
from("direct:search")
.to("langchain4j-embeddings:embed")
.transformDataType(new DataType("pgvector:embeddings"))
.setHeader(PgVectorHeaders.ACTION, constant(PgVectorAction.SIMILARITY_SEARCH))
.to("pgvector:myCollection")
.transformDataType(new DataType("pgvector:rag")); - route:
from:
uri: direct:search
steps:
- to:
uri: langchain4j-embeddings:embed
- transformDataType:
toType: "pgvector:embeddings"
- setHeader:
name: CamelPgVectorAction
constant: SIMILARITY_SEARCH
- to:
uri: pgvector:myCollection
- transformDataType:
toType: "pgvector:rag" Using with LangChain4j Embedding Store Component
For a simpler integration, use the langchain4j-embeddingstore component which provides a unified interface:
-
Java
-
YAML
from("direct:store")
.to("langchain4j-embeddings:embed")
.to("langchain4j-embeddingstore:myStore?action=ADD");
from("direct:search")
.to("langchain4j-embeddings:embed")
.to("langchain4j-embeddingstore:myStore?action=SEARCH&maxResults=5&returnTextContent=true"); - route:
from:
uri: direct:store
steps:
- to:
uri: langchain4j-embeddings:embed
- to:
uri: langchain4j-embeddingstore:myStore
parameters:
action: ADD
- route:
from:
uri: direct:search
steps:
- to:
uri: langchain4j-embeddings:embed
- to:
uri: langchain4j-embeddingstore:myStore
parameters:
action: SEARCH
maxResults: 5
returnTextContent: true Structured error exchange properties
When a LangChain4j embeddings call fails, Camel sets structured metadata on the exchange before the model exception propagates. This works even when GenAI observability is disabled.
| Exchange property | Meaning |
|---|---|
| Coarse category derived from the LangChain4j exception: |
| Not populated for LangChain4j providers (OpenAI-only today) |
Use categories when a route should branch on failure type without matching every LangChain4j exception class. See LangChain4j Chat structured error properties and AI LLM integration guide for related detail.