-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.py
385 lines (349 loc) · 15.2 KB
/
script.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import os
import sys
import time
import gradio as gr
import json
from dotenv import load_dotenv
import chromadb
from chromadb.config import Settings
from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.base import LLM
from langchain.agents import load_tools
from langchain.tools import Tool
from modules import chat, shared
from modules.text_generation import generate_reply
from modules.ui import gather_interface_values, list_interface_input_elements
from modules.utils import gradio
load_dotenv()
CTX_MAX = int(os.getenv("CTX_MAX"))
VERBOSE = "true" in os.getenv("VERBOSE").lower()
MAX_TASKS_DEFAULT = int(os.getenv("MAX_TASKS_DEFAULT"))
RECURSION_DEPTH_DEFAULT = int(os.getenv("RECURSION_DEPTH_DEFAULT"))
DISTANCE_CUTOFF_DEFAULT = float(os.getenv("DISTANCE_CUTOFF_DEFAULT"))
EXPANDED_CONTEXT_DEFAULT = "true" in os.getenv("EXPANDED_CONTEXT_DEFAULT").lower()
SEARX_HOST = os.getenv("SEARX_HOST")
TOP_K_WIKI = int(os.getenv("TOP_K_WIKI"))
WOLFRAM_APP_ID = os.getenv("WOLFRAM_APP_ID")
HUMAN_PREFIX = os.getenv("HUMAN_PREFIX")
ASSISTANT_PREFIX = os.getenv("ASSISTANT_PREFIX")
ASSESS_ABILITY_DIRECTIVE = os.getenv("ASSESS_ABILITY_DIRECTIVE")
DO_OBJECTIVE_DIRECTIVE = os.getenv("DO_OBJECTIVE_DIRECTIVE")
SPLIT_OBJECTIVE_DIRECTIVE = os.getenv("SPLIT_OBJECTIVE_DIRECTIVE")
ASSESS_TOOL_DIRECTIVE = os.getenv("ASSESS_TOOL_DIRECTIVE")
USE_TOOL_DIRECTIVE = os.getenv("USE_TOOL_DIRECTIVE")
GENERATE_THOUGHTS_DIRECTIVE = os.getenv("GENERATE_THOUGHTS_DIRECTIVE")
PRIMARY_DIRECTIVE = os.getenv("PRIMARY_DIRECTIVE")
SUMMARIZE_DIRECTIVE = os.getenv("SUMMARIZE_DIRECTIVE")
AgentOobaVars = {
"verbose" : VERBOSE,
"max-context" : CTX_MAX,
"waiting-input" : False,
"recursion-max" : RECURSION_DEPTH_DEFAULT,
"max-tasks" : MAX_TASKS_DEFAULT,
"max-summaries" : 5,
"expanded-context" : EXPANDED_CONTEXT_DEFAULT,
"chroma-cutoff" : DISTANCE_CUTOFF_DEFAULT,
"processed-task-storage" : None,
"main-objective": None,
"tools" : {},
"directives" : {
"Primary directive" : PRIMARY_DIRECTIVE,
"Assess ability directive" : ASSESS_ABILITY_DIRECTIVE,
"Do objective directive" : DO_OBJECTIVE_DIRECTIVE,
"Split objective directive" : SPLIT_OBJECTIVE_DIRECTIVE,
"Assess tool directive" : ASSESS_TOOL_DIRECTIVE,
"Use tool directive" : USE_TOOL_DIRECTIVE,
"Generate thoughts directive" : GENERATE_THOUGHTS_DIRECTIVE,
"Summarize directive" : SUMMARIZE_DIRECTIVE
},
"human-prefix" : HUMAN_PREFIX,
"assistant-prefix" : ASSISTANT_PREFIX
}
params = {
"display_name" : "AgentOoba",
"is_tab" : True
}
def ooba_call(prompt, state):
stops = [AgentOobaVars["human-prefix"], '</s>']
generator = generate_reply(prompt, state, stopping_strings=stops)
answer = ''
for a in generator:
if isinstance(a, str):
answer = a
else:
answer = a[0]
for stop in stops:
if stop in answer:
answer = answer[:answer.find(stop)]
if VERBOSE:
print(f"-----------------------INPUT-----------------------\n{prompt}\n", file=sys.stderr)
print(f"----------------------OUTPUT-----------------------\n{answer}\n", file=sys.stderr)
return answer
from extensions.AgentOoba.objective import Objective
class ChromaTaskStorage:
def __init__(self):
self.client = chromadb.Client(Settings(anonymized_telemetry=False))
self.collection = self.client.create_collection(name="processed-tasks")
def add_tasks(self, tasks, task_ids):
self.collection.add(
documents = tasks,
ids = task_ids
)
def task_exists(self, task):
results = self.collection.query(
query_texts=[task],
n_results=1
)
if len(results["distances"][0]) == 0:
return False
return results["distances"][0][0] < AgentOobaVars["chroma-cutoff"]
# Tools can be (hopefully, not all tested) any from https://python.langchain.com/en/latest/modules/agents/tools/getting_started.html
KNOWN_TOOLS = ["wikipedia", "searx-search", "requests_get", "requests_post"]
# Define Custom tool descriptions here. The keys here must match tool.name
CUSTOM_TOOL_DESCRIPTIONS_EXAMPLE = {
"Wikipedia" : "A collection of articles on various topics. Used when the task at hand is researching or acquiring general surface-level information about any topic. Input is a topic; the tool will then save general information about the topic to memory.",
"Searx Search" : "A URL search engine. Used when the task at hand is searching the internet for websites that mention a certain topic. Input is a search query; the tool will then save URLs for popular websites that reference the search query to memory.",
"Wolfram Alpha" : "A multipuporse calculator and information search engine. Used for mathematical computations and looking up specific numeric information. Input is a query or directive to calculate an expression; the tool will then save the expression and the result of the evaluation of that expression to memory.\nExample: Input - 'derivative of x^2' Output - 'derivative of x^2 is 2x'"
}
CUSTOM_TOOL_DESCRIPTIONS = {}
Tools = load_tools(
KNOWN_TOOLS,
searx_host=SEARX_HOST,
top_k_results=TOP_K_WIKI,
wolfram_alpha_appid=WOLFRAM_APP_ID
)
def setup_tools():
for tool in Tools:
AgentOobaVars["tools"][tool.name] = {}
AgentOobaVars["tools"][tool.name]["active"] = False
AgentOobaVars["tools"][tool.name]["execute"] = False
if tool.name in CUSTOM_TOOL_DESCRIPTIONS:
AgentOobaVars["tools"][tool.name]["desc"] = CUSTOM_TOOL_DESCRIPTIONS[tool.name]
else:
AgentOobaVars["tools"][tool.name]["desc"] = tool.description
AgentOobaVars["tools"][tool.name]["tool"] = tool
def update_tool_state(tool_name, statetype, value):
AgentOobaVars["tools"][tool_name][statetype] = value
def update_tool_description(tool_name, value):
AgentOobaVars["tools"][tool_name]['desc'] = value
def mainloop(ostr, state):
AgentOobaVars["processed-task-storage"] = ChromaTaskStorage()
AgentOobaVars["processed-task-storage"].add_tasks([ostr],["MAIN OBJECTIVE"])
yield f"<br>Thinking...<br>"
AgentOobaVars["main-objective"] = Objective(ostr, -1, 1, state)
while (not AgentOobaVars["main-objective"].done):
yield f'<div class="oobaAgentOutput"><br>{AgentOobaVars["main-objective"].to_string(True)}<br>Thinking...</div>'
AgentOobaVars["main-objective"].process_current_task()
if AgentOobaVars["waiting-input"]:
yield f'<div class="oobaAgentOutput"><br>{AgentOobaVars["main-objective"].to_string(True)}<br>Waiting for user input</div>'
# Give GPU a second to breathe :)
time.sleep(2)
while AgentOobaVars["waiting-input"]:
time.sleep(0.1)
yield f'<div class="oobaAgentOutput"><br>{AgentOobaVars["main-objective"].to_string(False)}<br>Done!</div>'
def gather_agentooba_parameters(
recursion_level,
distance_cutoff,
max_tasks,
expanded_context,
h_prefix,
a_prefix,
primary_directive,
assess_ability,
do_objective,
split_objective,
assess_tool,
use_tool,
gen_thoughts,
summarize
):
AgentOobaVars["recursion-max"] = recursion_level
AgentOobaVars["chroma-cutoff"] = distance_cutoff
AgentOobaVars["max-tasks"] = max_tasks
AgentOobaVars["expanded-context"] = expanded_context
AgentOobaVars["directives"]["Primary directive"] = primary_directive
AgentOobaVars["directives"]["Assess ability directive"] = assess_ability
AgentOobaVars["directives"]["Do objective directive"] = do_objective
AgentOobaVars["directives"]["Split objective directive"] = split_objective
AgentOobaVars["directives"]["Assess tool directive"] = assess_tool
AgentOobaVars["directives"]["Use tool directive"] = use_tool
AgentOobaVars["directives"]["Generate thoughts directive"] = gen_thoughts
AgentOobaVars["directives"]["Summarize directive"] = summarize
AgentOobaVars["human-prefix"] = h_prefix
AgentOobaVars["assistant-prefix"] = a_prefix
def ui():
state = gr.State({})
with gr.Column(elem_classes="oobaAgentBase"):
with gr.Accordion(label="Output"):
output = gr.HTML(label="Output", value="")
user_input = gr.Textbox(label="Goal for AgentOoba")
with gr.Row():
submit_button = gr.Button("Execute", variant="primary")
cancel_button = gr.Button("Cancel")
with gr.Accordion(label="Options", open=False):
with gr.Column():
recursion_level_slider = gr.Slider(
label="Recursion Depth",
minimum=1,
maximum=7,
step=1,
value=RECURSION_DEPTH_DEFAULT,
interactive=True
)
distance_cutoff_slider = gr.Slider(
label = "Task Similarity Cutoff (Higher = less repeat tasks, but might accidentally drop tasks)",
minimum = 0,
maximum = 1,
step = 0.01,
value = DISTANCE_CUTOFF_DEFAULT
)
max_tasks_slider = gr.Slider(
label="Max tasks in a list",
minimum=3,
maximum=12,
step=1,
value=MAX_TASKS_DEFAULT,
interactive=True
)
expanded_context_toggle = gr.Checkbox(label="Expanded Context (runs out of memory at high recursion)", value = EXPANDED_CONTEXT_DEFAULT)
with gr.Accordion(label="Tools", open = False):
setup_tools()
for tool_name in AgentOobaVars["tools"]:
with gr.Row():
cb_active = gr.Checkbox(label=tool_name, value=False, interactive=True)
cb_active.change(lambda x, tn=tool_name, statetype="active" : update_tool_state(tn, statetype, x), [cb_active])
cb_execute = gr.Checkbox(label="Execute", value=False, interactive=True)
cb_execute.change(lambda x, tn=tool_name, statetype="execute": update_tool_state(tn, statetype, x), [cb_execute])
textbox = gr.Textbox(
label="Tool description (as passed to the model)",
interactive=True,
value=AgentOobaVars["tools"][tool_name]["desc"]
)
textbox.change(lambda x, tn=tool_name: update_tool_description(tn, x), [textbox])
with gr.Accordion(label="Prompting", open = False):
with gr.Row():
human_prefix_input = gr.Textbox(label="Human prefix", value = HUMAN_PREFIX)
assistant_prefix_input = gr.Textbox(label="Assistant prefix", value = ASSISTANT_PREFIX)
human_prefix_def = gr.Textbox(visible=False, value = HUMAN_PREFIX)
assistant_prefix_def = gr.Textbox(visible=False, value = ASSISTANT_PREFIX)
directive_inputs = []
directive_defaults = []
for directive_name, directive in AgentOobaVars["directives"].items():
directive_inputs.append(gr.TextArea(label=directive_name, value = directive))
directive_defaults.append(gr.Textbox(visible=False, value = directive))
prompt_inputs = directive_inputs + [human_prefix_input, assistant_prefix_input]
prompt_defaults = directive_defaults + [human_prefix_def, assistant_prefix_def]
reset_prompts_button = gr.Button("Reset prompts to default")
with gr.Row():
export_prompts_button = gr.Button("Export prompts to JSON")
import_prompts_button = gr.Button("Import prompts from JSON")
with gr.Row():
exported_prompts = gr.File(interactive = False)
imported_prompts = gr.File(interactive = True, type="binary")
submit_event_1 = submit_button.click(
gather_interface_values,
inputs=gradio(list_interface_input_elements()),
outputs=state
).then(
gather_agentooba_parameters,
inputs=[
recursion_level_slider,
distance_cutoff_slider,
max_tasks_slider,
expanded_context_toggle,
human_prefix_input,
assistant_prefix_input
]+directive_inputs, outputs=None
).then(
mainloop, inputs=[user_input, state], outputs=output
)
submit_event_2 = user_input.submit(
gather_interface_values,
inputs=gradio(list_interface_input_elements()),
outputs=state
).then(
gather_agentooba_parameters,
inputs=[
recursion_level_slider,
distance_cutoff_slider,
max_tasks_slider,
expanded_context_toggle,
human_prefix_input,
assistant_prefix_input
]+directive_inputs, outputs=None
).then(
mainloop, inputs=[user_input, state], outputs=output
)
def cancel_agent():
AgentOobaVars["main-objective"].done = True
output.value = ""
cancel_event = cancel_button.click(
cancel_agent,
None,
None,
cancels = [submit_event_1, submit_event_2]
)
reset_event = reset_prompts_button.click(
lambda a,b,c,d,e,f,g,h,i,j: [a,b,c,d,e,f,g,h,i,j],
inputs = prompt_defaults,
outputs = prompt_inputs
)
def make_prompt_template():
d = AgentOobaVars["directives"].copy()
d["human-prefix"] = AgentOobaVars["human-prefix"]
d["assistant-prefix"] = AgentOobaVars["assistant-prefix"]
with open("extensions/AgentOoba/prompt_template.json", "w") as f:
f.write(json.dumps(d))
f.flush()
return "extensions/AgentOoba/prompt_template.json"
def import_prompt_template(template):
if not template:
return [p.value for p in prompt_inputs] + [human_prefix_input.value, assistant_prefix_input.value]
d = json.loads(template)
return [
d["Primary directive"],
d["Assess ability directive"],
d["Do objective directive"],
d["Split objective directive"],
d["Assess tool directive"],
d["Use tool directive"],
d["Generate thoughts directive"],
d["Summarize directive"],
d["human-prefix"],
d["assistant-prefix"]
]
export_event = export_prompts_button.click(
make_prompt_template,
inputs = None,
outputs = [exported_prompts]
)
import_event = import_prompts_button.click(
import_prompt_template,
inputs = imported_prompts,
outputs = prompt_inputs
)
def custom_css():
css = """
.oobaAgentBase {
margin: auto;
margin-left: auto;
margin-right: auto;
width: 50%;
max-width: 50%;
font-size: 1rem;
}
.oobaAgentOutput {
font-size: 1rem;
list-style-type: circle;
}
.oobaAgentOutputThinking {
font-size: 1rem;
border-left: 2px solid orange;
list-style-type: circle;
}
.oobaAgentOutputResource {
font-size: 1rem;
list-style-type: square;
}
"""
return css.strip()