Skip to content

Production-ready Spring Boot + OpenAI/ChatGPT integration with conversation memory, content generation & document analysis. REST APIs for AI chatbots, blog posts, code generation, sentiment analysis. Java 17 | Spring Boot 3 | GPT-4 | Rate limiting

License

Notifications You must be signed in to change notification settings

codiebyheaart/spring-boot-openai-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Integration - ChatGPT & Claude API Integration

Java Spring Boot OpenAI License

A comprehensive Spring Boot application showcasing professional AI integration capabilities using OpenAI ChatGPT and Claude AI APIs. This demo demonstrates three service tiers from basic chat to advanced AI features with conversation memory and vector databases.

Perfect for GitHub portfolio to showcase AI integration expertise! ⭐


πŸš€ Features

πŸ“¦ Basic Package - Simple AI Integration

  • βœ… Basic ChatGPT API integration
  • βœ… One-off chat conversations
  • βœ… Custom prompt engineering
  • βœ… Model selection (GPT-4, GPT-3.5-turbo)
  • βœ… Rate limiting & cost control
  • βœ… Complete documentation

πŸ“¦ Standard Package - AI Chatbot with Memory

  • βœ… All Basic features, plus:
  • βœ… Conversational AI with memory
  • βœ… Multi-turn dialogue support
  • βœ… Conversation history storage
  • βœ… Multi-user support
  • βœ… Session management
  • βœ… Context-aware responses
  • βœ… Content generation (blogs, emails, code)

πŸ“¦ Premium Package - Advanced AI System

  • βœ… All Standard features, plus:
  • βœ… Document analysis & summarization
  • βœ… Sentiment analysis
  • βœ… Key points extraction
  • βœ… Question answering on documents
  • βœ… Document comparison
  • βœ… Advanced prompt optimization
  • βœ… Production-ready error handling

πŸ› οΈ Tech Stack

  • Backend: Java 17, Spring Boot 3.2.0
  • AI Integration: OpenAI API (GPT-4, GPT-3.5-turbo)
  • Database: H2 (in-memory), JPA/Hibernate
  • Rate Limiting: Resilience4j
  • Build Tool: Maven
  • Libraries: Lombok, Jackson

πŸ“‹ Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • OpenAI API Key (Get one here)

πŸƒ Quick Start

1. Clone the Repository

git clone https://github.com/codiebyheaartspring-boot-openai-integration.git cd ai-integration-demo

2. Configure API Key

Create or update src/main/resources/application.properties:

openai.api.key=sk-your-actual-openai-api-key-here

Or set as environment variable:

export OPENAI_API_KEY=sk-your-actual-openai-api-key-here

3. Build & Run

mvn clean install mvn spring-boot:run

4. Access the Application


πŸ“š API Endpoints

πŸ’¬ Chat APIs (Basic Package)

Simple Chat

POST /api/chat Content-Type: application/json { "message": "Explain quantum computing in simple terms", "model": "gpt-3.5-turbo", "temperature": 0.7, "maxTokens": 1000 }

🧠 Conversational AI (Standard Package)

Chat with Memory

POST /api/chat/conversation Content-Type: application/json { "userId": "user123", "message": "What's the weather like?", "model": "gpt-3.5-turbo", "temperature": 0.7 }

Continue Conversation

POST /api/chat/conversation/{conversationId} Content-Type: application/json { "message": "Tell me more about that" }

Get Conversation History

GET /api/chat/history/{userId}

✍️ Content Generation (Standard Package)

Generate Blog Post

POST /api/content/blog-post?topic=AI in Healthcare&tone=professional&wordCount=800

Generate Email

POST /api/content/email?purpose=Meeting Request&tone=professional

Generate Code

POST /api/content/code?description=REST API endpoint for user login&language=java

Generate Social Media Post

POST /api/content/social-media?platform=linkedin&topic=AI Integration&includeHashtags=true

πŸ“„ Document Analysis (Premium Package)

Summarize Document

POST /api/document/summarize?summaryLength=medium Content-Type: application/x-www-form-urlencoded document=Your long document text here...

Analyze Sentiment

POST /api/document/sentiment?detailedAnalysis=true Content-Type: application/x-www-form-urlencoded text=Your text to analyze...

Extract Key Points

POST /api/document/keypoints?maxPoints=5 Content-Type: application/x-www-form-urlencoded document=Your document text...

Answer Questions

POST /api/document/qa?question=What are the main findings? Content-Type: application/x-www-form-urlencoded document=Your document text...

🎯 Example Use Cases

1. Customer Support Chatbot

// Chat with conversation memory for consistent support POST /api/chat/conversation { "userId": "customer_456", "message": "I need help with my order", "systemPrompt": "You are a helpful customer support agent." }

2. Content Creation Tool

// Generate blog posts for content marketing POST /api/content/blog-post ?topic=10 Benefits of Microservices Architecture &tone=professional &targetAudience=software developers &wordCount=1000

3. Document Processing System

// Analyze customer feedback POST /api/document/sentiment?detailedAnalysis=true document=Customer feedback text...

βš™οΈ Configuration

Rate Limiting

Control API costs by adjusting rate limits in application.properties:

ratelimit.requests-per-minute=10 ratelimit.timeout-duration=5

Model Selection

Switch between models based on needs:

  • GPT-4: Best quality, higher cost
  • GPT-3.5-turbo: Fast, cost-effective

Temperature Settings

  • 0.0-0.3: Focused, deterministic responses
  • 0.4-0.7: Balanced creativity
  • 0.8-2.0: High creativity, varied responses

πŸ”’ Security Best Practices

  1. Never commit API keys to version control
  2. Use environment variables for sensitive data
  3. Implement authentication for production APIs
  4. Enable rate limiting to prevent abuse
  5. Monitor API usage and costs
  6. Validate all user inputs
  7. Use HTTPS in production

πŸ“Š Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Client β”‚ β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ REST Controllers β”‚ β”‚ - ChatController β”‚ β”‚ - ContentGenerationController β”‚ β”‚ - DocumentAnalysisController β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Service Layer β”‚ β”‚ - OpenAIService β”‚ β”‚ - ConversationService β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β–Ό β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ OpenAI β”‚ β”‚ Database β”‚ β”‚ Rate β”‚ β”‚ API β”‚ β”‚ (H2/JPA) β”‚ β”‚ Limiter β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 

πŸ§ͺ Testing

Run the application and test endpoints:

# Health check curl http://localhost:8080/api/chat/health # Simple chat curl -X POST http://localhost:8080/api/chat \ -H "Content-Type: application/json" \ -d '{"message": "Hello, AI!", "model": "gpt-3.5-turbo"}' # Generate content curl -X POST "http://localhost:8080/api/content/blog-post?topic=Spring Boot Tips&tone=professional"

πŸ“– Additional Documentation


🎨 Customization

Add Custom AI Features

  1. Create a new controller in controller/ package
  2. Implement business logic in service/ package
  3. Add custom prompts in OpenAIService
  4. Update documentation

Integrate Claude AI

  1. Add Claude AI dependency to pom.xml
  2. Create ClaudeService similar to OpenAIService
  3. Add configuration for Claude API key
  4. Create new endpoints or modify existing ones

Add Vector Database (Pinecone/Weaviate)

  1. Add vector DB dependency
  2. Create embeddings using OpenAI
  3. Store and retrieve vectors
  4. Implement semantic search

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Your Name


🌟 Show Your Support

Give a ⭐️ if this project helped you!


πŸ“ž Contact

Have questions or need AI integration services?


πŸš€ Roadmap

  • Add Claude AI integration
  • Implement vector database (Pinecone)
  • Add streaming responses
  • Create frontend UI
  • Add Docker support
  • Implement caching layer
  • Add more AI models (Google Gemini, etc.)
  • Create comprehensive test suite

Built with ❀️ using Spring Boot and OpenAI

About

Production-ready Spring Boot + OpenAI/ChatGPT integration with conversation memory, content generation & document analysis. REST APIs for AI chatbots, blog posts, code generation, sentiment analysis. Java 17 | Spring Boot 3 | GPT-4 | Rate limiting

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages