59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
"""
|
|
Open WebUI Tool — Search Home Logs
|
|
Paste this into Open WebUI: Admin Panel → Tools → + New Tool
|
|
|
|
Name: Search Home Logs
|
|
Description: Searches Frigate camera events, Home Assistant history and system logs
|
|
"""
|
|
|
|
import requests
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class Tools:
|
|
class Valves(BaseModel):
|
|
search_api_url: str = Field(
|
|
default="http://192.168.1.132:8765/search",
|
|
description="URL of the log search API",
|
|
)
|
|
n_results: int = Field(default=10, description="Number of results to return")
|
|
|
|
def __init__(self):
|
|
self.valves = self.Valves()
|
|
|
|
def search_home_logs(self, query: str) -> str:
|
|
"""
|
|
Search home automation logs: Frigate camera detections, Home Assistant
|
|
entity state history, HA system logs and Docker container logs.
|
|
Use this when the user asks about past events at home, camera detections,
|
|
when doors/windows/lights changed state, or any system errors.
|
|
|
|
:param query: Natural language question about home events
|
|
:return: Relevant log entries with timestamps
|
|
"""
|
|
try:
|
|
r = requests.post(
|
|
self.valves.search_api_url,
|
|
json={"query": query, "n_results": self.valves.n_results},
|
|
timeout=30,
|
|
)
|
|
r.raise_for_status()
|
|
results = r.json()
|
|
|
|
if not results:
|
|
return "No relevant log entries found for this query."
|
|
|
|
lines = []
|
|
for item in results:
|
|
score = item.get("score", 0)
|
|
text = item.get("text", "")
|
|
meta = item.get("meta", {})
|
|
ts = meta.get("timestamp", "")
|
|
src = meta.get("source", item.get("collection", ""))
|
|
lines.append(f"[{src}] [{ts}] (relevance: {score})\n{text}")
|
|
|
|
return "\n\n---\n\n".join(lines)
|
|
|
|
except Exception as e:
|
|
return f"Error querying log search API: {e}"
|