A SwiftUI demonstration of Retrieval-Augmented Generation (RAG) using local embeddings and Google's Gemini API for text generation.
- Local Embeddings: Uses Apple's Natural Language framework for sentence embeddings
- Document Slicing: Splits documents into manageable chunks
- Semantic Retrieval: Finds relevant chunks using cosine similarity
- Reranking: Improves retrieval results with contextual features
- Real LLM Generation: Uses Google Gemini API for answer generation
- Interactive UI: Step-by-step visualization of the RAG pipeline
- Go to Google AI Studio
- Sign in with your Google account
- Click "Create API Key"
- Copy the API key
Open SwiftRAGDemo/Config.swift
and add your Gemini API key directly in the geminiAPIKey
property:
struct Config {
// MARK: - API Keys
static var geminiAPIKey: String {
// TODO: Replace with your Gemini API key for demo purposes
"your_actual_api_key_here"
}
// ...
}
Note:
- Do not commit your real API key to version control for production or public projects.
- This approach is for demo and local testing only.
- Open the project in Xcode
- Build and run the app
- Enter a question about the Superman vs. Iron Man story
- Tap "Run RAG Pipeline" to see each step in action
- View the results by tapping on each step
- Slicing: Splits the document into chunks
- Indexing: Creates embeddings for each chunk using local models
- Retrieval: Finds the most relevant chunks for your question
- Reranking: Improves the order of retrieved chunks
- Generation: Uses Gemini to generate a final answer
The app follows a clean architecture pattern with separated concerns:
- RAGService: Handles the core RAG pipeline (slicing, indexing, retrieval, reranking)
- GeminiService: Manages all Gemini API interactions and text generation
- Config: Centralized configuration management
- DemoModel: State management for the UI
- DemoView: SwiftUI interface
This separation makes it easy to:
- Swap out different LLM providers (just replace GeminiService)
- Test individual components in isolation
- Maintain and extend the codebase
SwiftRAGDemo/
├── DemoApp.swift # Main app entry point
├── DemoView.swift # SwiftUI interface
├── DemoModel.swift # State management
├── RAGService.swift # Core RAG implementation
├── GeminiService.swift # Gemini API integration
├── Config.swift # Configuration management
├── demo.md # Sample document
└── Assets.xcassets/ # App assets
- iOS 17.0+
- Xcode 15.0+
- Swift 5.9+
- Internet connection (for Gemini API calls)
Copyright Apps Bay Limited. All rights reserved.