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
8 changes: 8 additions & 0 deletions ollama/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ollama._client import AsyncClient, Client
from importlib.metadata import version, PackageNotFoundError
from ollama._types import (
ChatResponse,
EmbeddingsResponse,
Expand Down Expand Up @@ -57,3 +58,10 @@
ps = _client.ps
web_search = _client.web_search
web_fetch = _client.web_fetch
exists = _client.exists


try:
__version__ = version("ollama")
except PackageNotFoundError:
__version__ = "unknown"
8 changes: 8 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ def chat(
top_logprobs: Optional[int] = None,
format: Optional[Union[Literal['', 'json'], JsonSchemaValue]] = None,
options: Optional[Union[Mapping[str, Any], Options]] = None,
tool_choice: Optional[Literal['auto', 'none', 'required']] = None,
keep_alive: Optional[Union[float, str]] = None,
) -> Union[ChatResponse, Iterator[ChatResponse]]:
"""
Expand Down Expand Up @@ -396,6 +397,7 @@ def add_two_numbers(a: int, b: int) -> int:
think=think,
logprobs=logprobs,
top_logprobs=top_logprobs,
tool_choice=tool_choice,
format=format,
options=options,
keep_alive=keep_alive,
Expand Down Expand Up @@ -670,6 +672,12 @@ def ps(self) -> ProcessResponse:
'GET',
'/api/ps',
)
def exists(self, model: str) -> bool:
try:
self.show(model)
return True
except Exception:
return False

def web_search(self, query: str, max_results: int = 3) -> WebSearchResponse:
"""
Expand Down
6 changes: 5 additions & 1 deletion ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ class ToolCall(SubscriptableBaseModel):
"""
Model tool calls.
"""
id: Optional[str] = None

class Function(SubscriptableBaseModel):
"""
Expand Down Expand Up @@ -400,6 +401,9 @@ def serialize_model(self, nxt):
tools: Optional[Sequence[Tool]] = None
'Tools to use for the chat.'

tool_choice: Optional[str] = None
'Controls which tool the model should use. Options: "auto", "none", "required".'

think: Optional[Union[bool, Literal['low', 'medium', 'high']]] = None
'Enable thinking mode (for thinking models).'

Expand Down Expand Up @@ -572,7 +576,7 @@ class ShowResponse(SubscriptableBaseModel):

details: Optional[ModelDetails] = None

modelinfo: Optional[Mapping[str, Any]] = Field(alias='model_info')
modelinfo: Optional[Mapping[str, Any]] = Field(default=None, alias='model_info')

parameters: Optional[str] = None

Expand Down
13 changes: 13 additions & 0 deletions tests/test_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
sys.path.insert(0, '.') # force Python to use local folder first

from ollama._types import ShowResponse

test_data = {
"template": "test template",
"details": None,
}

response = ShowResponse(**test_data)
print(f"modelinfo: {response.modelinfo}")
print("✓ Fix works — no ValidationError")
17 changes: 17 additions & 0 deletions tests/test_tool_choice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
sys.path.insert(0, '.')

from ollama._types import ChatRequest

# Test that tool_choice is accepted
req = ChatRequest(
model="llama3.2",
tool_choice="auto"
)
print(f"tool_choice: {req.tool_choice}")
print("✓ tool_choice parameter works")

# Test default is None
req2 = ChatRequest(model="llama3.2")
print(f"tool_choice default: {req2.tool_choice}")
print("✓ default is None")
25 changes: 25 additions & 0 deletions tests/test_toolcall_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
sys.path.insert(0, '.')

from ollama._types import Message

# Test with id
tc1 = Message.ToolCall(
id="call_abc123",
function=Message.ToolCall.Function(
name="get_weather",
arguments={"city": "Hyderabad"}
)
)
print(f"id: {tc1.id}")
print(f"function: {tc1.function.name}")

# Test without id (backward compatibility)
tc2 = Message.ToolCall(
function=Message.ToolCall.Function(
name="get_weather",
arguments={"city": "Hyderabad"}
)
)
print(f"id default: {tc2.id}")
print("✓ Both work")