-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtg.py
135 lines (96 loc) · 4.28 KB
/
tg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from __future__ import annotations
import asyncio
import json
import logging
import string
import traceback
from aiogram import Bot, Dispatcher, Router
from aiogram.dispatcher import router
from aiogram.types import Message
from cerebras.cloud.sdk import RateLimitError
from cerebras_test import cerebras_chat
from conf import TG_API_TOKEN, DEBUG
from state.session import ensure_session, HISTORY, Session, reset_session
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=TG_API_TOKEN)
dp = Dispatcher()
router = Router()
@router.message()
async def echo_handler(message: Message) -> None:
print(json.dumps(message.dict()))
try:
username = message.from_user.username
if len(message.from_user.first_name) > 0:
username = message.from_user.first_name
if (
# True or
message.chat.id == -1001885182552
and message.chat.type == "supergroup"
and message.message_thread_id == 4521
): # Remixed Dungeon sandbox
text = message.text
uid = str(message.chat.id)
maybe_cmd = text.strip(string.whitespace + string.punctuation).split()[0]
if maybe_cmd.startswith("reset"):
reset_session(uid)
await message.reply("сессия сброшена")
return
session = ensure_session(uid)
session.user_text(text, username)
if maybe_cmd.startswith("статус"):
await message.reply(session.get_user_status())
return
if maybe_cmd.startswith("персона"):
make_persona_prompt = session.make_persona_prompt()
persona_candidate = cerebras_chat(make_persona_prompt)
if check_for_no(persona_candidate):
await message.reply(persona_candidate)
return
session.user_status(persona_candidate)
await message.reply(f"Персона обновлена: {persona_candidate}")
return
# check_reply = cerebras_chat(session.make_user_input_check_prompt())
#
# if not await check_for_yes(check_reply):
# await message.reply(check_reply)
# return
fix_reply = cerebras_chat(session.make_user_input_fix_prompt())
session.user_text(fix_reply, username)
session.npc_intent = cerebras_chat(session.make_intent_prompt())
reply = cerebras_chat(session.make_story_prompt())
session.llm_text(session.clean_llm_reply(reply))
user_params = cerebras_chat(session.make_user_params_update_prompt())
session.user_status(user_params)
#await message.reply(f"{username}:\n{user_params}")
updated_params = cerebras_chat(session.make_params_update_prompt())
updated_params = session.clean_llm_reply(updated_params)
session.params_updated(updated_params)
#await message.reply(updated_params)
updated_relations = cerebras_chat(session.make_relations_update_prompt())
updated_relations = session.clean_llm_reply(updated_relations)
session.relations_updated(updated_relations)
#await message.reply(updated_relations)
await message.reply(f"{session.active_user}:\n{session.user_intent}")
await message.reply(f"{session.llm_reply()}")
session.save()
except RateLimitError as rl:
await message.reply(f"На сегодня нафлудились: {rl.message}")
return
except Exception:
traceback.print_exc()
async def check_for_yes(check_reply):
test_reply = check_reply.lower().strip(string.punctuation + string.punctuation)
if test_reply.startswith("да") or test_reply.endswith("да"):
return True
async def check_for_no(check_reply):
test_reply = check_reply.lower().strip(string.punctuation + string.punctuation)
if test_reply.startswith("нет") or test_reply.endswith("нет"):
return True
async def main() -> None:
dp.include_router(router)
await dp.start_polling(bot, skip_updates=False)
# Start polling
if __name__ == "__main__":
asyncio.run(main())