Создание телеграм бота на python
Всем привет, создаю телеграмм бота с chatGPT на python, возникла проблема в коде. Вот сам код:
import telebot
import openai
from config import OpenAIKey
from config import TgKey
openai.api_key = OpenAIKey
bot = telebot.TeleBot(TgKey)
@bot.message_handler(commands = ['start'])
def welcome(message):
bot.send_message(message.chat.id, 'Привет, я твой маркетолог в телеграм')
@bot.message_handler(content_types = ['text'])
def talk(message):
response = client.completions.create(
model="text-davinci-003",
promt=message.text,
temperature=0.5,
max_tokens=1000,
top_p=1.0,
frequency_penalty=0.5,
presence_penalty=0.5
)
gpt_text = response['choices'][0]['text']
bot.send_message(message.chat.id, gpt_text)
bot.polling(non_stop=True)
Ошибка возникает на 14 строке, звучит так:
Exception has occurred: NameError
name 'client' is not defined
File "C:\Users\lolke\OneDrive\Рабочий стол\Project\main.py", line 14, in talk
response = client.completions.create(
^^^^^^
File "C:\Users\lolke\OneDrive\Рабочий стол\Project\main.py", line 26, in <module>
bot.polling(non_stop=True)
можете помочь пожалуйста?
Ответы (2 шт):
Автор решения: Anton Petrov
→ Ссылка
Он у тебя ругается, потому что переменная client не импортирована.
Автор решения: BS NIK Channel
→ Ссылка
Как и упоминал Anton Petrov. Отсутствует инициализация класса OpenAI(), которому приcваивается имя client
openai.api_key = OpenAIKey
client = openai.OpenAI()
bot = telebot.TeleBot(TgKey)
Попробуйте этот код, должен работать.