From c5cf0f4f9dbcb5208b709908cba0dfc2a5ed882b Mon Sep 17 00:00:00 2001 From: ParthSareen Date: Mon, 7 Apr 2025 14:29:41 -0700 Subject: [PATCH 1/2] types: allow single or multiple types tool property --- ollama/_client.py | 20 +++++++++++--------- ollama/_types.py | 2 +- tests/test_client.py | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/ollama/_client.py b/ollama/_client.py index 541d9c82..e70725af 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -330,19 +330,21 @@ def add_two_numbers(a: int, b: int) -> int: Returns `ChatResponse` if `stream` is `False`, otherwise returns a `ChatResponse` generator. """ + request_data = ChatRequest( + model=model, + messages=[message for message in _copy_messages(messages)], + tools=[tool for tool in _copy_tools(tools)], + stream=stream, + format=format, + options=options, + keep_alive=keep_alive, + ).model_dump(exclude_none=True) + print('Sending chat request to Ollama API:', request_data) return self._request( ChatResponse, 'POST', '/api/chat', - json=ChatRequest( - model=model, - messages=[message for message in _copy_messages(messages)], - tools=[tool for tool in _copy_tools(tools)], - stream=stream, - format=format, - options=options, - keep_alive=keep_alive, - ).model_dump(exclude_none=True), + json=request_data, stream=stream, ) diff --git a/ollama/_types.py b/ollama/_types.py index 0df0ddbf..bafae9f9 100644 --- a/ollama/_types.py +++ b/ollama/_types.py @@ -313,7 +313,7 @@ class Parameters(SubscriptableBaseModel): class Property(SubscriptableBaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) - type: Optional[str] = None + type: Optional[Union[str, Sequence[str]]] = None description: Optional[str] = None properties: Optional[Mapping[str, Property]] = None diff --git a/tests/test_client.py b/tests/test_client.py index ca29806d..2130eb39 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1139,7 +1139,7 @@ def func2(y: str) -> int: 'description': 'Test function', 'parameters': { 'type': 'object', - 'properties': {'x': {'type': 'string', 'description': 'A string'}}, + 'properties': {'x': {'type': 'string', 'description': 'A string', 'enum': ['a', 'b', 'c']}, 'y': {'type': ['integer', 'number'], 'description': 'An integer'}}, 'required': ['x'], }, }, From 7cc4e5c9f75a6d0e8352349b4e9e4ce9104a4346 Mon Sep 17 00:00:00 2001 From: ParthSareen Date: Mon, 7 Apr 2025 15:58:53 -0700 Subject: [PATCH 2/2] remove debug statement --- ollama/_client.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/ollama/_client.py b/ollama/_client.py index e70725af..541d9c82 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -330,21 +330,19 @@ def add_two_numbers(a: int, b: int) -> int: Returns `ChatResponse` if `stream` is `False`, otherwise returns a `ChatResponse` generator. """ - request_data = ChatRequest( - model=model, - messages=[message for message in _copy_messages(messages)], - tools=[tool for tool in _copy_tools(tools)], - stream=stream, - format=format, - options=options, - keep_alive=keep_alive, - ).model_dump(exclude_none=True) - print('Sending chat request to Ollama API:', request_data) return self._request( ChatResponse, 'POST', '/api/chat', - json=request_data, + json=ChatRequest( + model=model, + messages=[message for message in _copy_messages(messages)], + tools=[tool for tool in _copy_tools(tools)], + stream=stream, + format=format, + options=options, + keep_alive=keep_alive, + ).model_dump(exclude_none=True), stream=stream, )