-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
utils.py
53 lines (43 loc) · 1.88 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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},
)