File size: 2,058 Bytes
44d337e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54c5b76
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
#!/usr/bin/env python3
"""
Simple CLI for the Moffitt RAG Space.
Before running, run the following on your terminal:
    pip install requests python-dotenv
Then, in the same folder as this file demo_client.py, create a file named .env
Add in it the following line
RAG_API_KEY=password (replace the actual password here, no "" symbols needed)
Usage:
    python demo_client.py "What are the common types of cancer" -k 5
Environment variables:
    RAG_API_KEY   Your X-API-Key header value (e.g. "alpha")
    RAG_API_URL   (optional) override the default Space URL
"""
import argparse
import os
import sys
import requests
from dotenv import load_dotenv

load_dotenv()
DEFAULT_URL = "https://buildng-moffitt-rag-demo.hf.space/v1/query"


def call_rag_api(question: str, k: int = 5, url: str = DEFAULT_URL) -> str:
    api_key = os.getenv("RAG_API_KEY")
    if not api_key:
        sys.exit("RAG_API_KEY not set in environment")

    payload = {"query": question, "k": k}
    headers = {
        "Content-Type": "application/json",
        "X-API-Key": api_key,
    }

    response = requests.post(url, json=payload, headers=headers, timeout=30)
    response.raise_for_status()             # 4xx/5xx → raises HTTPError
    return response.json()["answer"]

def main() -> None:
    parser = argparse.ArgumentParser(description="Query the Moffitt RAG API")
    parser.add_argument("question", help="Your natural-language question")
    parser.add_argument("-k", type=int, default=5, help="Top-k passages to retrieve (default: 5)")
    args = parser.parse_args()

    answer = call_rag_api(args.question, args.k, DEFAULT_URL)
    print("\n=== Answer ===\n")
    print(answer)

if __name__ == "__main__":
    main()

"""
If you want to integrate this with your ChatGPT API,
Reuse the same call_rag_api functions,
and when you need it, simply call 
call_rag_api(question, k, DEFAULT_URL)
question is a string, your question, like "what is the risk of blood bone marrow transplant?"
k is an integer. Keep it a small integer (<10)
DEFAULT_URL is the variable above
"""