A Flask-based application providing multiple endpoints for performing vector searches and Retrieval-Augmented Generation (RAG) using MongoDB and LangChain. The application leverages embeddings for document retrieval and query augmentation.
Course: Open Learning
Semester: 6
This project was developed as part of coursework in Open Learning. It aims to provide a scalable API for vector search and RAG tasks using MongoDB, Flask, and LangChain, facilitating efficient document retrieval and query augmentation.
- Vector Search
- Retrieval-Augmented Generation
- Self-Querying Vector Search
- Self-Querying Retrieval-Augmented Generation
- Programming Languages/Technologies:
- Python
- Libraries/Frameworks:
- Flask
- LangChain
- Database:
- MongoDB
- Tools:
- Large Language Models (LLMs)
The application is designed as RESTful microservice with endpoints for vector search and RAG. MongoDB is used for data storage, and LangChain facilitates interaction with language models and embeddings.
llm-rag-api/
│
├── misc/ # Helper functions
│
├── rag/ # Customised RAG implementation
│
├── README.md # Project documentation
├── app.py # Flask API endpoints
└── requirements.txt # List of dependencies
Follow the instructions below to set up the project on your local machine.
-
Clone the Repository:
git clone https://github.com/grzgm/llm-rag-api cd llm-rag-api -
Install Dependencies:
pip install -r requirements.txt
-
Configure Environment Variables: Copy the contents of
.env.exampleto a.envfile in the project root and populate it with the necessary environment variables:MONGO_URI=<your_mongodb_uri> DB_NAME=<your_database_name> COLL_NAME=<your_collection_name> INDEX_NAME=<your_index_name> EMBEDDING_KEY=<your_embedding_key> DOCUMENT_CONTENT_DESCRIPTION=<document_content_description> -
Run the Flask Application:
flask run
- URL:
/ - Method:
GET - Description: Returns a simple greeting message to ensure the server is running.
- Parameters: None
- Response:
<p>Hello, World!</p>
- URL:
/vector-search - Method:
GET - Description: Performs a vector search on the MongoDB collection.
- Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
query |
string | Yes | The query to search for. |
projection |
JSON string | No | Custom projection for the MongoDB query. |
docs_num |
integer | No | Number of documents to return (default: 3). |
- Response: JSON array of documents matching the search query.
- URL:
/rag - Method:
GET - Description: Performs a RAG operation to retrieve documents and generate responses based on the query.
- Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
query |
string | Yes | The query to search for. |
projection |
JSON string | No | Custom projection for the MongoDB query. |
docs_num |
integer | No | Number of documents to return (default: 3). |
- Response: JSON object containing the RAG-generated response.
- URL:
/sq-vector-search - Method:
GET - Description: Performs a self-querying vector search, which automatically generates filters based on the query.
- Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
query |
string | Yes | The query to search for. |
projection |
JSON string | No | Custom projection for the MongoDB query. |
docs_num |
integer | No | Number of documents to return (default: 3). |
- Response: JSON array of documents matching the search query with auto-generated filters.
- URL:
/sq-rag - Method:
GET - Description: Performs a self-querying RAG operation to retrieve documents and generate responses based on the query with auto-generated filters.
- Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
query |
string | Yes | The query to search for. |
projection |
JSON string | No | Custom projection for the MongoDB query. |
docs_num |
integer | No | Number of documents to return (default: 3). |
- Response: JSON object containing the self-querying RAG-generated response.
To use the endpoints, send HTTP GET requests with the appropriate parameters to the Flask server. For example, to perform a vector search, use the following curl command:
curl -G \
--data-urlencode "query=your_search_query" \
--data-urlencode "projection={\"field\": 1}" \
--data-urlencode "docs_num=5" \
http://localhost:5000/vector-searchMake sure to adjust the parameters as needed.
- Grzegorz Malisz: Author.
