ID-based RAG FastAPI: A Comprehensive Guide

Nov 8, 2023 | Data Science

Integrating Langchain with FastAPI offers an opportunity to create a robust document indexing and retrieval system utilizing PostgreSQL and pgvector. This guide will walk you through the setup and functionalities of the ID-based RAG FastAPI project while ensuring that it’s user-friendly and informative.

Overview

The ID-based RAG FastAPI project is designed for asynchronous and scalable operations, providing a framework that allows document management through embeddings organized by file_id. Primarily aimed at integrating with LibreChat, this API can tackle various ID-based use cases, focusing on targeted queries by utilizing file metadata stored in a database.

Key Features

  • Document Management: Easily add, retrieve, and delete documents.
  • Vector Store: Efficient document retrieval utilizing Langchain’s vector store.
  • Asynchronous Support: Enhanced performance through async operations.

Getting Started

To kick things off, you’ll need to configure your environment and get the database up and running. Here’s how you can do it:

1. Configure .env File

Make sure to set up your environment variables by following the guidelines in the Environment Variables section below.

2. Set Up PostgreSQL pgvector Database

  • Run an existing PostgreSQL pgvector setup.
  • Using Docker:
    • To start both the RAG API and the database: docker compose up
    • To start the database only: docker compose -f .db-compose.yaml up

3. Run the API

  • Using Docker:
    • To start both the PostgreSQL pgvector and the RAG API: docker compose up
    • To run just the RAG API: docker compose -f .api-compose.yaml up
  • Locally:
    • Ensure DB_HOST is set to the correct database hostname.
    • Run the following commands (preferably in a virtual environment):
      bash
      pip install -r requirements.txt
      uvicorn main:app

Environment Variables

Before running the application, configure the following environment variables:

  • RAG_OPENAI_API_KEY: API key for OpenAI API Embeddings.
  • RAG_OPENAI_BASEURL: (Optional) Base URL for your OpenAI API Embeddings.
  • VECTOR_DB_TYPE: Select vector database type (default: pgvector).
  • POSTGRES_DB: Name of your PostgreSQL database.
  • DB_HOST: Hostname or IP address of the PostgreSQL database server.
  • More variables can be configured as outlined in the README.

Using Atlas MongoDB as the Vector Database

If you prefer Atlas MongoDB instead of pgvector, set the following environment variables:

  • VECTOR_DB_TYPE=atlas-mongo
  • ATLAS_MONGO_DB_URI=mongodb+srv:...
  • MONGO_VECTOR_COLLECTION=collection name

Cloud Installation Settings

AWS

Setting up RDS PostgreSQL with RAG API requires adhering to certain version requirements and following specific steps outlined in the README.

Troubleshooting

If you encounter issues during setup or execution, consider the following troubleshooting steps:

  • Ensure all environment variables are correctly set up.
  • Verify that your Docker installation is running without conflicts.
  • When using database connections, check the accessibility of the database host.
  • Use DEBUG_RAG_API=True for detailed logging output that may help identify errors.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox