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 shared by the endpoints. Each endpoint starts from a copy of it, and the options of the endpoint replace its values. | LangChain4jEmbeddingsConfiguration | ||
Autowired The LangChain4j EmbeddingModel that computes the embeddings, such as an OpenAiEmbeddingModel or OllamaEmbeddingModel bean. Autowired when the registry has exactly one EmbeddingModel. When it is not set, the model is created from provider or customProvider; with neither, every message fails. | 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. Ollama has no default and needs it; OpenAI uses https://api.openai.com/v1 when it is not set. | String | ||
The name of the embedding model at the provider (nomic-embed-text, text-embedding-3-small, …), when the model is created from the provider. It is set under the name that the builder of the provider uses for it, such as modelId on Hugging Face. Ollama has no default and needs it. | String | ||
The LangChain4j provider of the embedding model, to create the model from the options here (modelName, baseUrl, apiKey, timeout, and provider-specific model.* properties) instead of an EmbeddingModel bean. The LangChain4j module of the provider (dev.langchain4j:langchain4j-ollama, …) must be on the classpath; the Camel CLI downloads it. anthropic has no embedding model and fails. Endpoints with the same model options share one model. Ignored when embeddingModel is set, also when it is autowired. For a provider not listed, set customProvider instead. Enum values:
| String | ||
The sampling temperature, which embedding models do not use: the builders of the embedding models of OpenAI, Ollama, Azure OpenAI, Gemini and Hugging Face have no temperature, so setting it fails when the endpoint is created. It only works with a model class whose builder has a temperature. | Double | ||
The timeout of the HTTP requests to the provider (30s, 2m), when the model is created from the provider. OpenAI and Ollama use it as both the connect and the read timeout; when it is not set, they wait 15 seconds to connect and 60 seconds for the response. | Duration | ||
The fully qualified class name of the LangChain4j embedding model class of a provider that is not listed in provider (dev.langchain4j.model.jlama.JlamaEmbeddingModel), created from the options here through its static builder() as a listed provider is. The class must implement EmbeddingModel. Set either provider or customProvider; setting both fails. | String | ||
Provider-specific properties of the embedding model, set on the model’s builder as they are (model.dimensions=512 to shorten the vectors of an OpenAI text-embedding-3 model, model.maxRetries=5), when the model is created from the provider. A property that the builder does not have fails when the endpoint is created. 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 (set as accessToken on Hugging Face and gitHubToken on GitHub Models). Setting it for a provider that has none, such as Ollama, fails. | 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 LangChain4j EmbeddingModel that computes the embeddings, such as an OpenAiEmbeddingModel or OllamaEmbeddingModel bean. Autowired when the registry has exactly one EmbeddingModel. When it is not set, the model is created from provider or customProvider; with neither, every message fails. | 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. Ollama has no default and needs it; OpenAI uses https://api.openai.com/v1 when it is not set. | String | ||
The name of the embedding model at the provider (nomic-embed-text, text-embedding-3-small, …), when the model is created from the provider. It is set under the name that the builder of the provider uses for it, such as modelId on Hugging Face. Ollama has no default and needs it. | String | ||
The LangChain4j provider of the embedding model, to create the model from the options here (modelName, baseUrl, apiKey, timeout, and provider-specific model.* properties) instead of an EmbeddingModel bean. The LangChain4j module of the provider (dev.langchain4j:langchain4j-ollama, …) must be on the classpath; the Camel CLI downloads it. anthropic has no embedding model and fails. Endpoints with the same model options share one model. Ignored when embeddingModel is set, also when it is autowired. For a provider not listed, set customProvider instead. Enum values:
| String | ||
The sampling temperature, which embedding models do not use: the builders of the embedding models of OpenAI, Ollama, Azure OpenAI, Gemini and Hugging Face have no temperature, so setting it fails when the endpoint is created. It only works with a model class whose builder has a temperature. | Double | ||
The timeout of the HTTP requests to the provider (30s, 2m), when the model is created from the provider. OpenAI and Ollama use it as both the connect and the read timeout; when it is not set, they wait 15 seconds to connect and 60 seconds for the response. | Duration | ||
The fully qualified class name of the LangChain4j embedding model class of a provider that is not listed in provider (dev.langchain4j.model.jlama.JlamaEmbeddingModel), created from the options here through its static builder() as a listed provider is. The class must implement EmbeddingModel. Set either provider or customProvider; setting both fails. | String | ||
Provider-specific properties of the embedding model, set on the model’s builder as they are (model.dimensions=512 to shorten the vectors of an OpenAI text-embedding-3 model, model.maxRetries=5), when the model is created from the provider. A property that the builder does not have fails when the endpoint is created. 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 (set as accessToken on Hugging Face and gitHubToken on GitHub Models). Setting it for a provider that has none, such as Ollama, fails. | 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: | Deprecated The finish reason, which an embedding call does not have. The producer removes this header and does not set it. Deprecation note: The producer no longer sets this header, as the EmbeddingResponse of LangChain4j has no finish reason. Enum values:
| FinishReason | |
CamelLangChain4jEmbeddingsInputTokenCount (producer) Constant: | The number of input tokens of the embedded text, or of all the texts of a List body, as the provider reports it. Not set when the response has no token usage. | int | |
CamelLangChain4jEmbeddingsOutputTokenCount (producer) Constant: | The number of output tokens, as the provider reports it. Embedding models such as OpenAI and Ollama report none, and the header is then null. Not set when the response has no token usage. | int | |
CamelLangChain4jEmbeddingsTotalTokenCount (producer) Constant: | The number of input and output tokens together, as the provider reports it. Not set when the response has no token usage. | int | |
CamelLangChain4jEmbeddingsRequestModel (producer) Constant: | The name of the model that the request went to, read from the default request parameters or the modelName() of the EmbeddingModel, or unknown when Camel cannot read it. | String | |
CamelLangChain4jEmbeddingsResponseModel (producer) Constant: | The name of the model that computed the embeddings, from the EmbeddingResponse of LangChain4j: the model that the provider reports, or the configured model name. Not set when the response has none. | String | |
CamelLangChain4jEmbeddingsEmbedding (producer) Constant: | The Embedding of the text, when the body is a single text. Its vector is as the EmbeddingModel returned it, with the dimension of the model; Camel does not normalize it. The langchain4j-embeddingstore component reads it. Not set for a List body. | Embedding | |
CamelLangChain4jEmbeddingsEmbeddings (producer) Constant: | The Embeddings of a List body, one per element, in the order of the elements and of the CamelLangChain4jEmbeddingsTextSegments header. Not set for a single text. | List | |
CamelLangChain4jEmbeddingsVector (producer) Constant: | The vector of the Embedding of the text as a float array, when the body is a single text: one value per dimension of the model, as the model returned it (Camel does not normalize it). The embeddings data types of the vector database components, such as qdrant:embeddings, read it. Not set for a List body. | float[] | |
CamelLangChain4jEmbeddingsTextSegment (producer) Constant: | The TextSegment that was embedded, when the body is a single text: the body itself when it is a TextSegment, with its metadata, otherwise a TextSegment with the text of the body and no metadata. Not set for a List body. | TextSegment | |
CamelLangChain4jEmbeddingsTextSegments (producer) Constant: | The TextSegments that were embedded for a List body, one per element, in the order of the elements and of the CamelLangChain4jEmbeddingsEmbeddings header. Not set for a single text. | List |
Message Body
The message body at each side of the LangChain4j Embeddings component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The text to embed: a String, bytes, a stream, a file or another value Camel converts to its text, such as a char[], or a LangChain4j TextSegment; or a non-empty List of them to embed in one batch. A body or List element Camel reads only as its toString(), such as a Map or a POJO, fails, as does no body: marshal it first. |
Producer output |
| The body is kept. The vector goes in the CamelLangChain4jEmbeddingsVector header as a float array, with the Embedding and the TextSegment in the CamelLangChain4jEmbeddingsEmbedding and CamelLangChain4jEmbeddingsTextSegment headers; a List body gives Lists of them in the CamelLangChain4jEmbeddingsEmbeddings and CamelLangChain4jEmbeddingsTextSegments headers instead. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
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 embedding model class (any EmbeddingModel class with a builder(), such as dev.langchain4j.model.jlama.JlamaEmbeddingModel). 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 anthropic provider has no embedding model. The common options are modelName, baseUrl, apiKey, temperature and timeout (embedding models have no temperature), 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" The text to embed
The body is the text to embed: a String or a LangChain4j TextSegment. Bytes, a stream and a file (such as the body of a file consumer) are read as text, so a from("file:…") route needs no convertBodyTo(String.class). So is any other value Camel converts to its text, such as a char[], a ByteBuffer, a Netty ByteBuf, a number, or a type with its own type converter to String. A UUID, a URI, a URL, a java.time value or a Date is also embedded, as its toString() is its text. A non-empty List of them is embedded in one batch.
Any other body, or element of a List body, such as a Map or a POJO, fails the exchange with an InvalidPayloadException instead of being embedded as its toString(), since that is all Camel reads of it. The rule is the text itself: a value fails when the text Camel gives for it is no more than its toString(). Marshal such a body first, to JSON, say, or, when its toString() is the text to embed, convert it with convertBodyTo(String.class). No body fails too.
The body is kept. A single text sets the CamelLangChain4jEmbeddingsEmbedding, CamelLangChain4jEmbeddingsVector and CamelLangChain4jEmbeddingsTextSegment headers, and a List the CamelLangChain4jEmbeddingsEmbeddings and CamelLangChain4jEmbeddingsTextSegments headers. Each call removes the headers of an earlier call on the same exchange, so a single text after a batch is not stored as the old batch by a later embedding store ADD.
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
expression:
constant:
expression: UPSERT
- setHeader:
name: CamelQdrantPointId
expression:
constant:
expression: "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
expression:
constant:
expression: SIMILARITY_SEARCH
- setHeader:
name: CamelQdrantWithPayload
expression:
constant:
expression: "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
expression:
constant:
expression: 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
expression:
constant:
expression: 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.