File size: 2,391 Bytes
4fcd019
 
 
 
 
 
 
 
 
 
 
5b9a030
4fcd019
c5e3791
 
 
4fcd019
 
 
 
 
c5e3791
4fcd019
 
 
 
5b9a030
 
4fcd019
5b9a030
4fcd019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b9a030
 
 
 
 
4fcd019
 
 
 
 
 
 
 
 
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
"""
Natural language understanding for TalkToDoc.
Interprets basic health-related queries to identify symptoms, intent, or
information requests, per functional requirement 5 in the research
document.

This is a communication aid, not a diagnostic tool. The document is
explicit that the system does not provide formal medical diagnosis, so
this module only summarizes what the patient is communicating, it never
suggests a diagnosis or treatment.

Uses the OpenAI API, same as translation.py.

MOCK_MODE: if set to "true" in the env file, this uses simple keyword
matching instead of a real API call, so the rest of the app can be tested
for free, with no API key. Not a substitute for testing real NLU quality.
"""

import os
from dotenv import load_dotenv

load_dotenv("env")

MOCK_MODE = os.environ.get("MOCK_MODE", "").lower() == "true"

if not MOCK_MODE:
    from openai import OpenAI
    _client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

MODEL = "gpt-5.6-terra"

_MOCK_KEYWORDS = ["headache", "fever", "stomach", "cough", "dizzy", "pain", "vomit", "rash"]


def interpret_query(english_text):
    """
    english_text: the patient's message, already translated to English
    Returns a short plain-language summary of the likely symptoms, intent,
    or information request, to help the provider quickly understand what
    the patient needs. Not a diagnosis.
    """
    if MOCK_MODE:
        text_lower = english_text.lower()
        found = [word for word in _MOCK_KEYWORDS if word in text_lower]
        symptoms = ", ".join(found) if found else "an unspecified concern"
        return f"Patient reports {symptoms}. Requesting guidance. (Mock summary, no AI used.)"

    prompt = (
        "A patient sent the following message to a healthcare provider. "
        "In two sentences or less, summarize the likely symptoms, intent, "
        "or information request. Do not diagnose or suggest treatment, "
        "only summarize what the patient is communicating.\n\n"
        f"Message: {english_text}"
    )

    response = _client.chat.completions.create(
        model=MODEL,
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content.strip()


if __name__ == "__main__":
    import sys

    if len(sys.argv) < 2:
        print('Usage: python nlu.py "english text"')
    else:
        print(interpret_query(sys.argv[1]))