Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Update langchain
Tomas Valenta67642bcd9fe3
diff --git a/api-service/codegen/agent/base.py b/api-service/codegen/agent/base.pyindex c683db281..cd41ab2f7 100644--- a/api-service/codegen/agent/base.py+++ b/api-service/codegen/agent/base.py@@ -1,5 +1,3 @@-import traceback-from typing import Optional, Tuple, List, Dict, Union, castfrom langchain.agents.tools import InvalidToolfrom langchain.agents import AgentExecutor@@ -7,10 +5,8 @@ from langchain.agents.chat.base import ChatAgentfrom langchain.schema import AgentAction, AgentFinishfrom langchain.tools.base import BaseTool-from codegen.agent.parsing import parse_thoughts_and_actions, ToolLog-# FINAL_ANSWER_TRIGGERS = ["Final Answer:", "FinalAnswer", "Final Answer"]FINAL_ANSWER_ACTION = "Final Answer:"FINAL_ANSWER_ACTION_NO_WHITESPACE = "FinalAnswer"@@ -24,24 +20,12 @@ class CodegenAgent(ChatAgent):# if any(trigger in text for trigger in FINAL_ANSWER_TRIGGERS):if FINAL_ANSWER_ACTION in text:return "Final Answer", text.split(FINAL_ANSWER_ACTION)[-1].strip()- try:- # `ACTIONS_QUEUE` is not a real tool. The original implementation of `extract_tool_and_input` parses LLM's output. Our override doesn't do that.- # Instead, we parse the LLM's output inside the `_atake_next_step` of the AgentExecutor. We do that because sometimes the LLM malforms- # the desired format we specified in the prompt and puts <action> tags in the wrong place.- # We say "the raw LLM's output is an input of ACTIONS_QUEUE tool" and parse the ACTIONS_QUEUE "tool" in the `_atake_next_step` while trying to be more resilient towards- # malforrmed input and <action> tags in unexpected places.- return ACTIONS_QUEUE, text- except Exception as e:- print(traceback.format_exc())- 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.- # TODO: Improve, it doesn't always need to be malformed answer.- 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 with `xml.etree.ElementTree.fromstring()` and received this error:\n{e}Reminder, that you should follow the format I told you!",- )+ # `ACTIONS_QUEUE` is not a real tool. The original implementation of `extract_tool_and_input` parses LLM's output. Our override doesn't do that.+ # Instead, we parse the LLM's output inside the `_atake_next_step` of the AgentExecutor. We do that because sometimes the LLM malforms+ # the desired format we specified in the prompt and puts <action> tags in the wrong place.+ # We say "the raw LLM's output is an input of ACTIONS_QUEUE tool" and parse the ACTIONS_QUEUE "tool" in the `_atake_next_step` while trying to be more resilient towards+ # malforrmed input and <action> tags in unexpected places.+ return ACTIONS_QUEUE, textclass CodegenAgentExecutor(AgentExecutor):@@ -55,24 +39,19 @@ class CodegenAgentExecutor(AgentExecutor):# Otherwise we lookup the toolif tool_name in name_to_tool_map:tool = name_to_tool_map[tool_name]- # return_direct = tool.return_directcolor = color_mapping[tool_name]- # llm_prefix = "" if return_direct else self.agent.llm_prefix# We then call the tool on the tool input to get an observationobservation = await tool.arun(tool_input,verbose=self.verbose,color=color,- # llm_prefix=llm_prefix,- # observation_prefix=self.agent.observation_prefix,)else:observation = await InvalidTool().arun( # type: ignore- tool_name,+ tool_name=tool_name,+ tool_input=tool_input,verbose=self.verbose,color=None,- # llm_prefix="",- # observation_prefix=self.agent.observation_prefix,)return observation@@ -82,7 +61,7 @@ class CodegenAgentExecutor(AgentExecutor):color_mapping: Dict[str, str],inputs: Dict[str, str],intermediate_steps: List[Tuple[AgentAction, str]],- ) -> Union[AgentFinish, Tuple[AgentAction, str]]:+ ) -> Union[AgentFinish, List[Tuple[AgentAction, str]]]:"""Take a single step in the thought-action-observation loop.Override this to take control of how the agent makes and acts on choices.@@ -95,54 +74,61 @@ class CodegenAgentExecutor(AgentExecutor):# Sometimes the LLM decides to pass the value of `FINAL_ANSWER_ACTION` as a tool name.# Instead of trying to "force" the LLM to don't do that, we can just write a bit of# code and handle this case.- elif (- output.tool == FINAL_ANSWER_ACTION- or output.tool == FINAL_ANSWER_ACTION_NO_WHITESPACE- ):- return AgentFinish({"output": output.tool_input}, output.log)+ actions = [output] if isinstance(output, AgentAction) else output++ # TODO: Handle malformed actions# 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.- if output.tool == MALFORMED_ANSWER:- return output, output.tool_input- if self.callback_manager.is_async:- await self.callback_manager.on_agent_action(- output, verbose=self.verbose, color="green"- )- else:- self.callback_manager.on_agent_action(- output, verbose=self.verbose, color="green"- )+ observations: List[Tuple[AgentAction, str]] = []- if output.tool == ACTIONS_QUEUE:- # TODO: Assign observations to each tool separately. Currently we return observation only from the last tool in the list.- observation = ""- # Go through each action and run it.- # Collect outputs from each action.- for action in (- cast(ToolLog, action)- for action in parse_thoughts_and_actions(output.tool_input)- if action["type"] == "tool"+ for action in actions:+ if self.callback_manager.is_async:+ await self.callback_manager.on_agent_action(+ action,+ verbose=self.verbose,+ color="green",+ )+ else:+ self.callback_manager.on_agent_action(+ action,+ verbose=self.verbose,+ color="green",+ )++ if action.tool in (+ FINAL_ANSWER_ACTION,+ FINAL_ANSWER_ACTION_NO_WHITESPACE,):- observation = await self._arun_tool(- tool_name=action["tool_name"],- tool_input=action["tool_input"],+ return AgentFinish({"output": action.tool_input}, action.log)++ # Parse action queues+ tools = self.unwrap_actions_queue(action)++ tools_observations: List[str] = []++ for tool in tools:+ # We are running the tools one after another instead of using async gather+ # because order matter for the playground tools.+ tool_observation = await self._arun_tool(+ tool_name=tool["tool_name"],+ tool_input=tool["tool_input"],name_to_tool_map=name_to_tool_map,color_mapping=color_mapping,)- # observation = (- # observation- # + f"Output of action '{tool_name}':\n"- # + self._run_tool(- # tool_name=tool_name,- # tool_input=tool_input,- # name_to_tool_map=name_to_tool_map,- # color_mapping=color_mapping,- # )- # + "==="- # )- else:- raise ValueError("Unknown tool:", output.tool)- return output, observation+ tools_observations.append(tool_observation)++ # We don't return tool but the actions queue because we cannot reasonable reconstruct the log for each tool and we also want the "Thought" part to be there.+ observations.append((action, "\n".join(tools_observations)))++ return observations++ def unwrap_actions_queue(self, action: AgentAction):+ if action.tool == ACTIONS_QUEUE:+ yield from (+ cast(ToolLog, action)+ for action in parse_thoughts_and_actions(action.tool_input)+ if action["type"] == "tool"+ )diff --git a/api-service/codegen/codegen.py b/api-service/codegen/codegen.pyindex 1ade8201a..fc03f92ad 100644--- a/api-service/codegen/codegen.py+++ b/api-service/codegen/codegen.py@@ -28,27 +28,6 @@ from codegen.prompt import (get_human_instructions_prefix,)-# class OutputFinalCode(BaseTool):-# name = "OutputFinalCode"-# description = "This is the last tool you would use. You use it when you know the final server code and you want to output it. The input should be the final server code that does what the user required."--# def _run(self, final_code: str) -> str:-# return final_code--# async def _arun(self, final_code: str) -> str:-# raise NotImplementedError("OutputFinalCode does not support async")---# testing_instructions = """Here are your instructions:-# 1. Extract `email` from the incoming POST request.-# 2. If there's no email, respond back with an error.-# 3. Otherwise, respond back with the part of the email before the '@' sign.-# 4. Generate the full required server code and make sure it starts without any errors.-# 5. Test that the generated server from the previous step behaves as is required by making mock `curl` requests to the server.-# 6. Once all works without any bugs and errors, write the code to the file.-# 7. Deploy the code.-# """-class Codegen(BaseModel):input_variables: ClassVar[List[str]] = ["input", "agent_scratchpad", "method"]diff --git a/api-service/poetry.lock b/api-service/poetry.lockindex 085ff3c68..7d1d20577 100644--- a/api-service/poetry.lock+++ b/api-service/poetry.lock@@ -227,6 +227,18 @@ files = [{file = "blinker-1.5.tar.gz", hash = "sha256:923e5e2f69c155f2cc42dafbbd70e16e3fde24d2d4aa2ab72fbe386238892462"},]+[[package]]+name = "cachetools"+version = "5.3.0"+description = "Extensible memoizing collections and decorators"+category = "main"+optional = false+python-versions = "~=3.7"+files = [+ {file = "cachetools-5.3.0-py3-none-any.whl", hash = "sha256:429e1a1e845c008ea6c85aa35d4b98b65d6a9763eeef3e37e92728a12d1de9d4"},+ {file = "cachetools-5.3.0.tar.gz", hash = "sha256:13dfddc7b8df938c21a940dfa6557ce6e94a2f1cdfa58eb90c805721d58f2c14"},+]+[[package]]name = "certifi"version = "2022.12.7"@@ -501,6 +513,23 @@ files = [{file = "frozenlist-1.3.3.tar.gz", hash = "sha256:58bcc55721e8a90b88332d6cd441261ebb22342e238296bb330968952fbb3a6a"},]+[[package]]+name = "gptcache"+version = "0.1.10"+description = "GPTCache, a powerful caching library that can be used to speed up and lower the cost of chat applications that rely on the LLM service. GPTCache works as a memcache for AIGC applications, similar to how Redis works for traditional applications."+category = "main"+optional = false+python-versions = ">=3.8.1"+files = [+ {file = "gptcache-0.1.10-py3-none-any.whl", hash = "sha256:6d5baaed83784164d1cad29423c4e3433a7ae3a8794f7f4785d44ee27691dc19"},+ {file = "gptcache-0.1.10.tar.gz", hash = "sha256:75f63b181e5ce40aff1859711fe9496bd7569e3b2afcac3ec46cd98a8a0ce974"},+]++[package.dependencies]+cachetools = "*"+numpy = "*"+openai = "*"+[[package]]name = "greenlet"version = "2.0.2"@@ -860,20 +889,23 @@ i18n = ["Babel (>=2.7)"][[package]]name = "langchain"-version = "0.0.132"+version = "0.0.139"description = "Building applications with LLMs through composability"category = "main"optional = falsepython-versions = ">=3.8.1,<4.0"files = [- {file = "langchain-0.0.132-py3-none-any.whl", hash = "sha256:95fc748797467c24d3432ddddb41f04f5fa4a57f4c53d3b75c352c628faf6bc5"},- {file = "langchain-0.0.132.tar.gz", hash = "sha256:12fd6e159f5a1a155cb6702491fa4392fcde7dd6752d6263c9737cdeae570220"},+ {file = "langchain-0.0.139-py3-none-any.whl", hash = "sha256:2ea43780e1f788333f2b741b119d9870640a92c6f5f2d177d1ca0c98463bd097"},+ {file = "langchain-0.0.139.tar.gz", hash = "sha256:4ea86ad6192f45c65cbbdc24ff1a56aaac4bee8c038be982981369ca553590df"},][package.dependencies]aiohttp = ">=3.8.3,<4.0.0"+async-timeout = {version = ">=4.0.0,<5.0.0", markers = "python_version < \"3.11\""}dataclasses-json = ">=0.5.7,<0.6.0"+gptcache = ">=0.1.7"numpy = ">=1,<2"+openapi-schema-pydantic = ">=1.2,<2.0"pydantic = ">=1,<2"PyYAML = ">=5.4.1"requests = ">=2,<3"@@ -881,11 +913,11 @@ SQLAlchemy = ">=1,<2"tenacity = ">=8.1.0,<9.0.0"[package.extras]-all = ["aleph-alpha-client (>=2.15.0,<3.0.0)", "anthropic (>=0.2.4,<0.3.0)", "beautifulsoup4 (>=4,<5)", "boto3 (>=1.26.96,<2.0.0)", "cohere (>=3,<4)", "deeplake (>=3.2.9,<4.0.0)", "elasticsearch (>=8,<9)", "faiss-cpu (>=1,<2)", "google-api-python-client (==2.70.0)", "google-search-results (>=2,<3)", "huggingface_hub (>=0,<1)", "jina (>=3.14,<4.0)", "jinja2 (>=3,<4)", "manifest-ml (>=0.0.1,<0.0.2)", "networkx (>=2.6.3,<3.0.0)", "nlpcloud (>=1,<2)", "nltk (>=3,<4)", "nomic (>=1.0.43,<2.0.0)", "openai (>=0,<1)", "opensearch-py (>=2.0.0,<3.0.0)", "pgvector (>=0.1.6,<0.2.0)", "pinecone-client (>=2,<3)", "psycopg2-binary (>=2.9.5,<3.0.0)", "pyowm (>=3.3.0,<4.0.0)", "pypdf (>=3.4.0,<4.0.0)", "qdrant-client (>=1.1.1,<2.0.0)", "redis (>=4,<5)", "sentence-transformers (>=2,<3)", "spacy (>=3,<4)", "tensorflow-text (>=2.11.0,<3.0.0)", "tiktoken (>=0.3.2,<0.4.0)", "torch (>=1,<2)", "transformers (>=4,<5)", "weaviate-client (>=3,<4)", "wikipedia (>=1,<2)", "wolframalpha (==5.0.0)"]+all = ["aleph-alpha-client (>=2.15.0,<3.0.0)", "anthropic (>=0.2.4,<0.3.0)", "beautifulsoup4 (>=4,<5)", "cohere (>=3,<4)", "deeplake (>=3.2.21,<4.0.0)", "elasticsearch (>=8,<9)", "faiss-cpu (>=1,<2)", "google-api-python-client (==2.70.0)", "google-search-results (>=2,<3)", "huggingface_hub (>=0,<1)", "jina (>=3.14,<4.0)", "jinja2 (>=3,<4)", "manifest-ml (>=0.0.1,<0.0.2)", "networkx (>=2.6.3,<3.0.0)", "nlpcloud (>=1,<2)", "nltk (>=3,<4)", "nomic (>=1.0.43,<2.0.0)", "openai (>=0,<1)", "opensearch-py (>=2.0.0,<3.0.0)", "pgvector (>=0.1.6,<0.2.0)", "pinecone-client (>=2,<3)", "pinecone-text (>=0.4.2,<0.5.0)", "psycopg2-binary (>=2.9.5,<3.0.0)", "pyowm (>=3.3.0,<4.0.0)", "pypdf (>=3.4.0,<4.0.0)", "qdrant-client (>=1.1.2,<2.0.0)", "redis (>=4,<5)", "sentence-transformers (>=2,<3)", "spacy (>=3,<4)", "tensorflow-text (>=2.11.0,<3.0.0)", "tiktoken (>=0.3.2,<0.4.0)", "torch (>=1,<2)", "transformers (>=4,<5)", "weaviate-client (>=3,<4)", "wikipedia (>=1,<2)", "wolframalpha (==5.0.0)"]cohere = ["cohere (>=3,<4)"]llms = ["anthropic (>=0.2.4,<0.3.0)", "cohere (>=3,<4)", "huggingface_hub (>=0,<1)", "manifest-ml (>=0.0.1,<0.0.2)", "nlpcloud (>=1,<2)", "openai (>=0,<1)", "torch (>=1,<2)", "transformers (>=4,<5)"]openai = ["openai (>=0,<1)"]-qdrant = ["qdrant-client (>=1.1.1,<2.0.0)"]+qdrant = ["qdrant-client (>=1.1.2,<2.0.0)"][[package]]name = "lxml"@@ -1233,16 +1265,31 @@ dev = ["black (>=21.6b0,<22.0)", "pytest (>=6.0.0,<7.0.0)", "pytest-asyncio", "pembeddings = ["matplotlib", "numpy", "openpyxl (>=3.0.7)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)", "plotly", "scikit-learn (>=1.0.2)", "scipy", "tenacity (>=8.0.1)"]wandb = ["numpy", "openpyxl (>=3.0.7)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)", "wandb"]+[[package]]+name = "openapi-schema-pydantic"+version = "1.2.4"+description = "OpenAPI (v3) specification schema as pydantic class"+category = "main"+optional = false+python-versions = ">=3.6.1"+files = [+ {file = "openapi-schema-pydantic-1.2.4.tar.gz", hash = "sha256:3e22cf58b74a69f752cc7e5f1537f6e44164282db2700cbbcd3bb99ddd065196"},+ {file = "openapi_schema_pydantic-1.2.4-py3-none-any.whl", hash = "sha256:a932ecc5dcbb308950282088956e94dea069c9823c84e507d64f6b622222098c"},+]++[package.dependencies]+pydantic = ">=1.8.2"+[[package]]name = "packaging"-version = "23.0"+version = "23.1"description = "Core utilities for Python packages"category = "main"optional = falsepython-versions = ">=3.7"files = [- {file = "packaging-23.0-py3-none-any.whl", hash = "sha256:714ac14496c3e68c99c29b00845f7a2b85f3bb6f1078fd9f72fd20f0570002b2"},- {file = "packaging-23.0.tar.gz", hash = "sha256:b6ad297f8907de0fa2fe1ccbd26fdaf387f5f47c7275fedf8cce89f99446cf97"},+ {file = "packaging-23.1-py3-none-any.whl", hash = "sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61"},+ {file = "packaging-23.1.tar.gz", hash = "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f"},][[package]]@@ -2095,4 +2142,4 @@ multidict = ">=4.0"[metadata]lock-version = "2.0"python-versions = ">=3.10,<4.0"-content-hash = "a23dd82454720cc5fbb8bf48bcb63514f7950463121cb2911d63a9731fcd9a69"+content-hash = "3794275866ddd2394048ea4dbc852a0a5c29a6ac6c5de802eab895d40ad0e905"diff --git a/api-service/pyproject.toml b/api-service/pyproject.tomlindex ae3e89855..9e8524194 100644--- a/api-service/pyproject.toml+++ b/api-service/pyproject.toml@@ -15,10 +15,10 @@ lxml = "^4.9.2"asyncio = "^3.4.3"postgrest = "^0.10.6"quart-cors = "^0.6.0"-langchain = "^0.0.132"quart = "^0.18.4"replicate = "^0.8.1"huggingface-hub = "^0.13.4"+langchain = "^0.0.139"[tool.poetry.group.dev]optional = true