Spaces:
Sleeping
Sleeping
Update vector_handler.py
Browse files- vector_handler.py +24 -10
vector_handler.py
CHANGED
|
@@ -6,6 +6,8 @@ from langchain_pinecone import PineconeVectorStore
|
|
| 6 |
from langchain_core.documents import Document
|
| 7 |
from config import PINECONE_API_KEY
|
| 8 |
from chat_utils import get_chain
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Initialize Pinecone client
|
| 11 |
pc = Pinecone(api_key=PINECONE_API_KEY)
|
|
@@ -42,18 +44,30 @@ def create_vector_store(session_id, texts):
|
|
| 42 |
# Query vector store
|
| 43 |
def query_vector_store(session_id, question):
|
| 44 |
index_name = session_id
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
# Delete vector store with optional delay
|
| 59 |
def delete_vector_store(index_name, delay=0):
|
|
|
|
| 6 |
from langchain_core.documents import Document
|
| 7 |
from config import PINECONE_API_KEY
|
| 8 |
from chat_utils import get_chain
|
| 9 |
+
from fastapi import HTTPException
|
| 10 |
+
from pinecone.openapi_support.exceptions import NotFoundException
|
| 11 |
|
| 12 |
# Initialize Pinecone client
|
| 13 |
pc = Pinecone(api_key=PINECONE_API_KEY)
|
|
|
|
| 44 |
# Query vector store
|
| 45 |
def query_vector_store(session_id, question):
|
| 46 |
index_name = session_id
|
| 47 |
+
try:
|
| 48 |
+
index = pc.Index(index_name)
|
| 49 |
|
| 50 |
+
vectorstore = PineconeVectorStore(index=index, embedding=embedding_model)
|
| 51 |
+
retriever = vectorstore.as_retriever(
|
| 52 |
+
search_type="similarity_score_threshold",
|
| 53 |
+
search_kwargs={"k": 3, "score_threshold": 0.5},
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
docs = retriever.invoke(question)
|
| 57 |
+
chain = get_chain()
|
| 58 |
+
result = chain({"input_documents": docs, "question": question}, return_only_outputs=True)
|
| 59 |
+
return result["output_text"]
|
| 60 |
+
|
| 61 |
+
except NotFoundException as e:
|
| 62 |
+
raise HTTPException(
|
| 63 |
+
status_code=404,
|
| 64 |
+
detail=f"Session '{session_id}' has expired or does not exist."
|
| 65 |
+
)
|
| 66 |
+
except Exception as e:
|
| 67 |
+
raise HTTPException(
|
| 68 |
+
status_code=500,
|
| 69 |
+
detail=f"An unexpected error occurred: {str(e)}"
|
| 70 |
+
)
|
| 71 |
|
| 72 |
# Delete vector store with optional delay
|
| 73 |
def delete_vector_store(index_name, delay=0):
|