将我的 OpenAI 包更新到版本 1.1.1 后,我在尝试读取 ChatGPT API 响应时收到此错误:
“ChatCompletion”对象不可订阅
这是我的代码:
messages = [
{"role": "system", "content": '''You answer question about some service'''
},
{"role": "user", "content": 'The user question is ...'},
]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0
)
response_message = response["choices"][0]["message"]["content"]
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
我想将各种 PDF 中的文本发送到OpenAI 的 API。特别是针对二年级学生的 Summarize或TL;DR 摘要API。
PyMuPDF我可以使用OpenAI 提示从 PDF 中提取文本。
问题:当令牌数超过允许的 2049 时,如何最好地准备提示?
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 …
我希望 ChatGPT 记住过去的对话并进行一致(有状态)的对话。
我见过几个ChatGPT提示工程的代码。
有两种方法可以设计如下所示的提示(伪代码):
使用单个输入(便宜)<- 如果可能的话更好
堆叠所有以前的历史记录(昂贵,令牌限制)
def openai_chat(prompt):
completions = openai.Completion.create(
engine = "text-davinci-003",
prompt = prompt,
max_tokens = 1024,
n = 1,
temperature = 0.8,
)
response = completions.choices[0].text.strip()
return response
# 1. Use a single input
while True:
prompt = input("User: ")
completion = openai_chat(prompt)
# 2. Stack all of previous history (prompt + completion)
prompt = ""
while True:
cur_prompt = input("User: ")
prompt += cur_prompt # pseudo code
completion = openai_chat(prompt)
prompt …Run Code Online (Sandbox Code Playgroud) 我可以使用 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) class OpenaiClassifier():
def __init__(self, api_keys):
openai.api_key = api_keys['Openai']
def get_ratings(self, review):
prompt = f"Rate the following review as an integer from 1 to 5, where 1 is the worst and 5 is the best: \"{review}\""
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
n=1,
max_tokens=5,
temperature=0.5,
top_p=1
)
try:
rating = int(response.choices[0].text.strip())
return rating
except ValueError:
return None
Run Code Online (Sandbox Code Playgroud)
我想知道 /v1/completions 和 /v1/chat/completions 端点之间的主要区别是什么,以及如何使用这些模型进行文本分类:gpt-4、gpt-4-0314、gpt-4-32k、gpt-4 -32k-0314,gpt-3.5-turbo,gpt-3.5-turbo-0301
我正在使用node.js并想使用openai API
我刚刚从 openai Playground 复制了代码,它看起来像这样
export const askOpenAi = async () => {
const response = await openai.createCompletion("text-davinci-001", {
prompt: "\ninput: What is human life expectancy in the United States?\n",
temperature: 0,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["\n", "\ninput:"],
});
return response.data;
}
Run Code Online (Sandbox Code Playgroud)
openai的返回数据是这样的
{
id: '~~~',
object: 'text_completion',
created: ~~~,
model: 'text-davinci:001',
choices: [ { text: '', index: 0, logprobs: null, finish_reason: 'stop' } ]
}
Run Code Online (Sandbox Code Playgroud)
在操场上,这段代码运行得很好。
我怎样才能得到正确的回应?
我是 API 新手,我试图了解如何使用 OpenAI 的 GPT-3 API(使用 api.openai.com/v1/completions)从提示中获取响应。我正在使用邮递员来做到这一点。文档说只有一个必需的参数,即“模型”。但是,我收到一条错误消息,提示“您必须提供模型参数”,尽管我已经提供了该参数。
我究竟做错了什么?
我使用 OpenAI 的Whisper python 库进行语音识别。如何获取字级时间戳?
使用 OpenAI 的Whisper进行转录(在 Ubuntu 20.04 x64 LTS 上使用 Nvidia GeForce RTX 3090 进行测试):
conda create -y --name whisperpy39 python==3.9
conda activate whisperpy39
pip install git+https://github.com/openai/whisper.git
sudo apt update && sudo apt install ffmpeg
whisper recording.wav
whisper recording.wav --model large
Run Code Online (Sandbox Code Playgroud)
如果使用 Nvidia GeForce RTX 3090,请在后面添加以下内容conda activate whisperpy39:
pip install -f https://download.pytorch.org/whl/torch_stable.html
conda install pytorch==1.10.1 torchvision torchaudio cudatoolkit=11.0 -c pytorch
Run Code Online (Sandbox Code Playgroud) python speech-recognition timestamp openai-api openai-whisper
如何向 RetrievalQA.from_chain_type 添加内存?或者,如何向 ConversationalRetrievalChain 添加自定义提示?
在过去的两周里,我一直在尝试制作一个可以通过文档聊天的聊天机器人(因此不仅仅是语义搜索/质量保证,因此需要记忆),而且还可以使用自定义提示。我已经尝试了所有链的每种组合,到目前为止,我得到的最接近的是 ConversationalRetrievalChain,但没有自定义提示,以及 RetrievalQA.from_chain_type 但没有内存