-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
105 lines (85 loc) · 2.95 KB
/
Copy pathapp.py
File metadata and controls
105 lines (85 loc) · 2.95 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
100
101
102
103
104
105
"""Application entry point for the Router Agent.
Run with::
python app.py
"""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
from typing import Any
ROOT = Path(__file__).resolve().parent
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from config.logging_config import configure_logging, get_logger # noqa: E402
from config.settings import ConfigurationError, get_settings, reset_settings_cache # noqa: E402
from graph.llm_factory import build_llm, configure_langsmith # noqa: E402
from graph.workflow import RouterWorkflow # noqa: E402
logger = get_logger(__name__)
def _parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Run the Router Agent.")
parser.add_argument(
"query",
nargs="*",
help="Optional query to process. If omitted, an interactive prompt is shown.",
)
parser.add_argument(
"--config",
default=os.getenv("APP_CONFIG_PATH", "config/config.yaml"),
help="Path to the YAML configuration file.",
)
return parser.parse_args(argv)
def _build_workflow() -> RouterWorkflow:
reset_settings_cache()
try:
settings = get_settings()
except ConfigurationError as exc:
logger.error("config.error", extra={"error": str(exc)})
raise SystemExit(f"Configuration error: {exc}") from exc
configure_logging(settings)
configure_langsmith(settings)
llm = build_llm(settings)
return RouterWorkflow(settings=settings, llm=llm)
def _format_result(result: dict[str, Any]) -> str:
parts = [
f"Route: {result.get('route', 'unknown')}",
f"Confidence: {result.get('confidence', 0.0):.2f}",
f"Reasoning: {result.get('reasoning', '')}",
"",
f"Answer: {result.get('response', '')}",
]
path = result.get("execution_path") or []
if path:
parts.append("")
parts.append(f"Execution path: {' -> '.join(path)}")
return "\n".join(parts)
def main(argv: list[str] | None = None) -> int:
args = _parse_args(argv)
if args.config:
os.environ["APP_CONFIG_PATH"] = args.config
workflow = _build_workflow()
if args.query:
query = " ".join(args.query).strip()
if not query:
print("No query provided.", file=sys.stderr)
return 2
result = workflow.run(query)
print(_format_result(result))
return 0
print("Router Agent interactive shell. Type 'exit' or Ctrl-C to quit.")
while True:
try:
user_input = input("> ").strip()
except (EOFError, KeyboardInterrupt):
print()
break
if not user_input:
continue
if user_input.lower() in {"exit", "quit"}:
break
result = workflow.run(user_input)
print(_format_result(result))
print()
return 0
if __name__ == "__main__":
raise SystemExit(main())