Introduces websearch.py (Tavily) + knowledge.py to ground chat responses in search results, tagging.py to tag user and assistant messages via LLM and pick related topics for the Discord useless-tips job, and word_memory.py to track recent conversation nouns (surfaced in the new WordPool.vue sidebar) used to pick tip topics close to a related tag.
60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
import asyncio
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.core.config import CORS_ORIGINS
|
|
from app.core.discord_bot import start_discord_bot
|
|
from app.core.scheduler import start_scheduler
|
|
from app.core.word_memory import seed_from_history
|
|
from app.db.database import SessionLocal, init_db
|
|
from app.db.models import Message
|
|
from app.api import chat, memory, logs, system
|
|
|
|
|
|
def _seed_word_memory_from_db() -> None:
|
|
db = SessionLocal()
|
|
try:
|
|
rows = (
|
|
db.query(Message.content)
|
|
.filter(Message.role.in_(["user", "assistant"]))
|
|
.order_by(Message.created_at.desc())
|
|
.limit(200)
|
|
.all()
|
|
)
|
|
seed_from_history([content for (content,) in rows])
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
init_db()
|
|
_seed_word_memory_from_db()
|
|
start_scheduler()
|
|
asyncio.create_task(start_discord_bot())
|
|
yield
|
|
|
|
|
|
app = FastAPI(title="PlayChat API", version="0.1.0", lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
app.include_router(chat.router)
|
|
app.include_router(memory.router)
|
|
app.include_router(logs.router)
|
|
app.include_router(system.router)
|
|
|
|
|
|
@app.get("/health")
|
|
def health():
|
|
return {"status": "ok"}
|