-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserv.py
More file actions
59 lines (47 loc) · 1.8 KB
/
serv.py
File metadata and controls
59 lines (47 loc) · 1.8 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
import os
from dotenv import load_dotenv
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from vdb import search_q
from langchain_openai import OpenAI
load_dotenv()
os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
model = OpenAI()
parser = StrOutputParser()
template = """You are an educational assistant
and your job is to answer the question asked by the user based on the context provided.
This is the question: {question}
This is the context: {context}
"""
context_check_template="""You are a bot and your job is to verify if answer provided by me is relvant and correct in respect to the question or not.
Return yes if you think the answer is correct and no if you think the answer is incorrect.
this is the question {question}
this is the context {context}
NOTE- Only respond with yes or no.
"""
prompt = ChatPromptTemplate.from_template(template)
def execut(query):
print("Executing query...")
chain = prompt | model | parser
data = search_q(query, "notes")
print("Context retrieved:", data)
out = chain.invoke({"question": query, "context": data})
validity= answer_ver(query, out)
response=out
if not validity:
response=bail()
return response
def answer_ver(query, context):
print("Verifying answer...")
prompt2 = ChatPromptTemplate.from_template(context_check_template)
chain = prompt2 | model | parser
response = chain.invoke({"question": query, "context": context})
if response == "yes":
return True
return False
def bail():
return "Sorry, I cannot help you with that question at this time. If you have any other questions, feel free to ask."
if __name__ == "__main__":
query = "how can you solve coin changing"
output = execut(query)
print("Response:", output)