Skip to content

pathakvikash/supa_rag_chat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PDF Embeddings & Query API

A FastAPI application that extracts text from PDFs, generates embeddings using Sentence Transformers, stores them in Supabase, and allows querying with AI responses via Ollama.


Features

  • Upload PDFs and extract text in chunks with overlap.
  • Generate embeddings using all-MiniLM-L6-v2.
  • Store embeddings in Supabase with metadata (page number, chunk index, snippet).
  • Query your PDF database using embeddings similarity search.
  • Answer queries using contextual AI via Ollama.
  • Simple HTML interface for uploading PDFs.

RagTruth: Enhanced RAG with Claims Verification

RagTruth significantly improves the reliability and accuracy of AI-generated answers by integrating a claims verification process. This enhancement ensures that responses are not only contextually relevant but also factually supported by the provided documents.

How it Works:

  1. Initial Answer Generation: Ollama generates a preliminary answer based on the retrieved context.
  2. Claims Extraction: All distinct factual claims from the initial answer are extracted using Ollama.
  3. Claims Verification: Each extracted claim is verified against the original document chunks using an NLI (Natural Language Inference) model (cross-encoder/nli-deberta-v3-small). Claims are marked as SUPPORTED, CONTRADICTED, or NOT_ENOUGH_EVIDENCE.
  4. Final Answer Rewriting: Ollama rewrites the final answer using only the supported claims, ensuring conciseness and factual accuracy, while explicitly stating when there's insufficient evidence for unsupported claims.

This process reduces hallucinations and increases the trustworthiness of the AI's responses.


Tech Stack

  • FastAPI – Web API framework
  • PyMuPDF (fitz) – PDF text extraction
  • Supabase – Vector database and backend
  • Sentence Transformers – Generating embeddings
  • Ollama – AI language model API
  • python-dotenv – Manage environment variables
  • Requests – API calls to Ollama

Installation

  1. Clone the repository:
git clone https://github.com/pathakvikash/pdf-embedding-api.git
cd pdf-embedding-api

2. Create a virtual environment and activate it:

```bash
python -m venv venv
source venv/bin/activate   # Linux/macOS
venv\Scripts\activate      # Windows
```

3. Install dependencies:

```bash
pip install -r requirements.txt
```

4. Create a `.env` file with your credentials:

```env
SUPABASE_URL=https://your-supabase-url.supabase.co
SUPABASE_KEY=your-supabase-api-key
OLLAMA_API_URL=http://127.0.0.1:11434/api/generate
OLLAMA_MODEL=llama3.2:3b
```

---

## Usage

1. Run the FastAPI server:

```bash
uvicorn main:app --reload
or
python -m uvicorn main:app --reload
```

2. Open your browser at [http://127.0.0.1:8000](http://127.0.0.1:8000) access the UI start upload and interaction.

---

### API Endpoints

#### Upload PDF

- **URL:** `/upload`
- **Method:** `POST`
- **Body:** `file` (form-data)
- **Response:** JSON with inserted chunk count and source filename.

#### Query PDF Embeddings

- **URL:** `/query`
- **Method:** `POST`
- **Body:** `q` (form-data string)
- **Response:** JSON with AI-generated answer and matched text snippets.

#### Index Page

- **URL:** `/`
- **Method:** `GET`
- **Response:** HTML page for file upload.

---

## PDF Chunking

- Text is extracted page by page.
- Split into chunks of 300 words with 50-word overlap.
- Each chunk is stored with page number, chunk index, text snippet, and embedding.

---

## Supabase Setup

1. Create a table `pdf_embeddings`:

```sql
CREATE TABLE pdf_embeddings (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    source TEXT,
    page INT,
    chunk_index INT,
    text_snippet TEXT,
    embedding vector(384)
);
```

2. Create an RPC function `match_pdf_embeddings` for vector similarity search:

```sql
CREATE FUNCTION match_pdf_embeddings(query_embedding vector(384), match_count int)
RETURNS SETOF pdf_embeddings AS $$
    SELECT * FROM pdf_embeddings
    ORDER BY embedding <-> query_embedding
    LIMIT match_count;
$$ LANGUAGE SQL STABLE;
```

---

## Notes

- Ollama is used for AI responses; you can configure the endpoint and model via `.env`.
- Make sure Supabase has the `pgvector` extension enabled for vector operations.
- You can adjust chunk size and overlap in `extract_chunks()` function.

---

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors