Skip to content

Commit

Permalink
Merge pull request #29 from kamyabnazari/kn-trying-deployment
Browse files Browse the repository at this point in the history
Adding deployed functions
  • Loading branch information
kamyabnazari authored Jul 3, 2023
2 parents a45e6b5 + 8bd4d65 commit bc92dba
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Create a `.env` file in the root of the backend directory with the following var

```
OPENAI_API_KEY={your openai api key}
QDRANT_API_KEY={your qdrant api key}
POCKETBASE_ADMIN_EMAIL={your pocketbase admin email}
POCKETBASE_ADMIN_PASSWORD={your pocketbase admin password}
PUBLIC_POCKETBASE_URL=http://localhost:8090
PUBLIC_FRONTEND_URL=http://localhost:5173
PUBLIC_QDRANT_URL=http://localhost:6333
QDRANT__SERVICE_API_KEY={Only for Deployed Qdrant}
```

### Running the backend
Expand Down
9 changes: 8 additions & 1 deletion backend/chat_bot_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ def make_chain(documentId:str):

embeddings = OpenAIEmbeddings()

client = QdrantClient(os.getenv('PUBLIC_QDRANT_URL'))
url = os.getenv('PUBLIC_QDRANT_URL')
api_key = os.getenv('QDRANT__SERVICE_API_KEY')

if api_key:
client = QdrantClient(url=url, prefer_grpc=True, api_key=api_key)
else:
client = QdrantClient(url=url)

qdrant = Qdrant(client, documentId, embeddings)

return ConversationalRetrievalChain.from_llm(
Expand Down
35 changes: 23 additions & 12 deletions backend/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,27 @@ def create_embeddings_from_pdf_file(file_path: str, documentId: str):

# Step 3 + 4: Generate embeddings and store them in DB
embeddings = OpenAIEmbeddings()

url = os.getenv('PUBLIC_QDRANT_URL')
api_key = os.getenv('QDRANT__SERVICE_API_KEY')

qdrant = Qdrant.from_documents(
document_chunks,
embedding=embeddings,
url=url,
prefer_grpc=True,
api_key=api_key,
collection_name=documentId,
timeout=120
)
api_key = os.getenv('QDRANT__SERVICE_API_KEY')

if api_key:
Qdrant.from_documents(
document_chunks,
embedding=embeddings,
url=url,
prefer_grpc=True,
api_key=api_key,
collection_name=documentId,
timeout=120
)
else:
Qdrant.from_documents(
document_chunks,
embedding=embeddings,
url=url,
prefer_grpc=False,
collection_name=documentId,
timeout=120
)

15 changes: 15 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pdfkit
import io
import os
from qdrant_client import QdrantClient
import requests

# Python package for PDF parsing
Expand Down Expand Up @@ -80,6 +81,20 @@ async def read_favicon():
async def read_api_root():
return {"message": "Welcome to the EE API!"}

@app.post("/api/documents/{document_id}/delete_vector_file")
async def delete_api_vector_file(document_id: str):

url = os.getenv('PUBLIC_QDRANT_URL')
api_key = os.getenv('QDRANT__SERVICE_API_KEY')

if api_key:
client = QdrantClient(url=url, prefer_grpc=True, api_key=api_key)
else:
client = QdrantClient(url=url)

client.delete_collection(document_id)
return {"message": "Vector file deleted!"}

@app.post("/api/documents/{document_id}/send_new_message/{user_id}")
async def read_api_documents_send_new_message(document_id: str, user_id: str, request: Request):
# Accessing Request body and converting it to a dictionary
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/components/FileTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
try {
await pb.collection('documents').delete(documentID);
await axios({
url: `${env.PUBLIC_QDRANT_URL}/collections/${documentID}`,
method: 'delete',
url: `${env.PUBLIC_BACKEND_URL}/api/documents/${documentID}/delete_vector_file`,
method: 'post',
headers: { 'Content-Type': 'application/json' }
});
documentList = documentList.filter((document) => document.id !== documentID);
Expand Down
14 changes: 9 additions & 5 deletions frontend/src/lib/components/PDFViewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
let isLoading = true;
let pdfjsWorkerPromise = import('pdfjs-dist/build/pdf.worker.entry').then((worker) => {
let pdfjsWorker = URL.createObjectURL(new Blob([worker], { type: 'application/javascript' }));
GlobalWorkerOptions.workerSrc = pdfjsWorker;
return pdfjsWorker;
});
let pdfjsWorkerPromise = import('pdfjs-dist/build/pdf.worker.entry')
.then((worker) => {
let pdfjsWorker = URL.createObjectURL(new Blob([worker], { type: 'application/javascript' }));
GlobalWorkerOptions.workerSrc = pdfjsWorker;
return pdfjsWorker;
})
.catch((error) => {
console.error("Error importing 'pdfjs-dist/build/pdf.worker.entry':", error);
});
afterUpdate(() => {
previousDocumentUrl = generatedDocumentURL;
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/routes/(auth)/profile/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export const actions: Actions = {
documentList.forEach(async (document: Record) => {
// delete all user embeddings from Qdrant
await axios({
url: `${env.PUBLIC_QDRANT_URL}/collections/${document.id}`,
method: 'delete',
url: `${env.PUBLIC_BACKEND_URL}/api/documents/${document.id}/delete_vector_file`,
method: 'post',
headers: { 'Content-Type': 'application/json' },
httpAgent: new http.Agent({ family: 4 }), // Force IPv4
httpsAgent: new https.Agent({ family: 4 }) // Force IPv4
Expand Down

0 comments on commit bc92dba

Please sign in to comment.