examples: remove duplicate import and refactor message initialization

- Remove duplicate `from ollama import ChatResponse, chat` import
- Initialize `messages` as empty list instead of getting from kwargs
- Remove `reserved` set and `chat_kwargs` filtering logic
- Add `think` parameter to client function call
- Reformat client_fn call with explicit parameters on separate lines
This commit is contained in:
Xinyue 2026-02-16 17:20:25 +01:00
parent e6c8210330
commit ddb9fcd3e3

View File

@ -1,7 +1,5 @@
from ollama import ChatResponse, chat
from ollama import ChatResponse, chat
def ollama_chat_automatic_function_calling(
client_fn: chat, options: dict = None, **kwargs
@ -27,10 +25,9 @@ def ollama_chat_automatic_function_calling(
if model is None:
raise ValueError("model must be specified")
messages = kwargs.get("messages", [])
max_turns = kwargs.get("max_turns", 20)
tools = kwargs.get("tools", [])
tool_map = kwargs.get("tool_map")
messages = list()
last_response = None
def _to_msg_dict(msg):
@ -81,12 +78,14 @@ def ollama_chat_automatic_function_calling(
if not callable(client_fn):
raise ValueError("client_fn must be a callable")
reserved = {"messages", "max_turns", "model", "tools", "tool_map"}
chat_kwargs = {k: v for k, v in kwargs.items() if k not in reserved}
for _ in range(max_turns):
response = client_fn(
model=model, tools=tools, messages=messages, options=options, **chat_kwargs
model=model,
tools=tools,
messages=messages,
options=options,
think=think,
**chat_kwargs,
)
last_response = response
assistant_msg = response.message