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/cli.py +12 -4
- dao_ai/config.py +471 -44
- dao_ai/evaluation.py +543 -0
- dao_ai/memory/postgres.py +146 -35
- dao_ai/orchestration/core.py +33 -9
- dao_ai/orchestration/supervisor.py +23 -8
- dao_ai/orchestration/swarm.py +6 -1
- dao_ai/{prompts.py → prompts/__init__.py} +10 -1
- dao_ai/prompts/instructed_retriever_decomposition.yaml +58 -0
- dao_ai/prompts/instruction_reranker.yaml +14 -0
- dao_ai/prompts/router.yaml +37 -0
- dao_ai/prompts/verifier.yaml +46 -0
- dao_ai/providers/databricks.py +33 -12
- dao_ai/tools/instructed_retriever.py +366 -0
- dao_ai/tools/instruction_reranker.py +202 -0
- dao_ai/tools/mcp.py +21 -10
- dao_ai/tools/router.py +89 -0
- dao_ai/tools/vector_search.py +441 -134
- dao_ai/tools/verifier.py +159 -0
- dao_ai/utils.py +182 -2
- dao_ai/vector_search.py +9 -1
- {dao_ai-0.1.16.dist-info → dao_ai-0.1.18.dist-info}/METADATA +3 -3
- {dao_ai-0.1.16.dist-info → dao_ai-0.1.18.dist-info}/RECORD +26 -17
- {dao_ai-0.1.16.dist-info → dao_ai-0.1.18.dist-info}/WHEEL +0 -0
- {dao_ai-0.1.16.dist-info → dao_ai-0.1.18.dist-info}/entry_points.txt +0 -0
- {dao_ai-0.1.16.dist-info → dao_ai-0.1.18.dist-info}/licenses/LICENSE +0 -0
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
|