室内设计案例去什么网站,室内设计是什么,网站开发问题及解决,头像 wordpress错误1:openai.OpenAIError: The api_key client option must be set either by passing api_key.....
在通过openai创建客户端必须要设置api key#xff0c;如果你事先已经在本机的环境中设置未起效可以手动设置#xff0c;注意手动设置时不要用下面的形式
import openai
f…错误1:openai.OpenAIError: The api_key client option must be set either by passing api_key.....
在通过openai创建客户端必须要设置api key如果你事先已经在本机的环境中设置未起效可以手动设置注意手动设置时不要用下面的形式
import openai
from openai import OpenAIclient OpenAI()
openai.api_key YOUR API KEY如果你用的是clientOpenAI这种方式不正确正确的如下
from openai import OpenAIclient OpenAI(api_keyYOUR API KEY)错误2: openai.BadRequestError: Error code: 400 - {error: {message:...
这个错误100%是因为你传入数据格式的原因在这里需要参考文档来做比如我实现的function calling在这里需要描述函数之前的描述为
# 定义函数描述信息function_description {type: function,function: get_current_weather,description: 获取给定地点的当前天气,parameters: {type: object,properties: {location: {type: string,description: 城市名称例如“旧金山CA”},unit: {type: string,enum: [celsius, fahrenheit]}},required: [location]}}这时候无论怎么运行都会出错即BadRequestError但是其实是格式出错如果按照下面代码 # 定义函数描述信息function_description [{type: function,function: {name: get_current_weather,description: 获取给定地点的当前天气,parameters: {type: object,properties: {location: {type: string,description: 城市名称例如“旧金山CA”},unit: {type: string,enum: [celsius, fahrenheit]}},required: [location]}}}]即正确完整代码如下
import json
from openai import OpenAIdef get_current_weather(location, unitfahrenheit):# 实际为获取天气的接口return json.dumps({location: location,weather: rain,unit: unit,})if __name__ __main__:# 定义客户端client OpenAI(api_keysk-bXCWe1oKlkuDDdwsjFxUT3BlbkFJHeHTwSiT0aJ4UC0NrHyn)# 定义函数描述信息function_description [{type: function,function: {name: get_current_weather,description: 获取给定地点的当前天气,parameters: {type: object,properties: {location: {type: string,description: 城市名称例如“旧金山CA”},unit: {type: string,enum: [celsius, fahrenheit]}},required: [location]}}}]# 发起调用response client.chat.completions.create(modelgpt-3.5-turbo,messages[{role: user, content: 旧金山当前的天气如何}],tools[function_description],temperature0)tool_calls response.choices[0].message.tool_calls# 如果模型确定要调用一个函数if tool_calls:# 获取模型生成的参数arguments json.loads(tool_calls[0].function.arguments)# 调用本地函数weather_info get_current_weather(**arguments)print(weather_info) # 我们可以在这里看到函数调用查询到的天气信息输出
{location: San Francisco, CA, weather: rain, unit: fahrenheit}