Untitled
unknown
plain_text
3 years ago
2.7 kB
5
Indexable
import os
import openai
from datetime import datetime
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
TOKEN = 'САСИ ЛОХ'
openai.api_key = 'САСИ НЕГАР'
bot = Bot(TOKEN)
dp = Dispatcher(bot)
# Create a folder for chat history
HISTORY_FOLDER = 'history'
if not os.path.exists(HISTORY_FOLDER):
os.makedirs(HISTORY_FOLDER)
async def send_welcome(message: types.Message):
keyboard = types.InlineKeyboardMarkup()
author_button = types.InlineKeyboardButton(text="Автор", url="https://t.me/crap536")
keyboard.add(author_button)
await message.answer("Добро пожаловать в ChatGPT! Я являюсь языковой моделью, обученной OpenAI. Не стесняйтесь спрашивать меня о чем угодно, и я сделаю все возможное, чтобы дать полезный ответ.", reply_markup=keyboard)
@dp.message_handler(commands=['start'])
async def start_command_handler(message: types.Message):
await send_welcome(message)
@dp.message_handler()
async def handle_message(message: types.Message):
answer = ""
if message.text:
user_id = str(message.chat.id)[-1] # Get the last digit of the user's id
print(f"User: {message.chat.first_name} {message.chat.last_name}: {message.text}")
# Add message to chat history
now = datetime.now()
history_file = f"{HISTORY_FOLDER}/{message.chat.id}.txt"
with open(history_file, 'a') as f:
f.write(f"{now.strftime('%Y-%m-%d %H:%M:%S')} User {user_id}: {message.text}\n")
response = openai.Completion.create(
model="text-davinci-003",
prompt=message.text,
temperature=0.5,
max_tokens=2048,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.6,
stop=[" Human:", " AI:"]
)
answer = response['choices'][0]['text'].strip()
if answer:
await message.answer(answer)
# Add response to chat history
with open(history_file, 'a') as f:
f.write(f"{now.strftime('%Y-%m-%d %H:%M:%S')} ChatGPT {user_id}: {answer}\n")
else:
await message.answer("Я не смог найти ответ на ваш вопрос, попробуйте переформулировать вопрос.")
print(f"Bot: {answer}")
print("Бот успешно запущен! \n\nЛог сообщений:")
executor.start_polling(dp)Editor is loading...