Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions examples/async-tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ def add_two_numbers(a: int, b: int) -> int:
Returns:
int: The sum of the two numbers
"""
return a + b
# Tool arguments may contain numeric strings even for integer parameters.
return int(a) + int(b)


def subtract_two_numbers(a: int, b: int) -> int:
"""
Subtract two numbers
"""
return a - b
return int(a) - int(b)


# Tools can still be manually defined and passed into chat
Expand Down
44 changes: 44 additions & 0 deletions tests/test_async_tools_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import runpy
from pathlib import Path
from unittest.mock import AsyncMock, patch

import pytest

from ollama import ChatResponse


@pytest.fixture
def anyio_backend():
return 'asyncio'


@pytest.mark.anyio
@pytest.mark.parametrize(
('function_name', 'arguments', 'expected'),
[
('add_two_numbers', {'a': '30', 'b': '12'}, '42'),
('subtract_two_numbers', {'a': '30', 'b': '12'}, '18'),
('add_two_numbers', {'a': 30, 'b': 12}, '42'),
('subtract_two_numbers', {'a': 30, 'b': 12}, '18'),
],
)
async def test_async_tools_example_returns_numeric_tool_results(function_name, arguments, expected):
example = runpy.run_path(str(Path(__file__).parents[1] / 'examples' / 'async-tools.py'))
tool_response = ChatResponse(
message={
'role': 'assistant',
'tool_calls': [{'function': {'name': function_name, 'arguments': arguments}}],
},
)
chat = AsyncMock(side_effect=[tool_response, ChatResponse(message={'role': 'assistant', 'content': 'Done'})])

with patch('ollama.AsyncClient') as client:
client.return_value.chat = chat
await example['main']()

assert chat.await_count == 2
assert chat.await_args_list[1].kwargs['messages'] == [
{'role': 'user', 'content': 'What is three plus one?'},
tool_response.message,
{'role': 'tool', 'content': expected, 'tool_name': function_name},
]