From f3a4a391263ad86caedaae16d50031d43929f81e Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Tue, 13 Jun 2023 10:27:33 -0700 Subject: [PATCH 1/3] convert tools to openai --- langchain/tools/__init__.py | 2 ++ langchain/tools/convert_to_openai.py | 53 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 langchain/tools/convert_to_openai.py diff --git a/langchain/tools/__init__.py b/langchain/tools/__init__.py index 06c2a9be8ad07..4b93bb6c501d8 100644 --- a/langchain/tools/__init__.py +++ b/langchain/tools/__init__.py @@ -9,6 +9,7 @@ from langchain.tools.base import BaseTool, StructuredTool, Tool, tool from langchain.tools.bing_search.tool import BingSearchResults, BingSearchRun from langchain.tools.brave_search.tool import BraveSearch +from langchain.tools.convert_to_openai import format_tool_to_openai_function from langchain.tools.ddg_search.tool import DuckDuckGoSearchResults, DuckDuckGoSearchRun from langchain.tools.file_management.copy import CopyFileTool from langchain.tools.file_management.delete import DeleteFileTool @@ -122,4 +123,5 @@ "YouTubeSearchTool", "BraveSearch", "PubmedQueryRun", + "format_tool_to_openai_function", ] diff --git a/langchain/tools/convert_to_openai.py b/langchain/tools/convert_to_openai.py new file mode 100644 index 0000000000000..d780e8ab299be --- /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, + } From e1248b146a463373728c52729680fbc3ad7519c1 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Tue, 13 Jun 2023 10:35:04 -0700 Subject: [PATCH 2/3] cr --- tests/unit_tests/tools/test_public_api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit_tests/tools/test_public_api.py b/tests/unit_tests/tools/test_public_api.py index c57de5813961f..f5e8d33a2e9c7 100644 --- a/tests/unit_tests/tools/test_public_api.py +++ b/tests/unit_tests/tools/test_public_api.py @@ -62,6 +62,7 @@ "YouTubeSearchTool", "BraveSearch", "PubmedQueryRun", + "format_tool_to_openai_function", ] From b6b0e8b4ac3d9472af0e20779bb357e77b9426b8 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Tue, 13 Jun 2023 10:39:23 -0700 Subject: [PATCH 3/3] add notebook on functions --- docs/modules/agents/tools.rst | 9 ++ .../tools/tools_as_openai_functions.ipynb | 138 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 docs/modules/agents/tools/tools_as_openai_functions.ipynb diff --git a/docs/modules/agents/tools.rst b/docs/modules/agents/tools.rst index e94543493fc8b..02e134a1ff3da 100644 --- a/docs/modules/agents/tools.rst +++ b/docs/modules/agents/tools.rst @@ -25,6 +25,15 @@ Next, we have some examples of customizing and generically working with tools ./tools/custom_tools.ipynb ./tools/multi_input_tool.ipynb ./tools/tool_input_validation.ipynb + ./tools/human_approval.ipynb + +Tools are also usable outside of the LangChain ecosystem! Here are examples of doing so + +.. toctree:: + :maxdepth: 1 + :glob: + + ./tools/tools_as_openai_functions.ipynb In this documentation we cover generic tooling functionality (eg how to create your own) diff --git a/docs/modules/agents/tools/tools_as_openai_functions.ipynb b/docs/modules/agents/tools/tools_as_openai_functions.ipynb new file mode 100644 index 0000000000000..6fe92dfd79aa5 --- /dev/null +++ b/docs/modules/agents/tools/tools_as_openai_functions.ipynb @@ -0,0 +1,138 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4111c9d4", + "metadata": {}, + "source": [ + "# Tools as OpenAI Functions\n", + "\n", + "This notebook goes over how to use LangChain tools as OpenAI functions." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d65d8a60", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chat_models import ChatOpenAI\n", + "from langchain.schema import HumanMessage" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "abd8dc72", + "metadata": {}, + "outputs": [], + "source": [ + "model = ChatOpenAI(model=\"gpt-3.5-turbo-0613\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "dce2cdb7", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.tools import MoveFileTool, format_tool_to_openai_function" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "3b3dc766", + "metadata": {}, + "outputs": [], + "source": [ + "tools = [MoveFileTool()]\n", + "functions = [format_tool_to_openai_function(t) for t in tools]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "230a7939", + "metadata": {}, + "outputs": [], + "source": [ + "message = model.predict_messages([HumanMessage(content='move file foo to bar')], functions=functions)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "c118c940", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content='', additional_kwargs={'function_call': {'name': 'move_file', 'arguments': '{\\n \"source_path\": \"foo\",\\n \"destination_path\": \"bar\"\\n}'}}, example=False)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "message" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "d618e3d8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'move_file',\n", + " 'arguments': '{\\n \"source_path\": \"foo\",\\n \"destination_path\": \"bar\"\\n}'}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "message.additional_kwargs['function_call']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "751da79f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}