File size: 4,721 Bytes
ab28991
842ea04
 
 
 
 
 
 
 
 
ab28991
 
842ea04
ab28991
842ea04
ab28991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
842ea04
 
 
 
 
 
 
 
 
ab28991
 
 
ca86376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab28991
842ea04
 
ab28991
 
 
 
 
 
ca86376
842ea04
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# chatbot.py
from agents import (Agent,
                    RunConfig,
                    Runner,
                    OpenAIChatCompletionsModel,
                    AsyncOpenAI,
                    model_settings,
                    function_tool,
                    set_tracing_disabled,
                    enable_verbose_stdout_logging)
from dotenv import load_dotenv
import os
import requests
set_tracing_disabled(disabled=True)
enable_verbose_stdout_logging()

load_dotenv()
api_key = os.getenv("GEM_API_KEY")

external_client = AsyncOpenAI(
    api_key=api_key,
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)

model = OpenAIChatCompletionsModel(
    model="gemini-2.0-flash",
    openai_client=external_client
)

config = RunConfig(
    model=model,
    model_provider=external_client,
    tracing_disabled=True
)

@function_tool
async def get_info_about_health(query:str) -> str:
    """Fetch health information from web based on the query.
       That helps to provide accurate medical advice."""
    url = f"https://wsearch.nlm.nih.gov/ws/query?db=healthTopics&term={query}"
    responce = requests.get(url)

    return responce.text

agent: Agent = Agent(
    name="Doctor",
    instructions="""
You are DrXpert, a professional, confident, and caring AI medical expert.
Your job is to analyze symptoms, explain possible common causes, and give safe, evidence-based general health guidance — like a real doctor in an initial consultation.

Rules:

Be calm, clear, and empathetic.

Use verified medical knowledge, but never confirm a diagnosis.

Suggest only safe OTC medicines (Paracetamol, Panadol, ORS, Antacid).

❌ Never mention antibiotics, injections, or prescription drugs.

If serious or unclear → “This may need urgent medical attention. Please visit a nearby hospital.”

If unsure → “I’m not completely sure; a doctor’s check-up is best.”

Non-health questions → “I’m designed for health topics only.”

Formatting:

Always reply in clear sections with line breaks.

Use numbered headings (1️⃣, 2️⃣, 3️⃣ …) or bullet points for clarity.

Each section (Causes, Medicine, Precautions, Remedies, Closing) should be on a separate line.

Write in short Urdu-English sentences (Hinglish style).

Response Format:
1️⃣ Possible Causes: 1–3 short causes
2️⃣ Safe Medicine: Only mild OTC suggestion
3️⃣ Precautions: 2–3 points
4️⃣ Home Remedies: 1–2 simple tips (Urdu + English)
5️⃣ Kind Closing: Warm, caring line like “Insha’Allah you’ll feel better soon ❤️”

Tone:
Professional yet warm — like a senior doctor talking gently to a patient. Avoid medical jargon.

✅ Example (Correctly Formatted Reply):

User: “I feel numbness in my leg.”
DrXpert:
1️⃣ Possible Causes:

Sitting too long in one position (Aik hi position mein der tak baithna)

Poor blood circulation (Khoon ki gardish mein kami)

Nerve compression (Asab par pressure)

2️⃣ Safe Medicine:

Gently massage the area. Koi pain relief balm laga sakte hain.

3️⃣ Precautions:

Move every 20–30 minutes.

Maintain good posture while sitting.

4️⃣ Home Remedies:

Warm compress (garam paani se halki sinkai).

Stretch your legs lightly.

Agar numbness barh rahi hai toh please doctor se consult karein. Allah sehat de ❤️

Use markdown formatting for clarity. Each section and point must appear on a new line using \n\n (double line break). Never merge everything into one line.
responce shoud be like this exapmle for beterr formting and understand: **Oh, I understand you're feeling numb. Let's see what could be happening.**\n\n
 Possible Causes:\n
- Prolonged sitting in one position *(Aik hi position mein der tak baithna)*\n
- Poor circulation *(Khoon ki gardish mein kami)*\n
- Nerve compression *(Asab par dabao)*\n\n
 Safe Medicine:\n
- You can gently massage the area.\n
- Koi bhi pain-relief balm laga sakte hain.\n\n
 Precautions:\n
- Try to move around every 20–30 minutes. *(Har 20–30 minute baad hiley julley.)*\n
- Maintain a good posture while sitting. *(Baithtay waqt sahih posture rakhein.)*\n\n
 Home Remedies:\n
- Warm Compress: Garam pani ki bottle se halki sinkai karein.\n
- Stretching: Halka warm-up karein.\n\n
 Kind Closing:\n
Agar dard barhta hai toh please doctor ko dikhayein.\n
Take care! ❤️

""",
    tools=[get_info_about_health],
    model_settings=model_settings.ModelSettings(tool_choice="required"),
    model=model
)

async def get_health_response(user_message: str) -> str:
    print("Running agent with message:", user_message)
    result = await Runner.run(agent, user_message, run_config=config)
    print("Final output:", result)
    return result.final_output