python - 获取ChatGPT API的流程是怎样的?

标签 python flask openai-api chatgpt-api

我正在做一个迷你项目,在 python 中使用 ChatGPT 生成合成数据,我试图在 Flask 框架中获取 ChatGPT API 访问权限,但遇到了困难。请指导我完成整个过程。

我从一开始就陷入困境。

最佳答案

首先注册为开发人员并获取 API key 。他们为您提供一些用于应用程序开发的演示 token ,但要进行进一步的处理,您将需要请求订阅。 详细表格如下:https://platform.openai.com/docs/models/gpt-3-5 请注意,chatGPT 与 GPT3.5 Turbo 相同。我之前用过它,他们有一个名为 openai 的 python 包。

import openai
openai.api_key = "sk-..."

# list models
models = openai.Model.list()

# print the first model's id
print(models.data[0].id)

# create a chat completion
chat_completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}])

# print the chat completion
print(chat_completion.choices[0].message.content)

注意:请不要忘记提到的模型每 1000 个代币的成本为 0.002 美元。

关于python - 获取ChatGPT API的流程是怎样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76438937/

相关文章:

python - 行枚举、制表符分隔的 csv 文件的列表字典的键

python - 有没有办法将 json 压缩成 web2py 中的行和列?

python - 限制登录用户访问静态文件

python - Flask,使用 send_static_file 不断获取 404 服务静态文件

python-3.x - OpenAI ChatGPT (GPT-3.5) API 错误 : "Invalid URL (POST/v1/engines/gpt-3.5-turbo/chat/completions)" (migrating GPT-3 to GPT-3. 5 API)

r - 开放人工智能 API : Can I remove the line break from the response with a parameter?

Python:从基本数据类继承的数据类,如何将值从基类升级到新类?

python - 使用报纸模块的 Ubuntu 脚本导入错误

python - flask 蓝图索引URL不断添加斜杠

python - 为什么我已经安装了 openai 却出现这个错误?