testbot_v4 / db_operations.py
soyleyicicem's picture
Update db_operations.py
b4004d8 verified
import datetime
from qdrant_client import QdrantClient, models
from langchain_qdrant import Qdrant
class DatabaseOperations:
def __init__(self):
pass
def save_user_history(client, collection_name, question, answer, embeddings, point_id, user_id, session_id):
vector = embeddings.embed_documents([question])[0]
client.upsert(
collection_name=collection_name,
points=[
models.PointStruct(
id=point_id,
payload={"user_id": user_id,
"session_id": session_id,
"create_date": datetime.datetime.now().isoformat(),
"question": question,
"answer": answer},
vector=vector,
)
],
)
def save_user_history_demo(client, collection_name, question, answer, embeddings, point_id, manual, feedback):
vector = embeddings.embed_documents([question])[0]
client.upsert(
collection_name=collection_name,
points=[
models.PointStruct(
id=point_id,
payload={"manual": manual,
"create_date": datetime.datetime.now().isoformat(),
"question": question,
"answer": answer,
"feedback": feedback},
vector=vector,
)
],
)
def question_history_search(client, collection_name, car_id, question, embeddings, threshold=0.9):
CAR_ID = car_id
vector = embeddings.embed_documents([question])[0]
search_result = client.search(collection_name=collection_name,
query_vector=vector,
query_filter=models.Filter(
must=[
models.FieldCondition(key="car_id", match=models.MatchValue(value=CAR_ID)),
models.FieldCondition(key="source_name", match=models.MatchValue(value="User Question"))
]
),
score_threshold=threshold,
limit=1)
return search_result
def user_history_scroll(client, collection_name, user_id, key="user_id"):
history = client.scroll(collection_name=collection_name,
scroll_filter=models.Filter(
must=[
models.FieldCondition(key=key, match=models.MatchValue(value=user_id))
]
))
return history
def save_question_history(client, collection_name, question, answer, embeddings,
point_id, car_id, source_name, model_year):
vector = embeddings.embed_documents([question])[0]
client.upsert(
collection_name=collection_name,
points=[
models.PointStruct(
id=point_id,
payload={"source_name": source_name,
"page_content": question,
"car_id": car_id,
"model_year": model_year,
"create_date": datetime.datetime.now().isoformat(),
"answer": answer},
vector=vector,
)
],
)