Skip to content

iakashpatel/code-qa-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RAG-based Code Query API

This project provides a Retrieval-Augmented Generation (RAG) API for querying a codebase. It uses:

  • OpenAI embeddings and ChatCompletion (GPT models),
  • A FAISS index for vector similarity search,
  • FastAPI for the RESTful API.

Table of Contents


Prerequisites

  1. Python 3.9+ (Recommended)
  2. Virtual environment (e.g., venv or conda)
  3. FAISS (for vector indexing):
    • Conda (recommended):
      conda install -c pytorch faiss-cpu
    • Pip (if you find precompiled wheels):
      pip install faiss-cpu
    • Or build from source (FAISS GitHub)
  4. OpenAI Python Library and dependencies
  5. A valid OpenAI API key (for embeddings and GPT-based text generation).

Installation

  1. Clone this repository (or copy the code):

    git clone https://github.com/yourusername/rag-code-query.git
    cd rag-code-query
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate   # On Linux/Mac
    venv\Scripts\activate      # On Windows
  3. Install required packages:

    pip install -r requirements.txt

Environment Variables

export OPENAI_API_KEY="your-openai-api-key"

Project Structure

/
├── app.py # Main FastAPI application code
├── repository_processor.py
├── requirements.txt
├── README.md
└── ...

Usage

  1. Start the Server

Run the FastAPI application (assuming your main file is app.py):

uvicorn app:app --reload

This starts the server at http://127.0.0.1:8000.

  1. Index a Repository

Before querying a repository, you need to index it. The indexing process:

  • Chunks the code using chunk_repository.
  • Generates OpenAI embeddings for each chunk.
  • Builds a FAISS index (repository_index.faiss) and a metadata file (metadata.json).

You can automatically create or refresh the index by calling the /index endpoint or letting the code auto-initialize if the index/metadata files are missing.

example:

curl -X POST "http://127.0.0.1:8000/index
  1. Query the Repository

Once indexed, you can send queries to the /query endpoint. The endpoint:

  1. Retrieves the most relevant chunks from FAISS.
  2. Optionally summarizes if necessary.
  3. Uses GPT-4 to generate a final answer.

Example (replace placeholders accordingly)

curl --location 'http://127.0.0.1:8000/query' \
--header 'Content-Type: application/json' \
--data '{
    "question": "What does class Grip do?"
}'

Health Check:

curl http://127.0.0.1:8000/health

Notes & Troubleshooting

  1. FAISS Installation

If pip install faiss-cpu doe

conda install -c pytorch faiss-cpu

or build from source.

  1. OpenAI Rate Limits

If you encounter rate limits or 429 errors, you may need to throttle requests or upgrade your OpenAI plan.

  1. Large Repositories
  • Increase the chunk size or the number of retrieved chunks (k) carefully.
  • Summarize or do multi-step retrieval if you exceed GPT’s context window.
  1. Token Counting

If you’re hitting token limits, install and use tiktoken to check prompt lengths.

Summarize large chunks before sending them to GPT.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages