Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Tests [wip]
Vasek Mlejnsky01e245f53ea8
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.pyindex 3daaf8a23..9850add68 100644--- a/api-service/codegen/agent/base.py+++ b/api-service/codegen/agent/base.py@@ -150,10 +150,18 @@ class CodegenAgent(ChatAgent):if FINAL_ANSWER_ACTION in text:return "Final Answer", text.split(FINAL_ANSWER_ACTION)[-1].strip()try:+ print("+++ _extract_tool_and_input TEXT")+ print(text)+ print("--- _extract_tool_and_input TEXT")_, action_string, _ = text.split("```")+ print("+++ _extract_tool_and_input action_string")+ print(action_string)+ print("--- _extract_tool_and_input action_string")# A list of XML elements that represents actions.actions = parse_action_string(action_string)+ print("ACTIONS")+ print(actions)# We handle the case when the LLM starts specifying multiple actions in a single response.# Return a special `ACTIONS_QUEUE` identifier and the raw action string.diff --git a/api-service/codegen/callbacks/logs.py b/api-service/codegen/callbacks/logs.pyindex 2e7be716a..d791d1b70 100644--- a/api-service/codegen/callbacks/logs.py+++ b/api-service/codegen/callbacks/logs.py@@ -109,6 +109,12 @@ class LogsCallbackHandler(BaseCallbackHandler):# $INPUT# </action># ```+ print("+++ AGENT ACTION LOG")+ print(action.log)+ print("SPLIT:")+ print(action.log.split("Action:"))+ print("--- AGENT ACTION LOG")+thought, _ = action.log.split("Action:")thought = thought.removeprefix("Thought:")thought = thought.strip()diff --git a/api-service/codegen/prompt.py b/api-service/codegen/prompt.pyindex 4ada33732..b079881a2 100644--- a/api-service/codegen/prompt.py+++ b/api-service/codegen/prompt.py@@ -65,10 +65,12 @@ app.listen(port, () => {{]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.",- # "Deploy the code.",- "Thought: Here is the plan of what I will do based on the instructions I got:\n1.",+ "Think about it and plan your work first",+ "Always use run the code before you submit the answer",+ "Generate the full required server code and and make sure it starts without any errors",+ "Test that the generated server from the previous step works as 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",+ "Once you know the final code, output it as the 'Final answer:'",+ # "Thought: Here is the plan of how I will go about solving this based on the instructions I got:\n1.",]diff --git a/api-service/poetry.lock b/api-service/poetry.lockindex eb17317bc..e4fa98bbe 100644--- a/api-service/poetry.lock+++ b/api-service/poetry.lock@@ -544,6 +544,21 @@ files = [{file = "dotty_dict-1.3.1.tar.gz", hash = "sha256:4b016e03b8ae265539757a53eba24b9bfda506fb94fbce0bee843c6f05541a15"},]+[[package]]+name = "exceptiongroup"+version = "1.1.1"+description = "Backport of PEP 654 (exception groups)"+category = "main"+optional = false+python-versions = ">=3.7"+files = [+ {file = "exceptiongroup-1.1.1-py3-none-any.whl", hash = "sha256:232c37c63e4f682982c8b6459f33a8981039e5fb8756b2074364e5055c498c9e"},+ {file = "exceptiongroup-1.1.1.tar.gz", hash = "sha256:d484c3090ba2889ae2928419117447a14daf3c1231d5e30d0aae34f354f01785"},+]++[package.extras]+test = ["pytest (>=6)"]+[[package]]name = "flask"version = "2.2.3"@@ -918,6 +933,18 @@ zipp = {version = ">=3.1.0", markers = "python_version < \"3.10\""}docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"]testing = ["flake8 (<5)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)"]+[[package]]+name = "iniconfig"+version = "2.0.0"+description = "brain-dead simple config-ini parsing"+category = "main"+optional = false+python-versions = ">=3.7"+files = [+ {file = "iniconfig-2.0.0-py3-none-any.whl", hash = "sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374"},+ {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"},+]+[[package]]name = "invoke"version = "1.7.3"@@ -1338,6 +1365,22 @@ files = [[package.extras]testing = ["pytest", "pytest-cov"]+[[package]]+name = "pluggy"+version = "1.0.0"+description = "plugin and hook calling mechanisms for python"+category = "main"+optional = false+python-versions = ">=3.6"+files = [+ {file = "pluggy-1.0.0-py2.py3-none-any.whl", hash = "sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3"},+ {file = "pluggy-1.0.0.tar.gz", hash = "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159"},+]++[package.extras]+dev = ["pre-commit", "tox"]+testing = ["pytest", "pytest-benchmark"]+[[package]]name = "postgrest"version = "0.10.6"@@ -1436,6 +1479,30 @@ files = [[package.extras]plugins = ["importlib-metadata"]+[[package]]+name = "pytest"+version = "7.2.2"+description = "pytest: simple powerful testing with Python"+category = "main"+optional = false+python-versions = ">=3.7"+files = [+ {file = "pytest-7.2.2-py3-none-any.whl", hash = "sha256:130328f552dcfac0b1cec75c12e3f005619dc5f874f0a06e8ff7263f0ee6225e"},+ {file = "pytest-7.2.2.tar.gz", hash = "sha256:c99ab0c73aceb050f68929bc93af19ab6db0558791c6a0715723abe9d0ade9d4"},+]++[package.dependencies]+attrs = ">=19.2.0"+colorama = {version = "*", markers = "sys_platform == \"win32\""}+exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \"3.11\""}+iniconfig = "*"+packaging = "*"+pluggy = ">=0.12,<2.0"+tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""}++[package.extras]+testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"]+[[package]]name = "python-dateutil"version = "2.8.2"@@ -1913,6 +1980,18 @@ files = [[package.extras]doc = ["reno", "sphinx", "tornado (>=4.5)"]+[[package]]+name = "tomli"+version = "2.0.1"+description = "A lil' TOML parser"+category = "main"+optional = false+python-versions = ">=3.7"+files = [+ {file = "tomli-2.0.1-py3-none-any.whl", hash = "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc"},+ {file = "tomli-2.0.1.tar.gz", hash = "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f"},+]+[[package]]name = "tomlkit"version = "0.11.6"@@ -2246,4 +2325,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more[metadata]lock-version = "2.0"python-versions = ">=3.8.1,<4.0"-content-hash = "cd7de79f14ab9d21609c65df3e0be437c65747048d134ab6696d56eab8d2edf6"+content-hash = "2a3f6fd2f12f4cbddec4f8ae63b1a89be6952c663543d76c1803f347dd569405"diff --git a/api-service/pyproject.toml b/api-service/pyproject.tomlindex 66f772aa2..fe43ed0d2 100644--- a/api-service/pyproject.toml+++ b/api-service/pyproject.toml@@ -4,9 +4,12 @@ version = "0.1.0"description = ""authors = ["Devbook <[email protected]>"]+[tool.poetry.scripts]+test = "pytest"+[tool.poetry.dependencies]python = ">=3.8.1,<4.0"-flask = {extras = ["async"], version = "^2.2.3"}+flask = { extras = ["async"], version = "^2.2.3" }gunicorn = "^20.1.0"langchain = "^0.0.117"supabase = "^1.0.1"@@ -18,6 +21,7 @@ aenum = "^3.1.11"python-dotenv = "^1.0.0"openai = "^0.27.0"realtime = "^1.0.0"+pytest = "^7.2.2"[build-system]requires = ["poetry-core>=1.3.0"]diff --git a/api-service/test/test_action_parsing.py b/api-service/test/test_action_parsing.pynew file mode 100644index 000000000..9370c6356--- /dev/null+++ b/api-service/test/test_action_parsing.py@@ -0,0 +1,180 @@+"""Test LLM action parsing"""++# import pytest++from typing import Dict, List, NamedTuple+from codegen.agent.base import parse_action_string+++class Action(NamedTuple):+ tool: str+ input: str+++llm_outputs: List[Dict[str, str]] = [+ {+ "llm_output": """Thought: Lorem ipsum++Action:+```+<action tool="Name">+action input+</action>+```""",+ "expected_actions": [Action("Name", "action input")],+ },+ {+ "llm_output": """Thought: Lorem ipsum++Action:+```+<action tool="Name">+action input+</action>+<action tool="Name 2">+action input 2+</action>+```""",+ "expected_actions": [+ Action("Name", "action input"),+ Action("Name 2", "action input 2"),+ ],+ },+ {+ "llm_output": """+Thought: lorem ipsum++Action:+```+<action tool="Name1">content</action 1><action tool="Name2">content 2</action><action tool="Name3">content 3</action>'+```+""",+ "expected_actions": [+ Action(tool="Name1", input="content"),+ Action(tool="Name2", input="content 2"),+ Action(tool="Name3", input="content 3"),+ ],+ },+ {+ "llm_output": """Thought: I need to install the `email-validator` package using `InstallNPMDependencies`. Then, I need to write the post handler function that checks if the email is valid and sends the appropriate response. Finally, I need to add the post handler to the Express app and start the server using `app.listen()`.++Action:+```+<action tool="InstallNPMDependencies">+email-validator+</action>+```++```+<action tool="RunJavaScriptCode">+import express from 'express';+import validator from 'email-validator';++const app = express();+const port = 3000;++app.use(express.json());++app.post('/', (req, res) => {+ const email = req.body.email;+ if (!validator.validate(email)) {+ res.status(400).json({ error: 'Invalid email format' });+ } else {+ res.status(200).json({});+ }+});++app.listen(port, () => {+ console.log(`Server listening on port ${port}`);+});+</action>+```++""",+ "expected_actions": [+ Action(tool="InstallNPMDependencies", input="email-validator"),+ Action(+ tool="RunJavaScriptCode",+ input="""import express from 'express';+import validator from 'email-validator';++const app = express();+const port = 3000;++app.use(express.json());++app.post('/', (req, res) => {+ const email = req.body.email;+ if (!validator.validate(email)) {+ res.status(400).json({ error: 'Invalid email format' });+ } else {+ res.status(200).json({});+ }+});++app.listen(port, () => {+ console.log(`Server listening on port ${port}`);+});""",+ ),+ ],+ },+]++# llm_outputs = [+# """Thought: I need to install the `email-validator` package using `InstallNPMDependencies`. Then, I need to write the post handler function that checks if the email is valid and sends the appropriate response. Finally, I need to add the post handler to the Express app and start the server using `app.listen()`.++# Action:+# ```+# <action tool="InstallNPMDependencies">+# email-validator+# </action>+# ```++# ```+# <action tool="RunJavaScriptCode">+# import express from 'express';+# import validator from 'email-validator';++# const app = express();+# const port = 3000;++# app.use(express.json());++# app.post('/', (req, res) => {+# const email = req.body.email;+# if (!validator.validate(email)) {+# res.status(400).json({ error: 'Invalid email format' });+# } else {+# res.status(200).json({});+# }+# });++# app.listen(port, () => {+# console.log(`Server listening on port ${port}`);+# });+# </action>+# ```++# """,+# '<action tool="Name">content</action>',+# """+# Thought: lorem ipsum++# Action:+# ```+# <action tool="Name1">content</action 1><action tool="Name2">content 2</action><action tool="Name3">content 3</action>'+# ```+# """,+# ]+++def test_parsing_llm_action_output():+ """Test LLM action parsing."""++ output = llm_outputs[len(llm_outputs) - 1]+ parts = output["llm_output"].split("```")+ for part in parts:+ print(part)+ print("======")++ assert 1 == 1