-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_data.py
68 lines (49 loc) · 2.11 KB
/
query_data.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from langchain_chroma import Chroma
from langchain_huggingface import HuggingFaceEmbeddings
from groq import Groq
from langchain.prompts import ChatPromptTemplate
CHROMA_PATH = "chroma"
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
PROMPT_TEMPLATE = """
Answer the question based only on the following context:
{context}
---
Answer the question based on the above context: {question}
"""
def main():
if GROQ_API_KEY is None:
raise ValueError("GROQ_API_KEY environment variable is not set")
# Initialize the Groq client
client = Groq(api_key=GROQ_API_KEY)
# Initialize HuggingFaceEmbeddings
hf_embeddings = HuggingFaceEmbeddings(model_name="distilbert-base-uncased")
# Initialize Chroma with the embedding function
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=hf_embeddings)
print(f"Database loaded. Number of documents: {db._collection.count()}")
print("Welcome to the chat interface! Ask me a question:")
while True:
query_text = input("> ")
if query_text.lower() == "exit":
break
# Search the DB
try:
results = db.similarity_search_with_relevance_scores(query_text, k=3)
if len(results) == 0:
print("No relevant results found in the database.")
else:
print(f"Found {len(results)} relevant results.")
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
print(f"Context for the query:\n{context_text}")
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
prompt = prompt_template.format(context=context_text, question=query_text)
print(f"Prompt sent to Groq:\n{prompt}")
response = client.chat.completions.create(
model="llama3-70b-8192",
messages=[{"role": "user", "content": prompt}]
)
print(f"Response: {response.choices[0].message.content}")
except Exception as e:
print(f"An error occurred during querying: {e}")
if __name__ == "__main__":
main()