Skip to content

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

fix(app): pass model card's context_size to chat model (#2597)

JinyuLiuf55cb284e45f
diff --git a/src/agentscope/app/_service/_model.py b/src/agentscope/app/_service/_model.py
index 1057643c3..739391f0d 100644
--- a/src/agentscope/app/_service/_model.py
+++ b/src/agentscope/app/_service/_model.py
@@ -57,16 +57,18 @@ async def get_model(
parameters=parameters,
)
- # Override the formatter's input types with the built-in model card's
- # when one matches; custom models have no card, so keep the default.
+ # Override the context size and the formatter's input types with the
+ # built-in model card's when one matches; custom models have no card,
+ # so keep the defaults.
try:
for card in model_cls.list_models():
if card.name == config.model:
+ model.context_size = card.context_size
model.formatter.input_types = card.input_types
break
except Exception: # pylint: disable=broad-except
logger.debug(
- "Failed to look up model card for %s, using formatter defaults.",
+ "Failed to look up model card for %s, using defaults.",
config.model,
)
diff --git a/tests/service_model_test.py b/tests/service_model_test.py
new file mode 100644
index 000000000..443d1d684
--- /dev/null
+++ b/tests/service_model_test.py
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+"""Unit tests for :func:`get_model` — the chat model factory."""
+from types import SimpleNamespace
+from unittest import IsolatedAsyncioTestCase
+from unittest.mock import AsyncMock, MagicMock
+
+from agentscope.app._service._model import get_model
+from agentscope.app.storage import ChatModelConfig
+
+_CREDENTIAL = {"type": "deepseek_credential", "api_key": "test"}
+
+
+class GetModelContextSizeTest(IsolatedAsyncioTestCase):
+ """The factory must forward the model card's ``context_size``."""
+
+ async def test_uses_card_context_size(self) -> None:
+ """A matching card's value wins over the class default."""
+ access = MagicMock(
+ resolve_credential=AsyncMock(
+ return_value=SimpleNamespace(data=_CREDENTIAL),
+ ),
+ )
+
+ model = await get_model(
+ "user-1",
+ ChatModelConfig(
+ type="deepseek",
+ credential_id="cred-1",
+ model="deepseek-v4-flash",
+ parameters={},
+ ),
+ access,
+ )
+
+ self.assertEqual(1000000, model.context_size)
+
+ async def test_keeps_default_for_unknown_model(self) -> None:
+ """A model without a card keeps the constructor default."""
+ access = MagicMock(
+ resolve_credential=AsyncMock(
+ return_value=SimpleNamespace(data=_CREDENTIAL),
+ ),
+ )
+
+ model = await get_model(
+ "user-1",
+ ChatModelConfig(
+ type="deepseek",
+ credential_id="cred-1",
+ model="my-custom-model",
+ parameters={},
+ ),
+ access,
+ )
+
+ self.assertEqual(65536, model.context_size)