-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent.py
More file actions
112 lines (90 loc) · 3.65 KB
/
agent.py
File metadata and controls
112 lines (90 loc) · 3.65 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
import logging
import os
from runloop_api_client import Runloop
import openai
import ell
from ell import Message
from typing import List
from runloop_api_client.types import DevboxView
logger = logging.getLogger("agent")
SYSTEM_PROMPT = """
You are an expert python coder that specializes in making single file bash scripts.
""".strip()
USER_PROMPT = """
Write a command-line script that prints sys.argv[1:] as ascii art.
The program should be callable from the command line via `python script.py`.
Once you have generated the program, run it and print the output to stdout.
Use "hello runloop" as the argument. Fix and re-run the program until it works.
Once it works, use tools to read the final program from the `script.py` file and return
the contents verbatim in a code block.
In a separate code block, print the verbatim output from the last time you
ran the program.
""".strip()
MAX_ITERATIONS = 10
def run_agent(runloop: Runloop, devbox: DevboxView, openai_api_key: str):
openai.api_key = openai_api_key
@ell.tool()
def execute_shell_command(command: str):
"""Run a shell command in the devbox."""
return runloop.devboxes.execute_sync(devbox.id, command=command).stdout
@ell.tool()
def read_file(filename: str):
"""Reads a file on the devbox."""
return runloop.devboxes.read_file_contents(devbox.id, file_path=filename)
@ell.tool()
def write_file(filename: str, contents: str):
"""Writes a file on the devbox."""
runloop.devboxes.write_file_contents(
devbox.id, file_path=filename, contents=contents
)
@ell.complex(
model="gpt-4-turbo", tools=[execute_shell_command, read_file, write_file]
)
def invoke_agent(message_history: List[Message]):
"""Calls the LLM to generate the program."""
if not message_history:
logger.debug("invoke_agent: first call")
else:
logger.debug(
f"invoke_agent: calling again, last message = {message_history[-1].text}"
)
messages = [
ell.system(SYSTEM_PROMPT),
ell.user(USER_PROMPT),
] + message_history
return messages
message_history = []
result = invoke_agent(message_history)
num_iterations = 0
while result.tool_calls and num_iterations < MAX_ITERATIONS:
logger.debug(f"performing tool calls: {result.tool_calls}")
message_history.append(result)
result_message = result.call_tools_and_collect_as_message()
logger.debug(f"result_message = {result_message}")
message_history.append(result_message)
result = invoke_agent(message_history)
num_iterations += 1
# Per our instructions, the last message *should* be the final script
# and example usage.
print(result.text)
def main(openai_api_key: str, runloop_api_key: str):
runloop = Runloop(bearer_token=runloop_api_key)
logger.info("Creating devbox ...")
devbox = runloop.devboxes.create_and_await_running()
devbox_id = devbox.id
try:
run_agent(runloop, devbox, openai_api_key)
finally:
logger.info(f"Destroying devbox {devbox_id}...")
runloop.devboxes.shutdown(devbox_id)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
logging.getLogger("httpx").setLevel(logging.WARNING)
logger.info("Starting agent demo")
openai_api_key = os.environ.get("OPENAI_API_KEY")
if not openai_api_key:
raise ValueError("OPENAI_API_KEY is not set")
runloop_api_key = os.environ.get("RUNLOOP_API_KEY")
if not runloop_api_key:
raise ValueError("RUNLOOP_API_KEY is not set")
main(openai_api_key, runloop_api_key)