File size: 3,273 Bytes
09da05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
"""Local CLI to run the end-to-end chat flow."""

from __future__ import annotations

import sys
from typing import Optional

import typer

from adsp.app import QAService

app = typer.Typer(add_completion=False)


def _print_citations(response) -> None:
    if not response.citations:
        return
    typer.echo("\nCitations:")
    for idx, citation in enumerate(response.citations[:5], start=1):
        doc_id = citation.doc_id or "unknown"
        pages = ",".join(str(p) for p in (citation.pages or [])) or "-"
        indicator = citation.indicator_label or citation.indicator_id or "-"
        typer.echo(f"- [{idx}] {doc_id} pages={pages} indicator={indicator}")


@app.command()
def list_personas() -> None:
    """List available personas loaded from processed data."""

    qa = QAService()
    registry = qa.orchestrator.prompt_builder.registry
    for persona_id in registry.list_personas():
        persona = registry.get(persona_id)
        name = getattr(persona, "persona_name", None) or getattr(persona, "persona_id", None) or ""
        typer.echo(f"{persona_id}\t{name}")


@app.command()
def ask(
    query: str = typer.Argument(..., help="User question"),
    persona_id: str = typer.Option("default", "--persona-id", help="Persona id"),
    session_id: Optional[str] = typer.Option(None, "--session-id", help="Conversation/session id"),
    top_k: int = typer.Option(5, "--top-k", help="Top-k retrieved context blocks"),
    show_context: bool = typer.Option(False, "--show-context", help="Print retrieved context"),
) -> None:
    """Ask a single question and print the persona response."""

    qa = QAService()
    response = qa.ask_with_metadata(
        persona_id=persona_id,
        query=query,
        session_id=session_id,
        top_k=top_k,
    )
    typer.echo(response.answer)
    _print_citations(response)
    if show_context:
        typer.echo("\nContext:\n" + response.context)


@app.command()
def chat(
    persona_id: str = typer.Option("default", "--persona-id", help="Persona id"),
    session_id: Optional[str] = typer.Option("local", "--session-id", help="Conversation/session id"),
    top_k: int = typer.Option(5, "--top-k", help="Top-k retrieved context blocks"),
    show_context: bool = typer.Option(False, "--show-context", help="Print retrieved context"),
) -> None:
    """Interactive chat loop (type 'exit' to quit)."""

    qa = QAService()
    typer.echo(f"Persona: {persona_id} (session_id={session_id})")
    typer.echo("Type 'exit' to quit.\n")

    while True:
        try:
            user_text = input("> ").strip()
        except (EOFError, KeyboardInterrupt):
            typer.echo("\nBye.")
            return
        if not user_text:
            continue
        if user_text.lower() in {"exit", "quit"}:
            typer.echo("Bye.")
            return

        response = qa.ask_with_metadata(
            persona_id=persona_id,
            query=user_text,
            session_id=session_id,
            top_k=top_k,
        )
        typer.echo("\n" + response.answer)
        _print_citations(response)
        if show_context:
            typer.echo("\nContext:\n" + response.context)
        typer.echo("")


def main() -> None:
    app()


if __name__ == "__main__":
    sys.exit(main())