Get structured data from the model instead of free text: the method returns a Java record.
Source on GitHub · Back to portfolio
Get structured data from the model instead of free text: the method returns a Java record.
• A `record` as the return type of an AI Service method.
• Instructions in `@SystemMessage` that describe each field.
• `temperature(0.0)` to make the extraction more deterministic.
• If the model returns something that cannot be mapped to the record, LangChain4J raises an error. Handling that case (retry, validation, or a fallback) is the next step after this example.
• JDK 21
• Maven 3.9 or later
• An Anthropic API key
export ANTHROPIC_API_KEY=your-key-here
mvn -q compile exec:java
Jose Said Olano Garcia