当前位置: 首页 > news >正文

openai常见的两个错误:BadRequestError和OpenAIError

错误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"

如果你用的是client(OpenAI),这种方式不正确,正确的如下:

from openai import OpenAIclient = OpenAI(api_key="YOUR 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, unit="fahrenheit"):# 实际为获取天气的接口return json.dumps({"location": location,"weather": "rain","unit": unit,})if __name__ == '__main__':# 定义客户端client = OpenAI(api_key="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")# 定义函数描述信息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(model="gpt-3.5-turbo",messages=[{"role": "user", "content": "旧金山当前的天气如何?"}],tools=[function_description],temperature=0)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"}
http://www.lryc.cn/news/319540.html

相关文章:

  • 2核4g服务器够用吗?
  • 数据仓库数据分层详解
  • unity内存优化之AB包篇(微信小游戏)
  • 白话模电:3.三极管(考研面试与笔试常考问题)
  • LeetCode 395. 至少有K个重复字符的最长子串
  • C#重新认识笔记_ FixUpdate + Update
  • Django 解决新建表删除后无法重新创建等问题
  • Qt教程 — 3.3 深入了解Qt 控件:Input Widgets部件(2)
  • 数据分析-Pandas的直接用Matplotlib绘图
  • Jmeter---分布式
  • 安卓基础面试题
  • 如何在 Linux ubuntu 系统上搭建 Java web 程序的运行环境
  • Redis实现分布式锁源码分析
  • SCI 图像处理期刊
  • 数据结构-红黑树
  • 双指针、bfs与图论
  • RabbitMQ高级-高级特性
  • Word粘贴时出现“运行时错误53,文件未找到:MathPage.WLL“的解决方案
  • html元素基本使用
  • PHP+golang开源办公系统CRM管理系统
  • smartmontools-5.43交叉编译Smartctl
  • idea找不到或无法加载主类
  • 2.二进制的方式读写文件
  • Seata的详细解释
  • JS手写实现洋葱圈模型
  • 3.Windows下安装MongoDB和Compass教程
  • go反射实战
  • Docker 中 MySQL 的部署与管理
  • 基础练习题之函数
  • Java NIO浅析