aboutsummaryrefslogtreecommitdiff
path: root/llm.py
diff options
context:
space:
mode:
Diffstat (limited to 'llm.py')
-rwxr-xr-xllm.py28
1 files changed, 10 insertions, 18 deletions
diff --git a/llm.py b/llm.py
index 834a1a9..57e8e27 100755
--- a/llm.py
+++ b/llm.py
@@ -65,7 +65,7 @@ def path_checked(path: Path, cwd = Path.cwd()) -> Optional[Path]:
return path if path.is_relative_to(cwd) else None
-def inner_query_agent(actions: list, query: str, state = ()):
+def query_agent(actions: list, query: str, state = ()):
state = state + ({"role": "user", "content": query},)
@@ -76,7 +76,8 @@ def inner_query_agent(actions: list, query: str, state = ()):
# Replace all fenced JSON blocks
replacements = []
- for m in re.finditer(r"```json\s*(.*?)\s*```", res, re.DOTALL):
+ result_jsons = []
+ for m in re.finditer(r"^```json\s*(.*?)\s*^```", res, re.DOTALL | re.MULTILINE):
json_str = m.group(1)
try:
action = json.loads(json_str)
@@ -87,6 +88,7 @@ def inner_query_agent(actions: list, query: str, state = ()):
result_json = f(action)
if result_json is not None:
replacements.append((m.start(), m.end(), action["action"]))
+ result_jsons.append(result_json)
break
replaced_res = ""
@@ -97,44 +99,34 @@ def inner_query_agent(actions: list, query: str, state = ()):
replaced_res += res[i:]
if len(replacements) > 0:
- (res, state) = inner_query_agent(actions, json.dumps(result_json), state)
+ (res, state) = query_agent(actions, json.dumps(result_jsons), state)
return (replaced_res + "\n\n" + res, state)
else:
return (replaced_res, state)
-def query_agent(query: str, state = ()):
-
+def main():
actions = [ls, read, write, tldr]
-
- state = state + ({"role": "system",
+ state = ({"role": "system",
"content": f"""You are an autonomous coding agent that can help with programming tasks.
You can explore repositories, read files, search for information, and make changes to code.
You have access to the following actions, which you should embed in JSON markdown blocks "```json ACTION ```" in your responses.
{'\n'.join(map(lambda f: '* ' + f.__doc__, actions))}
+After reading the relevant files, remember to analyze the task at hand and break it into steps.
+
Once you are finished with your task, give a response with none of the above JSON commands.
"""},)
-
- state = state + ({"role": "system", "content": "Analyze the following user-given task and break it down into steps."},)
-
- return inner_query_agent(actions, query, state)
-
-def main():
-
- state = ()
-
while True:
query = input("> ").strip()
- (response, state) = query_agent(query, state)
+ (response, state) = query_agent(actions, query, state)
print(response)
if __name__ == "__main__":
main()
-