02 - AI Services
Declare a Java interface, and LangChain4J generates the implementation that calls Claude.
What it demonstrates
@SystemMessagesets the model's behavior.@UserMessagemarks the parameter that carries the question.AiServices.create(Interface.class, model)builds the proxy.- Change the text of
@SystemMessageand run again to see how the answers change.
Stack
| Component | Version / detail |
|---|---|
| Java | 21 |
| LangChain4j (Anthropic module) | 1.24.1 |
| Model | claude-haiku-5-5 |
| Build | Maven, exec-maven-plugin 3.5.0 |
Run
export ANTHROPIC_API_KEY=your-key-here
mvn -q compile exec:java