sdfsdf sdfsdf
sdfsdf sdfsdf

Reputation: 3

OpenAI API error: "NameError: name 'client' is not defined"

This is my code:

import telebot
import openai
 
bot = telebot.TeleBot("0")
openai.api_key = "0"
 
@bot.message_handler(content_types=['text'])
def lalala(message):
    print(message.chat.title, message.chat.username)
    if message.chat.id == -1002097745017:
    #print(message.text)
        if "@0" in message.text:
            message.text = (message.text).replace("@0 ", "")
            #print(message.text)
            response = client.completions.create(model="gpt-3.5-turbo-0613", prompt=message.text, max_tokens=1000)
            full_response = response['choices'][0]['text']  # Use the text property of the first element of the choices list to access the full response
            lines = full_response.splitlines()  # Split the response into individual lines
            for line in lines:  # Iterate over the lines
                try:
                    #print(line)
                    bot.send_message(message.chat.id, line)  # Send each line back to the user as a separate message
                except Exception as e:
                    print(e)
    else:
        bot.send_message(message.chat.id, "work only - tg.com/123123")
 
bot.polling(none_stop=True, interval=0)

I'm getting the following error:

NameError: name 'client' is not defined

If I change this...

response = client.completions.create(model="gpt-3.5-turbo-0613", prompt=message.text, max_tokens=1000)

...to this.

response = openai.Completion.create(model="text-davinci-003", prompt=message.text, max_tokens=1000)

Then I'm getting the following error:

This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?

Upvotes: 0

Views: 3398

Answers (1)

Rok Benko
Rok Benko

Reputation: 22930

You tried multiple combinations, but not the right one.

  • Use openai not client, because you didn't initialize OpenAI with client, but with openai.
  • Use completions.create method name, not Completion.create if you're using OpenAI Python SDK version >=1.0.0.
  • Use the Completions model, not the Chat Completions model. While the text-davinci-003 is a Completions model, it will not work because it was deprecated not long ago. Use gpt-3.5-turbo-instruct instead, which is the recommended replacement for the text-davinci-003.

The following should work:

response = openai.completions.create(model="gpt-3.5-turbo-instruct", prompt=message.text, max_tokens=1000)

Upvotes: 0

Related Questions