agentscope icon indicating copy to clipboard operation
agentscope copied to clipboard

使用PostAPIChatWrapper 如何传入temperature 和max_tokens 等参数

Open dengxiaotian123 opened this issue 9 months ago • 3 comments

我按如下示例,重新定义了一个装饰器,用于解析我部署的模型的返回,但是我想知道,如何传入temperature 和max_tokens 等参数 Image

dengxiaotian123 avatar Apr 29 '25 07:04 dengxiaotian123

@dengxiaotian123 首先取决于你的api的要求,例如你的api中需要一个post请求的json字段传入temperature,那么就在model_config中的json_args字段中传入temperature

例如

{
    'config_name': 'xx',
    'model_type': 'post_api_chat',
    'model_name': 'xx',
    'json_args': {
        'temperature': 0.5,
        'max_tokens': 15
    }
}

DavdGao avatar May 01 '25 02:05 DavdGao

@DavdGao 你好,还是自定义 llm 和官方接口的耦合问题

class MyNewModelWrapper(PostAPIChatWrapper):
    model_type: str = "qwen2.5-xj"
    def _parse_response(self, response: dict) -> ModelResponse:
        """解析来自 API 服务器的响应。

        Args:
            response (`dict`):
                从 API 服务器获取的响应,并通过
                `response.json()` 解析为统一的格式。

        Returns (`ModelResponse`):
            解析后的响应。
        """
        res = response["choices"][0]["message"]["content"]
        return ModelResponse(
            text=res
        )
post_api_model = MyNewModelWrapper(
            config_name="qwen2.5-xj",
            api_url=url,  # 目标 URL
            headers={
                "Content-Type": "application/json",  # 来自头部
                "X-ModelHub-Token": f"{api_key}",
            },
            json_args=json_args
        )

我自定义了一个ModelWrapper,并实例化post_api_model,当我想用ReActAgent的时候,如何将post_api_model耦合进去。下面是ReActAgent的调用方法,model_config_name如何填写?感谢🙏

jarvis = ReActAgent(
        name="assistant",
        model_config_name="",
        sys_prompt='你是一个名为小丁的查询小助手。',
        verbose=True,
        service_toolkit=service_toolkit,
    )

我看您在其他 issue https://github.com/modelscope/agentscope/issues/375中有类似的情况,您提示自己创建 agent,ReActAgent 也只能自己创建吗?

dengxiaotian123 avatar May 06 '25 09:05 dengxiaotian123

@dengxiaotian123 首先创建config,例如你的wrapper的model_type字段为qwen2.5-xj,那么创建如下的配置(配置中的model_type对应你新建的wrapper的model_type属性

import agentscope

agentscope.init(
    model_configs={
        "config_name": "my_config",
        "model_type": "qwen2.5-xj",
        "model_name": "xxx",
        # The other arguments
        "api_url": "xxx",
        "headers": {
            # ...
         },
         "json_args": {...}
    }
)

# Register your model wrapper calss
agentscope.register_model_wrapper_class(MyNewModelWrapper)

# Then specify the model by config name
jarvis = ReActAgent(
    name="assistant",
    model_config_name="my_config",
    sys_prompt='你是一个名为小丁的查询小助手。',
    verbose=True,
    service_toolkit=service_toolkit,
)

PS: as内置的react agent目前都是面向模型未知的情况实现的,因此不支持model_config_name为空,或者没有意义的model_config_name参数;如果需要,可以将React Agent的代码copy出来,将model属性传进去就行

DavdGao avatar May 07 '25 03:05 DavdGao