Skip to content

SwiftUI demo of Retrieval-Augmented Generation (RAG) using local embeddings and Google Gemini API. Features semantic search, reranking, and step-by-step visualization of the RAG pipeline.

Notifications You must be signed in to change notification settings

banghuazhao/swift-rag-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift RAG Demo with Gemini

A SwiftUI demonstration of Retrieval-Augmented Generation (RAG) using local embeddings and Google's Gemini API for text generation.

Main UI Step View Result View

Features

  • 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

Setup

1. Get a Gemini API Key

  1. Go to Google AI Studio
  2. Sign in with your Google account
  3. Click "Create API Key"
  4. Copy the API key

2. Configure the API Key (Demo Only)

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.

Usage

  1. Open the project in Xcode
  2. Build and run the app
  3. Enter a question about the Superman vs. Iron Man story
  4. Tap "Run RAG Pipeline" to see each step in action
  5. View the results by tapping on each step

How It Works

  1. Slicing: Splits the document into chunks
  2. Indexing: Creates embeddings for each chunk using local models
  3. Retrieval: Finds the most relevant chunks for your question
  4. Reranking: Improves the order of retrieved chunks
  5. Generation: Uses Gemini to generate a final answer

Architecture

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

Project Structure

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

Requirements

  • iOS 17.0+
  • Xcode 15.0+
  • Swift 5.9+
  • Internet connection (for Gemini API calls)

License

Copyright Apps Bay Limited. All rights reserved.

Releases

No releases published

Packages

No packages published

Languages