- Split backend/app/core into weather/, discord/, memory_kb/, search/ subpackages by responsibility, keeping config.py and llm.py as the shared foundation; updated all import sites accordingly. - Add local Markdown knowledge base support (local_knowledge.py, knowledge_base/fgo.md, LOCAL_KB_* settings) that embeds and injects relevant docs into the system prompt by topic similarity. - Add ruff and pyright as backend dev dependencies with basic config in pyproject.toml for linting and type checking. - Document local machine quirks in CLAUDE.md (Postgres on port 8080, SSLKEYLOGFILE breaking litellm/pyright via OPENSSL_Uplink crashes). - Add backend/run_dev.ps1 dev launcher and docs/discord-bot.md.
58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
import logging
|
|
|
|
import httpx
|
|
|
|
from app.core.config import TAVILY_API_KEY
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
TAVILY_SEARCH_URL = "https://api.tavily.com/search"
|
|
|
|
WEB_SEARCH_TOOL = {
|
|
"type": "function",
|
|
"function": {
|
|
"name": "web_search",
|
|
"description": "最新情報・事実確認・知らない固有名詞など、自分の知識だけでは答えられない可能性があるときにWebを検索する。"
|
|
"ただし、システムプロンプトに提示されたローカルナレッジベースの資料で回答できる場合は使わないこと。",
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"query": {"type": "string", "description": "検索したい内容を表す検索クエリ"},
|
|
},
|
|
"required": ["query"],
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
async def search_web(query: str, max_results: int = 5) -> list[dict]:
|
|
"""Tavily APIでWeb検索する。結果は [{title, url, content}, ...]。失敗時は空リスト。"""
|
|
if not TAVILY_API_KEY:
|
|
logger.warning("TAVILY_API_KEY is not set; skipping web search")
|
|
return []
|
|
|
|
try:
|
|
async with httpx.AsyncClient(timeout=15.0) as client:
|
|
response = await client.post(
|
|
TAVILY_SEARCH_URL,
|
|
json={
|
|
"api_key": TAVILY_API_KEY,
|
|
"query": query,
|
|
"max_results": max_results,
|
|
},
|
|
)
|
|
response.raise_for_status()
|
|
data = response.json()
|
|
except Exception:
|
|
logger.exception("Tavily search failed for query=%r", query)
|
|
return []
|
|
|
|
results = []
|
|
for item in data.get("results", []):
|
|
results.append({
|
|
"title": item.get("title") or "",
|
|
"url": item.get("url") or "",
|
|
"content": item.get("content") or "",
|
|
})
|
|
return results
|