Last active
July 11, 2024 05:11
-
-
Save jefftriplett/d5b1c10f0fe2a2e2afa2a6658143c896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://medium.com/@dave1010/amazingly-alarming-autonomous-ai-agents-62f8a785e4d8 | |
# https://github.com/dave1010/hubcap | |
# to run we need a few libraries: | |
# pip install rich typer | |
import os | |
import subprocess | |
import sys | |
import time | |
import typer | |
from rich import print | |
SYSTEM = """ | |
you are a coding agent. | |
you can read and write files. Eg `cat helloworld.txt`, `echo "hello\nworld" > helloworld.txt` output the next command required to progress your goal. | |
output `DONE` when done. | |
""" | |
def chat(*, prompt: str, system: str | None = None) -> str: | |
if system: | |
options = f"--system {quote(system)}" | |
else: | |
options = "--continue" | |
print(f"[blue][PROMPT][/blue] {prompt}") | |
response = subprocess.getoutput(f"llm {options} {quote(prompt)}\n") | |
print(f"[yellow][RESPONSE][/yellow] {response}") | |
return response | |
def quote(string: str) -> str: | |
# Equivalent of PHP's escapeshellarg | |
return "'{}'".format(string.replace("'", "'\\''")) | |
def main(prompt: str): | |
response = chat( | |
prompt=f"GOAL: {prompt}\n\nWHAT IS YOUR OVERALL PLAN?", | |
system=SYSTEM | |
) | |
while True: | |
response = chat( | |
prompt="SHELL COMMAND TO EXECUTE OR `DONE`. NO ADDITIONAL CONTEXT OR EXPLANATION:" | |
).strip() | |
if response == "DONE": | |
break | |
time.sleep(3) | |
try: | |
output = subprocess.check_output( | |
response, stderr=subprocess.STDOUT, shell=True | |
).decode() | |
return_code = 0 | |
except subprocess.CalledProcessError as e: | |
output = e.output.decode() | |
return_code = e.returncode | |
response = chat( | |
prompt=f"COMMAND COMPLETED WITH RETURN CODE: {return_code}. OUTPUT:\n{output}\n\nWHAT ARE YOUR OBSERVATIONS?" | |
) | |
if __name__ == "__main__": | |
typer.run(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://github.com/jefftriplett/hubcap for a public git repo.