Add web search knowledge base, message tagging, and word-memory tip topics
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.
This commit is contained in:
@@ -2,15 +2,21 @@ from sqlalchemy.orm import Session as DBSession
|
||||
from app.db.models import Memory, Message, Session
|
||||
from app.core.config import MEMORY_SUMMARIZE_THRESHOLD
|
||||
from app.core import llm
|
||||
from app.core import knowledge
|
||||
from app.core.tagging import tag_memory
|
||||
|
||||
|
||||
def get_context_messages(db: DBSession, session_id: str) -> list[dict]:
|
||||
"""短期メモリ(最近のメッセージ)と長期メモリ(要約)を結合してコンテキストを返す。"""
|
||||
"""蓄積知識・長期メモリ(要約)・短期メモリ(最近のメッセージ)を結合してコンテキストを返す。"""
|
||||
memories = db.query(Memory).filter(Memory.session_id == session_id).order_by(Memory.created_at).all()
|
||||
messages = db.query(Message).filter(Message.session_id == session_id).order_by(Message.created_at).all()
|
||||
|
||||
context: list[dict] = []
|
||||
|
||||
knowledge_context = knowledge.get_knowledge_context(db)
|
||||
if knowledge_context:
|
||||
context.append({"role": "system", "content": knowledge_context})
|
||||
|
||||
if memories:
|
||||
combined_summary = "\n\n".join(m.summary for m in memories)
|
||||
context.append({
|
||||
@@ -36,9 +42,13 @@ async def maybe_summarize(db: DBSession, session: Session) -> None:
|
||||
{"role": "system", "content": "以下の会話を簡潔に要約してください。重要な事実・決定・文脈を保持してください。"},
|
||||
*[{"role": m.role, "content": m.content} for m in to_summarize],
|
||||
]
|
||||
summary_text = await llm.complete(summary_prompt, session_id=session.id)
|
||||
summary_text = await llm.complete(summary_prompt, model=session.model, session_id=session.id)
|
||||
|
||||
db.add(Memory(session_id=session.id, summary=summary_text))
|
||||
memory = Memory(session_id=session.id, summary=summary_text)
|
||||
db.add(memory)
|
||||
for msg in to_summarize:
|
||||
db.delete(msg)
|
||||
db.commit()
|
||||
db.refresh(memory)
|
||||
|
||||
await tag_memory(db, memory)
|
||||
|
||||
Reference in New Issue
Block a user