-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
217 lines (197 loc) · 8.24 KB
/
run.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import argparse
from time import sleep
from typing import List, Dict, Union
from rich.console import Console
from rich.live import Live
from rich.spinner import Spinner
from config.config import *
from react.prompt import ReAct_Prompt, ReAct_Answer_Prompt
from react.schemas import Functions
from actions.search import SearchKnowledgeBase
from react.completion import NormalCompletion, TokenPadding, StreamCompletion
from react.func_call import FunctionCall
parser = argparse.ArgumentParser(description="Run the ReAct bot with custom settings.")
parser.add_argument(
"--pinecone_index_name",
default=None,
help="Name of the Pinecone index. Default is None.",
)
parser.add_argument(
"--pinecone_namespace",
default=None,
help="Name of the Pinecone namespace. Default is None.",
)
parser.add_argument(
"--model_name",
default="gpt-4",
help="Name of the model to be used. Default is 'gpt-4'.",
)
args = parser.parse_args()
def clear_terminal():
os.system("cls" if os.name == "nt" else "clear")
model_name = args.model_name
token_padding = TokenPadding(model_name)
complete = NormalCompletion(OPENAI_API_KEY)
stream_complete = StreamCompletion(OPENAI_API_KEY)
fc = FunctionCall(OPENAI_API_KEY)
search = SearchKnowledgeBase(
PINECONE_API_KEY,
PINECONE_ENV,
args.pinecone_index_name,
args.pinecone_namespace,
complete,
token_padding,
model=model_name,
)
console = Console()
def log_function(function_name: str, function_arguments: Union[Dict, str]):
mapping = {
"Thought": lambda function_arguments: console.print(
f'[bold green]Thought:[/bold green] [italic green]{function_arguments.get("thought_text")}[/italic green]'
),
"Action": lambda function_arguments: console.print(
f'[bold magenta]Action: {function_arguments.get("action_type")}[/bold magenta] => [underline]Search Text:[/underline] [italic magenta]{function_arguments.get("search_text")}[/italic magenta]'
),
"Answer": lambda function_arguments: console.print(
f"[bold blue]Answer: [/bold blue] [italic blue]{function_arguments}[/italic blue]"
),
"Observation": lambda function_arguments: console.print(
f"[bold cyan]Observation: [/bold cyan] [italic cyan]{function_arguments.get('observation_text')}[/italic cyan]"
),
"FinalAnswer": lambda function_arguments: console.print(
f"[bold green]Final Answer: [/bold green] [italic green]{function_arguments.get('reached_final_answer')}[/italic green]"
)
if function_arguments.get("reached_final_answer")
else console.print(
f"[bold red]Final Answer: [/bold red] [italic red]{function_arguments.get('reached_final_answer')}[/italic red]"
),
"Exit": lambda function_arguments: console.print(
f"[bold red]Exit: [/bold red] [red]{function_arguments.get('exit')}[/red]"
),
}
mapping[function_name](function_arguments)
def reAct(question: str):
history = """"""
history_calls = []
def agent(
question: Union[str, None],
function_name: Union[str, None],
function_arguments: Union[Dict, None],
history: str = """""",
false_counts: int = 0,
):
if false_counts >= 4:
return history, history_calls
if question:
history += f"Question: `{question}`"
with Live(
Spinner("dots", text="Thinking..."),
refresh_per_second=10,
transient=True,
):
fn, fa = fc(history, ReAct_Prompt, Functions.functions, model_name)
history_calls.append({"fn": fn, "fa": fa})
# print(f"""Function: {fn} | Arguments: {fa}""")
log_function(fn, fa)
# print(f"History: {history}")
return agent(None, fn, fa, history)
else:
if function_name == "Action":
search_text = function_arguments.get("search_text")
with Live(
Spinner("dots", text="Searching..."),
refresh_per_second=10,
transient=True,
):
answer = search(search_text)
history += f"\nSearchKB with search_text '{search_text}'"
history += f"\nSearch Result: {answer}"
# print("Search Result: ", answer)
# print(f"History: {history}")
log_function("Answer", answer)
with Live(
Spinner("dots", text="Thinking..."),
refresh_per_second=10,
transient=True,
):
fn, fa = fc(history, ReAct_Prompt, Functions.functions, model_name)
# print(f"""Function: {fn} | Arguments: {fa}""")
history_calls.append({"fn": fn, "fa": fa})
log_function(fn, fa)
return agent(None, fn, fa, history)
elif function_name == "Thought":
history += f"\nThought: {function_arguments.get('thought_text')}"
# print(f"History: {history}")
with Live(
Spinner("dots", text="Thinking..."),
refresh_per_second=10,
transient=True,
):
fn, fa = fc(history, ReAct_Prompt, Functions.functions, model_name)
# print(f"""Function: {fn} | Arguments: {fa}""")
history_calls.append({"fn": fn, "fa": fa})
log_function(fn, fa)
return agent(None, fn, fa, history)
elif function_name == "Observation":
history += f"""\nObservation: {function_arguments.get('observation_text')}
"""
# print(f"History: {history}")
with Live(
Spinner("dots", text="Thinking..."),
refresh_per_second=10,
transient=True,
):
fn, fa = fc(history, ReAct_Prompt, Functions.functions, model_name)
# print(f"""Function: {fn} | Arguments: {fa}""")
history_calls.append({"fn": fn, "fa": fa})
log_function(fn, fa)
return agent(None, fn, fa, history)
elif function_name == "Exit":
return history, history_calls
elif function_name == "FinalAnswer":
history += f"""\nFinal Answer: {function_arguments.get('reached_final_answer')}
"""
# print(f"History: {history}")
if function_arguments.get("reached_final_answer"):
print(f"Inside Final Answer Returning")
return history, history_calls
else:
false_counts += 1
if false_counts > 4:
return history, history_calls
else:
with Live(
Spinner("dots", text="Thinking..."),
refresh_per_second=10,
transient=True,
):
fn, fa = fc(
history,
ReAct_Prompt,
Functions.functions,
model_name,
)
history_calls.append({"fn": fn, "fa": fa})
log_function(fn, fa)
return agent(None, fn, fa, history, false_counts)
else:
print(f"ERROR: Called unavailable function. Terminating Agent!")
return history, history_calls
history, history_calls = agent(question, None, None, history)
message = f"""Question: `{question}` History: ```{history}```
"""
final_answer = ""
for msg in stream_complete(message, ReAct_Answer_Prompt, model_name):
final_answer += msg
clear_terminal()
print(final_answer, end="", flush=True)
sleep(0.1)
if __name__ == "__main__":
while True:
question = input("\nEnter your question: ")
question = question.rstrip().lstrip()
if question:
reAct(question)
else:
pass