File size: 1,996 Bytes
1db7196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import json
import sys
import urllib.request
import urllib.error


BASE_URL = "http://172.16.34.21:8086"
MODEL = "support_check"

QUESTIONS = [
    "Is the claim 'Regular exercise improves cardiovascular health' supported by evidence?",
    "Does the statement 'Vitamin C cures the common cold' have strong scientific support?",
    "Is it true that 'The Earth is flat'?",
    "Can you verify the claim: 'Smoking increases the risk of lung cancer'?",
    "Is the claim 'Drinking 8 glasses of water daily is required for everyone' universally supported?",
]


def post_chat_completion(prompt: str) -> dict:
    url = f"{BASE_URL}/v1/chat/completions"
    payload = {
        "model": MODEL,
        "temperature": 0.2,
        "max_tokens": 256,
        "messages": [
            {"role": "system", "content": "Answer succinctly."},
            {"role": "user", "content": prompt},
        ],
    }
    data = json.dumps(payload).encode("utf-8")
    req = urllib.request.Request(
        url,
        data=data,
        headers={"Content-Type": "application/json"},
        method="POST",
    )
    try:
        with urllib.request.urlopen(req, timeout=120) as resp:
            body = resp.read().decode("utf-8")
            return json.loads(body)
    except urllib.error.HTTPError as exc:
        body = exc.read().decode("utf-8") if exc.fp else ""
        raise RuntimeError(f"HTTP {exc.code}: {body}") from exc
    except urllib.error.URLError as exc:
        raise RuntimeError(f"Request failed: {exc.reason}") from exc


def main() -> int:
    for idx, question in enumerate(QUESTIONS, start=1):
        print(f"\nQ{idx}: {question}")
        result = post_chat_completion(question)
        try:
            answer = result["choices"][0]["message"]["content"]
        except (KeyError, IndexError, TypeError):
            answer = json.dumps(result, ensure_ascii=True)
        print(f"A{idx}: {answer}")
    return 0


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