Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Save raw logs to DB more frequently + also add tool outputs

Vasek Mlejnskyef043b23ada6
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.py
index 498b335c6..62f013259 100644
--- a/api-service/codegen/agent/base.py
+++ b/api-service/codegen/agent/base.py
@@ -1,4 +1,5 @@
from typing import Optional, Tuple, List, Dict, Union
+from html import unescape
import xml.etree.ElementTree as ET
from langchain.agents.tools import InvalidTool
@@ -26,6 +27,9 @@ def xml_escape(text: str) -> str:
return text
+# def xml_unescape(xml: str) -> str:
+
+
class CodegenAgent(ChatAgent):
# def _construct_scratchpad(
# self, intermediate_steps: List[Tuple[AgentAction, str]]
@@ -83,9 +87,14 @@ class CodegenAgent(ChatAgent):
root = ET.fromstring(f"<root>{escaped}</root>")
actions = root.findall("action")
+ # Because we XML-escaped action element's body (= tool input) so we could XML parse it,
+ # we need to unescape it now to get the actual tool input.
+ for a in actions:
+ a.text = unescape(a.text)
+
# We handle the case when the LLM starts specifying multiple actions in a single response.
if len(actions) > 1:
- return ACTIONS_QUEUE, action.strip()
+ return ACTIONS_QUEUE, actions.strip()
else:
return actions[0].attrib["tool"], actions[0].text
except Exception as e:
@@ -97,7 +106,6 @@ class CodegenAgent(ChatAgent):
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 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}")
# input = response["action_input"]
return (
@@ -153,6 +161,7 @@ class CodegenAgentExecutor(AgentExecutor):
"""
# Call the LLM to see what to do.
output = self.agent.plan(intermediate_steps, **inputs)
+
# If the tool chosen is the finishing tool, then we end and return.
if isinstance(output, AgentFinish):
return output
diff --git a/api-service/codegen/callback_handler.py b/api-service/codegen/callback_handler.py
index 41a0e56b7..c6f25860c 100644
--- a/api-service/codegen/callback_handler.py
+++ b/api-service/codegen/callback_handler.py
@@ -53,20 +53,16 @@ class RepeatedTimer(object):
class CustomCallbackHandler(BaseCallbackHandler):
_logs: List[Dict[str, str]] = []
+ _raw_logs: str = ""
_current_action_id: Optional[str] = None
_database: Database = PrivateAttr()
_run_id: str = PrivateAttr()
_file = open("out.txt", "w+")
- _raw_logs: str = ""
-
- _rt: RepeatedTimer = PrivateAttr()
-
def __init__(self, database: Database, run_id: str, **kwargs: Any):
super().__init__(**kwargs)
self._database = database
self._run_id = run_id
- self._rt = RepeatedTimer(1, self._push_raw_logs)
def _push_raw_logs(self) -> None:
if self._raw_logs:
@@ -81,6 +77,7 @@ class CustomCallbackHandler(BaseCallbackHandler):
def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
"""Run on new LLM token. Only available when streaming is enabled."""
self._raw_logs += token
+ self._push_raw_logs()
def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None:
"""Run when LLM ends running."""
@@ -118,6 +115,9 @@ class CustomCallbackHandler(BaseCallbackHandler):
"""Run when tool ends running."""
print("Finished tool")
+ self._raw_logs += f"\n{output}\n"
+ self._push_raw_logs()
+
# Update the correct action in the list with the new output
action = next(a for a in self._logs if a["id"] == self._current_action_id)
action["finish_at"] = str(datetime.datetime.now())