dao-ai 0.1.16__py3-none-any.whl → 0.1.18__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dao_ai/tools/router.py ADDED
@@ -0,0 +1,89 @@
1
+ """
2
+ Query router for selecting execution mode based on query characteristics.
3
+
4
+ Routes to internal execution modes within the same retriever instance:
5
+ - standard: Single similarity_search for simple queries
6
+ - instructed: Decompose -> Parallel Search -> RRF for constrained queries
7
+ """
8
+
9
+ from pathlib import Path
10
+ from typing import Any, Literal
11
+
12
+ import mlflow
13
+ import yaml
14
+ from langchain_core.language_models import BaseChatModel
15
+ from langchain_core.runnables import Runnable
16
+ from loguru import logger
17
+ from mlflow.entities import SpanType
18
+ from pydantic import BaseModel, ConfigDict, Field
19
+
20
+ # Load prompt template
21
+ _PROMPT_PATH = Path(__file__).parent.parent / "prompts" / "router.yaml"
22
+
23
+
24
+ def _load_prompt_template() -> dict[str, Any]:
25
+ """Load the router prompt template from YAML."""
26
+ with open(_PROMPT_PATH) as f:
27
+ return yaml.safe_load(f)
28
+
29
+
30
+ class RouterDecision(BaseModel):
31
+ """Classification of a search query into an execution mode.
32
+
33
+ Analyze whether the query contains explicit constraints that map to
34
+ filterable metadata columns, or is a simple semantic search.
35
+ """
36
+
37
+ model_config = ConfigDict(extra="forbid")
38
+ mode: Literal["standard", "instructed"] = Field(
39
+ description=(
40
+ "The execution mode. "
41
+ "Use 'standard' for simple semantic searches without constraints. "
42
+ "Use 'instructed' when the query contains explicit constraints "
43
+ "that can be translated to metadata filters."
44
+ )
45
+ )
46
+
47
+
48
+ @mlflow.trace(name="route_query", span_type=SpanType.LLM)
49
+ def route_query(
50
+ llm: BaseChatModel,
51
+ query: str,
52
+ schema_description: str,
53
+ ) -> Literal["standard", "instructed"]:
54
+ """
55
+ Determine the execution mode for a search query.
56
+
57
+ Args:
58
+ llm: Language model for routing decision
59
+ query: User's search query
60
+ schema_description: Column names, types, and filter syntax
61
+
62
+ Returns:
63
+ "standard" for simple queries, "instructed" for constrained queries
64
+ """
65
+ prompt_config = _load_prompt_template()
66
+ prompt_template = prompt_config["template"]
67
+
68
+ prompt = prompt_template.format(
69
+ schema_description=schema_description,
70
+ query=query,
71
+ )
72
+
73
+ logger.trace("Routing query", query=query[:100])
74
+
75
+ # Use LangChain's with_structured_output for automatic strategy selection
76
+ # (JSON schema vs tool calling based on model capabilities)
77
+ try:
78
+ structured_llm: Runnable[str, RouterDecision] = llm.with_structured_output(
79
+ RouterDecision
80
+ )
81
+ decision: RouterDecision = structured_llm.invoke(prompt)
82
+ except Exception as e:
83
+ logger.warning("Router failed, defaulting to standard mode", error=str(e))
84
+ return "standard"
85
+
86
+ logger.debug("Router decision", mode=decision.mode, query=query[:50])
87
+ mlflow.set_tag("router.mode", decision.mode)
88
+
89
+ return decision.mode