mirror of
https://github.com/ollama/ollama-python.git
synced 2026-01-13 21:57:16 +08:00
types: allow items and defs for tools (#501)
Some checks failed
test / test (3.10) (push) Has been cancelled
test / test (3.11) (push) Has been cancelled
test / test (3.12) (push) Has been cancelled
test / test (3.13) (push) Has been cancelled
test / test (3.8) (push) Has been cancelled
test / test (3.9) (push) Has been cancelled
test / lint (push) Has been cancelled
Some checks failed
test / test (3.10) (push) Has been cancelled
test / test (3.11) (push) Has been cancelled
test / test (3.12) (push) Has been cancelled
test / test (3.13) (push) Has been cancelled
test / test (3.8) (push) Has been cancelled
test / test (3.9) (push) Has been cancelled
test / lint (push) Has been cancelled
This commit is contained in:
parent
e33ceb7188
commit
65f94b4fba
@ -307,15 +307,19 @@ class Tool(SubscriptableBaseModel):
|
|||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
|
|
||||||
class Parameters(SubscriptableBaseModel):
|
class Parameters(SubscriptableBaseModel):
|
||||||
|
model_config = ConfigDict(populate_by_name=True)
|
||||||
type: Optional[Literal['object']] = 'object'
|
type: Optional[Literal['object']] = 'object'
|
||||||
|
defs: Optional[Any] = Field(None, alias='$defs')
|
||||||
|
items: Optional[Any] = None
|
||||||
required: Optional[Sequence[str]] = None
|
required: Optional[Sequence[str]] = None
|
||||||
|
|
||||||
class Property(SubscriptableBaseModel):
|
class Property(SubscriptableBaseModel):
|
||||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||||
|
|
||||||
type: Optional[Union[str, Sequence[str]]] = None
|
type: Optional[Union[str, Sequence[str]]] = None
|
||||||
|
items: Optional[Any] = None
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
enum: Optional[Sequence] = None
|
enum: Optional[Sequence[Any]] = None
|
||||||
|
|
||||||
properties: Optional[Mapping[str, Property]] = None
|
properties: Optional[Mapping[str, Property]] = None
|
||||||
|
|
||||||
@ -325,6 +329,15 @@ class Tool(SubscriptableBaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class ChatRequest(BaseGenerateRequest):
|
class ChatRequest(BaseGenerateRequest):
|
||||||
|
@model_serializer(mode='wrap')
|
||||||
|
def serialize_model(self, nxt):
|
||||||
|
output = nxt(self)
|
||||||
|
if 'tools' in output and output['tools']:
|
||||||
|
for tool in output['tools']:
|
||||||
|
if 'function' in tool and 'parameters' in tool['function'] and 'defs' in tool['function']['parameters']:
|
||||||
|
tool['function']['parameters']['$defs'] = tool['function']['parameters'].pop('defs')
|
||||||
|
return output
|
||||||
|
|
||||||
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None
|
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None
|
||||||
'Messages to chat with.'
|
'Messages to chat with.'
|
||||||
|
|
||||||
|
|||||||
@ -187,7 +187,7 @@ def test_function_with_only_description():
|
|||||||
|
|
||||||
tool = convert_function_to_tool(only_description).model_dump()
|
tool = convert_function_to_tool(only_description).model_dump()
|
||||||
assert tool['function']['description'] == 'A function with only a description.'
|
assert tool['function']['description'] == 'A function with only a description.'
|
||||||
assert tool['function']['parameters'] == {'type': 'object', 'properties': {}, 'required': None}
|
assert tool['function']['parameters'] == {'type': 'object', 'defs': None, 'items': None, 'required': None, 'properties': {}}
|
||||||
|
|
||||||
def only_description_with_args(x: int, y: int):
|
def only_description_with_args(x: int, y: int):
|
||||||
"""
|
"""
|
||||||
@ -199,9 +199,11 @@ def test_function_with_only_description():
|
|||||||
assert tool['function']['description'] == 'A function with only a description.'
|
assert tool['function']['description'] == 'A function with only a description.'
|
||||||
assert tool['function']['parameters'] == {
|
assert tool['function']['parameters'] == {
|
||||||
'type': 'object',
|
'type': 'object',
|
||||||
|
'defs': None,
|
||||||
|
'items': None,
|
||||||
'properties': {
|
'properties': {
|
||||||
'x': {'type': 'integer', 'description': '', 'enum': None},
|
'x': {'type': 'integer', 'description': '', 'enum': None, 'items': None},
|
||||||
'y': {'type': 'integer', 'description': '', 'enum': None},
|
'y': {'type': 'integer', 'description': '', 'enum': None, 'items': None},
|
||||||
},
|
},
|
||||||
'required': ['x', 'y'],
|
'required': ['x', 'y'],
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user