QwenLM / Qwen-Agent

Agent framework and applications built upon Qwen>=2.0, featuring Function Calling, Code Interpreter, RAG, and Chrome extension.
https://pypi.org/project/qwen-agent/
Other
3.43k stars 339 forks source link

自定义了个工具查询天气,函数要求传入参数城市,请问怎么从用户的输入中提取出城市啊? #335

Closed liguoyu666 closed 3 weeks ago

liguoyu666 commented 2 months ago

用户输入是正常的聊天,要求遇到天气相关的问题时调用该工具,不知道该咋把城市提取出来

@register_tool('search_weather') class SearchWeather(BaseTool): description = '天气查询' parameters = [{ 'name': 'prompt', 'type': 'string', 'description': '用中文回答城市的天气情况', 'required': True, }]

def call(self, params: str, **kwargs) -> str:
    params = self._verify_json_format_args(params)
    print(111111)
    # 基本参数配置
    apiUrl = 'http://apis.juhe.cn/simpleWeather/query'  # 接口请求URL
    apiKey = '*******'  # 在个人中心->我的数据,接口名称上方查看

    # 接口请求入参配置
    requestParams = {
        'key': apiKey,
        'city': params,  # params为整句输入的问题,怎么提取城市?
    }
    print(requestParams)
    # 发起接口网络请求
    response = requests.get(apiUrl, params=requestParams)

    # 解析响应结果
    if response.status_code == 200:
        responseResult = response.json()
        # 网络请求成功。可依据业务逻辑和接口文档说明自行处理。
        print(responseResult)
    else:
        # 网络异常等因素,解析结果异常。可依据业务逻辑自行处理。
        print('请求异常')
        responseResult = "调用search_weather工具查询超时"
    return responseResult
JianxinMa commented 2 months ago

请参考: https://github.com/QwenLM/Qwen-Agent/blob/main/qwen_agent/tools/amap_weather.py#L15 https://github.com/QwenLM/Qwen-Agent/blob/main/examples/assistant_weather_bot.py

liguoyu666 commented 2 months ago

请参考: https://github.com/QwenLM/Qwen-Agent/blob/main/qwen_agent/tools/amap_weather.py#L15 https://github.com/QwenLM/Qwen-Agent/blob/main/examples/assistant_weather_bot.py

谢谢指点

zwy440 commented 1 month ago

感觉没看懂呢