forked from agiresearch/AIOS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagent_repl.py
More file actions
148 lines (111 loc) · 3.8 KB
/
Copy pathagent_repl.py
File metadata and controls
148 lines (111 loc) · 3.8 KB
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
import argparse
from typing import List, Tuple
from aios.utils.utils import delete_directories, humanify_agent, parse_global_args
from aios.hooks.llm import useFactory, useKernel, useFIFOScheduler
from pyopenagi.agents.interact import Interactor
from dotenv import load_dotenv
import warnings
def clean_cache(root_directory):
targets = {
".ipynb_checkpoints",
"__pycache__",
".pytest_cache",
"context_restoration",
}
delete_directories(root_directory, targets)
def get_all_agents() -> dict[str, str]:
interactor = Interactor()
agents = interactor.list_available_agents()
agent_names = {}
for a in agents:
agent_names[humanify_agent(a["agent"])] = a["agent"]
return agent_names
def display_agents(agents: List[str]):
print("Available Agents:")
for i, (name) in enumerate(agents, 1):
print(f"{i}. {name}")
def get_user_choice(agents: List[Tuple[str, str]]) -> Tuple[str, str]:
while True:
try:
choice = int(input("Enter the number of the agent you want to use: "))
if 1 <= choice <= len(agents):
return agents[choice - 1]
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Please enter a valid number.")
def get_user_task(chosen_agent) -> str:
return input(f"Enter the task for the {chosen_agent}: ")
def main():
warnings.filterwarnings("ignore")
parser = parse_global_args()
args = parser.parse_args()
load_dotenv()
llm = useKernel(
llm_name=args.llm_name,
max_gpu_memory=args.max_gpu_memory,
eval_device=args.eval_device,
max_new_tokens=args.max_new_tokens,
log_mode=args.llm_kernel_log_mode,
use_backend=args.use_backend,
)
startScheduler, stopScheduler = useFIFOScheduler(
llm=llm, log_mode=args.scheduler_log_mode, get_queue_message=None
)
submitAgent, awaitAgentExecution = useFactory(
log_mode=args.agent_log_mode, max_workers=500
)
startScheduler()
print(
"""
\033c
Welcome to the Agent REPL.
Please select an agent by pressing tab.
To exit, press Ctrl+C.
"""
)
# check for getch
getch = None
try:
from getch import getch
except ImportError:
print(
"""
The terminal will NOT look pretty without getch. Please install it.
pip install getch
"""
)
getch = input
# shell loop
try:
while True:
chosen_agent = ""
agents = get_all_agents()
# shell prompt
print(f"[{args.llm_name}]> ", end="")
# check if the user put in a tab
if getch() != "\t":
continue
try:
from pyfzf.pyfzf import FzfPrompt
fzf = FzfPrompt()
selected = fzf.prompt(list(agents.keys()))
if len(selected) == 0:
print("No agent selected. Please try again.")
continue
chosen_agent = agents[selected[0]]
except ImportError:
print("pyfzf is not installed. Falling back to default reader.")
display_agents(list(agents.keys()))
chosen_agent, _ = "example/" + get_user_choice(agents)
task = get_user_task(chosen_agent)
try:
agent_id = submitAgent(agent_name=chosen_agent, task_input=task)
awaitAgentExecution(agent_id)
except Exception as e:
print(f"An error occurred: {str(e)}")
except KeyboardInterrupt:
stopScheduler()
clean_cache(root_directory="./")
if __name__ == "__main__":
main()