Skip to content

Commit 65f94b4

Browse files
authored
types: allow items and defs for tools (#501)
1 parent e33ceb7 commit 65f94b4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

ollama/_types.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,19 @@ class Function(SubscriptableBaseModel):
307307
description: Optional[str] = None
308308

309309
class Parameters(SubscriptableBaseModel):
310+
model_config = ConfigDict(populate_by_name=True)
310311
type: Optional[Literal['object']] = 'object'
312+
defs: Optional[Any] = Field(None, alias='$defs')
313+
items: Optional[Any] = None
311314
required: Optional[Sequence[str]] = None
312315

313316
class Property(SubscriptableBaseModel):
314317
model_config = ConfigDict(arbitrary_types_allowed=True)
315318

316319
type: Optional[Union[str, Sequence[str]]] = None
320+
items: Optional[Any] = None
317321
description: Optional[str] = None
318-
enum: Optional[Sequence] = None
322+
enum: Optional[Sequence[Any]] = None
319323

320324
properties: Optional[Mapping[str, Property]] = None
321325

@@ -325,6 +329,15 @@ class Property(SubscriptableBaseModel):
325329

326330

327331
class ChatRequest(BaseGenerateRequest):
332+
@model_serializer(mode='wrap')
333+
def serialize_model(self, nxt):
334+
output = nxt(self)
335+
if 'tools' in output and output['tools']:
336+
for tool in output['tools']:
337+
if 'function' in tool and 'parameters' in tool['function'] and 'defs' in tool['function']['parameters']:
338+
tool['function']['parameters']['$defs'] = tool['function']['parameters'].pop('defs')
339+
return output
340+
328341
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None
329342
'Messages to chat with.'
330343

tests/test_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def only_description():
187187

188188
tool = convert_function_to_tool(only_description).model_dump()
189189
assert tool['function']['description'] == 'A function with only a description.'
190-
assert tool['function']['parameters'] == {'type': 'object', 'properties': {}, 'required': None}
190+
assert tool['function']['parameters'] == {'type': 'object', 'defs': None, 'items': None, 'required': None, 'properties': {}}
191191

192192
def only_description_with_args(x: int, y: int):
193193
"""
@@ -199,9 +199,11 @@ def only_description_with_args(x: int, y: int):
199199
assert tool['function']['description'] == 'A function with only a description.'
200200
assert tool['function']['parameters'] == {
201201
'type': 'object',
202+
'defs': None,
203+
'items': None,
202204
'properties': {
203-
'x': {'type': 'integer', 'description': '', 'enum': None},
204-
'y': {'type': 'integer', 'description': '', 'enum': None},
205+
'x': {'type': 'integer', 'description': '', 'enum': None, 'items': None},
206+
'y': {'type': 'integer', 'description': '', 'enum': None, 'items': None},
205207
},
206208
'required': ['x', 'y'],
207209
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy