This application demonstrates how to use Spring AI to create an AI assistant that can solve complex problems through multiple rounds of tool calls.
- Integration with OpenAI's GPT models via Spring AI
- Custom tool implementations for:
- Weather information retrieval
- Basic calculator operations
- Information search
- Web interface for interacting with the AI assistant
- Support for multi-round conversations with tool calls
- Java 17 or higher
- Maven
- OpenAI API key
-
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY=your_api_key_here -
Build the application:
mvn clean package -
Run the application:
java -jar target/ai-demo-0.0.1-SNAPSHOT.jar -
Open your browser and navigate to
http://localhost:12000
Try asking the AI assistant:
- "What's the weather like in Tokyo?"
- "Calculate 1234 divided by 56"
- "Tell me about Spring AI"
- "I'm planning a trip to Paris. What's the weather there and what's 250 euros in dollars if the exchange rate is 1.08?"
- The application uses Spring AI to integrate with OpenAI's models
- Custom tools are implemented as Spring components
- When a user asks a question, the AI determines if it needs to use tools to answer
- If tools are needed, the AI makes tool calls and incorporates the results into its response
- The conversation history is maintained for context in follow-up questions
AiDemoApplication.java: Main Spring Boot applicationtools/: Custom tool implementationsservice/AiChatService.java: Service for handling AI chat functionalitycontroller/: REST controllers for the API and web interfacemodel/: Data models for requests and responses