Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,16 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
6 |
from utils.vector_search import get_similar_symptoms
|
7 |
from utils.sql_queries import get_diseases_by_symptoms, get_related_symptoms
|
8 |
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
@@ -66,3 +76,46 @@ def search(symptom: str, selected: list[str] = Query([])):
|
|
66 |
"related_symptoms": suggestions,
|
67 |
"semantic_matches": similar
|
68 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from utils.vector_search import get_similar_symptoms
|
7 |
from utils.sql_queries import get_diseases_by_symptoms, get_related_symptoms
|
8 |
import sqlite3
|
9 |
+
import os
|
10 |
+
import httpx
|
11 |
+
import json
|
12 |
+
from dotenv import load_dotenv
|
13 |
+
|
14 |
+
# Load environment variables
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# Get Groq API key from environment variables
|
18 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
19 |
|
20 |
app = FastAPI()
|
21 |
|
|
|
76 |
"related_symptoms": suggestions,
|
77 |
"semantic_matches": similar
|
78 |
}
|
79 |
+
|
80 |
+
@app.get("/disease-info")
|
81 |
+
async def disease_info(name: str):
|
82 |
+
"""
|
83 |
+
Get detailed information about a disease using Groq's LLM API
|
84 |
+
"""
|
85 |
+
if not GROQ_API_KEY:
|
86 |
+
return {"info": "<p>Error: Groq API key not found in environment variables.</p>"}
|
87 |
+
|
88 |
+
try:
|
89 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
90 |
+
response = await client.post(
|
91 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
92 |
+
headers={
|
93 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
94 |
+
"Content-Type": "application/json"
|
95 |
+
},
|
96 |
+
json={
|
97 |
+
"model": "llama3-70b-8192",
|
98 |
+
"messages": [
|
99 |
+
{
|
100 |
+
"role": "system",
|
101 |
+
"content": "You are a helpful medical assistant. Provide accurate, well-structured information about diseases. Format your response in HTML paragraphs with appropriate headings using <h4> tags. Include symptoms, causes, treatments, and prevention when possible. Keep responses concise but informative."
|
102 |
+
},
|
103 |
+
{
|
104 |
+
"role": "user",
|
105 |
+
"content": f"Provide detailed information about {name}, including symptoms, causes, diagnosis, treatments, and prevention methods when applicable."
|
106 |
+
}
|
107 |
+
],
|
108 |
+
"max_tokens": 1500,
|
109 |
+
"temperature": 0.5
|
110 |
+
}
|
111 |
+
)
|
112 |
+
|
113 |
+
if response.status_code == 200:
|
114 |
+
data = response.json()
|
115 |
+
content = data["choices"][0]["message"]["content"]
|
116 |
+
return {"info": content}
|
117 |
+
else:
|
118 |
+
return {"info": f"<p>Error: Failed to fetch information. Status code: {response.status_code}</p>"}
|
119 |
+
|
120 |
+
except Exception as e:
|
121 |
+
return {"info": f"<p>Error: {str(e)}</p>"}
|