Created
February 28, 2023 17:19
-
-
Save Priler/1c4e02e2aada2764d4cb0af20e4ff3e5 to your computer and use it in GitHub Desktop.
Python Aiogram ChatGPT simple bot via OpenAI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TOKEN = "<telegram-bot-token>" # obtained from @BotFather | |
OPENAI_TOKEN = "<openai-api-key>" # obtained from https://platform.openai.com/account/api-keys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Телеграм бот ChatGPT | |
import config | |
import logging | |
import openai | |
import asyncio | |
from gpytranslate import Translator | |
from aiogram import Bot, Dispatcher, executor, types | |
# log | |
logging.basicConfig(level=logging.INFO) | |
# init translator | |
t = Translator() | |
# init openai | |
openai.api_key = config.OPENAI_TOKEN | |
# init aiogram | |
bot = Bot(token=config.TOKEN) | |
dp = Dispatcher(bot) | |
@dp.message_handler() | |
async def gpt_answer(message: types.Message): | |
# await message.answer(message.text) | |
model_engine = "text-davinci-003" | |
max_tokens = 128 # default 1024 | |
prompt = await t.translate(message.text, targetlang="en") | |
completion = openai.Completion.create( | |
engine=model_engine, | |
prompt=prompt.text, | |
max_tokens=max_tokens, | |
temperature=0.5, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
await message.answer("ChatGPT: Генерирую ответ ...") | |
translated_result = await t.translate(completion.choices[0].text, targetlang="ru") | |
await message.answer(translated_result.text) | |
# run long-polling | |
if __name__ == "__main__": | |
executor.start_polling(dp, skip_updates=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
не рабочий...