diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..ba36365 --- /dev/null +++ b/app/config.py @@ -0,0 +1,26 @@ +"""Central, environment-driven configuration for the agent.""" +from __future__ import annotations + +import os +from pathlib import Path + +from dotenv import load_dotenv + +load_dotenv() + +PROJECT_ROOT = Path(__file__).resolve().parent.parent + +OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "llama3.2:3b") +OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434") + +EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2") + +POLICY_DOCS_DIR = Path(os.getenv("POLICY_DOCS_DIR", PROJECT_ROOT / "app" / "data" / "policies")) +INDEX_DIR = Path(os.getenv("INDEX_DIR", PROJECT_ROOT / "app" / "data" / "index")) +TICKETS_DB_PATH = Path(os.getenv("TICKETS_DB_PATH", PROJECT_ROOT / "app" / "data" / "tickets.db")) + +RAG_TOP_K = int(os.getenv("RAG_TOP_K", "4")) +RAG_MAX_REWRITES = int(os.getenv("RAG_MAX_REWRITES", "2")) + +CHUNK_SIZE = int(os.getenv("CHUNK_SIZE", "500")) +CHUNK_OVERLAP = int(os.getenv("CHUNK_OVERLAP", "50")) diff --git a/app/state.py b/app/state.py new file mode 100644 index 0000000..8d8b473 --- /dev/null +++ b/app/state.py @@ -0,0 +1,48 @@ +"""Shared state schemas for the main agent graph and the RAG subgraph.""" +from __future__ import annotations + +from typing import Any, Optional, TypedDict + + +class TraceStep(TypedDict): + node: str + detail: str + duration_ms: float + + +class AgentState(TypedDict, total=False): + # Input + user_query: str + + # Planning / decomposition (filled by classify_intent) + pending_tasks: list[str] # ordered queue of task types still to execute + rag_query: str # query to send into the RAG subgraph + calc_args: dict[str, Any] # extracted args for leave_balance_calculator + ticket_args: dict[str, Any] # extracted args for create_support_ticket + is_out_of_scope: bool + + # Intermediate results, keyed by task type + results: dict[str, Any] + + # Autonomous escalation flag, set by rag_answer on low-confidence RAG result + escalate: bool + escalate_reason: str + + # Observability + trace: list[TraceStep] + + # Output + final_answer: str + + +class RagState(TypedDict, total=False): + query: str + original_query: str + retries: int + max_retries: int + docs: list[dict[str, Any]] # [{"content": ..., "source": ..., "score": ...}] + relevant: bool + answer: str + confidence: str # "high" | "low" + sources: list[str] + trace: list[TraceStep] diff --git a/app/tracing.py b/app/tracing.py new file mode 100644 index 0000000..75490f6 --- /dev/null +++ b/app/tracing.py @@ -0,0 +1,9 @@ +"""Tiny shared helper for building trace entries used by both graphs and the Streamlit UI.""" +from __future__ import annotations + +import time + + +def trace_step(state: dict, node: str, detail: str, start: float) -> dict: + step = {"node": node, "detail": detail, "duration_ms": round((time.time() - start) * 1000, 1)} + return {"trace": state.get("trace", []) + [step]}