This Spring Boot application implements an advanced Retrieval-Augmented Generation (RAG) system that connects language models with SQL database retrieval using LangChain4j.
- Natural Language to SQL: Ask questions in plain English and get database insights
- Multiple LLM Support: Works with Google Gemini, Ollama, or LocalAI
- Interactive Interface: Simple web UI for asking questions
- Spring Boot Integration: Leverages Spring's dependency injection and configuration
- Java 17 or higher
- Maven
- MariaDB database (or adjust configurations for your preferred database)
- API keys for language models (Google AI Gemini, etc.)
git clone https://github.com/DIGI-UW/rag.git
cd rag
Sign in to Google Studio to get an API key for Gemini and add it to the env file
docker compose up --build
docker compose up
The application will be available at http://localhost:8080
Set up the following environment variables or modify application.properties
:
# Database configuration
DB_URL=jdbc:mariadb://localhost:3306/your_database
DB_USER=your_username
DB_PASSWORD=your_password
# Language model API keys
GEMINI_API_KEY=your_gemini_api_key
# Ollama configuration (if using)
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL_NAME=llama3
# LocalAI configuration (if using)
LOCAL_AI_BASE_URL=http://localhost:8080/v1
LOCAL_AI_MODEL_NAME=gpt-4
# Build the application
mvn clean install
# Run the application
mvn spring-boot:run
The application will be available at http://localhost:8080
- Open your browser and navigate to http://localhost:8080
- Type your database question in the chat interface
- Receive answers based on your database content
Example questions:
- "How many patients do we have?"
- "What are the names of the patients?"
- "what was the most prescribed drug?"
- Controller Layer: Handles HTTP requests and responses
- Service Layer: Coordinates between components
- Content Retriever: Translates questions to SQL and retrieves data
- LLM Integration: Uses language models to generate SQL and format responses
The SqlDatabaseContentRetriever
is potentially dangerous and should be used with caution:
- READ-ONLY ACCESS: The database user should have very limited read-only permissions
- DEVELOPMENT USE: Not recommended for production without thorough security measures
- SQL VALIDATION: Basic validation is performed but cannot guarantee safety
Modify AppConfig.java
to integrate additional language models:
@Bean
public ChatLanguageModel newModel() {
return NewModelProvider.builder()
.apiKey("your-api-key")
.modelName("model-name")
.build();
}
The frontend interface is located in src/main/resources/static/index.html
. Modify this file to customize the user experience.
- Built with LangChain4j
- Powered by Spring Boot