added RAG, Multiuser, TG bot
This commit is contained in:
@@ -1,44 +1,47 @@
|
||||
"""Инжект системных оповещений в чат без role=assistant (не ломает LLM-историю)."""
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import ChatSession, Message
|
||||
|
||||
DISPLAY_ONLY_ROLES = frozenset({"notice", "character"})
|
||||
|
||||
|
||||
def _latest_chat_session(db) -> ChatSession:
|
||||
session = db.scalar(
|
||||
select(ChatSession).order_by(ChatSession.updated_at.desc()).limit(1)
|
||||
)
|
||||
if not session:
|
||||
session = ChatSession(title="Уведомления")
|
||||
db.add(session)
|
||||
db.commit()
|
||||
db.refresh(session)
|
||||
return session
|
||||
|
||||
|
||||
def post_notice_to_latest_chat(content: str) -> int | None:
|
||||
"""Сохраняет notice в последний активный чат. Возвращает session_id."""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
session = _latest_chat_session(db)
|
||||
db.add(Message(session_id=session.id, role="notice", content=content))
|
||||
db.commit()
|
||||
return session.id
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def post_character_comment_to_latest_chat(content: str) -> int | None:
|
||||
"""Реплика персонажа в UI; не попадает в контекст LLM (в отличие от assistant)."""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
session = _latest_chat_session(db)
|
||||
db.add(Message(session_id=session.id, role="character", content=content))
|
||||
db.commit()
|
||||
return session.id
|
||||
finally:
|
||||
db.close()
|
||||
"""Инжект системных оповещений в чат без role=assistant (не ломает LLM-историю)."""
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import ChatSession, Message
|
||||
|
||||
DISPLAY_ONLY_ROLES = frozenset({"notice", "character"})
|
||||
|
||||
|
||||
def _latest_chat_session(db, user_id: int) -> ChatSession:
|
||||
session = db.scalar(
|
||||
select(ChatSession)
|
||||
.where(ChatSession.user_id == user_id)
|
||||
.order_by(ChatSession.updated_at.desc())
|
||||
.limit(1)
|
||||
)
|
||||
if not session:
|
||||
session = ChatSession(user_id=user_id, title="Уведомления")
|
||||
db.add(session)
|
||||
db.commit()
|
||||
db.refresh(session)
|
||||
return session
|
||||
|
||||
|
||||
def post_notice_to_latest_chat(content: str, user_id: int) -> int | None:
|
||||
"""Сохраняет notice в последний активный чат пользователя. Возвращает session_id."""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
session = _latest_chat_session(db, user_id)
|
||||
db.add(Message(session_id=session.id, role="notice", content=content))
|
||||
db.commit()
|
||||
return session.id
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def post_character_comment_to_latest_chat(content: str, user_id: int) -> int | None:
|
||||
"""Реплика персонажа в UI; не попадает в контекст LLM (в отличие от assistant)."""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
session = _latest_chat_session(db, user_id)
|
||||
db.add(Message(session_id=session.id, role="character", content=content))
|
||||
db.commit()
|
||||
return session.id
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Reference in New Issue
Block a user