OpenAI 的文本模型具有上下文长度,例如:Curie 的上下文长度为 2049 个标记。它们提供 max_tokens 和 stop 参数来控制生成序列的长度。因此,当获得停止令牌或达到 max_tokens 时,生成就会停止。
问题是:生成文本时,我不知道提示符包含多少个标记。因为我不知道,所以我无法设置 max_tokens = 2049 - number_tokens_in_prompt。
这使我无法为各种长度的文本动态生成文本。我需要的是继续生成直到停止令牌。
我的问题是:
我可以使用 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 日发布。任何有任何差异经验的人都欢迎提供反馈。
如何使用 API 将图像上传到聊天 gpt?你能给出一个可以做到这一点的代码示例吗?
我尝试查看文档,但他们没有上传 jpg 作为上下文的好方法。
我正在尝试构建一个 Discord 机器人,它使用 GPT-4 API 作为 Discord 上的聊天机器人。我有最新版本的 OpenAI 库,但当我运行代码时,它告诉我“发生错误:模块‘openai’没有属性‘ChatCompletion’”
我尝试卸载并重新安装 OpenAI 库,尝试使用完成端点并收到错误“这是一个聊天模型,v1/completions 端点不支持。您是否打算使用 v1/chat/completions?”
这是给我带来问题的代码片段:
async def get_gpt_response(prompt, history):
history_strings = [f"{message['role']}: {message['content']}" for message in history] # update history format
chat_prompt = '\n'.join(history_strings + [f"user: {prompt}"])
completions = openai.ChatCompletion.create(
engine=config["model"],
prompt=chat_prompt,
max_tokens=config["max_tokens"],
n=1,
temperature=config["temperature"],
)
return completions.choices[0].text.strip().split('assistant:', 1)[-1].strip()
Run Code Online (Sandbox Code Playgroud) 我看到这里有多种图像生成方法: https://platform.openai.com/docs/api-reference/images
但我只是想向聊天 gpt 发送一个 png 文件,问“这是什么?” 或类似的事情,然后得到回复。
我部署了 Azure 开放 AI 帐户和 GPT4 模型。我可以使用它的API进行图像到文本的描述吗?如果是,我将如何给它图像?我正在使用这段代码。但它给我一个错误。
import openai
# open ai key
openai.api_type = "azure"
openai.api_version = "2023-03-15-preview"
openai.api_base = 'https://xxxxxx.openai.azure.com/'
openai.api_key = "xxxxxxxxxxxxx"
image_url="https://cdn.repliers.io/IMG-X5925532_9.jpg"
def generate_image_description(image_url):
prompt = f"What is in this image? {image_url}"
print(prompt)
response = openai.ChatCompletion.create(
engine="GPT4v0314",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.0,
)
description = response.choices[0].text.strip()
return description
Run Code Online (Sandbox Code Playgroud)
错误就像;APIError:来自 API 的无效响应对象:“不支持的数据类型\n”(HTTP 响应代码为 400)
我在解释里面提到过。
我有以下代码实现 LangChain + ChatGPT 以回答给定数据的问题:
import { PineconeStore } from 'langchain/vectorstores/pinecone';
import { ConversationalRetrievalQAChain } from 'langchain/chains';
const CONDENSE_PROMPT = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`;
const QA_PROMPT = `You are a helpful AI assistant. Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say you don't …Run Code Online (Sandbox Code Playgroud) 我是 OpenAI API 的新手。我使用 GPT-3.5-Turbo,使用以下代码:
\nmessages = [\n {"role": "system", "content": "You\xe2\x80\x99re a helpful assistant"}\n ]\n\n while True:\n content = input("User: ")\n if content == 'end':\n save_log(messages)\n break\n messages.append({"role": "user", "content": content})\n\n completion = openai.ChatCompletion.create(\n model="gpt-3.5-turbo-16k",\n messages=messages\n )\n\n chat_response = completion.choices[0].message.content\n print(f'ChatGPT: {chat_response}')\n messages.append({"role": "assistant", "content": chat_response})\nRun Code Online (Sandbox Code Playgroud)\n结果:\n用户:谁是第一个登上月球的人?\nGPT:第一个踏上月球的人是美国宇航员尼尔·阿姆斯特朗,于 1969 年 7 月 20 日执行 NASA 阿波罗 11 号任务。\ n用户:他有多高?\nGPT:尼尔阿姆斯特朗身高约为 5 英尺 11 英寸(180 厘米)。
\n但它需要大量的代币。我听说 GPT-4 与 GPT-3 的不同之处在于它能够(自行)记住之前的消息。那是对的吗?
\n但是,如果我删除在“消息”列表中附加最新消息的行并仅发送一条消息:\n completion = openai.ChatCompletion.create( model="gpt-4", messages=[{"role": …
更新:他们似乎在 API 文档中犯了一个错误,现在已修复。
早些时候,它说“当调用gpt-4-vision-preview或 时gpt-3.5-turbo”,但现在改为“当调用gpt-4-1106-preview或 时gpt-3.5-turbo-1106”。
根据Text Generation - OpenAI API,“当调用gpt-4-vision-preview或时gpt-3.5-turbo,您可以将 response_format 设置{ type: "json_object" }为启用 JSON 模式。”
但是,以下代码会引发错误:
{'error': {'message': '1 validation error for Request\nbody -> response_format\n extra fields not permitted (type=value_error.extra)', 'type': 'invalid_request_error', 'param': None, 'code': None}}
Run Code Online (Sandbox Code Playgroud)
如果我发表评论"response_format": {"type": "json_object"},效果很好。
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"response_format": {"type": "json_object"},
"messages": [
{ …Run Code Online (Sandbox Code Playgroud) gpt-4 ×10
openai-api ×9
python ×4
chatgpt-api ×3
gpt-3 ×2
azure-openai ×1
chat-gpt-4 ×1
langchain ×1