借用openai api,做一个聊天机器人,别去openai主页啦~
Step 1.登录OpenAI账号
Step 2.生成API key
点击右上角的个人个人账户-> 点击 View API keys
点击生成一个秘钥(Create new secret key),并且复制
Step 3.安装OpenAI
pip install openai
指令在linux系统上可以,windows上不行!出错如图
所以利用anaconda重新配置了新的虚拟环境,操作如下,因为openai中的增强学习需要Gym
conda create -n open_ai python=3.6
conda activate open_ai
conda install git
conda install -c conda-forge ffmpeg
pip install git+https://github.com/Kojoley/atari-py.git
其他安装部分省略
如果出现
那么
git clone https://github.com/openai/gym.git
cd gym
pip install - .
再次下载openai就可以啦
pip install openai
Step 4.实现和执行代码
创建代码,如下所示。openai.api_key 将 API 生成时收到的密钥放入。
import openai
def ask_gpt(text: str):
openai.api_key = "PUT YOUR API KEY HERE"
res = openai.Completion.create(
engine="text-davinci-003", prompt=text, temperature=0.6, max_tokens=150
)
return res.choices[0].text
def main():
while True:
query = input("Ask a question: ")
res = ask_gpt(query)
print(f"{res}\n")
main()
然后就大功告成啦,✿✿ヽ(°▽°)ノ✿
最后的最后,我只想说ta真的是在一本正经的胡言乱语~
openai文本生成接口,是根据模型来确定价格的,不同的模型价格不同,有三个月的免费18美元试用额度 。
微调模型
通过使用训练数据微调基础模型来创建自己的自定义模型。微调模型后,只需为在对该模型的请求中使用的tokens付费。
MODEL训练使用
Ada$0.0004 / 1K tokens$0.0016 / 1K tokens
Babbage$0.0006 / 1K tokens$0.0024 / 1K tokens
Curie$0.0030 / 1K tokens$0.0120 / 1K tokens
Davinci$0.0300 / 1K tokens$0.1200 / 1K tokens
过程中的遇到的错误及解决【PS】
【PS1】openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details.
错误代码 429 - 超出了当前配额,请检查您的计划和账单详细信息。
此错误消息表明已达到 API 的每月最高支出(硬限制)。这意味着已消耗分配给您的计划的所有积分或单位,并且已达到计费周期的限制。
解决方案: 在限制速率内发送API 请求;给账户充钱或换一个有余额的api_key
查看usage
在2023.4.1.为止,现在过期了。
参考文献
【1】[RL] Windows 10에서 OpenAI Gym & Baselines 설치하기 (tistory.com)