Portfolio User commited on
Commit
b84779b
·
1 Parent(s): 86bb3cc

feat: switch AI backend to Groq for ultra-fast medical responses

Browse files
Files changed (2) hide show
  1. agent/core.py +17 -17
  2. requirements.txt +1 -1
agent/core.py CHANGED
@@ -1,4 +1,4 @@
1
- from google import genai
2
  import os
3
  from dotenv import load_dotenv
4
 
@@ -6,14 +6,14 @@ load_dotenv()
6
 
7
  class MedicalAgent:
8
  def __init__(self, api_key=None):
9
- self.api_key = api_key or os.getenv("GEMINI_API_KEY")
10
  if not self.api_key:
11
- raise ValueError("GEMINI_API_KEY not found. Please set it in your environment or .env file.")
12
 
13
- self.client = genai.Client(api_key=self.api_key)
14
 
15
- # Using Gemini 1.5 Flash (Correct ID for google-genai library)
16
- self.model_id = 'gemini-1.5-flash'
17
  self.system_instruction = (
18
  "You are an advanced Medical AI Assistant. Your goal is to provide accurate, "
19
  "helpful, and empathetic medical information based on available datasets. "
@@ -24,18 +24,18 @@ class MedicalAgent:
24
  )
25
 
26
  def get_response(self, user_input, context=""):
27
- prompt = f"User Question: {user_input}\n\nRelevant Medical Context/Data: {context}"
28
-
29
  try:
30
- response = self.client.models.generate_content(
31
  model=self.model_id,
32
- contents=user_input, # Use user_input directly for better context handling
33
- config={
34
- "system_instruction": self.system_instruction
35
- }
 
 
36
  )
37
- return response.text
38
  except Exception as e:
39
- if "429" in str(e) or "RESOURCE_EXHAUSTED" in str(e):
40
- return "🏥 The Medical Assistant is currently handling many users. Please wait a few moments and try your question again. (Gemini API limit reached)"
41
- return f"An unexpected error occurred: {str(e)}"
 
1
+ from groq import Groq
2
  import os
3
  from dotenv import load_dotenv
4
 
 
6
 
7
  class MedicalAgent:
8
  def __init__(self, api_key=None):
9
+ self.api_key = api_key or os.getenv("GROQ_API_KEY")
10
  if not self.api_key:
11
+ raise ValueError("GROQ_API_KEY not found. Please set it in your environment or .env file.")
12
 
13
+ self.client = Groq(api_key=self.api_key)
14
 
15
+ # Using Llama 3.3 70B on Groq for ultra-fast, high-quality medical responses
16
+ self.model_id = "llama-3.3-70b-specdec"
17
  self.system_instruction = (
18
  "You are an advanced Medical AI Assistant. Your goal is to provide accurate, "
19
  "helpful, and empathetic medical information based on available datasets. "
 
24
  )
25
 
26
  def get_response(self, user_input, context=""):
 
 
27
  try:
28
+ completion = self.client.chat.completions.create(
29
  model=self.model_id,
30
+ messages=[
31
+ {"role": "system", "content": self.system_instruction},
32
+ {"role": "user", "content": f"Context: {context}\n\nQuestion: {user_input}"}
33
+ ],
34
+ temperature=0.7,
35
+ max_tokens=1024,
36
  )
37
+ return completion.choices[0].message.content
38
  except Exception as e:
39
+ if "429" in str(e) or "rate_limit" in str(e).lower():
40
+ return "🏥 The Medical Assistant is currently handling many users. Please wait a few moments. (Groq API limit reached)"
41
+ return f"An unexpected error occurred with Groq: {str(e)}"
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- google-genai
2
  datasets
3
  pandas
4
  gradio==5.15.0
 
1
+ groq
2
  datasets
3
  pandas
4
  gradio==5.15.0