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
4 changes: 4 additions & 0 deletions ollama/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
ShowResponse,
StatusResponse,
Tool,
ToolChoice,
ToolChoiceFunction,
WebFetchResponse,
WebSearchResponse,
)
Expand All @@ -37,6 +39,8 @@
'ShowResponse',
'StatusResponse',
'Tool',
'ToolChoice',
'ToolChoiceFunction',
'WebFetchResponse',
'WebSearchResponse',
]
Expand Down
10 changes: 10 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
ShowResponse,
StatusResponse,
Tool,
ToolChoice,
ToolChoiceFunction,
WebFetchRequest,
WebFetchResponse,
WebSearchRequest,
Expand Down Expand Up @@ -312,6 +314,7 @@ def chat(
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None,
*,
tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable]]] = None,
tool_choice: Optional[Union[ToolChoice, Mapping[str, Any]]] = None,
stream: Literal[False] = False,
think: Optional[Union[bool, Literal['low', 'medium', 'high']]] = None,
logprobs: Optional[bool] = None,
Expand All @@ -328,6 +331,7 @@ def chat(
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None,
*,
tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable]]] = None,
tool_choice: Optional[Union[ToolChoice, Mapping[str, Any]]] = None,
stream: Literal[True] = True,
think: Optional[Union[bool, Literal['low', 'medium', 'high']]] = None,
logprobs: Optional[bool] = None,
Expand All @@ -343,6 +347,7 @@ def chat(
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None,
*,
tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable]]] = None,
tool_choice: Optional[Union[ToolChoice, Mapping[str, Any]]] = None,
stream: bool = False,
think: Optional[Union[bool, Literal['low', 'medium', 'high']]] = None,
logprobs: Optional[bool] = None,
Expand Down Expand Up @@ -392,6 +397,7 @@ def add_two_numbers(a: int, b: int) -> int:
model=model,
messages=list(_copy_messages(messages)),
tools=list(_copy_tools(tools)),
tool_choice=tool_choice,
stream=stream,
think=think,
logprobs=logprobs,
Expand Down Expand Up @@ -944,6 +950,7 @@ async def chat(
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None,
*,
tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable]]] = None,
tool_choice: Optional[Union[ToolChoice, Mapping[str, Any]]] = None,
stream: Literal[False] = False,
think: Optional[Union[bool, Literal['low', 'medium', 'high']]] = None,
logprobs: Optional[bool] = None,
Expand All @@ -960,6 +967,7 @@ async def chat(
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None,
*,
tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable]]] = None,
tool_choice: Optional[Union[ToolChoice, Mapping[str, Any]]] = None,
stream: Literal[True] = True,
think: Optional[Union[bool, Literal['low', 'medium', 'high']]] = None,
logprobs: Optional[bool] = None,
Expand All @@ -975,6 +983,7 @@ async def chat(
messages: Optional[Sequence[Union[Mapping[str, Any], Message]]] = None,
*,
tools: Optional[Sequence[Union[Mapping[str, Any], Tool, Callable]]] = None,
tool_choice: Optional[Union[ToolChoice, Mapping[str, Any]]] = None,
stream: bool = False,
think: Optional[Union[bool, Literal['low', 'medium', 'high']]] = None,
logprobs: Optional[bool] = None,
Expand Down Expand Up @@ -1025,6 +1034,7 @@ def add_two_numbers(a: int, b: int) -> int:
model=model,
messages=list(_copy_messages(messages)),
tools=list(_copy_tools(tools)),
tool_choice=tool_choice,
stream=stream,
think=think,
logprobs=logprobs,
Expand Down
25 changes: 25 additions & 0 deletions ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,28 @@ class Property(SubscriptableBaseModel):
function: Optional[Function] = None



class ToolChoiceFunction(SubscriptableBaseModel):
"""
Forces the model to call a specific tool by name.

Example::

tool_choice=ToolChoiceFunction(function=ToolChoiceFunction.Function(name='get_weather'))
"""

class Function(SubscriptableBaseModel):
name: str
'Name of the function to call.'

type: Literal['function'] = 'function'
function: Function
'The function to force the model to call.'


ToolChoice = Union[Literal['none', 'auto', 'required'], ToolChoiceFunction]


class ChatRequest(BaseGenerateRequest):
@model_serializer(mode='wrap')
def serialize_model(self, nxt):
Expand All @@ -400,6 +422,9 @@ def serialize_model(self, nxt):
tools: Optional[Sequence[Tool]] = None
'Tools to use for the chat.'

tool_choice: Optional[ToolChoice] = None
"""Controls which tool the model calls: 'none', 'auto', 'required', or ToolChoiceFunction."""

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

Expand Down