Introduction
Integrating AI into an application doesn’t always require an agentic approach or complex prompt engineering always. Often, we simply want to leverage the power of AI to make an application more robust and user-friendly.
For example, consider an ecommerce application where a customer submits a service request via email or text:
Items purchased in order ORD-002 not working. I want to return it. Thanks. - Omkar
To process the request programmatically, the application needs to extract a structured format:
{"customerName":"Omkar","oderId":"ORD-002","issue":"Request to return order"}
The Embabel agentic framework allows you to harness the power of AI wherever it is needed within a Spring Boot application.
Implementation
The full code is available here.
Add the Embabel dependency:
<dependency>
<groupId>com.embabel.agent</groupId>
<artifactId>embabel-agent-starter-openai</artifactId>
<version>${embabel.version}</version>
</dependency>
In this case, we are using OpenAI models. Embabel supports other model providers like Gemini, Anthropic etc.
Inject com.embabel.agent.api.common.Ai into your Spring component:
public SampleController(Ai ai) {
this.ai = ai;
}
We will use Ai to make a call to LLMs.
In the following controller API, the LLM parses the customer query and returns a formatted JSON output.
@GetMapping("/api")
public ResponseEntity<CustomerQuery> customerQuery(@RequestParam String query) {
CustomerQuery customerQuery = ai.withAutoLlm()
.createObject("""
Customer query: %s
""".formatted(query), CustomerQuery.class);
return ResponseEntity.ok(customerQuery);
}
Generating a Java object from a natural language query makes it easy for the application to process the data further.
Testing
Export or set environment variable for your model provider’s API key. In this case, use OPENAI_API_KEY.
export OPENAI_API_KEY=xxxx
Run the Spring Boot application.
mvn spring-boot:run
Make API call:
$ curl --get \
--data-urlencode "query=Items purchased in order ORD-002 not working. I want to return it. Thanks. - Omkar" \
http://localhost:8080/api
{"customerName":"Omkar","oderId":"ORD-002","issue":"Items purchased in order not working, customer wants to return it"}
Congratulations! 🎉 We have successfully converted a natural language query into a Java object.
LLMs, in combination with frameworks like Embabel, makes it a breeze to utilize the power of AI across your entire application.
Please let me know your thoughts in the comments below.