|
|
|
@ -270,7 +270,8 @@ def convert_to_openai_function(
|
|
|
|
|
Args:
|
|
|
|
|
function: Either a dictionary, a pydantic.BaseModel class, or a Python function.
|
|
|
|
|
If a dictionary is passed in, it is assumed to already be a valid OpenAI
|
|
|
|
|
function.
|
|
|
|
|
function or a JSON schema with top-level 'title' and 'description' keys
|
|
|
|
|
specified.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
A dict version of the passed in function which is compatible with the
|
|
|
|
@ -278,8 +279,21 @@ def convert_to_openai_function(
|
|
|
|
|
"""
|
|
|
|
|
from langchain_core.tools import BaseTool
|
|
|
|
|
|
|
|
|
|
if isinstance(function, dict):
|
|
|
|
|
# already in OpenAI function format
|
|
|
|
|
if isinstance(function, dict) and all(
|
|
|
|
|
k in function for k in ("name", "description", "parameters")
|
|
|
|
|
):
|
|
|
|
|
return function
|
|
|
|
|
# a JSON schema with title and description
|
|
|
|
|
elif isinstance(function, dict) and all(
|
|
|
|
|
k in function for k in ("title", "description", "properties")
|
|
|
|
|
):
|
|
|
|
|
function = function.copy()
|
|
|
|
|
return {
|
|
|
|
|
"name": function.pop("title"),
|
|
|
|
|
"description": function.pop("description"),
|
|
|
|
|
"parameters": function,
|
|
|
|
|
}
|
|
|
|
|
elif isinstance(function, type) and issubclass(function, BaseModel):
|
|
|
|
|
return cast(Dict, convert_pydantic_to_openai_function(function))
|
|
|
|
|
elif isinstance(function, BaseTool):
|
|
|
|
@ -288,8 +302,10 @@ def convert_to_openai_function(
|
|
|
|
|
return convert_python_function_to_openai_function(function)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"Unsupported function type {type(function)}. Functions must be passed in"
|
|
|
|
|
f" as Dict, pydantic.BaseModel, or Callable."
|
|
|
|
|
f"Unsupported function\n\n{function}\n\nFunctions must be passed in"
|
|
|
|
|
" as Dict, pydantic.BaseModel, or Callable. If they're a dict they must"
|
|
|
|
|
" either be in OpenAI function format or valid JSON schema with top-level"
|
|
|
|
|
" 'title' and 'description' keys."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -301,13 +317,14 @@ def convert_to_openai_tool(
|
|
|
|
|
Args:
|
|
|
|
|
tool: Either a dictionary, a pydantic.BaseModel class, Python function, or
|
|
|
|
|
BaseTool. If a dictionary is passed in, it is assumed to already be a valid
|
|
|
|
|
OpenAI tool or OpenAI function.
|
|
|
|
|
OpenAI tool, OpenAI function, or a JSON schema with top-level 'title' and
|
|
|
|
|
'description' keys specified.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
A dict version of the passed in tool which is compatible with the
|
|
|
|
|
OpenAI tool-calling API.
|
|
|
|
|
"""
|
|
|
|
|
if isinstance(tool, dict) and "type" in tool:
|
|
|
|
|
if isinstance(tool, dict) and tool.get("type") == "function" and "function" in tool:
|
|
|
|
|
return tool
|
|
|
|
|
function = convert_to_openai_function(tool)
|
|
|
|
|
return {"type": "function", "function": function}
|
|
|
|
|