Welcome to the Spring AI Chat Bot project! This application demonstrates how to create a chatbot with streaming capabilities using Spring AI and Spring Boot. By leveraging the power of Spring AI, we've built a robust and scalable solution for real-time AI-powered conversations.
This project showcases the integration of Spring AI with Spring Boot to create a chatbot that supports both traditional request-response interactions and streaming responses. It's an excellent starting point for developers looking to implement AI-powered chat functionality in their Spring applications.
- Java 23
- Maven 3.6.3 or newer
- Spring Boot 3.3.4
- Spring AI 1.0.0-M2
The project relies on the following key dependencies:
spring-boot-starter-web
: For building web applications with Spring MVCspring-ai-anthropic-spring-boot-starter
: Spring AI starter for Anthropic's AI modelsspring-boot-starter-test
: For testing Spring Boot applications
To get started with this project, ensure you have Java 23 and Maven installed on your system. Then, follow these steps:
-
Set up your Anthropic API key:
- Locate
application.properties
in thesrc/main/resources
directory - Add the following line, replacing
your_api_key
with your actual Anthropic API key:spring.ai.anthropic.api-key=your_api_key
- Specify that you want to use the Claude 3.5 Sonnet Model (or the model of your choice)
spring.ai.anthropic.chat.options.model=claude-3-5-sonnet-20240620
- Locate
-
Build the project:
mvn clean install
-
Run the application:
mvn spring-boot:run
The application will start, and you'll be able to access the chat endpoints.
Once the application is running, you can interact with it using the following endpoints:
-
Traditional chat (POST request):
POST http://localhost:8080/chat?message=Your message here
-
Streaming chat (GET request):
GET http://localhost:8080/stream?message=Your message here
You can use tools like cURL, Postman, or create a simple frontend to interact with these endpoints. I like to use httpie with the --stream
option
http --stream :8080/stream message=="Write a short overview of generative AI"
The ChatController
class is the heart of our application. It handles both traditional and streaming chat requests:
@RestController
@CrossOrigin
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@PostMapping("/chat")
public String chat(@RequestParam String message) {
return chatClient.prompt()
.user(message)
.call()
.content();
}
@GetMapping("/stream")
public Flux<String> chatWithStream(@RequestParam String message) {
return chatClient.prompt()
.user(message)
.stream()
.content();
}
}
This controller demonstrates two key methods:
chat
: Handles traditional request-response interactions.chatWithStream
: Provides a streaming response using Spring WebFlux'sFlux
.
The Application
class is a standard Spring Boot application class:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
This class bootstraps the Spring application context and runs the embedded web server.
This Spring AI Chat Bot project showcases the power and flexibility of Spring AI when integrated with Spring Boot. It provides a solid foundation for building AI-powered chat applications with both traditional and streaming capabilities.
We encourage you to explore the code, experiment with different AI models, and extend the functionality to suit your specific needs. If you have any questions or run into issues, please don't hesitate to open an issue in the repository.
Happy coding, and enjoy building intelligent, responsive chat applications with Spring AI!