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

   …n4j AI Service from a route
   
   ## What this does
   
   Adds `camel-langchain4j-service`, a producer-only component that invokes an 
AI Service the user
   built themselves with LangChain4j, from a route.
   
   ```java
   AiServices.builder(Assistant.class).chatModel(model).build();  // the user 
builds it
   ```
   
   ```java
   from("direct:ask")
       .to("langchain4j-service:myAssistant");                     // the route 
calls it
   ```
   
   The component owns nothing about the model. No `ChatModel`, no memory, no 
tools, no RAG. All of that stays in the user's hands, configured through 
LangChain4j's own builder. Camel resolves the bean from the registry, picks the 
method, binds the parameters from the exchange, invokes, and puts the return 
value in the body.
   
   ## Why
   
   `camel-langchain4j-agent` builds the AI Service for you, from endpoint 
options. That works when the use case fits the options it exposes. It does not 
when the user already has an AI Service, with their own memory provider, tools, 
guardrails or retrieval augmentor, and only wants to call it from a route.
   
   The ownership split between the two components is written up in 
[`design/langchain4j-service.adoc`](design/langchain4j-service.adoc), added by 
this PR.
   
   ## How parameters are bound
   
   A LangChain4j AI Service method carries annotations from 
`dev.langchain4j.service`:
   
   ```java
   String chat(@UserMessage String text, @V("tone") String tone, @MemoryId 
String memoryId);
   ```
   
   The component maps the body to `@UserMessage`, the 
`CamelLangChain4jServiceVariables` header to the `@V` parameters, and the 
`CamelLangChain4jServiceMemoryId` header to `@MemoryId`.
   
   Method and annotation discovery go through `camel-bean`. That is deliberate: 
`BeanInfo` walks the whole type hierarchy, so it finds annotations declared on 
an interface even when the registered object is a class a framework generated, 
which is the Quarkus and Spring shape. Reimplementing that walk would have been 
a second, worse copy of it.
   
   Binding itself could not be delegated. See the comment below, there is an 
open question for maintainers there.
   
   ## camel-bean change
   
   One addition: `MethodInfo.getParameterAnnotations()`, returning 
`Annotation[][]`, tagged `@since 4.24`. Bean binding already collected those 
annotations internally without exposing them. Purely additive, no existing 
signature changes.
   
   ## Validation
   
   The service lookup and the validations run when the producer starts, not on 
the first message, so a misconfigured route fails at startup with a message 
naming the service and the endpoint.
   Rejected at startup: no such bean, an ambiguous method on a multi-method 
service, an overloaded method name, a `TokenStream` return type, and invalid 
parameter binding such as two `@UserMessage` parameters or a duplicated `@V` 
name.
   
   ## Tests
   
   69 unit tests, no network. Validation, method resolution, parameter binding, 
prompt rendering, exceptions, lifecycle.
   
   A conformance suite runs the same route contract twice. Once as a unit test 
against a mocked OpenAI server (`camel-test-infra-openai-mock`), with no model. 
Once as an integration test against an Ollama test-infra container and real 
LangChain4j objects.
   
   The integration test always runs against Ollama. It carries the same 
`@DisabledIfSystemProperty(named = "ci.env.name")` guard as the integration 
tests of `camel-langchain4j-agent` and `camel-langchain4j-chat`, so it is 
skipped in CI and nowhere else.
   
   It also adds three scenarios that cannot be unit tests, because each one 
asserts that a model behaved rather than that the component did: the model 
answered from its rendered `@SystemMessage`, it recalled a name across two 
turns of one memory id, and it elected to call a tool from plain language. A 
stub cannot decide to read its system message, recall anything, or choose to 
call a tool.
   
   ## Docs
   
   Component doc, catalog entries and the design doc under `design/`, all 
regenerated and committed.
   
   ---
   
   Draft on purpose. The open question in the comment below is worth settling 
before this is marked ready for review.
   
   _Claude Code on behalf of @zbendhiba_
   
   🤖 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