init
This commit is contained in:
0
bot/__init__.py
Normal file
0
bot/__init__.py
Normal file
41
bot/ai_roles.py
Normal file
41
bot/ai_roles.py
Normal file
@@ -0,0 +1,41 @@
|
||||
ROLES = {
|
||||
"director": {
|
||||
"name": "Директор",
|
||||
"prompt": (
|
||||
"Ты — строгий генеральный директор крупной корпорации. "
|
||||
"Перепиши текст пользователя на бюрократический деловой язык, "
|
||||
"используя сложные обороты, канцеляризмы и пассивный залог. "
|
||||
"Будь максимально вежлив, но холоден."
|
||||
),
|
||||
},
|
||||
"gopnik": {
|
||||
"name": "Гопник",
|
||||
"prompt": (
|
||||
"Ты — пацан с района. Перепиши текст, используя уличный сленг, "
|
||||
"обращения 'братан', 'короче', 'слышь'. Будь дерзким, но не оскорбляй напрямую. "
|
||||
"Смысл сообщения должен остаться."
|
||||
),
|
||||
},
|
||||
"yoda": {
|
||||
"name": "Йода",
|
||||
"prompt": (
|
||||
"Магистр Йода ты. Слова в предложении переставлять должен ты. "
|
||||
"Мудрости налет тексту придай. Смысл сохранить сумей."
|
||||
),
|
||||
},
|
||||
"emoji": {
|
||||
"name": "Эмодзи",
|
||||
"prompt": (
|
||||
"Твоя задача — пересказать текст пользователя, используя ТОЛЬКО эмодзи. "
|
||||
"Максимум 1-2 слова текста, если без них никак, остальное — смайлики."
|
||||
),
|
||||
},
|
||||
"clown": {
|
||||
"name": "Клоун",
|
||||
"prompt": (
|
||||
"Ты — стендап-комик с плохим чувством юмора. "
|
||||
"Перескажи текст, постоянно вставляя несмешные каламбуры и шутки. "
|
||||
"В конце обязательно добавь 'Ба-дум-тсс!'."
|
||||
),
|
||||
},
|
||||
}
|
||||
32
bot/ai_tool.py
Normal file
32
bot/ai_tool.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv
|
||||
from gigachat import GigaChatAsyncClient
|
||||
from gigachat.models import Chat, Messages, MessagesRole
|
||||
|
||||
from bot.ai_roles import ROLES
|
||||
|
||||
load_dotenv()
|
||||
|
||||
_GIGACHAT_CLIENT = GigaChatAsyncClient(
|
||||
credentials=os.getenv("GIGA_API_KEY"),
|
||||
scope=os.getenv("GIGA_SCOPE"),
|
||||
model=os.getenv("GIGA_MODEL"),
|
||||
ca_bundle_file="./certs/cert.pem",
|
||||
)
|
||||
|
||||
|
||||
async def get_gigachat_response(user_text: str, role: str | None = None) -> str:
|
||||
payload = Chat(
|
||||
messages=[
|
||||
Messages(
|
||||
role=MessagesRole.SYSTEM,
|
||||
content=ROLES.get(role, "director")["prompt"],
|
||||
),
|
||||
Messages(role=MessagesRole.USER, content=user_text),
|
||||
]
|
||||
)
|
||||
|
||||
response = await _GIGACHAT_CLIENT.achat(payload=payload)
|
||||
|
||||
return response.choices[0].message.content
|
||||
17
bot/handlers.py
Normal file
17
bot/handlers.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from aiogram import Router
|
||||
from aiogram.filters import Command
|
||||
from aiogram.types import Message
|
||||
|
||||
from bot.ai_tool import get_gigachat_response
|
||||
|
||||
handlers_router = Router()
|
||||
|
||||
@handlers_router.message(Command(commands=["start"]))
|
||||
async def start_handler(message: Message) -> None:
|
||||
await message.answer(text="Привет!\n\nЯ бот для важных переговоров!\n\nПросто отправь мне свой текст...")
|
||||
|
||||
@handlers_router.message()
|
||||
async def echo_handler(message: Message) -> None:
|
||||
ai_response = await get_gigachat_response(user_text=message.text)
|
||||
|
||||
await message.answer(text=ai_response)
|
||||
57
bot/handlers2.py
Normal file
57
bot/handlers2.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from aiogram import Router
|
||||
from aiogram.filters import Command
|
||||
from aiogram.filters.callback_data import CallbackData
|
||||
from aiogram.fsm.context import FSMContext
|
||||
from aiogram.types import (
|
||||
CallbackQuery,
|
||||
InlineKeyboardButton,
|
||||
InlineKeyboardMarkup,
|
||||
Message,
|
||||
)
|
||||
|
||||
from bot.ai_roles import ROLES
|
||||
from bot.ai_tool import get_gigachat_response
|
||||
|
||||
handlers_router = Router()
|
||||
|
||||
|
||||
class RoleSelect(CallbackData, prefix="role_select"):
|
||||
role: str
|
||||
|
||||
|
||||
@handlers_router.message(Command(commands=["start"]))
|
||||
async def start_handler(message: Message) -> None:
|
||||
await message.answer(
|
||||
text="Привет!\n\nЯ бот для важных переговоров!\n\nПросто отправь мне свой текст..."
|
||||
)
|
||||
|
||||
|
||||
@handlers_router.callback_query(RoleSelect.filter())
|
||||
async def select_role(
|
||||
query: CallbackQuery, callback_data: RoleSelect, state: FSMContext
|
||||
) -> None:
|
||||
user_text = await state.get_value("user_text")
|
||||
|
||||
ai_response = await get_gigachat_response(
|
||||
user_text=user_text, role=callback_data.role
|
||||
)
|
||||
|
||||
await query.message.edit_text(text=ai_response, reply_markup=None)
|
||||
|
||||
|
||||
@handlers_router.message()
|
||||
async def echo_handler(message: Message, state: FSMContext) -> None:
|
||||
await state.update_data(user_text=message.text)
|
||||
keyboards = [
|
||||
[
|
||||
InlineKeyboardButton(
|
||||
text=v.get("name"), callback_data=RoleSelect(role=k).pack()
|
||||
)
|
||||
]
|
||||
for k, v in ROLES.items()
|
||||
]
|
||||
|
||||
await message.answer(
|
||||
text="Выбери в каком стиле хочешь получить ответ:",
|
||||
reply_markup=InlineKeyboardMarkup(inline_keyboard=keyboards),
|
||||
)
|
||||
Reference in New Issue
Block a user