Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix broken parsing testing

Tomas Valentaf791c1fa1d8d
diff --git a/api-service/test/conftest.py b/api-service/test/conftest.py
new file mode 100644
index 000000000..6979fe3c0
--- /dev/null
+++ b/api-service/test/conftest.py
@@ -0,0 +1,34 @@
+import pytest
+from typing import Dict, List, Any, cast
+
+from codegen.agent.parsing import ToolLog, parse_thoughts_and_actions
+
+
+class Helpers:
+ @staticmethod
+ def test_llm_outputs_parsing(llm_outputs: List[Dict[str, Any]]):
+ """Test LLM action parsing."""
+
+ for output in llm_outputs:
+ actions = (
+ cast(ToolLog, action)
+ for action in parse_thoughts_and_actions(output["llm_output"])
+ if action["type"] == "tool"
+ )
+
+ for action, expected_action in zip(
+ actions, cast(List[ToolLog], output["expected_actions"])
+ ):
+ assert action["tool_name"] == expected_action["tool_name"]
+ assert (
+ action.get("tool_input", "")
+ .strip()
+ .startswith(expected_action.get("tool_input").strip())
+ )
+
+ print("-----------")
+
+
+def helpers():
+ return Helpers
diff --git a/api-service/test/test_action_parsing.py b/api-service/test/test_action_parsing.py
index 0b5a7a27b..7d0b06083 100644
--- a/api-service/test/test_action_parsing.py
+++ b/api-service/test/test_action_parsing.py
@@ -1,17 +1,8 @@
"""Test LLM action parsing"""
-# import pytest
+from typing import Dict, List, Any
-from typing import Dict, List, NamedTuple, Any
-from codegen.agent.base import (
- separate_thought_and_action,
- parse_action_string,
-)
-
-
-class Action(NamedTuple):
- tool: str
- input: str
+from codegen.agent.parsing import ToolLog
llm_outputs: List[Dict[str, Any]] = [
@@ -23,7 +14,11 @@ What should the post request handler do?
</action>
```""",
"expected_actions": [
- Action("AskHuman", "What should the post request handler do?")
+ ToolLog(
+ type="tool",
+ tool_name="AskHuman",
+ tool_input="What should the post request handler do?",
+ ),
],
},
{
@@ -35,7 +30,13 @@ Action:
action input
</action>
```""",
- "expected_actions": [Action("Name", "action input")],
+ "expected_actions": [
+ ToolLog(
+ type="tool",
+ tool_name="Name",
+ tool_input="action input",
+ ),
+ ],
},
{
"llm_output": """Thought: Lorem ipsum
@@ -50,8 +51,16 @@ action input 2
</action>
```""",
"expected_actions": [
- Action("Name", "action input"),
- Action("Name 2", "action input 2"),
+ ToolLog(
+ type="tool",
+ tool_name="Name",
+ tool_input="action input",
+ ),
+ ToolLog(
+ type="tool",
+ tool_name="Name 2",
+ tool_input="action input 2",
+ ),
],
},
{
@@ -64,9 +73,21 @@ Action:
```
""",
"expected_actions": [
- Action(tool="Name1", input="content"),
- Action(tool="Name2", input="content 2"),
- Action(tool="Name3", input="content 3"),
+ ToolLog(
+ type="tool",
+ tool_name="Name1",
+ tool_input="content",
+ ),
+ ToolLog(
+ type="tool",
+ tool_name="Name2",
+ tool_input="content 2",
+ ),
+ ToolLog(
+ type="tool",
+ tool_name="Name3",
+ tool_input="content 3",
+ ),
],
},
{
@@ -106,10 +127,15 @@ app.listen(port, () => {
""",
"expected_actions": [
- Action(tool="InstallNPMDependencies", input="email-validator"),
- Action(
- tool="RunJavaScriptCode",
- input="""import express from 'express';
+ ToolLog(
+ type="tool",
+ tool_name="InstallNPMDependencies",
+ tool_input="email-validator",
+ ),
+ ToolLog(
+ type="tool",
+ tool_name="RunJavaScriptCode",
+ tool_input="""import express from 'express';
import validator from 'email-validator';
const app = express();
@@ -135,15 +161,6 @@ app.listen(port, () => {
]
-def test_parsing_llm_action_output():
+def test_parsing_llm_action_output(helpers):
"""Test LLM action parsing."""
-
- for output in llm_outputs:
- _, action_string = separate_thought_and_action(output["llm_output"])
- parsed_actions = parse_action_string(action_string)
-
- for parsed_action, expected_action in zip(
- parsed_actions, output["expected_actions"]
- ):
- assert parsed_action.attrib["tool"] == expected_action.tool
- assert parsed_action.text.strip() == expected_action.input.strip()
+ helpers.test_llm_outputs_parsing(llm_outputs)
diff --git a/api-service/test/test_incomplete_action_parsing.py b/api-service/test/test_incomplete_action_parsing.py
index 826443105..63e26a017 100644
--- a/api-service/test/test_incomplete_action_parsing.py
+++ b/api-service/test/test_incomplete_action_parsing.py
@@ -1,18 +1,8 @@
"""Test LLM incomplete action parsing"""
-# import pytest
-
-from typing import Dict, List, NamedTuple, Any
-from codegen.agent.base import (
- separate_thought_and_action,
- parse_action_string,
-)
-
-
-class Action(NamedTuple):
- tool: str
- input: str
+from typing import Dict, List, Any
+from codegen.agent.parsing import ToolLog
llm_outputs: List[Dict[str, Any]] = [
{
@@ -23,21 +13,16 @@ What should the post request handler do?
</
```""",
"expected_actions": [
- Action("AskHuman", "What should the post request handler do?")
+ ToolLog(
+ type="tool",
+ tool_name="AskHuman",
+ tool_input="What should the post request handler do?",
+ )
],
},
]
-def test_parsing_llm_incomplete_action_output():
+def test_parsing_llm_incomplete_action_output(helpers):
"""Test LLM action parsing."""
-
- for output in llm_outputs:
- _, action_string = separate_thought_and_action(output["llm_output"])
- parsed_actions = parse_action_string(action_string)
-
- for parsed_action, expected_action in zip(
- parsed_actions, output["expected_actions"]
- ):
- assert parsed_action.attrib["tool"] == expected_action.tool
- assert parsed_action.text.strip() == expected_action.input.strip()
+ helpers.test_llm_outputs_parsing(llm_outputs)
diff --git a/api-service/test/test_simple_format_action_parsing.py b/api-service/test/test_simple_format_action_parsing.py
index 1111b5622..dc50a0dfb 100644
--- a/api-service/test/test_simple_format_action_parsing.py
+++ b/api-service/test/test_simple_format_action_parsing.py
@@ -1,31 +1,23 @@
"""Test LLM incomplete action parsing"""
-# import pytest
-
-from typing import Dict, List, NamedTuple, Any
-from codegen.agent.base import (
- parse_thoughts_and_actions,
- separate_thought_and_action,
- parse_action_string,
-)
-
-
-class Action(NamedTuple):
- tool: str
- input: str
+from typing import Dict, List, Any
+from codegen.agent.parsing import ToolLog
llm_outputs: List[Dict[str, Any]] = [
{
"llm_output": """
-This is a thought:
-Action:
-<action tool="AskHuman">
-What should the post request handler do?
-</
+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>
""",
"expected_actions": [
- Action("AskHuman", "What should the post request handler do?")
+ ToolLog(
+ type="tool",
+ tool_name="InstallNPMDependencies",
+ tool_input="package_name_1 package_name_2",
+ ),
],
},
{
@@ -37,7 +29,11 @@ This is a thought:
Action:
""",
"expected_actions": [
- Action("AskHuman", "What should the post request handler do?")
+ ToolLog(
+ type="tool",
+ tool_name="AskHuman",
+ tool_input="What should the post request handler do?",
+ ),
],
},
{
@@ -57,8 +53,16 @@ This is a thought:
<?
""",
"expected_actions": [
- Action("AskHuman", "What should the post request handler do?"),
- Action("AskHuman", "What"),
+ ToolLog(
+ type="tool",
+ tool_name="AskHuman",
+ tool_input="What should the post request handler do?",
+ ),
+ ToolLog(
+ type="tool",
+ tool_name="AskHuman",
+ tool_input="What",
+ ),
],
},
{
@@ -76,29 +80,21 @@ What
thought after?
""",
"expected_actions": [
- Action("AskHuman", "What should the post request handler do?"),
- Action("AskHuman", "What"),
+ ToolLog(
+ type="tool",
+ tool_name="AskHuman",
+ tool_input="What should the post request handler do?",
+ ),
+ ToolLog(
+ type="tool",
+ tool_name="AskHuman",
+ tool_input="What",
+ ),
],
},
]
-def test_parsing_llm_simple_format_action_output():
+def test_parsing_llm_simple_format_action_output(helpers):
"""Test LLM action parsing."""
-
- for output in llm_outputs:
- # _, action_string = separate_thought_and_action(output["llm_output"])
- # parsed_actions = parse_action_string(output["llm_output"])
-
- thought, parsed_actions = parse_thoughts_and_actions(output["llm_output"])
-
- # print("Thought", thought)
-
- for parsed_action, expected_action in zip(
- parsed_actions, output["expected_actions"]
- ):
- # print("Action", parsed_action.text.strip())
- assert parsed_action.attrib["tool"] == expected_action.tool
- assert parsed_action.text.strip().startswith(expected_action.input.strip())
-
- print("-----------")
+ helpers.test_llm_outputs_parsing(llm_outputs)