Repository navigation
Expand file tree
/
Copy pathclassifier.py
More file actions
99 lines (77 loc) · 3.11 KB
/
Copy pathclassifier.py
File metadata and controls
99 lines (77 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import sys
from pathlib import Path
from typing import Any
from pydantic import BaseModel
import config
MODES_FILE = Path(__file__).parent / "docs" / "modes.md"
PATH_PARAMS = {"folder"}
PARAM_DESCRIPTIONS = {
"folder": "Path relative to vault (or absolute)",
"query": "The search query or topic",
"year": "4-digit year (e.g. 2026)",
"month": "Month number 1-12 (e.g. 3 for March)",
"max_results": "Number of results to return (default 5)",
}
INT_PARAMS = {"year", "month", "max_results"}
BOOL_PARAMS: set[str] = set()
_CLASSIFICATION_PROMPT = """\
You are a workflow classifier. Analyse the user request and the available workflow modes.
Extract the mode name, any parameters present in the request, and list required parameters that are missing.
Available modes:
---
{modes_doc}
---
User request: {user_prompt} /no_think"""
class RoutingResult(BaseModel):
mode: str | None
params: dict[str, Any]
missing: list[str]
confidence: float
def classify(user_prompt: str) -> RoutingResult:
"""Ask the LLM to match the prompt to a workflow mode and extract its parameters."""
modes_doc = MODES_FILE.read_text(encoding="utf-8")
prompt = _CLASSIFICATION_PROMPT.format(modes_doc=modes_doc, user_prompt=user_prompt)
result = config.client.chat(
model=config.MODEL_NAME,
messages=[{"role": "user", "content": prompt}],
format=RoutingResult.model_json_schema()
)
return RoutingResult.model_validate_json(result.message.content)
def collect_missing_params(missing: list[str], params: dict) -> dict:
"""Interactively prompt the user for any parameters the LLM could not extract from the request."""
if not missing:
return params
config.log("Some information is needed:\n")
try:
for name in missing:
desc = PARAM_DESCRIPTIONS.get(name, name)
value = input(f" {name} ({desc}): ").strip()
if name in INT_PARAMS:
value = int(value)
elif name in BOOL_PARAMS:
value = value.lower() in ("true", "yes", "1")
params[name] = value
except KeyboardInterrupt:
config.log("Cancelled.")
sys.exit(0)
return params
def prepend_vault_path(params: dict) -> dict:
"""Expand relative folder params to absolute paths under VAULT_PATH."""
for key in PATH_PARAMS:
if key in params and params[key]:
params[key] = os.path.join(config.VAULT_PATH, params[key])
return params
def resolve(routing: RoutingResult, user_prompt: str) -> dict:
"""Complete params: default query to the raw prompt, silently fill missing path params, then collect any remaining required values."""
params = routing.params
if not params.get("query"):
params["query"] = user_prompt
routing.missing = [p for p in routing.missing if p != "query"]
for key in PATH_PARAMS:
if key in routing.missing and key not in params:
params[key] = ""
missing = [p for p in routing.missing if p not in PATH_PARAMS]
params = collect_missing_params(missing, params)
params = prepend_vault_path(params)
return params