Skip to content

AZapata27/ai-integration-springai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI Integration Spring Boot Backend

A powerful Spring Boot backend for AI-powered applications that integrates various language models (LLMs), vector databases, and natural language processing capabilities.

πŸš€ Features

  • Basic Chat Interface: Direct communication with AI models like GPT-4 and locally hosted models via Ollama
  • RAG (Retrieval Augmented Generation): Enhanced responses with context from your knowledge base
  • Function Calling: Execute predefined Java functions from natural language prompts
  • Recommendation Engine: Book and author recommendations based on chat history analysis
  • Image Generation: AI-powered image creation from text descriptions
  • Speech-to-Text: Audio transcription services

πŸ› οΈ Tech Stack

  • Framework: Spring Boot 3.2+
  • AI SDKs:
    • Spring AI (for OpenAI, Ollama integration)
    • LangChain4j (for RAG, embeddings, function calling)
  • Vector Database: Chroma DB
  • LLMs:
    • OpenAI GPT models (3.5/4)
    • Ollama (for local model deployment)
  • Database: PostgreSQL (user data, chat history)
  • Authentication: JWT-based auth
  • Documentation: OpenAPI/Swagger

πŸ“‹ Prerequisites

  • Java 17+
  • Maven 3.8+
  • Docker & Docker Compose
  • Chroma DB instance
  • API keys for OpenAI (if using their services)
  • Ollama setup (for local models)

🚦 Getting Started

  1. Clone the repository

    git clone https://github.com/your-username/ai-integration-spring.git
    cd ai-integration-spring
  2. Configure environment variables Create a .env file in the root directory with the following variables:

    OPENAI_API_KEY=your_openai_key
    CHROMA_DB_HOST=localhost
    CHROMA_DB_PORT=8000
    OLLAMA_API_URL=http://localhost:11434
    DB_USERNAME=postgres
    DB_PASSWORD=yourpassword
    
  3. Start the services

    docker-compose up -d
  4. Build and run the application

    mvn clean install
    mvn spring-boot:run
  5. Verify the setup Open your browser and navigate to http://localhost:8080/swagger-ui.html

πŸ”Œ API Endpoints

Chat Controllers

  • POST /api/chat/basic - Basic chat with LLM
  • POST /api/chat/rag - Enhanced chat with RAG

Function Calling Controller

  • POST /api/function/call - Execute functions via natural language

Embedding Controller

  • POST /api/embedding/generate - Generate embeddings
  • POST /api/embedding/search - Search similar documents

Recommendation Controller

  • GET /api/recommendations/books - Get book recommendations
  • GET /api/recommendations/authors - Get author recommendations

Image Controller

  • POST /api/images/generate - Generate images from text prompts

Transcript Controller

  • POST /api/transcript/audio - Convert audio to text

πŸ“ Project Structure

ai-integration-spring/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/
β”‚   β”‚   β”œβ”€β”€ java/com/example/aiintegration/
β”‚   β”‚   β”‚   β”œβ”€β”€ config/          # Configuration classes
β”‚   β”‚   β”‚   β”œβ”€β”€ controller/      # REST controllers
β”‚   β”‚   β”‚   β”œβ”€β”€ model/           # Entity classes
β”‚   β”‚   β”‚   β”œβ”€β”€ repository/      # Data access
β”‚   β”‚   β”‚   β”œβ”€β”€ service/         # Business logic
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ ai/          # AI service implementations
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ embedding/   # Embedding services
β”‚   β”‚   β”‚   β”‚   └── llm/         # LLM providers
β”‚   β”‚   β”‚   β”œβ”€β”€ dto/             # Data transfer objects
β”‚   β”‚   β”‚   └── util/            # Utility classes
β”‚   β”‚   └── resources/
β”‚   β”‚       β”œβ”€β”€ application.yml  # Application config
β”‚   β”‚       └── static/          # Static resources
β”‚   └── test/                    # Test classes
β”œβ”€β”€ .mvn/wrapper/                # Maven wrapper
β”œβ”€β”€ docker-compose.yml           # Docker services
β”œβ”€β”€ Dockerfile                   # Application container
β”œβ”€β”€ pom.xml                      # Maven dependencies
└── README.md                    # This file

πŸ”§ Configuration

Vector Database Setup

The application uses Chroma DB as the vector database. Configure connection in application.yml:

chroma:
  url: ${CHROMA_DB_HOST}:${CHROMA_DB_PORT}
  collection: ai-documents

LLM Configuration

Configure multiple LLM providers:

spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      chat:
        options:
          model: gpt-4
          temperature: 0.7
    ollama:
      base-url: ${OLLAMA_API_URL}
      chat:
        options:
          model: llama3

🧩 Extending the Application

Adding New Functions for Function Calling

  1. Create a new service with the function implementation
  2. Register the function in FunctionRegistry
  3. Add function metadata to the available tools

Adding New RAG Sources

  1. Create a document loader in the repository package
  2. Implement the chunking strategy
  3. Register the new source in the EmbeddingService

πŸ“Š Performance Considerations

  • Caching: Implement Redis caching for frequently accessed embeddings
  • Async Processing: Use Spring's async capabilities for non-blocking operations
  • Connection Pooling: Configure proper database and HTTP client pooling

πŸ›‘οΈ Security

  • All API endpoints are secured with JWT authentication
  • LLM API keys are stored securely using environment variables
  • Input validation and sanitization for all user inputs
  • Rate limiting on sensitive endpoints

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

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

About

Comprehensive Spring AI Integration Showcase with OpenAI and Expanding LLMs Integrations

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors