-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_data.py
More file actions
61 lines (46 loc) · 1.7 KB
/
Copy pathquery_data.py
File metadata and controls
61 lines (46 loc) · 1.7 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
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 detect_intent(query_text):
"""Use GPT to detect user intent."""
model = ChatOpenAI()
prompt = f"""
Classify the user's intent based on this query:
Query: {query_text}
Possible intents:
- Gratitude
- Greeting
- Question about manuals
- Other
Return only the intent.
"""
return model.predict(prompt).strip()
def query_data(query_text):
intent = detect_intent(query_text)
if intent == "Greeting":
return "Hello! How can I assist you today?"
elif intent == "Gratitude":
return "You're welcome!"
#db hazirlama
embedding_function = OpenAIEmbeddings()
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function)
#3 en iyi sonuc
results = db.similarity_search_with_relevance_scores(query_text, k=3)
if len(results) == 0 or results[0][1] < 0.7:
return "I couldn’t find any relevant information. Could you please rephrase or provide more details?"
#prompt hazirlama
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)
#cevap
model = ChatOpenAI()
response_text = model.predict(prompt)
return response_text