OpenAI的API代码测试

1.阅读API文档

  1. 官方文档:https://platform.openai.com/docs/quickstart
  2. 查看token额度:https://platform.openai.com/settings/organization/billing/overview

2.尝试代码调用

import openai

openai.api_key = "OPENAI_API_KEY"
completion = openai.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
        {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
    ]
)

print(completion.choices[0].message.content)

第一波会直接报错,提示内容如下:

openai.RateLimitError: Error code: 429 - {‘error’: {‘message’: ‘You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.’, ‘type’: ‘insufficient_quota’, ‘param’: None, ‘code’: ‘insufficient_quota’}}

为什么要让看官方文档和账户余额,是因为调用API时候,使用的token是收费的。

最简单的方式就是充值购买token,或者使用免费的API。地址为:https://github.com/popjane/free_chatgpt_api

授权成功后请妥善保存,如图:
在这里插入图片描述

3.使用KEY实验

import openai

# optional; defaults to `os.environ['OPENAI_API_KEY']`
openai.api_key = "OPENAI_API_KEY"
openai.base_url = "https://free.gpt.ge/v1/"

completion = openai.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
        {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}
    ]
)

print(completion.choices[0].message.content)

运行后输出结果如图:
在这里插入图片描述
代码可以成功运行,可以愉快体验了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

l8947943

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值