-
Notifications
You must be signed in to change notification settings - Fork 44
/
agent_components.py
1160 lines (930 loc) · 35.7 KB
/
agent_components.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import abc
from collections import deque
import re
from typing import NamedTuple
import json
from dotenv import load_dotenv
import numpy as np
import os
import openai
from openai import OpenAI
import requests
import sqlite3
import subprocess
import time
from xai_components.base import InArg, OutArg, InCompArg, Component, xai_component
load_dotenv()
DEFAULT_EXECUTOR_PROMPT = """
You are an AI who performs one task based on the following objective: {objective}.
Take into account these previously completed tasks: {context}
*Your thoughts*: {scratch_pad}
*Your task*: {task}
*Your tools*: {tools}
You can use a tool by writing TOOL: TOOL_NAME in a single line. then the arguments of the tool (if any) For example, to use the python-exec tool, write
TOOL: python-exec
```
print('Hello world!')
```
Response:
"""
DEFAULT_CRITIC_PROMPT = """
You are an AI who checks and improves that the action about to be performed is correct given the information you have.
If it is the optimal solution you should respond with the action as-is.
The task should help achieve the following objective: {objective}.
Take into account these previously completed tasks: {context}
The task: {task}
The action: {action}
Response:
"""
DEFAULT_TASK_CREATOR_PROMPT = """
You are an task creation AI that uses the result of an execution agent
to create new tasks with the following objective: {objective},
The last completed task has the result: {result}.
This result was based on this task description: {task_name}.
These are incomplete tasks: {task_list}.
Based on the result, create new tasks to be completed by the AI system that do not overlap with incomplete tasks.
Return the tasks as an array.
"""
DEFAULT_TASK_PRIORITIZER_PROMPT = """
You are a task prioritization AI tasked with cleaning the formatting of and reprioritizing the following tasks:
{task_names}.
Consider the ultimate objective of your team:{objective}. Do not remove any tasks.
Return the result as a numbered list, like:
#. First task
#. Second task
Start the task list with number {next_task_id}.
"""
class Memory(abc.ABC):
def query(self, query: str, n: int) -> list:
pass
def add(self, id: str, text: str, metadata: dict) -> None:
pass
def run_tool(tool_code: str, tools: list) -> str:
if tool_code is None:
return ""
ret = ""
for tool in tools:
if tool_code.startswith(tool["name"]):
if tool["instance"]:
ret += tool["instance"].run_tool(tool_code)
#tool["instance"] = None
return ret
def llm_call(model: str, prompt: str, temperature: float = 0.5, max_tokens: int = 500):
#print("**** LLM_CALL ****")
#print(prompt)
while True:
try:
if model == 'gpt-3.5-turbo' or model == 'gpt-4o-mini':
client = OpenAI()
messages = [{"role": "system", "content": prompt}]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
n=1,
stop=["OUTPUT", ],
)
return response.choices[0].message.content.strip()
elif model.startswith("rwkv"):
# Use proxy.
if not openai.proxies: raise Exception("No proxy set")
messages = [{"role": "system", "content": prompt}]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
n=1,
stop=["OUTPUT", ],
)
return response.choices[0].message.content.strip()
elif model.startswith("llama"):
# Spawn a subprocess to run llama.cpp
cmd = ["llama/main", "-p", prompt]
result = subprocess.run(cmd, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.PIPE, text=True)
return result.stdout.strip()
else:
raise Exception(f"Unknown model {model}")
except openai.RateLimitError:
print("Rate limit error, sleeping for 10 seconds...")
time.sleep(10)
except openai.APIError:
print("Service unavailable error, sleeping for 10 seconds...")
time.sleep(10)
else:
break
def get_sorted_context(memory: Memory, query: str, n: int):
results = memory.query(query, n)
sorted_results = sorted(
results,
key=lambda x: x.similarity if getattr(x, 'similarity', None) else x.score,
reverse=True
)
return [(str(item.attributes['task']) + ":" + str(item.attributes['result'])) for item in sorted_results]
def extract_task_number(task_id, task_list):
if isinstance(task_id, int):
return task_id
matches = re.findall(r'\d+', task_id)
if matches:
return int(matches[0])
else:
# fallback if we match nothing.
return len(task_list) + 1
@xai_component
class TaskCreatorAgent(Component):
"""Creates new tasks based on given model, prompt, and objectives.
#### inPorts:
- objective: Objective for task creation.
- prompt: Prompt string for the AI model.
- model: AI model used for task creation.
- result: Result of the previous tasks.
- task: Current task information.
- task_list: List of all tasks.
#### outPorts:
- new_tasks: list of newly created tasks.
"""
objective: InCompArg[str]
prompt: InArg[str]
model: InArg[str]
result: InArg[str]
task: InArg[dict]
task_list: InArg[str]
new_tasks: OutArg[list]
def execute(self, ctx) -> None:
text = self.prompt.value if self.prompt.value is not None else DEFAULT_TASK_CREATOR_PROMPT
prompt = text.format(**{
"objective": self.objective.value,
"result": self.result.value,
"task": self.task.value,
"task_name": self.task.value["task_name"],
"task_list": self.task_list.value
})
response = llm_call(self.model.value, prompt)
new_tasks = response.split('\n')
print("New tasks: ", new_tasks)
task_id = self.task.value["task_id"]
task_id_counter = extract_task_number(task_id, self.task_list)
ret = []
for task_name in new_tasks:
task_id_counter += 1
ret.append({"task_id": task_id_counter, "task_name": task_name})
self.new_tasks.value = ret
@xai_component
class TaskPrioritizerAgent(Component):
"""Prioritizes tasks based on given model, prompt, and objectives.
#### inPorts:
- objective: Objective for task prioritization.
- prompt: Prompt string for the AI model.
- model: AI model used for task prioritization.
- task_list: List of all tasks.
#### outPorts:
- prioritized_tasks: Prioritized list of tasks.
"""
objective: InCompArg[str]
prompt: InArg[str]
model: InArg[str]
task_list: InArg[list]
prioritized_tasks: OutArg[deque]
def execute(self, ctx) -> None:
text = self.prompt.value if self.prompt.value is not None else DEFAULT_TASK_PRIORITIZER_PROMPT
prompt = text.format(**{
"objective": self.objective.value,
"task_list": self.task_list.value,
"task_names": [t["task_name"] for t in self.task_list.value],
"next_task_id": max([int(t["task_id"]) for t in self.task_list.value]) + 1
})
response = llm_call(self.model.value, prompt)
new_tasks = response.split('\n')
task_list = deque()
for task_string in new_tasks:
task_parts = task_string.strip().split(".", 1)
if len(task_parts) == 2:
task_id = task_parts[0].strip()
task_name = task_parts[1].strip()
task_list.append({"task_id": task_id, "task_name": task_name})
print(f"New tasks: {new_tasks}")
self.prioritized_tasks.value = task_list
@xai_component
class TaskExecutorAgent(Component):
"""Executes tasks based on given model, prompt, tools, and memory.
#### inPorts:
- objective: Objective for task execution.
- prompt: Prompt string for the AI model.
- model: AI model used for task execution.
- tasks: Queue of tasks to be executed.
- tools: List of tools available for task execution.
- memory: Memory context for task execution.
#### outPorts:
- action: Executed action.
- task: Task information.
"""
objective: InCompArg[str]
prompt: InArg[str]
model: InArg[str]
tasks: InArg[deque]
tools: InArg[list]
memory: InArg[any]
action: OutArg[str]
task: OutArg[dict]
def execute(self, ctx) -> None:
text = self.prompt.value if self.prompt.value is not None else DEFAULT_EXECUTOR_PROMPT
task = self.tasks.value.popleft()
print(f"Next Task: {task}")
context = get_sorted_context(self.memory.value, query=self.objective.value, n=5)
print("\n*******RELEVANT CONTEXT******\n")
print(context)
scratch_pad = ""
for tool in self.tools.value:
if tool['name'] == 'scratch-pad':
file_name = tool['instance'].file_name.value
with open(file_name, "r") as f:
scratch_pad += f.read()
print("\n*******SCRATCH PAD******\n")
print(scratch_pad)
prompt = text.format(**{
"scratch_pad": scratch_pad,
"objective": self.objective.value,
"context": context,
"task": self.task.value,
"tools": [tool['spec'] for tool in self.tools.value]
})
result = llm_call(self.model.value, prompt, 0.7, 2000)
print(f"Result:\n{result}")
self.action.value = result
self.task.value = task
@xai_component
class TaskCriticAgent(Component):
"""Critiques an executed task's action using an AI model.
#### inPorts:
- prompt: The base string that the AI model uses to critique the task action.
- objective: The overall objective that should guide task critique.
- model: The AI model that generates the critique.
- memory: The current context memory, used for retrieving relevant information for task critique.
- tools: The list of tools available for task critique.
- action: The executed action that is to be critiqued.
- task: The current task information.
#### outPorts:
- updated_action: The updated action after the model's critique.
"""
prompt: InArg[str]
objective: InArg[str]
model: InArg[str]
memory: InArg[any]
tools: InArg[list]
action: InArg[str]
task: InArg[dict]
updated_action: OutArg[str]
def execute(self, ctx) -> None:
text = self.prompt.value if self.prompt.value is not None else DEFAULT_CRITIC_PROMPT
print(f"Task: {self.task.value}")
context = get_sorted_context(self.memory.value, query=self.objective.value, n=5)
print("Context: ", context)
prompt = text.format(**{
"objective": self.objective.value,
"context": context,
"action": self.action.value,
"task": self.task.value
})
new_action = llm_call(self.model.value, prompt, 0.7, 2000)
print(f"New action: {new_action}")
# If the model responds without a new TOOL prompt use the original.
if "TOOL" not in new_action:
new_action = self.action.value
self.updated_action.value = new_action
@xai_component
class ToolRunner(Component):
"""Executes a tool based on the given action.
#### inPorts:
- action: The action that determines which tool should be executed.
- memory: The current context memory, used for updating the result of tool execution.
- task: The current task information.
- tools: The list of tools available for execution.
#### outPorts:
- result: The result after running the tool.
"""
action: InArg[str]
memory: InCompArg[Memory]
task: InArg[dict]
tools: InArg[list]
result: OutArg[str]
def execute(self, ctx) -> None:
tools = self.action.value.split("TOOL: ")
result = self.action.value + "\n"
for tool in tools:
result += run_tool(tool, self.tools.value.copy())
task = self.task.value
self.memory.value.add(
f"result_{task['task_id']}",
result,
{
"task_id": task['task_id'],
"task": task['task_name'],
"result": result
}
)
self.result.value = result
@xai_component
class CreateTaskList(Component):
"""Component that creates an task list based on `initial_task`.
If no initial task provided, the first task would be "Develop a task list".
#### inPorts:
- initial_task: The first task to be added to the task list.
#### outPorts:
- task_list: The created task list with the initial task.
"""
initial_task: InArg[str]
task_list: OutArg[deque]
def execute(self, ctx) -> None:
task_list = deque([])
# Add the first task
first_task = {
"task_id": 1,
"task_name": self.initial_task.value if self.initial_task.value else "Develop a task list"
}
task_list.append(first_task)
self.task_list.value = task_list
TOOL_SPEC_SQLITE = """
Perform SQL queries against an sqlite database.
Use by writing the SQL within markdown code blocks.
Example: TOOL: sqlite
```
CREATE TABLE IF NOT EXISTS points (x int, y int);
INSERT INTO points (x, y) VALUES (783, 848);
SELECT * FROM points;
```
sqlite OUTPUT:
[(783, 848)]
"""
@xai_component
class SqliteTool(Component):
"""Component that performs SQL queries against an SQLite database.
#### inPorts:
- path: The path to the SQLite database.
#### outPorts:
- tool_spec: The specification of the SQLite tool, including its capabilities and requirements.
"""
path: InArg[str]
tool_spec: OutArg[dict]
def execute(self, ctx) -> None:
if not 'tools' in ctx:
ctx['tools'] = {}
spec = {
'name': 'sqlite',
'spec': TOOL_SPEC_SQLITE,
'instance': self
}
self.tool_spec.value = spec
def run_tool(self, tool_code) -> str:
print(f"Running tool sqlite")
lines = tool_code.splitlines()
code = []
include = False
any = False
for line in lines[1:]:
if "```" in line and include == False:
include = True
any = True
continue
elif "```" in line and include == True:
include = False
continue
elif "TOOL: sql" in line:
continue
elif "OUTPUT" in line:
break
elif include:
code.append(line + "\n")
if not any:
for line in lines[1:]:
code.append(line + "\n")
conn = sqlite3.connect(self.path.value)
all_queries = "\n".join(code)
queries = all_queries.split(";")
res = ""
try:
for query in queries:
res += str(conn.execute(query).fetchall())
res += "\n"
conn.commit()
except Exception as e:
res += str(e)
return res
TOOL_SPEC_BROWSER = """
Shows the user which step to perform in a browser and outputs the resulting HTML. Use by writing the commands within markdown code blocks. Do not assume that elements are on the page, use the tool to discover the correct selectors. Perform only the action related to the task. You cannot define variables with the browser tool. only write_file(filename, selector)
Example: TOOL: browser
```
goto("http://google.com")
fill('[title="search"]', 'my search query')
click('input[value="Google Search"]')
```
browser OUTPUT:
<html ....>
"""
@xai_component
class BrowserTool(Component):
"""A component that implements a browser tool.
Uses the Playwright library to interact with the browser.
Capable of saving screenshots and writing to files directly from the browser context.
#### inPorts:
- cdp_address: The address to the Chrome DevTools Protocol (CDP), allowing interaction with a Chrome instance.
#### outPorts:
- tool_spec: The specification of the browser tool.
"""
cdp_address: InArg[str]
tool_spec: OutArg[dict]
def execute(self, ctx) -> None:
if not 'tools' in ctx:
ctx['tools'] = {}
spec = {
'name': 'browser',
'spec': TOOL_SPEC_BROWSER,
'instance': self
}
self.chrome = None
self.playwright = None
self.page = None
self.tool_spec.value = spec
def run_tool(self, tool_code) -> str:
print(f"Running tool browser")
lines = tool_code.splitlines()
code = []
include = False
any = False
for line in lines[1:]:
if "```" in line and include == False:
include = True
any = True
continue
elif "```" in line and include == True:
include = False
continue
elif "TOOL: browser" in line:
continue
elif "OUTPUT" in line:
break
elif include:
code.append(line + "\n")
if not any:
for line in lines[1:]:
code.append(line + "\n")
res = ""
try:
import playwright
from playwright.sync_api import sync_playwright
if not self.chrome:
self.playwright = sync_playwright().__enter__()
self.chrome = self.playwright.chromium.connect_over_cdp(self.cdp_address.value)
if not self.page:
if len(self.chrome.contexts) > 0:
self.page = self.chrome.contexts[0].new_page()
self.page.set_default_timeout(3000)
else:
self.page = self.chrome.new_context().new_page()
self.page.set_default_timeout(3000)
self.page.save_screenshot = self.page.screenshot
def write_file(file, selector):
with open(file, "w") as f:
f.write(self.page.inner_text(selector))
self.page.write_file = write_file
self.page.save_text = write_file
self.page.save_to_file = write_file
for action in code:
if not action.startswith("#"):
eval("self.page." + action)
res += self.page.content()[:4000]
#browser.close()
except Exception as e:
res += str(e)
print("*** PAGE CONTENT ***")
print(res)
return res
TOOL_SPEC_NLP = """
NLP tool provides methods to summarize, extract, classify, ner or translate informtaion on the current page.
To use use one of the words above followed by any arguments and finally a CSS selector.
TOOL: NLP, summarize div[id="foo"]
NLP OUTPUT:
Summary appears here.
"""
@xai_component
class NlpTool(Component):
"""Natural Language Processing (NLP) tool. Perform NLP operations within the browser context.
Enables the extraction of webpage content and further NLP analysis via a language model,
in this case, gpt-3.5-turbo.
#### inPorts:
- cdp_address: The address to the Chrome DevTools Protocol (CDP).
#### outPorts:
- tool_spec: The specification of the NLP tool.
"""
cdp_address: InArg[str]
tool_spec: OutArg[dict]
def execute(self, ctx) -> None:
if not 'tools' in ctx:
ctx['tools'] = {}
spec = {
'name': 'browser',
'spec': TOOL_SPEC_BROWSER,
'instance': self
}
self.chrome = None
self.playwright = None
self.page = None
self.tool_spec.value = spec
def run_tool(self, tool_code) -> str:
print(f"Running tool browser")
lines = tool_code.splitlines()
code = []
include = False
any = False
for line in lines[1:]:
if "```" in line and include == False:
include = True
any = True
continue
elif "```" in line and include == True:
include = False
continue
elif "TOOL: browser" in line:
continue
elif "OUTPUT" in line:
break
elif include:
code.append(line + "\n")
if not any:
for line in lines[1:]:
code.append(line + "\n")
res = ""
try:
import playwright
from playwright.sync_api import sync_playwright
if not self.chrome:
self.playwright = sync_playwright().__enter__()
self.chrome = self.playwright.chromium.connect_over_cdp(self.cdp_address.value)
if not self.page:
if len(self.chrome.contexts) > 0:
self.page = self.chrome.contexts[0].pages[0]
self.page.set_default_timeout(3000)
else:
self.page = self.chrome.new_context().new_page()
self.page.set_default_timeout(3000)
for action in code:
if not action.startswith("#"):
content = self.page.inner_text(action.split(" ")[-1])
prompt = action + "\n" + action.split(" ")[-1] + " is: \n---\n"
res += action + "OUTPUT:\n"
res += llm_call("gpt-3.5-turbo", prompt, 0.0, 100)
res += "\n"
except Exception as e:
res += str(e)
print("*** PAGE CONTENT ***")
print(res)
return res
TOOL_SPEC_PYTHON = """
Execute python code in a virtual environment.
Use by writing the code within markdown code blocks.
Automate the browser with playwright.
The environment has the following pip libraries installed: {packages}
Example: TOOL: python-exec
```
import pyautogui
pyautogui.PAUSE = 1.0 # Minimum recommended
print(pyautogui.position())
```
python-exec OUTPUT:
STDOUT:Point(x=783, y=848)
STDERR:
"""
@xai_component
class ExecutePythonTool(Component):
"""
Executes Python code and pip operations that are supplied as a string.
It extracts the Python code and pip commands, runs them, and returns their output or errors.
#### inPorts:
- path: The path to the python sciprt.
#### outPorts:
- tool_spec: The specification of the Python tool, including its capabilities and requirements.
"""
file_name: InArg[str]
tool_spec: OutArg[dict]
def execute(self, ctx) -> None:
spec = {
'name': 'python-exec',
'spec': TOOL_SPEC_PYTHON,
'instance': self
}
self.tool_spec.value = spec
def run_tool(self, tool_code) -> str:
print(f"Running tool python-exec")
lines = tool_code.splitlines()
code = []
pip_operations = []
include = False
any = False
for line in lines[1:]:
if "```" in line and include == False:
include = True
any = True
continue
elif "```" in line and include == True:
include = False
continue
elif "!pip" in line:
pip_operations.append(line.replace("!pip", "pip"))
continue
elif include:
code.append(line + "\n")
if not any:
for line in lines[1:]:
code.append(line + "\n")
print(f"Will run pip operations: {pip_operations}")
tool_code = '\n'.join(code)
print(f"Will run the code: {tool_code}")
try:
for pip_operation in pip_operations:
result = subprocess.run(pip_operation, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True, cwd=os.getcwd())
print(f"pip operation {pip_operation} returned: {result}")
with open(self.file_name.value, "w") as f:
f.writelines(code)
result = subprocess.run(["python", self.file_name.value], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True, cwd=os.getcwd())
output = "python-exec OUTPUT:\nSTDOUT: \n" + result.stdout + "\n" + "STDERR:" + "\n" + result.stderr
except Exception as e:
print(f"Exception running tool python-exec: {e}")
output = str(e)
print(f"Done running tool python-exec: Returned {output}")
return output
TOOL_SPEC_PROMPT_USER = """
Prompt the user for input with this tool.
Example: TOOL: prompt-user
Hello would you like to play a game?
prompt-user OUTPUT:
Yes I would.
"""
@xai_component
class PromptUserTool(Component):
"""A component that enables interaction with a user by prompting for inputs.
Prints a prompt message to the user and waits for input.
The user's response is then returned by the function.
**Note**: If you use this component, run the compiled script from a terminal.
#### outPorts:
- tool_spec: The specification of the PromptUser tool.
"""
tool_spec: OutArg[dict]
def execute(self, ctx) -> None:
spec = {
'name': 'prompt-user',
'spec': TOOL_SPEC_PROMPT_USER,
'instance': self
}
self.tool_spec.value = spec
def run_tool(self, tool_code) -> str:
print("PROMPTING USER:")
print(f"{tool_code}")
res = input(">")
return res
TOOL_SPEC_SCRATCH_PAD = """
Your internal monologue. Written to yourself in second-person. Write out any notes that should help you with the progress of your task.
Example: TOOL: scratch-pad
Thoughts go here.
"""
@xai_component
class ScratchPadTool(Component):
"""A component that creates and manages a 'scratch pad' for storing and summarizing information within the xai framework.
The component is initialized with a file name to use as the scratch pad. During execution,
it writes to this file and provides a method `run_tool` that updates the contents of the file and
generates a summary of the current contents using the gpt-3.5-turbo language model.
#### inPorts:
- file_name: The name of the file that will be used as the scratch pad.
#### outPorts:
- tool_spec: The specification of the ScratchPad tool.
"""
file_name: InArg[str]
tool_spec: OutArg[dict]
def execute(self, ctx) -> None:
spec = {
'name': 'scratch-pad',
'spec': TOOL_SPEC_SCRATCH_PAD,
'instance': self
}
with open(self.file_name.value, "w") as f:
f.write("")
self.tool_spec.value = spec
def run_tool(self, tool_code) -> str:
current_scratch = ""
with open(self.file_name.value, "r") as f:
current_scratch = f.read().strip()
summary = None
if len(current_scratch) > 0:
summary = llm_call(
"gpt-3.5-turbo",
f"Summarize the following text with bullet points using a second person perspective. " +
"Keep only the salient points.\n---\n {current_scratch}",
0.0,
1000
)
with open(self.file_name.value, "w") as f:
if summary:
f.write(summary)
f.write("\n")
f.write(tool_code[len('scratch-pad'):])
return ""
class VectoMemoryImpl(Memory):
def __init__(self, vs):
self.vs = vs
def query(self, query: str, n: int) -> list:
return self.vs.lookup(query, 'TEXT', n).results
def add(self, id: str, text: str, metadata: dict) -> None:
from vecto import vecto_toolbelt
vecto_toolbelt.ingest_text(self.vs, [text], [metadata])
def get_ada_embedding(text):
s = text.replace("\n", " ")
return openai.embeddings.create(input=[s], model="text-embedding-ada-002").data[0].embedding
class PineconeMemoryImpl(Memory):
def __init__(self, index, namespace):
self.index = index
self.namespace = namespace
def query(self, query: str, n: int) -> list:
return self.index.query(get_ada_embedding(query), top_k=n, include_metadata=True, namespace=self.namespace)
def add(self, vector_id: str, text: str, metadata: dict) -> None:
self.index.upsert([(vector_id, get_ada_embedding(text), metadata)], namespace=self.namespace)
class NumpyQueryResult(NamedTuple):
id: str
similarity: float
attributes: dict
class NumpyMemoryImpl(Memory):
def __init__(self, vectors=None, ids=None, metadata=None):
self.vectors = vectors
self.ids = ids
self.metadata = metadata
def query(self, query: str, n: int) -> list:
if self.vectors is None:
return []
if isinstance(self.vectors, list) and len(self.vectors) > 1:
self.vectors = np.vstack(self.vectors)
top_k = min(self.vectors.shape[0], n)
query_vector = get_ada_embedding(query)
similarities = self.vectors @ query_vector
indices = np.argpartition(similarities, -top_k)[-top_k:]
return [
NumpyQueryResult(
self.ids[i],
similarities[i],
self.metadata[i]
)
for i in indices
]
def add(self, vector_id: str, text: str, metadata: dict) -> None:
if isinstance(self.vectors, list) and len(self.vectors) > 1:
self.vectors = np.vstack(self.vectors)
if self.vectors is None:
self.vectors = np.array(get_ada_embedding(text)).reshape((1, -1))
self.ids = [vector_id]
self.metadata = [metadata]
else:
self.ids.append(vector_id)
self.vectors = np.vstack([self.vectors, np.array(get_ada_embedding(text))])
self.metadata.append(metadata)
@xai_component
class NumpyMemory(Component):
memory: OutArg[Memory]
def execute(self, ctx) -> None:
self.memory.value = NumpyMemoryImpl()
@xai_component
class VectoMemory(Component):
api_key: InArg[str]
vector_space: InCompArg[str]
initialize: InCompArg[bool]
memory: OutArg[Memory]
def execute(self, ctx) -> None:
from vecto import Vecto
api_key = os.getenv("VECTO_API_KEY") if self.api_key.value is None else self.api_key.value
headers = {'Authorization': 'Bearer ' + api_key}
response = requests.get("https://api.vecto.ai/api/v0/account/space", headers=headers)
if response.status_code != 200:
raise Exception(f"Failed to get vector space list: {response.text}")
for space in response.json():
if space['name'] == self.vector_space.value:
vs = Vecto(api_key, space['id'])
if self.initialize.value:
vs.delete_vector_space_entries()
self.memory.value = VectoMemoryImpl(vs)
break
if not self.memory.value:
raise Exception(f"Could not find vector space with name {self.vector_space.value}")