Skip to content

Commit

Permalink
Trying some regex heuristics to make job completion more predictable
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed Apr 21, 2023
1 parent b2c611e commit 60ad22c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
4 changes: 2 additions & 2 deletions golemgpt/actions/write_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def write_file_action(filename: str, content: str, **kwargs) -> str:
"""Write a file and return its info."""
cwd, path = Path.cwd(), (Path.cwd() / filename)
try:
path.relative_to(cwd)
relname = path.relative_to(cwd)
except ValueError:
return "Rejected, file is outside of working dir."

Expand All @@ -18,4 +18,4 @@ def write_file_action(filename: str, content: str, **kwargs) -> str:
with path.open("w") as file:
file.write(content)

return f"File {path} is saved."
return f'File "{relname}" is saved.'
42 changes: 31 additions & 11 deletions golemgpt/golems/general.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from re import compile as re_compile, DOTALL
from json import loads as json_loads, JSONDecodeError
from typing import Callable, List
from golemgpt.settings import Settings
Expand All @@ -19,6 +20,10 @@ def load_runner(settings: Settings) -> Callable:

class General:
prompt = ''
# Not-a-JSON naive preambule regex:
naive_preambule_re = re_compile(r'([^\[\{]+)', DOTALL)
# naive_list_re = re_compile(r'(\[[\s\S]+\])', DOTALL)
# naive_obj_re = re_compile(r'(\{[\s\S]+\})', DOTALL)

# TODO: Implement review_action_plan() ?
# TODO: Spawn a new Golem to make parseable action plan from the reply ?
Expand Down Expand Up @@ -91,16 +96,28 @@ def parse_reply(self, reply: str) -> List[dict]:
"""Parse the reply into an action plan."""
console.debug(f"Parse plan:\n{reply}\n")
reply = reply.strip()
try:
self.action_plan = json_loads(reply)
except JSONDecodeError:
raise
# Try to parse the last line only
# lines = reply.splitlines()
# if len(lines) > 1:
# self.parse_plan(lines.pop())
# else:
# raise
parsed = json_loads(reply)
if isinstance(parsed, dict):
parsed = [parsed]
self.action_plan = parsed
# Reply might contain a preambule, try to extract the JSON only
# list_match = self.naive_list_re.search(reply)
# obj_match = self.naive_obj_re.search(reply)
# if list_match:
# matched = list_match.group()
# console.debug(f"List found: {matched}")
# elif obj_match:
# matched = obj_match.group()
# console.debug(f"Object found: {matched}")
# matched = '[' + matched.lstrip('{').rstrip('}') + ']'
# else:
# console.debug(f"No JSON found: {reply}")
# raise JSONDecodeError("Not a JSON", reply, 0)
# Regex are greedy and matched part might be noisy
# try:
# self.action_plan = json_loads(matched)
# except JSONDecodeError:
# self.action_plan = json_loads(reply)

def update_plan(self, prompt: str, attempt: int = 0) -> None:
"""Ask to update the plan based on the prompt."""
Expand All @@ -114,6 +131,9 @@ def update_plan(self, prompt: str, attempt: int = 0) -> None:
try:
self.parse_reply(reply)
except JSONDecodeError:
preambule = self.naive_preambule_re.search(reply)
if preambule:
reply = preambule.group()
question = (
"Does the following mean current job is finished"
f" (optional ask to start a new one)?\n\n{reply}"
Expand All @@ -129,6 +149,6 @@ def run_next_action(self) -> str:
action, result = self.runner(action_item, golem=self)
if not result:
return ''
return f'"{action}" completed, result: {result}'
return f'Completed "{action}" with result:\n{result}'
else:
raise JobFinished()
2 changes: 1 addition & 1 deletion golemgpt/utils/chat/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, settings: Settings, memory: BaseMemory) -> None:
self.memory = memory

def send_message(
self, content, temperature: float = 0.1, role: str = 'user',
self, content, temperature: float = 0, role: str = 'user',
max_tokens: int = 0
) -> Tuple[str, list]:
"""Send a message to the dialog and return the reply."""
Expand Down

0 comments on commit 60ad22c

Please sign in to comment.