Function Call实践(一)——调用国内大模型测试 #52

Open
opened 2024-12-09 00:12:02 +08:00 by 11175663820cs · 0 comments

function call 实践1,换另一个国内的大模型测试。本来以为只是换一个API key的小实验,结果好像不同大模型对于message的输入在加入functiona call等外接以后有一个验证,需要调整使用方式。

总结一下最开始的尝试是用通义千问的API,然后根据模板修改一下运行,遇到了invalid parameter的报错。然后发现有message的规范和验证,最后照千问自己的模式改动程序成功。

下面从头开始,因为这个是本地运行,所以环境变量设置取消,需要安装dashscope

首先做查找天气的工具, 然后写相应的描述, 找了一个限时免费的qwen2.5-3b-instruct模型,初始化,测试

1.png

让模型提取关键词来调用工具

2.png

重写messages,因为遇到报错:BadRequestError: Error code: 400 - {'error': {'code': 'invalid_parameter_error', 'param': None, 'message': '<400> InternalError.Algo.InvalidParameter: messages with role "tool" must be a response to a preceeding message with "tool_calls".', 'type': 'invalid_request_error'}, 'id': 'chatcmpl-da56e9ef-e1db-9dbb-a1e0-abe410241c48', 'request_id': 'da56e9ef-e1db-9dbb-a1e0-abe410241c48'}。 说需要加入相对应的assistant role才可以。

3.png

但是加入以后继续报错:

4.png

此时的message是

[{'role': 'user', 'content': 'What is the weather like in Beijing?'}, {'role': 'assistant', 'function_call': {'name': 'get_weather', 'arguments': '{"location": "Beijing"}'}}, {'role': 'tool', 'content': '"{\"coord\": {\"lon\": 116.3972, \"lat\": 39.9075}, \"weather\": [{\"id\": 800, \"main\": \"Clear\", \"description\": \"\\u6674\", \"icon\": \"01n\"}], \"base\": \"stations\", \"main\": {\"temp\": -7.06, \"feels_like\": -7.06, \"temp_min\": -7.06, \"temp_max\": -7.06, \"pressure\": 1029, \"humidity\": 17, \"sea_level\": 1029, \"grnd_level\": 1024}, \"visibility\": 10000, \"wind\": {\"speed\": 0.87, \"deg\": 262, \"gust\": 1.41}, \"clouds\": {\"all\": 0}, \"dt\": 1733669329, \"sys\": {\"type\": 1, \"id\": 9609, \"country\": \"CN\", \"sunrise\": 1733613810, \"sunset\": 1733647766}, \"timezone\": 28800, \"id\": 1816670, \"name\": \"Beijing\", \"cod\": 200}"', 'tool_call_id': 'call_13489c83979a4849b46419'}]

我的想法是千问的设定是需要前一次的id和后一次的一样,所以这样分段式的message有问题,于是按照千问提供的function调用模板创造一个智能体来解决这个问题。

5.png

我觉得通过2次不同的提问,其中第二次不要当作用过tools,而是通过提示词来把已知信息注入应该也是可以的。

function call 实践1,换另一个国内的大模型测试。本来以为只是换一个API key的小实验,结果好像不同大模型对于message的输入在加入functiona call等外接以后有一个验证,需要调整使用方式。 总结一下最开始的尝试是用通义千问的API,然后根据模板修改一下运行,遇到了invalid parameter的报错。然后发现有message的规范和验证,最后照千问自己的模式改动程序成功。 下面从头开始,因为这个是本地运行,所以环境变量设置取消,需要安装dashscope 首先做查找天气的工具, 然后写相应的描述, 找了一个限时免费的qwen2.5-3b-instruct模型,初始化,测试 [![1.png](/attachments/0c7c877c-982d-4522-a474-0298f2939fd0)](url) 让模型提取关键词来调用工具 [![2.png](/attachments/8d77ee1c-3968-4e08-9a5d-8189b873d2c0)](url) 重写messages,因为遇到报错:BadRequestError: Error code: 400 - {'error': {'code': 'invalid_parameter_error', 'param': None, 'message': '<400> InternalError.Algo.InvalidParameter: messages with role "tool" must be a response to a preceeding message with "tool_calls".', 'type': 'invalid_request_error'}, 'id': 'chatcmpl-da56e9ef-e1db-9dbb-a1e0-abe410241c48', 'request_id': 'da56e9ef-e1db-9dbb-a1e0-abe410241c48'}。 说需要加入相对应的assistant role才可以。 [![3.png](/attachments/79b6055a-f856-4ea2-b11d-99994227b784)](url) 但是加入以后继续报错: [![4.png](/attachments/f3ed2ee3-cdb5-433f-948c-5c22cb6d816f)](url) 此时的message是 [{'role': 'user', 'content': 'What is the weather like in Beijing?'}, {'role': 'assistant', 'function_call': {'name': 'get_weather', 'arguments': '{"location": "Beijing"}'}}, {'role': 'tool', 'content': '"{\\"coord\\": {\\"lon\\": 116.3972, \\"lat\\": 39.9075}, \\"weather\\": [{\\"id\\": 800, \\"main\\": \\"Clear\\", \\"description\\": \\"\\\\u6674\\", \\"icon\\": \\"01n\\"}], \\"base\\": \\"stations\\", \\"main\\": {\\"temp\\": -7.06, \\"feels_like\\": -7.06, \\"temp_min\\": -7.06, \\"temp_max\\": -7.06, \\"pressure\\": 1029, \\"humidity\\": 17, \\"sea_level\\": 1029, \\"grnd_level\\": 1024}, \\"visibility\\": 10000, \\"wind\\": {\\"speed\\": 0.87, \\"deg\\": 262, \\"gust\\": 1.41}, \\"clouds\\": {\\"all\\": 0}, \\"dt\\": 1733669329, \\"sys\\": {\\"type\\": 1, \\"id\\": 9609, \\"country\\": \\"CN\\", \\"sunrise\\": 1733613810, \\"sunset\\": 1733647766}, \\"timezone\\": 28800, \\"id\\": 1816670, \\"name\\": \\"Beijing\\", \\"cod\\": 200}"', 'tool_call_id': 'call_13489c83979a4849b46419'}] 我的想法是千问的设定是需要前一次的id和后一次的一样,所以这样分段式的message有问题,于是按照千问提供的function调用模板创造一个智能体来解决这个问题。 [![5.png](/attachments/600627a6-28c9-49ea-bdc0-5c71c8745e8e)](url) 我觉得通过2次不同的提问,其中第二次不要当作用过tools,而是通过提示词来把已知信息注入应该也是可以的。
119 KiB
291 KiB
78 KiB
47 KiB
139 KiB
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: HswOAuth/llm_share#52
No description provided.