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) |
ВНИМАНИЕ!! Чтобы убрать переводчик, нужно сделать следующие изменения:
- Нужно заменить в 33 строке c "prompt=prompt.text," на "prompt=message.text,"
- Нужно заменить в 43 строке с "await message.answer(translated_result.text)" на "await message.answer(completion.choices[0].text)"
- Нужно удалить строки 6, 14, 30, 42
И все! Теперь chatGPT будет писать на таком языке, на котором вы задали ему вопрос.
рабочий код # Телеграм бот ChatGPT
import config
import logging
import openai
import asyncio
import os
from aiogram import Bot, Dispatcher, executor, types
log
logging.basicConfig(level=logging.INFO)
init translator
init openai
openai.api_key = ""
init aiogram
bot = Bot(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
completion = openai.Completion.create(
engine=model_engine,
prompt=message.text,
max_tokens=max_tokens,
temperature=0.5,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
await message.answer("ChatGPT: Генерирую ответ ...")
await message.answer(completion.choices[0].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
как вообще запустить бота, и обязательно ли нужен свой api?