forked from QuangBK/localLLM_guidance
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagent_react.py
104 lines (84 loc) · 4 KB
/
agent_react.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# import guidance
valid_answers = ['Action', 'Final Answer']
valid_tools = ['Google Search']
prompt_start_template = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
Answer the following questions as best you can. You have access to the following tools:
Google Search: A wrapper around Google Search. Useful for when you need to answer questions about current events. The input is the question to search relavant information.
Strictly use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Google Search]
Action Input: the input to the action, should be a question.
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
For examples:
Question: How old is CEO of Microsoft wife?
Thought: First, I need to find who is the CEO of Microsoft.
Action: Google Search
Action Input: Who is the CEO of Microsoft?
Observation: Satya Nadella is the CEO of Microsoft.
Thought: Now, I should find out Satya Nadella's wife.
Action: Google Search
Action Input: Who is Satya Nadella's wife?
Observation: Satya Nadella's wife's name is Anupama Nadella.
Thought: Then, I need to check Anupama Nadella's age.
Action: Google Search
Action Input: How old is Anupama Nadella?
Observation: Anupama Nadella's age is 50.
Thought: I now know the final answer.
Final Answer: Anupama Nadella is 50 years old.
### Input:
{{question}}
### Response:
Question: {{question}}
Thought: {{gen 't1' stop='\\n'}}
{{select 'answer' logprobs='logprobs' options=valid_answers}}: """
prompt_mid_template = """{{history}}{{select 'tool_name' options=valid_tools}}
Action Input: {{gen 'actInput' stop='\\n'}}
Observation: {{do_tool tool_name actInput}}
Thought: {{gen 'thought' stop='\\n'}}
{{select 'answer' logprobs='logprobs' options=valid_answers}}: """
prompt_final_template = """{{history}}{{select 'tool_name' options=valid_tools}}
Action Input: {{gen 'actInput' stop='\\n'}}
Observation: {{do_tool tool_name actInput}}
Thought: {{gen 'thought' stop='\\n'}}
{{select 'answer' options=valid_answers}}: {{gen 'fn' stop='\\n'}}"""
class CustomAgentGuidanceReAct:
def __init__(self, guidance, tools, num_iter=3):
self.guidance = guidance
self.tools = tools
self.num_iter = num_iter
def do_tool(self, tool_name, actInput):
return self.tools[tool_name](actInput)
def __call__(self, query):
prompt_start = self.guidance(prompt_start_template)
result_start = prompt_start(question=query, valid_answers=valid_answers)
result_mid = result_start
for _ in range(self.num_iter - 1):
if result_mid['answer'] == 'Final Answer':
break
history = result_mid.__str__()
prompt_mid = self.guidance(prompt_mid_template)
result_mid = prompt_mid(history=history, do_tool=self.do_tool, valid_answers=valid_answers, valid_tools=valid_tools)
if result_mid['answer'] != 'Final Answer':
history = result_mid.__str__()
prompt_mid = self.guidance(prompt_final_template)
result_final = prompt_mid(history=history, do_tool=self.do_tool, valid_answers=['Final Answer'], valid_tools=valid_tools)
else:
history = result_mid.__str__()
prompt_mid = self.guidance(history + "{{gen 'fn' stop='\\n'}}")
result_final = prompt_mid()
return result_final
class StandardPrompt:
def __init__(self, guidance, tools, num_iter=3):
self.guidance = guidance
self.tools = tools
self.num_iter = num_iter
def __call__(self, query):
task = self.guidance(query)
final_response = task()
history = final_response.__str__()
return history, final_response