- chat histories are managed in a MongoDB collection
- user ids and conversation ids are stored in the "SessionId" field (with value: {user_id}%{conversation_id})
- messages are stored in the "history" field
- Azure Authentication realized with the package FastAPI-Azure-Auth
- The project provides API for general chat conversations (
base
) and for a RAG use case (rag
) considering some example documents
- Requirements
- Docker
- Azure Account
- Python 3.12 or greater; Pip 24.2 or greater
- Create a MongoDB cloud account on the website: https://www.mongodb.com/
- Create a cluster on your MongoDB account
- Create a database within the created cluster
- Create two collections within the created database
- A collection for managing chat history (e.g.
chat_histories
) - A collection for storing embedding documents for the rag use case (e.g.
vector_collection
)
- A collection for managing chat history (e.g.
- Create for the collection
vector_collection
an Atlas Search Index (e.g.vsearch_index
) with the following properties{ "fields": [ { "numDimensions": 1536, "path": "embedding", "similarity": "cosine", "type": "vector" }, { "path": "source", "type": "filter" } ] }
- Extract the MongoDB connection string for your created cluster
- Navigate to Overview page of your created cluster and click on the
Connect
button - On the popup window select Python driver as option and extract the connection string with the structure
mongodb+srv://<mongodb_username>:<mongodb_password>@<mongodb_cluster>
- where
mongodb_cluster
has the structure: <cluster_name>.<additional_str>.mongodb.net
- Navigate to Overview page of your created cluster and click on the
- Create a
.env
file in root directory of the project and copy the contents from the.env.template
file - Replace in the file the variables for Azure OpenAI, Azure App Registration, and MongoDB setup
- Create a Python virtual environment
or use anaconda
virtualenv path/to/venv/langserve_env
conda create --name langserve_env python=3.12
- Activate the virtual environment
or for anaconda
source path/to/venv/langserve_env/bin/activate
conda activate langserve_env
- Install packages
pip install -r requirements.txt
Alternative using Docker:
-
Nativate to the root directory of the project in your terminal and build the docker image with the command
docker build -t langservegpt .
-
On your terminal (with the activated Python virtual environment) execute
jupyter notebook
on your terminal and open theembeddings/mongodb_atlas.ipynb
notebook file -
Execute all cells of the notebook from top to down to insert the example documents into the vector collection
-
Check on your MongoDB cloud account whether the documents are inserted correctly
-
If project setup was realized with virtualenv or conda, execute the following command on your terminal (with activated Python virtual environment and on the root directory of the project):
python app.py
If project setup was realized with docker, execute the following command on your terminal:
docker run -p 8000:8000 langservegpt
-
You can access now the Swagger UI with http://localhost:8000/docs on your browser
- Before trying out any APIs, it is necessary to authorize youself on the Swagger UI by clicking on the
Authorize
button on the top-right corner of the page and to login into your Microsoft Azure Account - After login successfully into your Microsof Azure Account on the Swagger UI, the
/set-cookie/
GET API (available under thedefault
section) needs to be executed with an example user ID to set theuser_id
cookie on your browser - Now the APIs under the
base
andrag
sections can be executed (e.g./base/invoke
/base/stream
,/rag/invoke
,/rag/stream
)- For the
base
andrag
APIs it it necessary to set theconversation_id
value in the request body (settinguser_id
is not necessary since it was already set in theuser_id
cookie).
- For the