Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

core[patch]: support labeled json schema as tools #18935

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions libs/core/langchain_core/utils/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,20 @@ def convert_to_openai_function(
"""
from langchain_core.tools import BaseTool

if isinstance(function, dict):
# already in OpenAI function format
baskaryan marked this conversation as resolved.
Show resolved Hide resolved
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")
):
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):
Expand All @@ -288,8 +300,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 {function}. Functions 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."
)


Expand Down
27 changes: 24 additions & 3 deletions libs/core/tests/unit_tests/utils/test_function_calling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, List, Literal, Optional, Type
from typing import Any, Callable, Dict, List, Literal, Optional, Type

import pytest

Expand Down Expand Up @@ -49,8 +49,29 @@ def _run(self, *args: Any, **kwargs: Any) -> Any:
return DummyFunction()


@pytest.fixture()
def json_schema() -> Dict:
return {
"title": "dummy_function",
"description": "dummy function",
"type": "object",
"properties": {
"arg1": {"description": "foo", "type": "integer"},
"arg2": {
"description": "one of 'bar', 'baz'",
"enum": ["bar", "baz"],
"type": "string",
},
},
"required": ["arg1", "arg2"],
}


def test_convert_to_openai_function(
pydantic: Type[BaseModel], function: Callable, dummy_tool: BaseTool
pydantic: Type[BaseModel],
function: Callable,
dummy_tool: BaseTool,
json_schema: Dict,
) -> None:
expected = {
"name": "dummy_function",
Expand All @@ -69,7 +90,7 @@ def test_convert_to_openai_function(
},
}

for fn in (pydantic, function, dummy_tool, expected):
for fn in (pydantic, function, dummy_tool, json_schema, expected):
actual = convert_to_openai_function(fn) # type: ignore
assert actual == expected

Expand Down
Loading