Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Last active February 21, 2024 16:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jefftriplett/d5b1c10f0fe2a2e2afa2a6658143c896 to your computer and use it in GitHub Desktop.
Save jefftriplett/d5b1c10f0fe2a2e2afa2a6658143c896 to your computer and use it in GitHub Desktop.
# 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)
@jefftriplett
Copy link
Author

See https://github.com/jefftriplett/hubcap for a public git repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment