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:
@@ -0,0 +1,37 @@
|
||||
from sqlalchemy.orm import Session as DBSession
|
||||
|
||||
from app.core.config import KNOWLEDGE_CONTEXT_LIMIT
|
||||
from app.db.models import KnowledgeEntry, KnowledgeSource
|
||||
|
||||
|
||||
def save_knowledge(db: DBSession, query: str, summary: str, sources: list[dict]) -> KnowledgeEntry:
|
||||
"""Web検索で得た知見をDBに保存する。"""
|
||||
entry = KnowledgeEntry(query=query, summary=summary)
|
||||
db.add(entry)
|
||||
db.flush()
|
||||
for src in sources:
|
||||
if not src.get("url"):
|
||||
continue
|
||||
db.add(KnowledgeSource(entry_id=entry.id, title=src.get("title", ""), url=src["url"]))
|
||||
db.commit()
|
||||
db.refresh(entry)
|
||||
return entry
|
||||
|
||||
|
||||
def get_knowledge_context(db: DBSession) -> str | None:
|
||||
"""蓄積された知識を、システムプロンプトに注入できる形のテキストにして返す。"""
|
||||
entries = (
|
||||
db.query(KnowledgeEntry)
|
||||
.order_by(KnowledgeEntry.created_at.desc())
|
||||
.limit(KNOWLEDGE_CONTEXT_LIMIT)
|
||||
.all()
|
||||
)
|
||||
if not entries:
|
||||
return None
|
||||
|
||||
blocks = []
|
||||
for entry in reversed(entries):
|
||||
urls = ", ".join(s.url for s in entry.sources) or "出典不明"
|
||||
blocks.append(f"- 「{entry.query}」について: {entry.summary}\n (出典: {urls})")
|
||||
|
||||
return "以下はこれまでのWeb検索で得た知識です。関連する話題が出たら活用してください:\n\n" + "\n".join(blocks)
|
||||
Reference in New Issue
Block a user