Skip to content

Build and run agents you can see, understand and trust.

fix(model): unify `client_kwargs` in ollama and dashscope chat model classes (#986)

qbc141b2c46c81b
diff --git a/src/agentscope/model/_anthropic_model.py b/src/agentscope/model/_anthropic_model.py
index cf2329295..a9bc28629 100644
--- a/src/agentscope/model/_anthropic_model.py
+++ b/src/agentscope/model/_anthropic_model.py
@@ -45,7 +45,7 @@ class AnthropicChatModel(ChatModelBase):
max_tokens: int = 2048,
stream: bool = True,
thinking: dict | None = None,
- client_kwargs: dict | None = None,
+ client_kwargs: dict[str, JSONSerializableObject] | None = None,
generate_kwargs: dict[str, JSONSerializableObject] | None = None,
**kwargs: Any,
) -> None:
@@ -71,7 +71,8 @@ class AnthropicChatModel(ChatModelBase):
"budget_tokens": 1024
}
- client_kwargs (`dict | None`, optional):
+ client_kwargs (`dict[str, JSONSerializableObject] | None`, \
+ optional):
The extra keyword arguments to initialize the Anthropic client.
generate_kwargs (`dict[str, JSONSerializableObject] | None`, \
optional):
diff --git a/src/agentscope/model/_dashscope_model.py b/src/agentscope/model/_dashscope_model.py
index 83b50dbf3..8e404a9f7 100644
--- a/src/agentscope/model/_dashscope_model.py
+++ b/src/agentscope/model/_dashscope_model.py
@@ -55,6 +55,7 @@ class DashScopeChatModel(ChatModelBase):
enable_thinking: bool | None = None,
generate_kwargs: dict[str, JSONSerializableObject] | None = None,
base_http_api_url: str | None = None,
+ **_kwargs: Any,
) -> None:
"""Initialize the DashScope chat model.
@@ -77,6 +78,8 @@ class DashScopeChatModel(ChatModelBase):
base_http_api_url (`str | None`, optional):
The base URL for DashScope API requests. If not provided,
the default base URL from the DashScope SDK will be used.
+ **_kwargs (`Any`):
+ Additional keyword arguments.
"""
if enable_thinking and not stream:
logger.info(
diff --git a/src/agentscope/model/_gemini_model.py b/src/agentscope/model/_gemini_model.py
index bfa02557c..285fb6375 100644
--- a/src/agentscope/model/_gemini_model.py
+++ b/src/agentscope/model/_gemini_model.py
@@ -38,7 +38,7 @@ class GeminiChatModel(ChatModelBase):
api_key: str,
stream: bool = True,
thinking_config: dict | None = None,
- client_kwargs: dict = None,
+ client_kwargs: dict[str, JSONSerializableObject] | None = None,
generate_kwargs: dict[str, JSONSerializableObject] | None = None,
**kwargs: Any,
) -> None:
@@ -64,7 +64,8 @@ class GeminiChatModel(ChatModelBase):
"thinking_budget": 1024 # Max tokens for reasoning
}
- client_kwargs (`dict`, default `None`):
+ client_kwargs (`dict[str, JSONSerializableObject] | None`, \
+ optional):
The extra keyword arguments to initialize the Gemini client.
generate_kwargs (`dict[str, JSONSerializableObject] | None`, \
optional):
diff --git a/src/agentscope/model/_ollama_model.py b/src/agentscope/model/_ollama_model.py
index 4d1b1af28..f82bdcf9d 100644
--- a/src/agentscope/model/_ollama_model.py
+++ b/src/agentscope/model/_ollama_model.py
@@ -21,7 +21,7 @@ from .._logging import logger
from .._utils._common import _json_loads_with_repair
from ..message import ToolUseBlock, TextBlock, ThinkingBlock
from ..tracing import trace_llm
-
+from ..types import JSONSerializableObject
if TYPE_CHECKING:
from ollama._types import ChatResponse as OllamaChatResponse
@@ -40,32 +40,40 @@ class OllamaChatModel(ChatModelBase):
keep_alive: str = "5m",
enable_thinking: bool | None = None,
host: str | None = None,
+ client_kwargs: dict[str, JSONSerializableObject] | None = None,
+ generate_kwargs: dict[str, JSONSerializableObject] | None = None,
**kwargs: Any,
) -> None:
"""Initialize the Ollama chat model.
Args:
- model_name (`str`):
- The name of the model.
- stream (`bool`, default `True`):
- Streaming mode or not.
- options (`dict`, default `None`):
- Additional parameters to pass to the Ollama API. These can
- include temperature etc.
- keep_alive (`str`, default `"5m"`):
- Duration to keep the model loaded in memory. The format is a
- number followed by a unit suffix (s for seconds, m for minutes
- , h for hours).
- enable_thinking (`bool | None`, default `None`)
- Whether enable thinking or not, only for models such as qwen3,
- deepseek-r1, etc. For more details, please refer to
- https://ollama.com/search?c=thinking
- host (`str | None`, default `None`):
- The host address of the Ollama server. If None, uses the
- default address (typically http://localhost:11434).
- **kwargs (`Any`):
- Additional keyword arguments to pass to the base chat model
- class.
+ model_name (`str`):
+ The name of the model.
+ stream (`bool`, default `True`):
+ Streaming mode or not.
+ options (`dict`, default `None`):
+ Additional parameters to pass to the Ollama API. These can
+ include temperature etc.
+ keep_alive (`str`, default `"5m"`):
+ Duration to keep the model loaded in memory. The format is a
+ number followed by a unit suffix (s for seconds, m for minutes
+ , h for hours).
+ enable_thinking (`bool | None`, default `None`)
+ Whether enable thinking or not, only for models such as qwen3,
+ deepseek-r1, etc. For more details, please refer to
+ https://ollama.com/search?c=thinking
+ host (`str | None`, default `None`):
+ The host address of the Ollama server. If None, uses the
+ default address (typically http://localhost:11434).
+ client_kwargs (`dict[str, JSONSerializableObject] | None`, \
+ optional):
+ The extra keyword arguments to initialize the Ollama client.
+ generate_kwargs (`dict[str, JSONSerializableObject] | None`, \
+ optional):
+ The extra keyword arguments used in Ollama API generation.
+ **kwargs (`Any`):
+ Additional keyword arguments to pass to the base chat model
+ class.
"""
try:
@@ -80,11 +88,13 @@ class OllamaChatModel(ChatModelBase):
self.client = ollama.AsyncClient(
host=host,
+ **(client_kwargs or {}),
**kwargs,
)
self.options = options
self.keep_alive = keep_alive
self.think = enable_thinking
+ self.generate_kwargs = generate_kwargs or {}
@trace_llm
async def __call__(
@@ -130,6 +140,7 @@ class OllamaChatModel(ChatModelBase):
"stream": self.stream,
"options": self.options,
"keep_alive": self.keep_alive,
+ **self.generate_kwargs,
**kwargs,
}
diff --git a/src/agentscope/model/_openai_model.py b/src/agentscope/model/_openai_model.py
index 3a505f46f..e7f0b4813 100644
--- a/src/agentscope/model/_openai_model.py
+++ b/src/agentscope/model/_openai_model.py
@@ -72,7 +72,7 @@ class OpenAIChatModel(ChatModelBase):
stream: bool = True,
reasoning_effort: Literal["low", "medium", "high"] | None = None,
organization: str = None,
- client_kwargs: dict = None,
+ client_kwargs: dict[str, JSONSerializableObject] | None = None,
generate_kwargs: dict[str, JSONSerializableObject] | None = None,
**kwargs: Any,
) -> None:
@@ -95,11 +95,12 @@ class OpenAIChatModel(ChatModelBase):
organization (`str`, default `None`):
The organization ID for OpenAI API. If not specified, it will
be read from the environment variable `OPENAI_ORGANIZATION`.
- client_kwargs (`dict`, default `None`):
+ client_kwargs (`dict[str, JSONSerializableObject] | None`, \
+ optional):
The extra keyword arguments to initialize the OpenAI client.
generate_kwargs (`dict[str, JSONSerializableObject] | None`, \
optional):
- The extra keyword arguments used in OpenAI API generation,
+ The extra keyword arguments used in OpenAI API generation,
e.g. `temperature`, `seed`.
**kwargs (`Any`):
Additional keyword arguments.