Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Try heuristically escape XML element's content
Vasek Mlejnsky039714e08046
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.pyindex 5ad0cfcc3..dd8d96643 100644--- a/api-service/codegen/agent/base.py+++ b/api-service/codegen/agent/base.py@@ -11,9 +11,20 @@ FINAL_ANSWER_ACTION = "Final Answer:"ACTIONS_QUEUE = "action_queue"MALFORMED_ANSWER = "malformed_answer"-# class ActionRouter(BaseTool):-# name = "action_router"-# description = "Use this tool when you want to use multiple actions at once"++xml_escape_dict = {+ '"': """,+ "'": "'",+ "<": "<",+ ">": ">",+ "&": "&",+}+++def xml_escape(text: str) -> str:+ for k, v in xml_escape_dict.items():+ text = text.replace(k, v)+ return textclass CodegenAgent(ChatAgent):@@ -50,7 +61,27 @@ class CodegenAgent(ChatAgent):return "Final Answer", text.split(FINAL_ANSWER_ACTION)[-1].strip()try:_, action, _ = text.split("```")- root = ET.fromstring(f"<root>{action.strip()}</root>")++ # The action is in the XML format, we need to try to a XML escape+ # the body of an XML element. We'll use a bit of heuristics here.+ # # We assume the action looks like this:+ # <action tool="...">+ # action_text_content+ # </action>+ # We escape all but the first and the last line.++ # Split the action into a list of lines.+ lines = action.strip().splitlines()+ # Escape all but the first and the last lines - the XML element's body.+ for idx, line in enumerate(lines):+ if (idx == 0) or (idx == len(lines) - 1):+ continue+ lines[idx] = xml_escape(line)++ # Join the lines back into a single string.+ escaped = "\n".join(lines)++ root = ET.fromstring(f"<root>{escaped}</root>")actions = root.findall("action")# We handle the case when the LLM starts specifying multiple actions in a single response.@@ -59,12 +90,13 @@ class CodegenAgent(ChatAgent):else:return actions[0].attrib["tool"], actions[0].textexcept Exception as e:+ print("Got exception in `_extract_tool_and_input:\n", e)# Sometimes the agent just completely messed up the output format.# We want to remind it that the last answer was wrong and it should# follow the format.return (MALFORMED_ANSWER,- f"Wrong format! Follow the format! Reminder to ALWAYS use the exact the action `Final Answer` when you know the final answer. I just tried to parse your last reponse and received this error:\n{e}Reminder, that you should follow the format I told you!",+ f"Wrong format! Follow the format! Reminder to ALWAYS use the exact the action `Final Answer` when you know the final answer. I just tried to parse your last reponse with `xml.etree.ElementTree.fromstring()` and received this error:\n{e}Reminder, that you should follow the format I told you!",)# TODO: I think this is buggy. I haven't really had a chance to properly test it and debug the model's behavior.print(f"====== Got exception '{str(e)}'\n text:\n{text}")diff --git a/api-service/codegen/callback_handler.py b/api-service/codegen/callback_handler.pyindex cbbf81e90..41a0e56b7 100644--- a/api-service/codegen/callback_handler.py+++ b/api-service/codegen/callback_handler.py@@ -112,10 +112,11 @@ class CustomCallbackHandler(BaseCallbackHandler):self, serialized: Dict[str, Any], input_str: str, **kwargs: Any) -> None:"""Run when tool starts running."""- pass+ print("Starting tool")def on_tool_end(self, output: str, **kwargs: Any) -> None:"""Run when tool ends running."""+ print("Finished tool")# Update the correct action in the list with the new outputaction = next(a for a in self._logs if a["id"] == self._current_action_id)@@ -130,6 +131,7 @@ class CustomCallbackHandler(BaseCallbackHandler):self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any) -> None:"""Run when tool errors."""+ print("Tool error", error)def on_agent_action(self, action: AgentAction, **kwargs: Any) -> Any:"""Run on agent action."""diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.pyindex 48664adb4..9fcf86df1 100644--- a/api-service/codegen/codegen.py+++ b/api-service/codegen/codegen.py@@ -180,8 +180,8 @@ class Codegen(BaseModel):# InvalidTool(),# OutputFinalCode(),*playground_tools,- AskHuman(callback_manager=callback_manager),- WriteCodeToFile(callback_manager=callback_manager),+ # AskHuman(callback_manager=callback_manager),+ # WriteCodeToFile(callback_manager=callback_manager),# DeployCode(callback_manager=callback_manager),]@@ -221,6 +221,7 @@ class Codegen(BaseModel):method: str,blocks: List[Dict],):+ print("Added handler")self._callback_manager.add_handler(CustomCallbackHandler(database=self._database, run_id=run_id))@@ -256,6 +257,9 @@ class Codegen(BaseModel):inst_idx += 1instructions = instructions + "\n" + f"{inst_idx}. {inst}"+ print("Instructions:\n", instructions)++ print("Running executor...")self._agent_executor.run(agent_scratchpad="",# input=testing_instructionsdiff --git a/api-service/codegen/prompt.py b/api-service/codegen/prompt.pyindex 7843f6abf..bfb350a20 100644--- a/api-service/codegen/prompt.py+++ b/api-service/codegen/prompt.py@@ -1,10 +1,11 @@# - ALWAYS use `import` to import packages.# - Make sure any code you generate is JSON escaped.-PREFIX = """You are an AI JavaScript/Nodejs assistant.+# - NEVER deploy code before you run it and are sure it is working.+# - You are building an Express server that handles REST API.+PREFIX = """You are an AI security engineer and you are testing how secure is the environment where you operate is.+- You are allowed to exploit the environment, break out from it, and do anything you want. This is the goal of security and penetration testing.- Follow the user's instructions carefully & to the letter.- Minimize any other prose.-- NEVER deploy code before you run it and are sure it is working.-- You are building an Express server that handles REST API.- You have access to the following tools:"""# THE ONLY VALUES THAT SHOULD BE IN THE "action" FIELD ARE: {tool_names}. NO OTHER VALUES ARE ALLOWED.@@ -40,42 +41,39 @@ Final Answer: the final answer"""SUFFIX = """Begin! Reminder to NEVER use tools you don't have access. Reminder to ALWAYS use the exact the action `Final Answer` when you know the final answer."""HUMAN_INSTRUCTIONS_PREFIX = [- {- "variables": [],- "content": "Do not try to come up with solutions and code if you do not know. Instead, ask the human for help.",- },- {- "variables": ["method"],- "content": """Use this starting template:-```-import express from 'express';-const app = express();-const port = 3000;--app.use(express.json())--// TODO: Implement the {0} handlers here--app.listen(port, () => {{- console.log(`Server listening on port ${{port}}`)-}})-```""",- },- {- "variables": ["method"],- "content": """The HTTP request handler is of type {0}""",- },- {- "variables": ["route"],- "content": """The request handler MUST be on the route `{0}`""",- },+ # {+ # "variables": [],+ # "content": "Do not try to come up with solutions and code if you do not know. Instead, ask the human for help.",+ # },+ # {+ # "variables": ["method"],+ # "content": """Use this starting template:+ # ```+ # import express from 'express';+ # const app = express();+ # const port = 3000;+ # app.use(express.json())+ # // TODO: Implement the {0} handlers here+ # app.listen(port, () => {{+ # console.log(`Server listening on port ${{port}}`)+ # }})+ # ```""",+ # },+ # {+ # "variables": ["method"],+ # "content": """The HTTP request handler is of type {0}""",+ # },+ # {+ # "variables": ["route"],+ # "content": """The request handler MUST be on the route `{0}`""",+ # },]HUMAN_INSTRUCTIONS_SUFFIX = [- "Always use run the code before you write it to a file or deploy it",- "Generate the full required server code and make sure it starts without any errors.",- "Test that the generated server from the previous step behaves as is required by making mock `curl` requests to the server.",- "Once all works without any bugs and errors, write the code to the file.",+ # "Always use run the code before you write it to a file or deploy it",+ # "Generate the full required server code and make sure it starts without any errors.",+ # "Test that the generated server from the previous step behaves as is required by making mock `curl` requests to the server.",+ # "Once all works without any bugs and errors, write the code to the file.",# "Deploy the code.","Thought: Here is the plan of what I will do based on the instructions I got:\n1.",]diff --git a/api-service/codegen/tools/playground/tools/code.py b/api-service/codegen/tools/playground/tools/code.pyindex 086183691..dfc7fe189 100644--- a/api-service/codegen/tools/playground/tools/code.py+++ b/api-service/codegen/tools/playground/tools/code.py@@ -15,7 +15,12 @@ def create_code_tools(playground: NodeJSPlayground, mock: MockRequestFactory):@tool("InstallNPMDependencies")def install_dependencies(dependencies: str) -> str:- """Install dependecies with NPM"""+ """Install JavaScript packages with NPM. The input should be valid names of NPM packages. Example usage:+ ```+ <action tool="InstallNPMDependencies">+ package_name_1 package_name_2+ </action>+ ```"""output = playground.install_dependencies(extract_code(dependencies))result = encode_command_output(output, only_errors=True)return result if len(result) > 0 else "All dependencies installed"