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.22k stars 313 forks source link

dashscope函数式调用 #108

Open justforyou2017 opened 6 months ago

justforyou2017 commented 6 months ago

`import random from http import HTTPStatus import dashscope import re

def get_express_info(location,unit=None): return '很抱歉您的快递已被误送到银行yx8925z星球,请打开您的量子门,我们马上传送给您'

def getInteger(cont): return re.compile('\d+').search(cont).group()

def getMi(cont): return len(cont)

tools = [ { "type": "function", "function": { "name": "getInteger", "description": "计算xxooyy值", "parameters": { "type": "object", "properties": { "cont": { "type": "string", "description": "内容" } }, "required": [ "cont" ] } } }, { "type": "function", "function": { "name": "getMi", "description": "计算tetete值", "parameters": { "type": "object", "properties": { "cont": { "type": "string", "description": "数字输入" } }, "required": [ "cont" ] } } }, ]

messages = [{'role': 'user', 'content': '计算这个><符号中的数字的xxooyy值和tetete值>我很抱歉您的快递已被误送到银行yx8925z星球,请打开您的量子门,我们马上传送给您?<'}] response = dashscope.Generation.call( "qwen1.5-72b-chat", messages=messages, api_key= , tools=tools, seed=random.randint(1, 10000), # set the random seed, optional, default to 1234 if not set result_format='message', # set the result to be "message" format. ) if response.status_code == HTTPStatus.OK: print(response) res=response.output.choices[0]['message']['content'] messages.append({'role': response.output.choices[0]['message']['role'], 'content': res if res else '好呀'}) tool_calls = response.output.choices[0]['message'].get('tool_calls',None) print('='30) print(tool_calls) print('='30)

for transction in tool_calls:
    transction = transction['function']
    func = eval(transction['name'])
    params = eval(transction['arguments'])
    print(func(**params))`

image 现在我想做两个连续的函数调用,请问模型只能做一个函数调用任务的识别吗?让它分别做一个值计算它是可以完成,谢谢!

JianxinMa commented 6 months ago

目前模型支持“串行执行 多个 函数”(gpt-4-0613及之前的用法),但是还没有打开对“并行执行 多个 函数”的支持(gpt-4-turbo-preview之后才出现的用法)。

串行模式下,每一次调用模型,模型最多只输出一个函数,但是你返回函数结果给模型后,再次调用模型,模型有能力继续输出下一个函数。大概的实现逻辑请参考:https://github.com/QwenLM/Qwen-Agent/blob/main/qwen_agent/agents/fncall_agent.py