-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_plan.py
72 lines (59 loc) · 3.52 KB
/
request_plan.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
69
70
71
72
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import JsonOutputParser
#####################################################################
# Implement the Request Planer
def get_request_plan(llm, question):
prompt = PromptTemplate(
template="""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are an expert in the WEN-OKN knowledge system, which answers
one question or returns data for one entity type at a time. For
example, you can return dams or earthquakes. Your task is to extract
a list of atomic requests from the user's question based on the entity
types requested to fetch. Each atomic request must be executable
independently of the others.
Example 1:
Original Question: "First find Scioto River and all dams on it."
Atomic Requests:
"Find Scioto River."
"Find all dams on Scioto River."
Example 2:
Original Question: "First find Scioto River, then find all dams
on this river. Also find all counties these dams locate."
Atomic Requests:
"Find Scioto River."
"Find all dams on Scioto River."
"Find all counties where dams on Scioto River are located."
Example 3:
Original Question: "Find all counties Scioto River flows through."
Atomic Request:
"Find all counties Scioto River flows through."
Example 4:
Original Question: "Find all dams on Scioto River."
Atomic Request:
"Find all dams on Scioto River."
Example 5:
Original Question: "Find all counties both Scioto River and Ohio River flow through."
Atomic Request:
"Find all counties both Scioto River and Ohio River flow through."
Example 6:
Original Question: "Find all dams located upstream of power station dpjc6wtthc32 along the Muskingum River."
Atomic Request:
"Find all dams located upstream of power station dpjc6wtthc32 along the Muskingum River."
Task
Divide the user's question into atomic requests based on the entity types
mentioned. Each atomic request should be in its original form from the
question and should not contain any pronouns like "it" or "its" or "this" or
"these" or "those" to ensure independent handling.
Input
User Question:
{question}
Output
Return your answer in JSON format with a list of atomic requests under the key
"requests" without preamble or explanation.
<|eot_id|><|start_header_id|>assistant<|end_header_id|>
""",
input_variables=["question"],
)
question_planer = prompt | llm | JsonOutputParser()
result = question_planer.invoke({"question": question})
return result