File size: 2,480 Bytes
e63c592
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import json
import sys
from typing import Any, Dict

import httpx


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description=(
            "Smoke-test the /chat endpoint with Tavily web search fallback. "
            "This expects TAVILY_API_KEY to be configured in the backend environment."
        )
    )
    parser.add_argument(
        "--backend-url",
        type=str,
        default="http://localhost:8000",
        help="Base URL of the running backend (default: http://localhost:8000).",
    )
    parser.add_argument(
        "--namespace",
        type=str,
        default="dev",
        help="Target Pinecone namespace (default: dev).",
    )
    parser.add_argument(
        "--query",
        type=str,
        default="Latest news about retrieval-augmented generation benchmarks",
        help="Query that is likely to require fresh web information.",
    )
    return parser.parse_args()


def main() -> int:
    args = parse_args()
    base_url = args.backend_url
    url = f"{base_url.rstrip('/')}/chat"

    payload: Dict[str, Any] = {
        "query": args.query,
        "namespace": args.namespace,
        "top_k": 3,
        "use_web_fallback": True,
        "min_score": 0.9,  # Intentionally high to encourage web fallback
        "max_web_results": 5,
        "chat_history": [],
    }

    print(f"[smoke_chat_web] POST {url} with payload:", file=sys.stderr)
    print(json.dumps(payload, indent=2), file=sys.stderr)

    with httpx.Client(timeout=60.0) as client:
        resp = client.post(url, json=payload)
        try:
            resp.raise_for_status()
        except httpx.HTTPError as exc:
            print(f"[smoke_chat_web] /chat request failed: {exc}", file=sys.stderr)
            if resp.content:
                print(resp.text, file=sys.stderr)
            return 1

        data = resp.json()
        print("[smoke_chat_web] /chat response:", file=sys.stderr)
        print(json.dumps(data, indent=2), file=sys.stderr)

    answer = data.get("answer", "")
    sources = data.get("sources", [])
    web_sources = [s for s in sources if s.get("source") == "web"]

    print("\nAnswer:\n", answer)
    print(f"\nTotal sources returned: {len(sources)}")
    print(f"Sources from web search: {len(web_sources)}")

    for src in web_sources[:5]:
        print(f"- {src.get('title')} ({src.get('url', '')})")

    return 0


if __name__ == "__main__":
    raise SystemExit(main())