Spaces:
Running
Running
File size: 8,027 Bytes
85e3d20 |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
""" This file defines the basic agent class that can be used to implement different agents. """
import json
import sys
import os
import re
import glob
import copy
from argparse import Namespace
from abc import abstractmethod, ABC
import reactagent.high_level_actions as high_level_actions
from reactagent.schema import Action, EnhancedJSONEncoder
from reactagent.llm import complete_text
initial_prompt = """You are a helpful research assistant. You have access to the following tools:
{tools_prompt}
Research Problem: {task_description}
Always respond in this format exactly:
{format_prompt}
Observation:
```
the result of the action
```
"""
format_prompt_dict = {
"Thought": "What you are currently doing, what actions to perform and why",
"Action": "the action to take, should be one of the names of the tools",
"Action Input": "the input to the action as a valid JSON string",
}
class Agent(ABC):
""" Base class for agents. """
def __init__(self, args, env):
self.args = args
self.log_dir = os.path.join(args.log_dir, "agent_log")
self.action_infos = env.action_infos
tool_names = list(env.action_infos.keys())
self.all_tool_names = copy.deepcopy(tool_names)
actions_remove_from_prompt = ["Read File", "Write File", "Append File", "Retrieval from Research Log", "Append Summary to Research Log", "Python REPL", "Request Help", "Edit Script (AI)"]
actions_remove_from_prompt.extend(args.actions_remove_from_prompt)
for t in actions_remove_from_prompt:
# remove tool name but in case of missing tool name, don't crash
try:
tool_names.remove(t)
except:
pass
for t in args.actions_add_to_prompt:
# remove tool name but in case of missing tool name, don't crash
try:
tool_names.append(t)
except:
pass
self.prompt_tool_names = tool_names
high_level_actions.EDIT_SCRIPT_MODEL = args.edit_script_llm_name
high_level_actions.EDIT_SCRIPT_MAX_TOKENS = args.edit_script_llm_max_tokens
self.tools_prompt = self.construct_tools_prompt(tool_names, env.action_infos)
self.history_steps = []
# self.initialize_logging()
# if self.args.resume:
# list_of_files = glob.glob(os.path.join(self.args.resume, f"agent_log/agent_{self.args.resume_step}_*.json"))
# latest_file = max(list_of_files, key=os.path.getctime)
# print("Restoring agent from {}".format(latest_file))
# self.restore(latest_file)
def initialize_logging(self):
""" Initialize logging folder for the agent. """
if os.path.exists(self.log_dir):
print("Log dir {} already exists. Overwriting.".format(self.log_dir))
else:
os.makedirs(self.log_dir)
with open(os.path.join(self.log_dir, "main_log"), "w", 1) as f:
f.write("Enabled Tools in Prompt:" + str(self.prompt_tool_names) + "\n")
f.write("================================Start=============================\n")
print("Agent is up! See progress in {}".format(os.path.join(self.log_dir, "main_log")))
def save(self, file_path):
""" Save the agent state to a file. """
with open(file_path, "w") as f:
try:
json.dump(self.__dict__, f, indent=4,cls=EnhancedJSONEncoder)
except:
print("save agent state failed", file=sys.stderr)
pass
def restore(self, file_path):
""" Restore the agent state from a file."""
with open(file_path, "r") as f:
agent_state = json.load(f)
agent_state["args"] = Namespace(**agent_state["args"])
for key, value in agent_state.items():
if key == "log_dir":
continue
if key == "action_infos":
continue
setattr(self, key, value)
############# Helper Functions ################
@staticmethod
def construct_tool_prompt(tool_name, action_info):
""" Construct the prompt for a single tool."""
tool = action_info
usage = ",\n ".join([f"\"{k}\": [{v}]" for k, v in tool.usage.items()])
tools_prompt = f"""{tool.description}
Usage:
```
Action: {tool_name}
Action Input: {{
{usage}
}}
Observation: [{tool.return_value}]
```
""".strip() + "\n\n"
return tools_prompt
@classmethod
def construct_tools_prompt(cls, tool_names, action_infos):
""" Construct the prompt for all tools."""
tools_prompt = ""
for tool_name in tool_names:
tools_prompt += f"""- {tool_name}:
"""
tools_prompt += cls.construct_tool_prompt(tool_name, action_infos[tool_name])
return tools_prompt
@staticmethod
def sanitize_json_string(s):
""" Try to sanitize a string to be a valid JSON string."""
s = s.strip("```json").strip("```").strip()
s = s.replace('\\', '\\\\') # Escape backslashes first
s = s.replace('/', '\\/') # Escape forward slashes
s = s.replace('\b', '\\b') # Escape backspaces
s = s.replace('\f', '\\f') # Escape form feeds
s = s.replace('\r', '\\r') # Escape carriage returns
s = s.replace('\t', '\\t') # Escape horizontal tabs
# triple quotes are a problem
return re.sub(r'"([^"]*)"', lambda m: '"' + m.group(1).replace('\n', '\\n').replace('\"', '\\"') + '"', s)
@classmethod
def parse_action_input(cls, s, action_info):
""" Parse the action input from a string to a dictionary using different methods."""
try:
try:
d = json.loads(s)
except:
# try to sanitize the string
s = cls.sanitize_json_string(s)
d = json.loads(s)
if set(d.keys()) != set(action_info.usage.keys()):
raise Exception("Argument mismatch")
return d
except Exception as e:
try:
# as a fallback, try to match the string with regex
return cls.parse_action_input_by_matching(s, action_info)
except:
raise e
@staticmethod
def parse_action_input_by_matching(s, action_info):
""" Parse the action input from a string to a dictionary using regex."""
entries = list(action_info.usage.keys())
index = s.find('{')
s = s[index + 1:]
index = s.rfind('}')
s = s[:index]
pattern = ""
for e in entries:
pattern += f'"{e}":([\s\S]*),\s*'
pattern = pattern[:-4]
result = re.search(pattern, s, re.MULTILINE)
if result is None:
raise Exception("Invalid Format")
result = { e: r.strip().strip('\"') for e, r in zip(entries, result.groups())}
# # in case for write to file directly
# if "content" in result:
# import ast
# result["content"] = ast.literal_eval("\"" + result["content"] + "\"")
return result
@staticmethod
def print_action(entries, valid_format_entires):
""" Print the action in a readable format."""
return "".join([ k + ": " + json.dumps(entries[k]) for k in valid_format_entires])
@staticmethod
def parse_entries(s, entries):
""" Parse the entries from the string generated by LLM using regex."""
entries = [ e.strip() for e in entries]
pattern = ""
for e in entries:
e = e.replace("[", "\[").replace("]", "\]")
pattern += f"{e}:([\s\S]*)"
result = re.search(pattern, s, re.MULTILINE)
if result is None:
raise Exception("Invalid: " + s)
parsed = [r for r in result.groups()]
return {e: parsed[idx] for idx, e in enumerate(entries)}
|