gansheer opened a new pull request, #31038:
URL: https://github.com/apache/camel/pull/31038

   # Description
   
   Ref CAMEL-27544
   
   ## What this brings
   
   `CamelContentRetriever`, the retrieval half of a RAG setup: a LangChain4j 
`ContentRetriever` backed by a Camel endpoint (a route, a Kamelet or a 
component). An AI service can then retrieve content from any system Camel 
reaches (a REST API, a database, a search index, a mainframe), next to or 
instead of an embedding store. It is the read-side counterpart of 
`camel-langchain4j-ingest`, whose metadata it reads to cite ingested documents.
   
   It lives in `camel-langchain4j-core` (package 
`org.apache.camel.component.langchain4j.core.rag`), as a plain class rather 
than an endpoint: LangChain4j calls it, Camel does not. Support level: Preview.
   
   ```java
   CamelContentRetriever orders = new CamelContentRetriever(camelContext, 
"orders", "direct:orders")
           .setTimeout(Duration.ofSeconds(2))
           .setOnError(OnError.EMPTY)
           .setMaxResults(5);
   camelContext.addService(orders);
   
   RetrievalAugmentor augmentor = DefaultRetrievalAugmentor.builder()
           .queryRouter(new DefaultQueryRouter(orders, knowledgeBase))
           .contentInjector(DefaultContentInjector.builder()
                   .metadataKeysToInclude(List.of("camel_retrieval_source", 
"camel_retrieval_document_id"))
                   .build())
           .build();
   ```
   
   ## Technical details
   
   ```
   retrieve(query)
      │
      ├─ admitted? (concurrency + queueCapacity) ──no──> REJECTED, follows 
onError
      │
      ├─ InOut exchange on the retriever's own bounded pool, never the caller 
thread
      │    body: the query text
      │    properties: CamelLangChain4jRetrieverUserMessage, 
CamelLangChain4jRetrieverMemoryId,
      │                CamelLangChain4jRetrieverParams (read-only invocation 
parameters)
      │
      ├─ wait up to timeout (queue time included) ──late──> TIMEOUT, follows 
onError
      ├─ route exception ─────────────────────────────────> ROUTE_FAILURE, 
follows onError
      │
      └─ convert the body ──cannot──> CONVERSION, follows onError
           String / TextSegment / Content   as is
           EmbeddingMatch                   embedded item + score and embedding 
id
           Map                              "key: value" lines
           anything else                    Camel type converter
           Collection / array / Iterator / Stream: split, read up to maxResults 
/ maxChars
   ```
   
   - **Identity.** The query text and the original user message are untrusted. 
The only identity a route should use is the AI service invocation parameters, 
read in the route with e.g. 
`${exchangeProperty.CamelLangChain4jRetrieverParams[customerId]}`.
   - **Citation.** Every item gets `camel_retrieval_source`, and 
`camel_retrieval_document_id` from the `documentIdKey` entry or metadata key, 
the id set by the route, or the `camel_ingest_document_id` written by 
`camel-langchain4j-ingest`. Response headers prefixed with 
`langchain4j.metadata.` become item metadata, but cannot set the 
`camel_retrieval_*` or `camel_ingest_*` keys.
   - **Failure policy.** `onError=FAIL` (default) throws a 
`CamelExecutionException`; `EMPTY` logs a warning and returns no content, so a 
failing source degrades the answer instead of failing the AI call. A 
`VirtualMachineError` is always rethrown, and an interrupted caller keeps its 
interrupt.
   - **Bounds.** `timeout` 30s by default, `concurrency` 2, `queueCapacity` 64, 
`maxResults` and `maxChars` 0 (unlimited, as `maxDocumentSize` in ingest). The 
bounds are enforced by the retriever itself, so they hold with virtual threads 
(`camel.threads.virtual.enabled`), where the Camel pool would otherwise block 
instead of reject.
   - **Resources.** Reading stops at the limits without looking ahead, and a 
body holding a resource (a `Stream`, a `Closeable` iterator such as the 
`Scanner` of `tokenize`) is closed: after conversion, after a route failure, 
and for an answer that arrives after the timeout. A body that the route closes 
on completion (a camel-sql `StreamList`) gives no content; the doc says to 
materialize it in the route.
   - **Monitoring.** Retrieval and failure counters per reason (`REJECTED`, 
`STOPPED`, `TIMEOUT`, `ROUTE_FAILURE`, `CONVERSION`), also JMX attributes of 
the retriever's service MBean, so a source failing behind `OnError.EMPTY` stays 
visible.
   - **Lifecycle.** Registered with `CamelContext.addService(...)`, which 
starts and stops it; settings are frozen once started. On stop, queued calls 
follow `onError` and running ones are interrupted.
   
   Documentation: new page `langchain4j-content-retriever` under "others" (nav, 
catalog, `MojoHelper`).
   
   Tests: `CamelContentRetrieverTest`, `ContentConverterTest`, 
`QueryMetadataMapperTest`, `CamelContentRetrieverAugmentorTest` (end to end 
through a `DefaultRetrievalAugmentor`) and 
`CamelContentRetrieverVirtualThreadsTest`.
   
   Follow-up, separately: caller-scoped retrieval when the AI call is started 
from a route (`InvocationParameters` from `camel-langchain4j-service`, see 
#30391, and from `camel-langchain4j-agent`).
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [x] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
   
   Developed with the help of Claude Code (Claude Opus 5.5).
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to