Lakika
@Lakika
Sicario

Как решить ошибку Error: module 'openai' has no attribute 'ChatCompletion'?

Пытаюсь сделать связь между телеграмм ботом, и апи ии
Запускаю бота, пишу вопрос, а в ответ ошибка.
В лога вот что.

Error: module 'openai' has no attribute 'ChatCompletion'
2023-11-10 18:05:30,289 - ERROR - Error: module 'openai' has no attribute 'ChatCompletion'
2023-11-10 18:05:30,289 - ERROR - Error: module 'openai' has no attribute 'ChatCompletion'


Гуглил, но решения предлагаемые в ветках форумов, мне не помогают.

Вот код:
КОД

import telebot
import openai
import logging
import os


TELEGRAM_TOKEN = '123'

OPENAI_API_KEY = '123'

# Настройка логирования
log_file = 'bot.log'
if os.path.exists(log_file):
    os.remove(log_file)

logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

bot = telebot.TeleBot(TELEGRAM_TOKEN)

@bot.message_handler(commands=['start'])
def send_welcome(message):
    welcome_text = 'Привет! Я готов отвечать на ваши вопросы.'
    bot.reply_to(message, welcome_text)
    logging.info(f'Sent message to {message.chat.id}: {welcome_text}')

@bot.message_handler(func=lambda message: True)
def handle_message(message):
    try:
        # Запрос к OpenAI API
        openai.api_key = OPENAI_API_KEY
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-0613",  
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": message.text}
            ]
        )

        answer = response.choices[0].message['content']
        bot.send_message(message.chat.id, answer)
        logging.info(f'Received message from {message.chat.id}: {message.text}')
        logging.info(f'Sent message to {message.chat.id}: {answer}')
    except Exception as e:
        error_message = f'Error: {e}'
        print(error_message)
        bot.send_message(message.chat.id, "Произошла ошибка при обработке вашего запроса.")
        logging.error(error_message)

if __name__ == '__main__':
    bot.polling(none_stop=True)

  • Вопрос задан
  • 295 просмотров
Пригласить эксперта
Ответы на вопрос 1
Snakecharmer
@Snakecharmer
Широкопрофильный менеджер и аналитик
Попробуйте просто обновить OpenAI Python SDK до последней версии. Либо удалить и переустановить заново. Скорее всего, у вас просто устаревшая версия.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы