Skip to content

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

feat(classifier): let a per-call model override reach the Jev system_one call (#3424)

lihongyuan9922b41813c16f
diff --git a/src/agentscope/classifier/_jev/_model.py b/src/agentscope/classifier/_jev/_model.py
index 03efc323a..6e3539af0 100644
--- a/src/agentscope/classifier/_jev/_model.py
+++ b/src/agentscope/classifier/_jev/_model.py
@@ -136,21 +136,25 @@ class JevClassifierModel(ClassifierModelBase):
The typed questions keyed by name.
**kwargs (`Any`):
Per-call TypeSafe options, e.g. ``extra_headers`` and
- ``extra_body``.
+ ``extra_body``. ``model`` may also be set here, in which case
+ it overrides the instance default for this call only.
Returns:
`ClassifierResponse`:
The typed probabilistic answers keyed by question name.
"""
start_time = perf_counter()
+ # Per-call options override defaults.
response = await self.client.system_one(
- state=state,
- questions={
- name: _to_sdk_question(question)
- for name, question in questions.items()
+ **{
+ "state": state,
+ "questions": {
+ name: _to_sdk_question(question)
+ for name, question in questions.items()
+ },
+ "model": self.model,
+ **kwargs,
},
- model=self.model,
- **kwargs,
)
return ClassifierResponse(
model=response.model,
diff --git a/tests/classifier_jev_test.py b/tests/classifier_jev_test.py
index 8c78cd65f..96f8d9eb3 100644
--- a/tests/classifier_jev_test.py
+++ b/tests/classifier_jev_test.py
@@ -203,3 +203,83 @@ class JevClassifierModelTest(IsolatedAsyncioTestCase):
"requires a compatible optional",
):
JevClassifierModel(TypeSafeCredential(api_key="secret"))
+
+ @staticmethod
+ def _response(model: str) -> SimpleNamespace:
+ """Build a System One response carrying one noul answer."""
+ return SimpleNamespace(
+ model=model,
+ answers={
+ "urgent": typesafe_sdk.NoulAnswer(type="noul", noul=0.8),
+ },
+ usage=typesafe_sdk.Usage(input_tokens=7, output_tokens=1),
+ )
+
+ @patch("typesafe_sdk.AsyncTypeSafeClient")
+ async def test_per_call_model_replaces_the_default(
+ self,
+ client_cls: Any,
+ ) -> None:
+ """``model=`` is forwarded once, with the caller's value."""
+ client = MagicMock()
+ client.system_one = AsyncMock(return_value=self._response("jev-1.5"))
+ client_cls.return_value = client
+ model = JevClassifierModel(
+ TypeSafeCredential(api_key="secret"),
+ model="jev-latest",
+ )
+
+ await model(
+ state="I was charged twice.",
+ questions={"urgent": BinaryQuestion(instructions="Urgent?")},
+ model="jev-1.5",
+ extra_body={"trace": True},
+ )
+
+ call_kwargs = client.system_one.await_args.kwargs
+ self.assertDictEqual(
+ {
+ **call_kwargs,
+ "questions": {
+ name: question.model_dump()
+ for name, question in call_kwargs["questions"].items()
+ },
+ },
+ {
+ "state": "I was charged twice.",
+ "questions": {
+ "urgent": {
+ "type": "noul",
+ "instructions": "Urgent?",
+ },
+ },
+ "model": "jev-1.5",
+ "extra_body": {"trace": True},
+ },
+ )
+
+ @patch("typesafe_sdk.AsyncTypeSafeClient")
+ async def test_per_call_model_is_not_remembered(
+ self,
+ client_cls: Any,
+ ) -> None:
+ """The instance default applies again on the next call."""
+ client = MagicMock()
+ client.system_one = AsyncMock(return_value=self._response("jev-1.5"))
+ client_cls.return_value = client
+ model = JevClassifierModel(
+ TypeSafeCredential(api_key="secret"),
+ model="jev-latest",
+ )
+ questions = {"urgent": BinaryQuestion(instructions="Urgent?")}
+
+ await model(state="one", questions=questions, model="jev-1.5")
+ await model(state="two", questions=questions)
+
+ self.assertListEqual(
+ [
+ call.kwargs["model"]
+ for call in client.system_one.await_args_list
+ ],
+ ["jev-1.5", "jev-latest"],
+ )