Using the OpenAI API
A Spring AI Chat Client using the OpenAI API serves as an application that allows users to interact with AI-driven conversational agents powered by OpenAI's language models.
The integration enables businesses and developers to create smart chatbots or virtual assistants that can handle customer inquiries, provide information, and enhance user engagement through natural conversation.
Author: Jeffrey Spaan
Created: 2024-09-27
Last updated: 2024-09-27
- Rapid Development and Scalability
Spring Framework provides robust features and a wide range of tools, making it easier to develop and scale applications. The modular architecture allows you to add new functionalities or integrate additional services without significant refactoring.
- Enhanced User Experience
Natural Conversations, leveraging the OpenAI API, allows for sophisticated natural language processing, enabling more intuitive and engaging interactions. This can lead to improved customer satisfaction and retention.
- Seamless Integration with Existing Systems
The Spring ecosystem supports microservices architecture, making it simple to integrate the chat client with other services (like databases, user authentication, and third-party APIs), facilitating a cohesive application environment.
With the provided example and guidelines, you can effectively implement a Spring AI Chat Client in your Spring Boot projects.
For this basic example we will start with a simple REST endpoint which we can call to see the Spring AI Chat Client at work. Let's create an application using the dependencies as previewed:
This Spring Framework dependency will provide us with all the necessary functionality to create and manage our REST endpoints.
The OpenAI Spring dependency is a library that simplifies the integration of OpenAI's API within a Spring application.
To use the OpenAI API, you need to provide your API key. You can do this by adding the following configuration to your application.yml
file.
If you don't have an API key, you can sign up for one at the OpenAI website.
spring:
application:
name: spring-ai-chat-client
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4o-mini
max-tokens: 100
The OPEANAI_API_KEY is an environment variable that you can set in your system or in your IDE. This way, you can keep your API key secure and avoid exposing it in your codebase.
In this example the gpt-4o-mini model
is used, but you can choose a different model based on your requirements. The gpt-4o-mini model is the affordable and intelligent small model for fast, lightweight tasks.
See all model specifications and pricing
The max-tokens
parameter specifies the maximum number of tokens that the model can generate in a single response.
To interact with the OpenAI API, you need to create a configuration class that will provide the necessary beans to your application.
@Configuration
class Config {
@Bean
ChatClient chatClient(ChatClient.Builder builder) {
return builder.defaultSystem("You are a friendly chat bot that answers question in the voice of a {voice}")
.build();
}
}
The ChatClient
bean will be used to interact with the OpenAI API. The ChatClient.Builder
class is provided by the OpenAI Spring library and allows you to configure the chat client with the necessary settings.
In this example configuration, we set the default system message that the chat client will use when starting a conversation. You can customize this message to suit your application's needs.
Now, let's create a REST controller that will expose an endpoint to interact with the chat client.
@RestController
class ChatClientController {
private final ChatClient chatClient;
ChatClientController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/chat")
Map<String, String> completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message, String voice) {
return Map.of("completion",
chatClient.prompt()
.system(sp -> sp.param("voice", voice))
.user(message)
.call()
.content());
}
}
In this controller, we inject the ChatClient
bean and create a GET
endpoint /chat
that accepts a query parameter message
. The message
parameter is the user's input, and the voice
parameter is used to customize the chatbot's response.
The completion
method calls the chatClient.prompt()
method to start a conversation. We then set the system parameters using the system
method and pass the user's message using the user
method. Finally, we call the call
method to get the chatbot's response.
You can now run your Spring Boot application and access the /chat
endpoint to interact with the chat client.
- To test the REST endpoint, a tool like Postman can be used to send HTTP requests.
- A Postman collection is added within the repository
src/main/resources/postman/collection-to-import.json
Endpoint: http://localhost:8080/chat
Params:
message
(optional): The user's message to the chatbot.voice
(optional): The voice in which the chatbot responds.
If you have any questions in regard to this repository and/or documentation, please do reach out.
Don't forget to:
- Star the repository
- Follow me for more interesting repositories!