mirror of
https://github.com/huggingface/transformers.git
synced 2025-08-02 03:01:07 +06:00
Tool calling: support more types (#35776)
* Tool calling: support NoneType for function return type
This commit is contained in:
parent
f19135afc7
commit
44393df089
@ -19,6 +19,7 @@ import types
|
|||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
from types import NoneType
|
||||||
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, get_args, get_origin, get_type_hints
|
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, get_args, get_origin, get_type_hints
|
||||||
|
|
||||||
from packaging import version
|
from packaging import version
|
||||||
@ -77,6 +78,7 @@ def _get_json_schema_type(param_type: str) -> Dict[str, str]:
|
|||||||
float: {"type": "number"},
|
float: {"type": "number"},
|
||||||
str: {"type": "string"},
|
str: {"type": "string"},
|
||||||
bool: {"type": "boolean"},
|
bool: {"type": "boolean"},
|
||||||
|
NoneType: {"type": "null"},
|
||||||
Any: {},
|
Any: {},
|
||||||
}
|
}
|
||||||
if is_vision_available():
|
if is_vision_available():
|
||||||
|
@ -419,6 +419,31 @@ class JsonSchemaGeneratorTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(schema["function"], expected_schema)
|
self.assertEqual(schema["function"], expected_schema)
|
||||||
|
|
||||||
|
def test_return_none(self):
|
||||||
|
def fn(x: int) -> None:
|
||||||
|
"""
|
||||||
|
Test function
|
||||||
|
|
||||||
|
Args:
|
||||||
|
x: The first input
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
schema = get_json_schema(fn)
|
||||||
|
expected_schema = {
|
||||||
|
"name": "fn",
|
||||||
|
"description": "Test function",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"x": {"type": "integer", "description": "The first input"},
|
||||||
|
},
|
||||||
|
"required": ["x"],
|
||||||
|
},
|
||||||
|
"return": {"type": "null"},
|
||||||
|
}
|
||||||
|
self.assertEqual(schema["function"], expected_schema)
|
||||||
|
|
||||||
def test_everything_all_at_once(self):
|
def test_everything_all_at_once(self):
|
||||||
def fn(
|
def fn(
|
||||||
x: str, y: Optional[List[Union[str, int]]], z: Tuple[Union[str, int], str] = (42, "hello")
|
x: str, y: Optional[List[Union[str, int]]], z: Tuple[Union[str, int], str] = (42, "hello")
|
||||||
|
Loading…
Reference in New Issue
Block a user