I originally implemented Faramir, which is a service that uses OpenAI to valid and format phone numbers, in Python with the Langchain library. On the one hand, a key benefit of the microservice architecture is that you can implement services in different languages. That’s especially useful when working with LLMs since there’s a rich Python ecosystem.
But on the other hand, it’s convenient to have a consistent technology stack - e.g. Java/Spring Boot - across services. That’s valuable if the code that integrates with the LLM needs to invoke functionality that’s written in Java. So I decided to reimplement Faramir in Java with Spring Boot.
Using Spring AI
There’s a few Java libraries for interacting with LLMs, including langchain4j, which is the Java version of the Python-based Langchain library. But there’s also a new library called Spring AI that’s designed to make it easy to integrate LLMs with Spring Boot applications.
Using Spring AI to interact with an LLM is remarkably straightforward. Here’s the class that validates phone numbers:
@Component
public class PhoneNumberParser {
private final ChatClient chatClient;
public PhoneNumberParser(ChatClient chatClient) {
this.chatClient = chatClient;
}
public String parse(String phoneNumber) {
ChatResponse response = chatClient.call(
new Prompt("""...""".formatted(phoneNumber)));
return response.getResult().getOutput().getContent();
}
}
There’s also a couple of properties that need to be set in application.properties
:
spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.openai.chat.options.model=gpt-4
In typical Spring Boot-style, auto-configuration takes care of the details.
To learn more
To learn more, please take a look at the Github repository.
Need help with accelerating software delivery?
I’m available to help your organization improve agility and competitiveness through better software architecture: training workshops, architecture reviews, etc.