我正在制作一个 Python 脚本以通过其 API 使用 OpenAI。但是,我收到此错误:
openai.error.RateLimitError:您超出了当前配额,请检查您的计划和账单详细信息
我的脚本如下:
#!/usr/bin/env python3.8
# -*- coding: utf-8 -*-
import openai
openai.api_key = "<My PAI Key>"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": "Tell the world about the ChatGPT API in the style of a pirate."}
]
)
print(completion.choices[0].message.content)
Run Code Online (Sandbox Code Playgroud)
我正在声明 shebang python3.8,因为我正在使用pyenv。我认为它应该可以工作,因为我做了 0 个 API 请求,所以我假设我的代码中有错误。
OpenAI 的文本模型具有上下文长度,例如:Curie 的上下文长度为 2049 个标记。它们提供 max_tokens 和 stop 参数来控制生成序列的长度。因此,当获得停止令牌或达到 max_tokens 时,生成就会停止。
问题是:生成文本时,我不知道提示符包含多少个标记。因为我不知道,所以我无法设置 max_tokens = 2049 - number_tokens_in_prompt。
这使我无法为各种长度的文本动态生成文本。我需要的是继续生成直到停止令牌。
我的问题是:
我正在向完成端点发出请求。我的提示是 1360 个令牌,经 Playground 和 Tokenizer 验证。我不会显示提示,因为对于这个问题来说有点太长了。
这是我使用 openai npm 包在 Nodejs 中对 openai 的请求。
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt,
max_tokens: 4000,
temperature: 0.2
})
Run Code Online (Sandbox Code Playgroud)
在 Playground 中进行测试时,响应后我的总代币数为 1374。
通过完成 API 提交提示时,我收到以下错误:
error: {
message: "This model's maximum context length is 4097 tokens, however you requested 5360 tokens (1360 in your prompt; 4000 for the completion). Please reduce your prompt; or completion length.",
type: 'invalid_request_error',
param: null,
code: null
}
Run Code Online (Sandbox Code Playgroud)
如果您能够解决这个问题,我很想听听您是如何做到的。
import discord
import openai
import os
openai.api_key = os.environ.get("OPENAI_API_KEY")
#Specify the intent
intents = discord.Intents.default()
intents.members = True
#Create Client
client = discord.Client(intents=intents)
async def generate_response(message):
prompt = f"{message.author.name}: {message.content}\nAI:"
response = openai.Completion.create(
engine="gpt-3.5-turbo",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
return response.choices[0].text.strip()
@client.event
async def on_ready():
print(f"We have logged in as {client.user}")
@client.event
async def on_message(message):
if message.author == client.user:
return
response = await generate_response(message)
await message.channel.send(response)
discord_token = 'DiscordToken'
client.start(discord_token)
Run Code Online (Sandbox Code Playgroud)
我尝试使用不同的方式来访问 API 密钥,包括添加到环境变量。
我还能尝试什么或者哪里出错了,对于编程来说还很陌生。错误信息:
openai.error.AuthenticationError:未提供 API 密钥。您可以使用“openai.api_key =”在代码中设置 API …
我可以使用 gpt-3.5-turbo-0301 模型访问 ChatGPT API,但不能使用任何 gpt-4 模型。这是我用来测试这个的代码(它不包括我的 openai API 密钥)。代码按编写的方式运行,但是当我用“gpt-4”、“gpt-4-0314”或“gpt-4-32k-0314”替换“gpt-3.5-turbo-0301”时,它给了我一个错误“openai.error.InvalidRequestError:模型:gpt-4不存在”。我有 ChatGPT+ 订阅,正在使用自己的 API 密钥,并且可以通过 OpenAI 自己的界面成功使用 gpt-4。
如果我使用 gpt-4-0314 或 gpt-4-32k-0314,也会出现同样的错误。我看过几篇文章,声称此或类似的代码可以使用“gpt-4”作为模型规范,我在下面粘贴的代码来自其中一篇。有谁知道是否可以通过Python + API访问gpt-4模型,如果可以,你该怎么做?
openai_key = "sk..."
openai.api_key = openai_key
system_intel = "You are GPT-4, answer my questions as if you were an expert in the field."
prompt = "Write a blog on how to use GPT-4 with python in a jupyter notebook"
# Function that calls the GPT-4 API
def ask_GPT4(system_intel, prompt):
result = openai.ChatCompletion.create(model="gpt-3.5-turbo-0301",
messages=[{"role": "system", "content": …Run Code Online (Sandbox Code Playgroud) gpt-4注意:这个问题最初是问和之间的区别gpt-4-0314。截至 2023 年 6 月 15 日,有新的快照模型可用(例如gpt-4-0613),因此问题及其答案也与接下来几个月内出现的任何未来快照模型相关。
谁能帮我解释一下gpt-4和gpt-4-0314(出现在下面的 OpenAI Playground 下拉菜单中)之间的区别吗?我检查了各种搜索引擎,从 OpenAI 论坛结果来看并不清楚。
GPT-4-0314 可能指 GPT-4 的特定版本或发行版,具有一组特定的更新或改进,可能于 3 月 14 日发布。任何有任何差异经验的人都欢迎提供反馈。
我不断收到如下错误
Request timed out: HTTPSConnectionPool(host='api.openai.com', port=443): Read timed out. (read timeout=600)
当我运行下面的代码时
def generate_gpt3_response(user_text, print_output=False):
"""
Query OpenAI GPT-3 for the specific key and get back a response
:type user_text: str the user's text to query for
:type print_output: boolean whether or not to print the raw output JSON
"""
time.sleep(5)
completions = ai.Completion.create(
engine='text-davinci-003', # Determines the quality, speed, and cost.
temperature=0.5, # Level of creativity in the response
prompt=user_text, # What the user typed in
max_tokens=150, # Maximum …Run Code Online (Sandbox Code Playgroud) OpenAI 文档model中有关微调 API 属性的说明有点令人困惑:
模型
要微调的基本模型的名称。您可以选择“ada”、“babbage”、“curie”、“davinci”之一或 2022 年 4 月 21 日之后创建的微调模型。
我的问题:微调基本模型还是微调模型更好?
ada我从with file创建了一个微调模型mydata1K.jsonl:
ada + mydata1K.jsonl --> ada:ft-acme-inc-2022-06-25
Run Code Online (Sandbox Code Playgroud)
现在我有一个更大的样本文件mydata2K.jsonl,我想用它来改进微调模型。在这第二轮微调中,是ada再次微调好还是对我微调后的模型进行微调好ada:ft-acme-inc-2022-06-25?我假设这是可能的,因为我的微调模型是在 2022 年 4 月 21 日之后创建的。
ada + mydata2K.jsonl --> better-model
Run Code Online (Sandbox Code Playgroud)
或者
ada:ft-acme-inc-2022-06-25 + mydata2K.jsonl --> even-better-model?
Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个聊天机器人,由于我使用的是 Windows 11,它不允许我迁移到较新的 OpenAI 库或降级它。ChatCompletion我可以用其他功能替换该功能以在我的版本上使用吗?
这是代码:
import openai
openai.api_key = "private"
def chat_gpt(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message['content'].strip()
if __name__ == "__main__":
while True:
user_input = input("You: ")
if user_input.lower() in ["quit", "exit", "bye"]:
break
response = chat_gpt(user_input)
print("Bot:", response)
Run Code Online (Sandbox Code Playgroud)
这是完整的错误:
...您尝试访问 openai.ChatCompletion,但 openai>=1.0.0 不再支持此功能 - 请参阅https://github.com/openai/openai-python上的API 了解自述文件。
您可以运行
openai migrate自动升级您的代码库以使用 1.0.0 界面。或者,您可以将安装固定到旧版本,例如 <pip install openai==0.28>
此处提供了详细的迁移指南:https ://github.com/openai/openai-python/discussions/742
我尝试通过 pip 进行升级和降级。
如何指定 Open AI 补全应返回的字数?
例如想象我问人工智能这个问题
埃隆·马斯克是谁?
我可以使用什么参数来确保 AI 发回的结果小于或等于 300 个字?
我认为max_tokens参数是为了这个,但它似乎max_tokens是为了分解输入而不是输出。
openai-api ×10
python ×5
chatgpt-api ×4
gpt-3 ×4
gpt-4 ×3
completion ×1
discord ×1
fine-tuning ×1
javascript ×1
node.js ×1
pip ×1
prompt ×1