abbe7a18fc
- P0: hooks.py 3-tuple fix, trigger_dispatcher Contract, contacts/plugin unregister_actions_by_owner - P0: 5 test files — check_permission mocks removed, hardcoded DB credential → env var - P1: attachment_service DmsFile via Contract helper, restore_registry/history_hooks dedup - P1: mail/plugin restore unregister, mcp_client datetime.now(UTC), saved_views/filters patterns - P1: ProtectedRoute fail-closed, 13 test assertion fixes (bcrypt, DB-URLs, SECRET_KEYs) - P2: deprecated notifications → post_system_message (3 files), forgejo Base, report_generator lazy import - P2: webhooks permissions, deps.py/roles.py plugin perms removed, import_export default - P2: address/tags/entity_links patterns removed, worker.py Contract-Umgehungen fixed - P2: 28 frontend TODOs (hardcoded constants, deprecated notification API) - P3: dead code, duplicates, deprecated imports, private attr, __import__ inline - P3: 8 frontend TODOs (LucideIcons, inline styles, XSS, i18n) - ruff: 838 → 0 (612 auto-fix + 246 manual + 27 F821 regression fix) - F821: 30 → 0 (AutomationDefinition, DmsFile, user_id, Path, Any, String) - Contract-Umgehungen: 2 neue gefunden (worker.py:169, worker.py:280) und gefixt
125 lines
3.9 KiB
Python
125 lines
3.9 KiB
Python
"""KI query understanding and result aggregation via LiteLLM."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import uuid
|
|
from typing import Any
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.ai.llm_client import llm_complete
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
QUERY_ANALYZE_SYSTEM = (
|
|
"Du bist ein Query-Analyzer fuer ein CRM. "
|
|
"Analysiere die Suchanfrage und gib JSON zurueck: "
|
|
'{"normalized_query": str, "entities": {"person": str|null, "contact": str|null, "topic": str|null}, '
|
|
'"intent": str, "semantic_terms": [str], "suggested_filters": {}}'
|
|
)
|
|
|
|
RESULT_AGGREGATE_SYSTEM = (
|
|
"Du bist ein Result-Aggregator. Fasse Ergebnisse zusammen und generiere Facetten: "
|
|
'{"summary": str, "facets": {"types": {}, "dates": {}, "people": []}, "suggestions": [str]}'
|
|
)
|
|
|
|
# Sensible default model used when no provider-specific model is configured.
|
|
# llm_complete() resolves credentials/model prefix from the central config.
|
|
DEFAULT_LLM_MODEL = "ollama/deepseek-v4-flash"
|
|
|
|
|
|
def _fallback_query_analysis(query: str) -> dict[str, Any]:
|
|
return {
|
|
"normalized_query": query,
|
|
"entities": {},
|
|
"intent": "search",
|
|
"semantic_terms": [],
|
|
"suggested_filters": {},
|
|
}
|
|
|
|
|
|
def _fallback_aggregate(results: list[dict], query: str) -> dict[str, Any]:
|
|
return {
|
|
"summary": f"{len(results)} Ergebnisse gefunden",
|
|
"facets": {},
|
|
"suggestions": [],
|
|
}
|
|
|
|
|
|
def _parse_json_content(content: str) -> dict[str, Any]:
|
|
"""Parse LLM JSON response, stripping markdown code fences if present."""
|
|
content = content.strip()
|
|
if content.startswith("```"):
|
|
content = content.split("\n", 1)[-1] if "\n" in content else content[3:]
|
|
if content.endswith("```"):
|
|
content = content[:-3].strip()
|
|
return json.loads(content)
|
|
|
|
|
|
async def llm_analyze_query(
|
|
query: str,
|
|
db: AsyncSession | None = None,
|
|
tenant_id: uuid.UUID | None = None,
|
|
) -> dict[str, Any]:
|
|
"""Analyze a search query using LLM for intent, entities, and semantic terms.
|
|
|
|
Falls back to a simple dict if LLM fails.
|
|
"""
|
|
try:
|
|
result = await llm_complete(
|
|
model=DEFAULT_LLM_MODEL,
|
|
messages=[
|
|
{"role": "system", "content": QUERY_ANALYZE_SYSTEM},
|
|
{"role": "user", "content": query},
|
|
],
|
|
temperature=0.1,
|
|
max_tokens=500,
|
|
response_format={"type": "json_object"},
|
|
db=db,
|
|
tenant_id=tenant_id,
|
|
)
|
|
return _parse_json_content(result["content"])
|
|
except Exception:
|
|
logger.warning("LLM query analysis failed, using fallback", exc_info=True)
|
|
return _fallback_query_analysis(query)
|
|
|
|
|
|
async def llm_aggregate_results(
|
|
results: list[dict],
|
|
query: str,
|
|
db: AsyncSession | None = None,
|
|
tenant_id: uuid.UUID | None = None,
|
|
) -> dict[str, Any]:
|
|
"""Aggregate search results using LLM for summary, facets, and suggestions.
|
|
|
|
Falls back to a simple dict if LLM fails.
|
|
"""
|
|
if not results:
|
|
return _fallback_aggregate(results, query)
|
|
try:
|
|
# Truncate results to avoid token overflow
|
|
compact = [
|
|
{"entity_type": r.get("entity_type"), "title": r.get("title", "")[:100]}
|
|
for r in results[:50]
|
|
]
|
|
user_msg = json.dumps({"query": query, "results": compact})
|
|
|
|
result = await llm_complete(
|
|
model=DEFAULT_LLM_MODEL,
|
|
messages=[
|
|
{"role": "system", "content": RESULT_AGGREGATE_SYSTEM},
|
|
{"role": "user", "content": user_msg},
|
|
],
|
|
temperature=0.1,
|
|
max_tokens=1000,
|
|
response_format={"type": "json_object"},
|
|
db=db,
|
|
tenant_id=tenant_id,
|
|
)
|
|
return _parse_json_content(result["content"])
|
|
except Exception:
|
|
logger.warning("LLM result aggregation failed, using fallback", exc_info=True)
|
|
return _fallback_aggregate(results, query)
|