git clone git@github.com:dawkrish/potpie-test.git
cd potpie-test
python3 -m venv venv
pip install -r requirements.txt
- Python (>=3.10) because we use pattern-matching
- Flask for REST API
- ChromaDB as vector DB to store the embeddings (we have done persistently)
- PyPDF2 to get text from pdf files
- python-docx to get text from docx files
- dotenv to load environment variables
- HuggingFace Embeddings (to embed text and later used to make vectorstore)
- Langchain ties up everything
- llm used is ChatMistralAI, currently I have used all the tokens :"
-
User uploads the file.
-
User creats an asset_id with the end point
POST /api/document/process- text is extracted out for different extension types using
get_file_text(embeddings.py) save_embeddings(embeddings.py) is called to save the embedding.- creates a uuid and assigns to
asset_id get_huggingface_embeddings(embeddings.py) is used for embeddings (we have a default-embedder tooget_default_embeddings)chroma_collection.addto save the embedding- returns
asset_id
- creates a uuid and assigns to
- the end point returns
asset_id
- text is extracted out for different extension types using
-
User creates a "thread_id" with the end point
POST /api/chat/start- This end point takes the
asset_idfrom the previous end point - It returns a
thread_id. - The thread
thread_to_asset_mapexists to keep mappings, its a in-memory global variable - Now after getting the thread_id user can start chatting.
- This end point takes the
-
End point
POST /api/chat/messageis used for to chat.user_messagethe user prompt/query andthread_idwhich we got from last end point is needed- we get response from
response_to_message(rag.py) - we use mistral-ai for now, it can be changed
- The response will be in stream; Streaming can be turned off.
- at this end point, the "chat-message" gets stored in the variable
chat_history, another in-memory global variable - The chats are non-persistent. We can integrate sqlite3 to save the chats.
-
End point
GET /api/chat/history/<thread_id>returns the specific chat.
Generate from Mistral-AI
This API provides endpoints for processing documents and managing chat sessions. Below are the available endpoints and their usage.
Endpoint: /api/document/process
Method: POST
Description: Processes a document and saves its embeddings.
Request Body:
{
"file_path": "path/to/your/file"
}Response:
- Success (200):
{ "asset_id": "generated_asset_id" } - Error (400):
{ "error": "file_path is required" }
Endpoint: /api/chat/start
Method: POST
Description: Starts a new chat session based on a given asset ID.
Request Body:
{
"asset_id": "your_asset_id"
}Response:
- Success (200):
{ "thread_id": "generated_thread_id" } - Error (400):
{ "error": "asset_id is required" } - Error (404):
{ "error": "Invalid asset_id, no embeddings exist for it" }
Endpoint: /api/chat/message
Method: POST
Description: Sends a user message and receives an AI response.
Request Body:
{
"thread_id": "your_thread_id",
"user_message": "your_message"
}Response:
- Success (200):
{ "ai_response": "generated_ai_response" } - Error (400):
{ "error": "thread_id is required" } - Error (400):
{ "error": "user_message is required" } - Error (404):
{ "error": "Invalid chat-thread id, create a new one" }
Endpoint: /api/chat/history/<thread_id>
Method: GET
Description: Retrieves the chat history for a given thread ID.
Response:
- Success (200):
{ "chat_history": [ { "timestamp": "2023-10-01T12:00:00Z", "user_message": "your_message", "ai_response": "generated_ai_response" } ] } - Error (400):
{ "error": "thread_id does not exist in memory" }
- Better handling of chat-history, possibly using
lang-graph - Options to chose LLM
- Options for streaming
- Making code more flexible
- Python files documentation using Mistral-AI model Codestral (it is awesome.)