Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Simplify parsing tests; Fix parsing test assert; Fix parsing bug [WIP]
Tomas Valentaf2d25b5a495a
diff --git a/api-service/codegen/tools/async_tool.py b/api-service/codegen/tools/async_tool.pyindex d6a5645a2..83dc483e8 100644--- a/api-service/codegen/tools/async_tool.py+++ b/api-service/codegen/tools/async_tool.py@@ -1,17 +1,49 @@-from typing import Awaitable, Callable, Optional+from typing import Awaitable, Callable, Optional, castfrom langchain.agents import Toolfrom langchain.tools.base import BaseTool+from codegen.agent.parsing import ToolLog, parse_thoughts_and_actions+def func(_: str):raise NotImplementedError()-def async_tool(name: str) -> Callable:+def async_tool(name: str | None = None) -> Callable:+ """You don't have to specify tool's name if there is an example of the tool use in the docstring."""+def _make_tool(arun: Optional[Callable[[str], Awaitable[str]]]) -> BaseTool:+ nonlocal nameif arun.__doc__ is None:raise NotImplementedError(f"Missing docstring for {name} async tool")+ # Check if the tool name in an example in tool description matches with the name of the tool.+ # This error is hard to detect because the model still sometimes uses the correct name+ # and not the incorrect name from the example.+ tool_example = next(+ (+ cast(ToolLog, tool)+ for tool in parse_thoughts_and_actions(arun.__doc__.strip())+ if tool["type"] == "tool"+ ),+ None,+ )++ print("example", tool_example)++ if tool_example and not name and tool_example["tool_name"]:+ name = tool_example["tool_name"]++ if not name:+ raise ValueError(+ f"Tool name is not specified. Either specify it in the decorator parameter or add a tool description with an example to the docstring."+ )++ if tool_example and name != tool_example["tool_name"]:+ raise ValueError(+ f'Specified tool name "{name}" differs from the example tool name "{tool_example["tool_name"]}" in the tool description'+ )+description = f"{arun.__doc__.strip()}"tool_ = Tool(name=name,diff --git a/api-service/test/conftest.py b/api-service/test/conftest.pyindex 6979fe3c0..6880f4677 100644--- a/api-service/test/conftest.py+++ b/api-service/test/conftest.py@@ -10,11 +10,13 @@ class Helpers:"""Test LLM action parsing."""for output in llm_outputs:- actions = (+ actions = [cast(ToolLog, action)for action in parse_thoughts_and_actions(output["llm_output"])if action["type"] == "tool"- )+ ]++ assert len(actions) == len(output["expected_actions"])for action, expected_action in zip(actions, cast(List[ToolLog], output["expected_actions"])diff --git a/api-service/test/test_action_parsing.py b/api-service/test/test_action_parsing.pyindex 7d0b06083..87e1e896d 100644--- a/api-service/test/test_action_parsing.py+++ b/api-service/test/test_action_parsing.py@@ -6,6 +6,19 @@ from codegen.agent.parsing import ToolLogllm_outputs: List[Dict[str, Any]] = [+ {+ "llm_output": """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": [+ ToolLog(+ type="tool",+ tool_name="InstallNPMDependencies",+ tool_input="package_name_1 package_name_2",+ ),+ ],+ },{"llm_output": """Action:```@@ -158,6 +171,92 @@ app.listen(port, () => {),],},+ {+ "llm_output": """+ <action tool="InstallNPMDependencies">+ package_name_1 package_name_2+ </action>+""",+ "expected_actions": [+ ToolLog(+ type="tool",+ tool_name="InstallNPMDependencies",+ tool_input="package_name_1 package_name_2",+ ),+ ],+ },+ {+ "llm_output": """+<action tool="AskHuman">+What should the post request handler do?+</action>+This is a thought:+Action:+""",+ "expected_actions": [+ ToolLog(+ type="tool",+ tool_name="AskHuman",+ tool_input="What should the post request handler do?",+ ),+ ],+ },+ {+ "llm_output": """+<action tool="AskHuman">+What should the post request handler do?+</action>+This is a thought:++<?+<action tool="AskHuman">+What+</action>++This is a thought:++<?+""",+ "expected_actions": [+ ToolLog(+ type="tool",+ tool_name="AskHuman",+ tool_input="What should the post request handler do?",+ ),+ ToolLog(+ type="tool",+ tool_name="AskHuman",+ tool_input="What",+ ),+ ],+ },+ {+ "llm_output": """+This is a thought+Greater <?+And action:++<action tool="AskHuman">+What should the post request handler do?+</action>+<action tool="AskHuman">+What+</action>+thought after?+""",+ "expected_actions": [+ ToolLog(+ type="tool",+ tool_name="AskHuman",+ tool_input="What should the post request handler do?",+ ),+ ToolLog(+ type="tool",+ tool_name="AskHuman",+ tool_input="What",+ ),+ ],+ },]diff --git a/api-service/test/test_simple_format_action_parsing.py b/api-service/test/test_simple_format_action_parsing.pydeleted file mode 100644index dc50a0dfb..000000000--- a/api-service/test/test_simple_format_action_parsing.py+++ /dev/null@@ -1,100 +0,0 @@-"""Test LLM incomplete action parsing"""--from typing import Dict, List, Any--from codegen.agent.parsing import ToolLog--llm_outputs: List[Dict[str, Any]] = [- {- "llm_output": """-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": [- ToolLog(- type="tool",- tool_name="InstallNPMDependencies",- tool_input="package_name_1 package_name_2",- ),- ],- },- {- "llm_output": """-<action tool="AskHuman">-What should the post request handler do?-</action>-This is a thought:-Action:-""",- "expected_actions": [- ToolLog(- type="tool",- tool_name="AskHuman",- tool_input="What should the post request handler do?",- ),- ],- },- {- "llm_output": """-<action tool="AskHuman">-What should the post request handler do?-</action>-This is a thought:--<?-<action tool="AskHuman">-What-</action>--This is a thought:--<?-""",- "expected_actions": [- ToolLog(- type="tool",- tool_name="AskHuman",- tool_input="What should the post request handler do?",- ),- ToolLog(- type="tool",- tool_name="AskHuman",- tool_input="What",- ),- ],- },- {- "llm_output": """-This is a thought-Greater <?-And action:--<action tool="AskHuman">-What should the post request handler do?-</action>-<action tool="AskHuman">-What-</action>-thought after?-""",- "expected_actions": [- 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(helpers):- """Test LLM action parsing."""- helpers.test_llm_outputs_parsing(llm_outputs)