The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
convert tools to openai (#6100)
Harrison Chase970b2f9d3863
diff --git a/langchain/tools/__init__.py b/langchain/tools/__init__.pyindex 06c2a9be8a..4b93bb6c50 100644--- a/langchain/tools/__init__.py+++ b/langchain/tools/__init__.py@@ -9,6 +9,7 @@ from langchain.tools.azure_cognitive_services import (from langchain.tools.base import BaseTool, StructuredTool, Tool, toolfrom langchain.tools.bing_search.tool import BingSearchResults, BingSearchRunfrom langchain.tools.brave_search.tool import BraveSearch+from langchain.tools.convert_to_openai import format_tool_to_openai_functionfrom langchain.tools.ddg_search.tool import DuckDuckGoSearchResults, DuckDuckGoSearchRunfrom langchain.tools.file_management.copy import CopyFileToolfrom langchain.tools.file_management.delete import DeleteFileTool@@ -122,4 +123,5 @@ __all__ = ["YouTubeSearchTool","BraveSearch","PubmedQueryRun",+ "format_tool_to_openai_function",]diff --git a/langchain/tools/convert_to_openai.py b/langchain/tools/convert_to_openai.pynew file mode 100644index 0000000000..d780e8ab29--- /dev/null+++ b/langchain/tools/convert_to_openai.py@@ -0,0 +1,53 @@+from typing import TypedDict++from langchain.tools import BaseTool, StructuredTool+++class FunctionDescription(TypedDict):+ """Representation of a callable function to the OpenAI API."""++ name: str+ """The name of the function."""+ description: str+ """A description of the function."""+ parameters: dict+ """The parameters of the function."""+++def format_tool_to_openai_function(tool: BaseTool) -> FunctionDescription:+ """Format tool into the open AI function API."""+ if isinstance(tool, StructuredTool):+ schema_ = tool.args_schema.schema()+ # Bug with required missing for structured tools.+ required = sorted(schema_["properties"]) # BUG WORKAROUND+ return {+ "name": tool.name,+ "description": tool.description,+ "parameters": {+ "type": "object",+ "properties": schema_["properties"],+ "required": required,+ },+ }+ else:+ if tool.args_schema:+ parameters = tool.args_schema.schema()+ else:+ parameters = {+ # This is a hack to get around the fact that some tools+ # do not expose an args_schema, and expect an argument+ # which is a string.+ # And Open AI does not support an array type for the+ # parameters.+ "properties": {+ "__arg1": {"title": "__arg1", "type": "string"},+ },+ "required": ["__arg1"],+ "type": "object",+ }++ return {+ "name": tool.name,+ "description": tool.description,+ "parameters": parameters,+ }diff --git a/tests/unit_tests/tools/test_public_api.py b/tests/unit_tests/tools/test_public_api.pyindex c57de58139..f5e8d33a2e 100644--- a/tests/unit_tests/tools/test_public_api.py+++ b/tests/unit_tests/tools/test_public_api.py@@ -62,6 +62,7 @@ _EXPECTED = ["YouTubeSearchTool","BraveSearch","PubmedQueryRun",+ "format_tool_to_openai_function",]