Camel Components

LangChain4j Web Search

Since Camel 4.8

Only producer is supported

The LangChain4j Web Search component provides support for web searching using the LangChain4j Web Search Engines.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-langchain4j-web-search</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

langchain4j-web-search:searchId[?options]

Where searchId 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, *.yaml files, 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 Web Search component supports the following options which are listed below.

Name Description Default Type

additionalParams (producer)

More parameters for the search engine, as a Map of names to values, such as the parameters of the search API that no option covers. Which names are used depends on the engine, and the Tavily engine ignores them.

Map

configuration (producer)

The configuration that each endpoint starts from. Options set on the endpoint override it.

LangChain4jWebSearchConfiguration

geoLocation (producer)

The geographic location of the search results, such as a country code, in the format that the search engine expects. The Tavily engine ignores it.

String

language (producer)

The language of the search results, in the format that the search engine expects. The Tavily engine ignores it.

String

lazyStartProducer (producer)

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

maxResults (producer)

The number of results that the producer asks the search engine for. With 1 the body is a single result, otherwise a List. An engine can return fewer results and can have its own maximum.

1

Integer

resultType (producer)

What the producer returns for each result: CONTENT its content as a String, or its snippet when the result has no content (as some engines give none), SNIPPET its snippet, or LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT the dev.langchain4j.web.search.WebSearchOrganicResult. The body is a single value when the request asks for one result, otherwise a List, and null when there is no result.

Enum values:

  • LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT

  • CONTENT

  • SNIPPET

CONTENT

LangChain4jWebSearchResultType

safeSearch (producer)

Whether the search engine filters explicit content out of the results. If not set, LangChain4j uses true. The Tavily engine ignores it.

Boolean

startIndex (producer)

The index of the first search result to return, in the numbering that the search engine uses. The Tavily engine ignores it.

Integer

startPage (producer)

The page number of the search results to return. If not set, LangChain4j uses 1. The Tavily engine ignores it.

Integer

webSearchEngine (producer)

Autowired The dev.langchain4j.web.search.WebSearchEngine that runs the search, for example a TavilyWebSearchEngine or a GoogleCustomWebSearchEngine of the LangChain4j web search engine modules. It can be set on the endpoint or on the component. If neither sets it, the single instance in the registry is used, and the producer fails to start when there is none.

WebSearchEngine

autowiredEnabled (advanced)

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

webSearchRequest (advanced)

A dev.langchain4j.web.search.WebSearchRequest that the producer sends as it is. When set, the producer does not read the message body and does not use the other search options, and only a maxResults of 1 in this request gives a single result instead of a List.

WebSearchRequest

Endpoint Options

The LangChain4j Web Search endpoint is configured using URI syntax:

langchain4j-web-search:searchId

With the following path and query parameters:

Path Parameters

Name Description Default Type

searchId (producer)

Required A name that identifies the endpoint. The producer does not use it.

String

Query Parameters

Name Description Default Type

additionalParams (producer)

More parameters for the search engine, as a Map of names to values, such as the parameters of the search API that no option covers. Which names are used depends on the engine, and the Tavily engine ignores them.

Map

geoLocation (producer)

The geographic location of the search results, such as a country code, in the format that the search engine expects. The Tavily engine ignores it.

String

language (producer)

The language of the search results, in the format that the search engine expects. The Tavily engine ignores it.

String

maxResults (producer)

The number of results that the producer asks the search engine for. With 1 the body is a single result, otherwise a List. An engine can return fewer results and can have its own maximum.

1

Integer

resultType (producer)

What the producer returns for each result: CONTENT its content as a String, or its snippet when the result has no content (as some engines give none), SNIPPET its snippet, or LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT the dev.langchain4j.web.search.WebSearchOrganicResult. The body is a single value when the request asks for one result, otherwise a List, and null when there is no result.

Enum values:

  • LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT

  • CONTENT

  • SNIPPET

CONTENT

LangChain4jWebSearchResultType

safeSearch (producer)

Whether the search engine filters explicit content out of the results. If not set, LangChain4j uses true. The Tavily engine ignores it.

Boolean

startIndex (producer)

The index of the first search result to return, in the numbering that the search engine uses. The Tavily engine ignores it.

Integer

startPage (producer)

The page number of the search results to return. If not set, LangChain4j uses 1. The Tavily engine ignores it.

Integer

webSearchEngine (producer)

Autowired The dev.langchain4j.web.search.WebSearchEngine that runs the search, for example a TavilyWebSearchEngine or a GoogleCustomWebSearchEngine of the LangChain4j web search engine modules. It can be set on the endpoint or on the component. If neither sets it, the single instance in the registry is used, and the producer fails to start when there is none.

WebSearchEngine

lazyStartProducer (producer (advanced))

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

webSearchRequest (advanced)

A dev.langchain4j.web.search.WebSearchRequest that the producer sends as it is. When set, the producer does not read the message body and does not use the other search options, and only a maxResults of 1 in this request gives a single result instead of a List.

WebSearchRequest

Message Body

The message body at each side of the LangChain4j Web Search 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

any

The search terms as text; another body is converted to text, so a Map or a POJO is searched as its toString(), and no body fails. With webSearchRequest the body is not read.

Producer output

text or null

The content of the first result as text (its snippet with resultType SNIPPET, the WebSearchOrganicResult with LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT), or null when there is no result. A result without content gives its snippet. With maxResults above 1 it is a List of them, which can hold nulls.

  • With resultType=LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT: pojo or null

Not known when maxResults or webSearchRequest is set.

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

Using a specific Web Search Engine

The Camel LangChain4j web search component provides an abstraction for interacting with various types of Web Search Engines supported by LangChain4j.

To integrate with a specific Web Search Engine, users should follow the steps described below, which explain how to integrate with Tavily.

Add the dependency for LangChain4j Tavily Web Search Engine support :

<dependency>
      <groupId>dev.langchain4j</groupId>
      <artifactId>langchain4j-web-search-engine-tavily</artifactId>
    <version>x.x.x</version>
</dependency>

Initialize the Web Search Engine instance, and bind it to the Camel Registry:

Java-only: binding a Tavily Web Search Engine to the registry
@BindToRegistry("web-search-engine")
WebSearchEngine tavilyWebSearchEngine = TavilyWebSearchEngine.builder()
    .apiKey(tavilyApiKey)
    .includeRawContent(true)
    .build();

The web search engine is autowired when it is the only WebSearchEngine in the registry. Otherwise, it should be added as a configured parameter to the Camel route.

Example:
  • Java

  • XML

  • YAML

from("direct:web-search")
    .to("langchain4j-web-search:test?webSearchEngine=#web-search-engine");
<route>
  <from uri="direct:web-search"/>
  <to uri="langchain4j-web-search:test?webSearchEngine=#web-search-engine"/>
</route>
- route:
    from:
      uri: direct:web-search
      steps:
        - to:
            uri: langchain4j-web-search:test
            parameters:
              webSearchEngine: "#web-search-engine"

To switch to another Web Search Engine and its corresponding dependency, replace the langchain4j-web-search-engine-tavily dependency with the appropriate dependency for the desired web search engine. Update the initialization parameters accordingly in the code snippet provided above.

Customizing Web Search Results

By default, the maxResults property is set to 1. You can adjust this value to retrieve a list of results.

Retrieving a single result or a list of strings

When maxResults is set to 1, you can by default retrieve by default the content as a single string.

Java-only: retrieving a single search result as a string
String response = template.requestBody("langchain4j-web-search:test", "Who won the European Cup in 2024?", String.class);

When maxResults is greater than 1, you can retrieve a list of strings.

Java-only: retrieving multiple search results as a list of strings
List<String> responses = template.requestBody("langchain4j-web-search:test?maxResults=3", "Who won the European Cup in 2024?", List.class);

Retrieving different types of Results

You can get different types of Results.

With the default resultType = CONTENT, a result without content returns its snippet instead. Some engines give no content: Google Custom Search never does, and Tavily only with includeRawContent(true).

When resultType = SNIPPET, you will get a single or list (depending on maxResults value) of Strings containing the snippets.

When resultType = LANGCHAIN4J_WEB_SEARCH_ORGANIC_RESULT, you will get a single or list (depending on maxResults value) of Objects of type WebSearchOrganicResult containing all the response created under the hood by Langchain4j Web Search.

Advanced usage of WebSearchRequest

When defining a WebSearchRequest, the Camel LangChain4j web search component will default to this request, instead of creating one from the body and config parameters.

When using a WebSearchRequest, the body and the parameters of the search will be ignored. Use this parameter with caution.

A WebSearchRequest should be bound to the registry.

Java-only: binding a WebSearchRequest to the registry
@BindToRegistry("web-search-request")
WebSearchRequest request = WebSearchRequest.builder()
    .searchTerms("Who won the European Cup in 2024?")
    .maxResults(2)
    .build();

The request is not autowired: set it on each endpoint that should use it with the webSearchRequest option.

Example of route:
  • Java

  • XML

  • YAML

from("direct:web-search")
    .to("langchain4j-web-search:test?webSearchRequest=#web-search-request");
<route>
  <from uri="direct:web-search"/>
  <to uri="langchain4j-web-search:test?webSearchRequest=#web-search-request"/>
</route>
- route:
    from:
      uri: direct:web-search
      steps:
        - to:
            uri: langchain4j-web-search:test
            parameters:
              webSearchRequest: "#web-search-request"