1.阅读API文档
- 官方文档:https://platform.openai.com/docs/quickstart
- 查看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)
运行后输出结果如图:
代码可以成功运行,可以愉快体验了。