Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
feat(tuner): Add Logger and Tinker Support (#1178)
Xuchen Pan8b5b3503dc0c
diff --git a/examples/tuner/react_agent/main.py b/examples/tuner/react_agent/main.pyindex 5edd5a86a..6df2fc5b6 100644--- a/examples/tuner/react_agent/main.py+++ b/examples/tuner/react_agent/main.py@@ -115,6 +115,24 @@ if __name__ == "__main__":inference_engine_num=4,tensor_parallel_size=1,)++ # If you have no GPU and want to use Tinker,+ # uncomment the following code+ # If you want to use local deployed TuFT+ # set `base_url` to your TuFT server address.+ #+ # from agentscope.tuner import TinkerConfig+ # tuner_model = TunerModelConfig(+ # model_path="Qwen/Qwen3-4B-Instruct-2507",+ # max_model_len=24576,+ # max_tokens=16384,+ # temperature=1.0,+ # tinker_config=TinkerConfig(+ # rank=16,+ # base_url=None,+ # ),+ # )+algorithm = AlgorithmConfig(algorithm_type="multi_step_grpo",group_size=8,diff --git a/src/agentscope/tuner/__init__.py b/src/agentscope/tuner/__init__.pyindex 0dd65cb10..5836d9ade 100644--- a/src/agentscope/tuner/__init__.py+++ b/src/agentscope/tuner/__init__.py@@ -6,7 +6,7 @@ from ._dataset import DatasetConfigfrom ._judge import JudgeType, JudgeOutputfrom ._workflow import WorkflowType, WorkflowOutputfrom ._algorithm import AlgorithmConfig-from ._model import TunerModelConfig+from ._model import TunerModelConfig, TinkerConfigfrom ._config import check_judge_function, check_workflow_function@@ -19,6 +19,7 @@ __all__ = ["JudgeOutput","DatasetConfig","TunerModelConfig",+ "TinkerConfig","check_workflow_function","check_judge_function",]diff --git a/src/agentscope/tuner/_config.py b/src/agentscope/tuner/_config.pyindex 8d2d224db..917cb0ee4 100644--- a/src/agentscope/tuner/_config.py+++ b/src/agentscope/tuner/_config.py@@ -36,6 +36,7 @@ def _to_trinity_config(Config,TasksetConfig,InferenceModelConfig,+ TinkerConfig,)config, auto_config = _load_config_from_path_or_default(config_path)@@ -85,6 +86,11 @@ def _to_trinity_config(**model.get_config(),)config.explorer.rollout_model.enable_history = True+ if model.tinker_config is not None:+ config.model.tinker = TinkerConfig(+ **model.tinker_config.get_config(),+ )+ config.model.tinker.enable = Trueif auxiliary_models is not None:for name, aux_chat_model in auxiliary_models.items():model_config = InferenceModelConfig(@@ -184,7 +190,7 @@ def check_workflow_function(func (Callable): The function to check."""essential_params = ["task", "model"]- optional_params = ["auxiliary_models"]+ optional_params = ["auxiliary_models", "logger"]_check_function_signature(func,essential_params,@@ -201,7 +207,7 @@ def check_judge_function(func (Callable): The function to check."""essential_params = ["task", "response"]- optional_params = ["auxiliary_models"]+ optional_params = ["auxiliary_models", "logger"]_check_function_signature(func,essential_params,diff --git a/src/agentscope/tuner/_judge.py b/src/agentscope/tuner/_judge.pyindex 487c85dc7..5dffe402a 100644--- a/src/agentscope/tuner/_judge.py+++ b/src/agentscope/tuner/_judge.py@@ -1,6 +1,7 @@# -*- coding: utf-8 -*-"""The judge module for tuner."""from typing import Any, Callable, Dict, Awaitable+from logging import Loggerfrom pydantic import BaseModel, Fieldfrom ..model import ChatModelBase@@ -19,7 +20,7 @@ class JudgeOutput(BaseModel):JudgeType = Callable[- [Dict, Any, Dict[str, ChatModelBase]],+ [Dict, Any, Dict[str, ChatModelBase] | None, Logger | None],Awaitable[JudgeOutput],]# A judge function type for tuning.@@ -30,10 +31,13 @@ JudgeType = Callable[# response (`Any`):# The response field of the WorkflowOutput generated by the# corresponding workflow.-# auxiliary_models (`Dict[str, ChatModelBase] | None`):+# auxiliary_models (`Dict[str, ChatModelBase] | None`, optional):# A dictionary of additional chat models available for LLM-as-a-Judge# usage. The keys are model names, and the values are the corresponding# `ChatModelBase` instances.+# logger (`Logger | None`, optional):+# An optional logger for logging information during the judge+# execution.# Returns:# `JudgeOutput`:# The reward value assigned by the judge function along with optionaldiff --git a/src/agentscope/tuner/_model.py b/src/agentscope/tuner/_model.pyindex 2fff987f1..25d882c87 100644--- a/src/agentscope/tuner/_model.py+++ b/src/agentscope/tuner/_model.py@@ -1,5 +1,6 @@# -*- coding: utf-8 -*-"""TunerModelConfig definition."""+from __future__ import annotationsfrom typing import Dict, Anyfrom pydantic import BaseModel, Field@@ -67,6 +68,13 @@ class TunerModelConfig(BaseModel):default="deepseek_r1",)+ tinker_config: TinkerConfig | None = Field(+ description=(+ "The configuration for Tinker. " "If None, Tinker is not used."+ ),+ default=None,+ )+def get_config(self) -> Dict[str, Any]:"""Get the model configuration.@@ -83,3 +91,58 @@ class TunerModelConfig(BaseModel):"enable_openai_api": True,"enable_auto_tool_choice": True,}+++class TinkerConfig(BaseModel):+ """Model configuration for Tinker."""++ rank: int = Field(+ description="The LoRA rank of the Tinker model.",+ default=16,+ )++ seed: int | None = Field(+ description=(+ "The seed for initializing LoRA weights in the model. "+ "If None, weights are initialized randomly."+ ),+ default=None,+ )++ train_mlp: bool = Field(+ description="Whether to add LoRA to the MLP layers.",+ default=True,+ )++ train_attn: bool = Field(+ description="Whether to add LoRA to the attention layers.",+ default=True,+ )++ train_unembed: bool = Field(+ description="Whether to add LoRA to the unembedding layer.",+ default=True,+ )++ base_url: str | None = Field(+ description=(+ "The base URL for Tinker services. If None, the default "+ "service URL is used."+ ),+ default=None,+ )++ def get_config(self) -> Dict[str, Any]:+ """Get the Tinker model configuration.++ Returns:+ `Dict[str, Any]`: The Tinker model configuration dictionary.+ """+ return {+ "rank": self.rank,+ "seed": self.seed,+ "train_mlp": self.train_mlp,+ "train_attn": self.train_attn,+ "train_unembed": self.train_unembed,+ "base_url": self.base_url,+ }diff --git a/src/agentscope/tuner/_workflow.py b/src/agentscope/tuner/_workflow.pyindex 814cf502b..506da8179 100644--- a/src/agentscope/tuner/_workflow.py+++ b/src/agentscope/tuner/_workflow.py@@ -1,5 +1,6 @@# -*- coding: utf-8 -*-"""The workflow module for tuner."""+from logging import Loggerfrom typing import Any, Callable, Dict, Awaitablefrom pydantic import BaseModel, Fieldfrom ..model import ChatModelBase@@ -30,7 +31,7 @@ class WorkflowOutput(BaseModel):WorkflowType = Callable[- [Dict, ChatModelBase, Dict[str, ChatModelBase]],+ [Dict, ChatModelBase, Dict[str, ChatModelBase] | None, Logger | None],Awaitable[WorkflowOutput],]# An agent workflow function type for tuning.@@ -41,12 +42,14 @@ WorkflowType = Callable[# model (`ChatModelBase`):# The primary chat model used in the workflow, this is the main model# being tuned.-# auxiliary_models (`Dict[str, ChatModelBase] | None`):+# auxiliary_models (`Dict[str, ChatModelBase] | None`, optional):# A dictionary of additional chat models available for LLM-as-a-Judge# usage. The keys are model names, and the values are the corresponding# `ChatModelBase` instances. Note that these auxiliary models are not# tuned during the workflow.-+# logger (`Logger | None`, optional):+# An optional logger for logging information during the workflow+# execution.# Returns:# `WorkflowOutput`:# The workflow execution results, including optional reward, rawdiff --git a/tests/tuner_test.py b/tests/tuner_test.pyindex 6b9f7357a..5a5f411e1 100644--- a/tests/tuner_test.py+++ b/tests/tuner_test.py@@ -5,6 +5,8 @@from unittest.async_case import IsolatedAsyncioTestCasefrom typing import Dict, Any+from logging import Logger+from agentscope.tuner import TunerModelConfig, WorkflowOutput, JudgeOutputfrom agentscope.tuner._config import (check_judge_function,@@ -16,6 +18,7 @@ async def correct_workflow_func(task: Dict,model: TunerModelConfig,auxiliary_models: Dict[str, TunerModelConfig],+ logger: Logger,) -> WorkflowOutput:"""Correct interface matching the workflow type."""return WorkflowOutput(@@ -56,6 +59,7 @@ async def correct_judge_func(task: Dict,response: Any,auxiliary_models: Dict[str, TunerModelConfig],+ logger: Logger,) -> JudgeOutput:"""Correct interface matching the judge type."""return JudgeOutput(