Reputation: 343
I am currently working on a chatbot, and as I am using Windows 11 it does not let me migrate to newer OpenAI library or downgrade it. Could I replace the ChatCompletion
function with something else to work on my version?
This is the code:
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)
And this is the full error:
... You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.
You can run
openai migrate
to automatically upgrade your codebase to use the 1.0.0 interface.Alternatively, you can pin your installation to the old version, e.g. <pip install openai==0.28>
A detailed migration guide is available here: https://github.com/openai/openai-python/discussions/742
I tried both upgrading and downgrading through pip.
Upvotes: 20
Views: 74237
Reputation: 23088
The method you're trying to use doesn't work with the OpenAI Python SDK >=v1.0.0
(if you're using Python) or OpenAI Node.js SDK >=v4.0.0
(if you're using Node.js). See the Python SDK migration guide or the Node.js SDK migration guide.
The old SDK (i.e., v0.28.1
) works with the following method:
client.ChatCompletion.create()
The new SDK (i.e., >=v1.0.0
) works with the following method:
client.chat.completions.create()
Note: Be careful because the API is case-sensitive (i.e., client.Chat.Completions.create()
will not work with the new SDK version).
The old SDK (i.e., v3.3.0
) works with the following method:
client.createChatCompletion()
The new SDK (i.e., >=v4.0.0
) works with the following method:
client.chat.completions.create()
Note: Be careful because the API is case-sensitive (i.e., client.Chat.Completions.create()
will not work with the new SDK version).
v1.0.0
working exampleIf you run test.py
, the OpenAI API will return the following completion:
Hello! How can I assist you today?
test.py
import os
from openai import OpenAI
client = OpenAI(
api_key = os.getenv("OPENAI_API_KEY"),
)
completion = client.chat.completions.create(
model = "gpt-3.5-turbo",
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
]
)
print(completion.choices[0].message.content.strip())
v4.0.0
working exampleIf you run test.js
, the OpenAI API will return the following completion:
Hello! How can I assist you today?
test.js
const OpenAI = require("openai");
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
async function main() {
const completion = await client.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" },
],
});
console.log(completion.choices[0].message.content.trim());
}
main();
Upvotes: 12
Reputation: 1411
Try updating to the latest and using:
from openai import OpenAI
client = OpenAI(
# defaults to os.environ.get("OPENAI_API_KEY")
api_key="private",
)
def chat_gpt(prompt):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content.strip()
EDIT: message.['content']
-> message.content
on the return of this function, as a message object is not subscriptable
error is thrown while using message.['content']
. Also, update link from pointing to the README
(subject to change) to migration guide specific to this code.
Upvotes: 34