Skip to content

LauLemGroup/SpringAI-QuickStart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring AI POC - Java AI Assistant

Objective

This project is a demonstration of integrating Spring AI with OpenAI to create a Java-specialized AI assistant. The chatbot is capable of:

  • Answering questions about Java development
  • Memorizing conversation context
  • Accessing custom tools (date/time)
  • Integrating MCP (Model Context Protocol) clients to extend functionality

Overview of Main Spring AI Classes

1. ChatClient

  • Role: Main client for interacting with the AI model (OpenAI)
  • Usage: Building prompts and calling the language model
  • Configuration: Defined in the controller with advisors for memory management

2. MessageWindowChatMemory

  • Role: Stores and manages conversation message history
  • Configuration: Limited to a maximum of 10 messages to optimize API calls
  • Benefit: Allows the model to maintain conversation context

3. MessageChatMemoryAdvisor

  • Role: Advisor that automatically injects history into each model call
  • Usage: Ensures that each message to the AI includes previous context

4. SystemPromptTemplate

  • Role: Template to define system role and behavior
  • Configuration: Defines the AI as a Java expert and enforces Markdown responses

5. SyncMcpToolCallbackProvider

  • Role: Tool provider via MCP protocol
  • Usage: Integrates external tools available via the MCP server
  • Configuration: Connected to the server https://mcp.context7.com

6. @Tool Annotation

  • Role: Decorator to expose Java methods as tools for the AI
  • Example: DateTimeTools exposes getCurrentDateTime() to the AI

Architecture

ChatController
├── POST /api/v1/chat          → Simple chat with memory
├── POST /api/v1/chat/tools    → Chat with custom tools (DateTimeTools)
└── POST /api/v1/chat/mcp      → Chat with MCP tools + custom tools

Running the Application

Prerequisites

  • Java 25+
  • Maven
  • OpenAI API key (defined via the API_KEY environment variable)

Configuration

Setting the API key:

export API_KEY="your-openai-key"

Startup

# With Maven
./mvnw spring-boot:run

# Or with the compiled JAR
java -jar target/SpringAIArticle-0.0.1-SNAPSHOT.jar

The application will start on http://localhost:8080


Possible Requests

1. Simple Chat (with memory)

curl -X POST http://localhost:8080/api/v1/chat \
  -H "Content-Type: application/json" \
  -d '"Hello, my name is Alexandre, and you re Bottest!"'

2. Chat with Custom Tools

curl -X POST http://localhost:8080/api/v1/chat/tools \
  -H "Content-Type: application/json" \
  -d '"What time is it now?"'

3. Chat with MCP Tools + Custom Tools

curl -X POST http://localhost:8080/api/v1/chat/mcp \
  -H "Content-Type: application/json" \
  -d '"Answer these two requests: - What are the latest Spring Boot versions? Find them in MCP Context7. - Get the time and display it."'

Technologies Used

  • Framework: Spring Boot 4.0.3
  • AI: Spring AI 2.0.0 + OpenAI
  • Protocol: MCP (Model Context Protocol)
  • Language: Java 25

About

Spring AI QuickStart - A Java-specialized AI assistant built with Spring Boot and Spring AI, showcasing chat memory, custom tools, and MCP (Model Context Protocol) integration via OpenAI.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages