Skip to content

Instantly share code, notes, and snippets.

@Priler
Created February 28, 2023 17:19

Revisions

  1. Priler created this gist Feb 28, 2023.
    2 changes: 2 additions & 0 deletions config.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    TOKEN = "<telegram-bot-token>" # obtained from @BotFather
    OPENAI_TOKEN = "<openai-api-key>" # obtained from https://platform.openai.com/account/api-keys
    47 changes: 47 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # Телеграм бот 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)