ChatGPT API调用

安装OPENAI的包

pip install --upgrade openai

而后从chatgpt官网得到相应的API

注意:key只会出现一次所以要自己记好

而后打开电脑:系统-->系统信息-->高级系统设置-->系统属性中的“高级”-->环境变量-->新建系统变量。

变量名:OPENAI_API_KEY
变量值:保存的密钥

而后打开python进行测试:

中间遇到一个BUG,报错如下:
openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

解决方案,代码改为如下形式:
import os
import openai


# 创建一个 GPT-3 请求
openai.api_key = "密钥"

# openai.organization = 
completion = openai.ChatCompletion.create(
  model="gpt-3.5-turbo-16k",
  messages=[
    {"role": "user", "content": "Hello, Nice to meet you"}
  ]
)

print(completion.choices[0].message)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值