mirror of
https://github.com/zebrajr/localGPT.git
synced 2025-12-06 12:20:53 +01:00
54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
import os
|
|
import csv
|
|
from datetime import datetime
|
|
from constants import EMBEDDING_MODEL_NAME
|
|
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
|
from langchain.embeddings import HuggingFaceBgeEmbeddings
|
|
from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
|
|
|
def log_to_csv(question, answer):
|
|
|
|
log_dir, log_file = "local_chat_history", "qa_log.csv"
|
|
# Ensure log directory exists, create if not
|
|
if not os.path.exists(log_dir):
|
|
os.makedirs(log_dir)
|
|
|
|
# Construct the full file path
|
|
log_path = os.path.join(log_dir, log_file)
|
|
|
|
# Check if file exists, if not create and write headers
|
|
if not os.path.isfile(log_path):
|
|
with open(log_path, mode="w", newline="", encoding="utf-8") as file:
|
|
writer = csv.writer(file)
|
|
writer.writerow(["timestamp", "question", "answer"])
|
|
|
|
# Append the log entry
|
|
with open(log_path, mode="a", newline="", encoding="utf-8") as file:
|
|
writer = csv.writer(file)
|
|
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
writer.writerow([timestamp, question, answer])
|
|
|
|
|
|
def get_embeddings(device_type="cuda"):
|
|
if "instructor" in EMBEDDING_MODEL_NAME:
|
|
return HuggingFaceInstructEmbeddings(
|
|
model_name=EMBEDDING_MODEL_NAME,
|
|
model_kwargs={"device": device_type},
|
|
embed_instruction="Represent the document for retrieval:",
|
|
query_instruction="Represent the question for retrieving supporting documents:",
|
|
)
|
|
|
|
elif "bge" in EMBEDDING_MODEL_NAME:
|
|
return HuggingFaceBgeEmbeddings(
|
|
model_name=EMBEDDING_MODEL_NAME,
|
|
model_kwargs={"device": device_type},
|
|
query_instruction="Represent this sentence for searching relevant passages:",
|
|
)
|
|
|
|
else:
|
|
return HuggingFaceEmbeddings(
|
|
model_name=EMBEDDING_MODEL_NAME,
|
|
model_kwargs={"device": device_type},
|
|
)
|