From 09d8d344a28dc73049b45f5733ad443e8437b51d Mon Sep 17 00:00:00 2001 From: Federico Villa <61495946+federicovilla55@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:09:26 +0100 Subject: [PATCH] Filter invalid parameters in Ollama client requests (#5983) Remove unrecognized parameters in Ollama API calls. --------- Co-authored-by: Eric Zhu --- .../src/autogen_ext/models/ollama/_ollama_client.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py b/python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py index 9e14f310e..5bf9a263c 100644 --- a/python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py +++ b/python/packages/autogen-ext/src/autogen_ext/models/ollama/_ollama_client.py @@ -82,14 +82,22 @@ ollama_chat_request_fields: dict[str, Any] = [m for m in inspect.getmembers(Chat 1 ] OLLAMA_VALID_CREATE_KWARGS_KEYS = set(ollama_chat_request_fields.keys()) | set( - ("model", "messages", "tools", "stream", "format", "options", "keep_alive") + ("model", "messages", "tools", "stream", "format", "options", "keep_alive", "response_format") ) +# NOTE: "response_format" is a special case that we handle for backwards compatibility. +# It is going to be deprecated in the future. def _create_args_from_config(config: Mapping[str, Any]) -> Dict[str, Any]: + if "response_format" in config: + warnings.warn( + "Using response_format will be deprecated. Use json_output instead.", + DeprecationWarning, + stacklevel=2, + ) create_args = {k.lower(): v for k, v in config.items() if k.lower() in OLLAMA_VALID_CREATE_KWARGS_KEYS} dropped_keys = [k for k in config.keys() if k.lower() not in OLLAMA_VALID_CREATE_KWARGS_KEYS] - logger.info(f"Dropped the following unrecognized keys from create_args: {dropped_keys}") + trace_logger.info(f"Dropped the following unrecognized keys from create_args: {dropped_keys}") return create_args # create_args = {k: v for k, v in config.items() if k in create_kwargs} @@ -411,6 +419,7 @@ class BaseOllamaChatCompletionClient(ChatCompletionClient): # Copy the create args and overwrite anything in extra_create_args create_args = self._create_args.copy() create_args.update(extra_create_args) + create_args = _create_args_from_config(create_args) response_format_value: JsonSchemaValue | Literal["json"] | None = None