openai error:messages with role “tool“ must be a response to a preceeding message with “tool_calls“.

在使用openai 调用 tools 的时候,会报错:

openai.BadRequestError: Error code: 400 - 
{'error': {'code': 'invalid_parameter_error', 'param': None, 'message': 'messages with role "tool" must be a response to a preceeding message with "tool_calls".', 'type': 'invalid_request_error'}, 

输入的messages 类似下面,“tool” role 直接在 “user” 之后,而没有 “tool_calls”。

[{'role': 'system', 'content': 'You are a helpful assistant.'}, 
{'role': 'user', 'content': '海淀区天气怎么样'}, 
{'role': 'tool', 'name': 'get_current_weather', 'content': '北京市海淀区--sunny--28'}, 
{'role': 'user', 'content': '朝阳区天气怎么样'}]

DEBUG
messages 中应该append openai接口返回的message,也就是 tool_calls 信息, 然后再加上 “tool”返回的message。这样就能连续提问了。

# messages
[{'role': 'system', 'content': 'You are a helpful assistant.'}, 
{'role': 'user', 'content': '海淀区天气怎么样'},
#接口返回message
{'content': '', 'role': 'assistant', 'function_call': None, 'tool_calls': [{'id': 'call_d55670dfg34567a4', 'function': {'arguments': '{"location": "北京市海淀区", "format": "celsius"}', 'name': 'get_current_weather'}, 'type': 'function', 'index': 0}]},
#调用tool结果
{'role': 'tool', 'name': 'get_current_weather', 'content': '北京市海淀区--sunny--28'},
#继续提问
{'role': 'user', 'content': '朝阳区呢'}, 
#接口返回message
{'content': '', 'role': 'assistant', 'function_call': None, 'tool_calls': [{'id': 'call_9f623dfh56ga80e', 'function': {'arguments': '{"location": "北京市朝阳区", "format": "celsius"}', 'name': 'get_current_weather'}, 'type': 'function', 'index': 0}]}, 
#调用tool结果
{'role': 'tool', 'name': 'get_current_weather', 'content': '北京市朝阳区--sunny--28'}]
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值