-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_data.py
More file actions
46 lines (34 loc) · 1.52 KB
/
Copy pathquery_data.py
File metadata and controls
46 lines (34 loc) · 1.52 KB
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
import argparse
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain.prompts import ChatPromptTemplate
CHROMA_PATH = "chroma"
PROMPT_TEMPLATE = """
Answer the question based only on the following context:
{context}
---
Answer the question based on the above context: {question}
"""
def main(query_text):
embedding_function = OpenAIEmbeddings()
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
# Search the database
results = db.similarity_search_with_relevance_scores(query_text, k=3)
if len(results) == 0 or results[0][1] < 0.5:
return "No relevant results found.", []
# Extract context and format the query
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
prompt = prompt_template.format(context=context_text, question=query_text)
# Use ChatOpenAI to get a response
model = ChatOpenAI()
response = model.invoke(prompt)
# Extract response content
response_content = response.content if hasattr(response, 'content') else response
# Extract sources
sources = []
for doc, _score in results:
source = doc.metadata.get("source", "Unknown").replace("data\\", "").replace("data/", "")
page = doc.metadata.get("page_number", "Unknown")
sources.append(f"{source}, Page: {page}")
return response_content, sources