Spaces:
Running
Running
feat: add groq fallback to gemini agent calls
Browse files- src/core/agent.py +49 -9
src/core/agent.py
CHANGED
|
@@ -1,5 +1,13 @@
|
|
| 1 |
from google import genai
|
| 2 |
from google.genai import types
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from .prompts import STUDY_AGENT_PROMPT
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
|
@@ -37,13 +45,24 @@ def generate_study_notes(text, tone="Academic", focus="General Summary", length=
|
|
| 37 |
if use_web_search:
|
| 38 |
config.tools = [{"google_search": {}}]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def initialize_chat(pdf_text, chat_history=None, use_web_search=False):
|
| 49 |
from google.genai import types
|
|
@@ -82,5 +101,26 @@ def initialize_chat(pdf_text, chat_history=None, use_web_search=False):
|
|
| 82 |
return chat
|
| 83 |
|
| 84 |
def send_chat_message(chat_session, user_message):
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from google import genai
|
| 2 |
from google.genai import types
|
| 3 |
+
from google.api_core.exceptions import (
|
| 4 |
+
ServiceUnavailable,
|
| 5 |
+
ResourceExhausted,
|
| 6 |
+
DeadlineExceeded,
|
| 7 |
+
InternalServerError,
|
| 8 |
+
)
|
| 9 |
+
import streamlit as st
|
| 10 |
+
from .llm_client import groq_client
|
| 11 |
from .prompts import STUDY_AGENT_PROMPT
|
| 12 |
import os
|
| 13 |
from dotenv import load_dotenv
|
|
|
|
| 45 |
if use_web_search:
|
| 46 |
config.tools = [{"google_search": {}}]
|
| 47 |
|
| 48 |
+
try:
|
| 49 |
+
response = client.models.generate_content(
|
| 50 |
+
model="gemini-2.5-flash",
|
| 51 |
+
contents=prompt,
|
| 52 |
+
config=config
|
| 53 |
+
)
|
| 54 |
+
return response.text
|
| 55 |
+
except (ServiceUnavailable, ResourceExhausted, DeadlineExceeded, InternalServerError, Exception) as e:
|
| 56 |
+
st.warning("⚠️ Gemini unavailable, switching to Groq fallback...")
|
| 57 |
+
try:
|
| 58 |
+
fallback_response = groq_client.chat.completions.create(
|
| 59 |
+
model="llama-3.1-8b-instant",
|
| 60 |
+
messages=[{"role": "user", "content": prompt}]
|
| 61 |
+
)
|
| 62 |
+
return fallback_response.choices[0].message.content
|
| 63 |
+
except Exception:
|
| 64 |
+
st.error("Both Gemini and Groq are unavailable. Please try again later.")
|
| 65 |
+
return None
|
| 66 |
|
| 67 |
def initialize_chat(pdf_text, chat_history=None, use_web_search=False):
|
| 68 |
from google.genai import types
|
|
|
|
| 101 |
return chat
|
| 102 |
|
| 103 |
def send_chat_message(chat_session, user_message):
|
| 104 |
+
try:
|
| 105 |
+
response = chat_session.send_message(user_message)
|
| 106 |
+
return response.text
|
| 107 |
+
except (ServiceUnavailable, ResourceExhausted, DeadlineExceeded, InternalServerError, Exception) as e:
|
| 108 |
+
st.warning("⚠️ Gemini unavailable, switching to Groq fallback...")
|
| 109 |
+
try:
|
| 110 |
+
messages = []
|
| 111 |
+
if hasattr(chat_session, 'get_history'):
|
| 112 |
+
for msg in chat_session.get_history():
|
| 113 |
+
role = "user" if msg.role == "user" else "assistant"
|
| 114 |
+
text = msg.parts[0].text if (msg.parts and len(msg.parts) > 0) else ""
|
| 115 |
+
messages.append({"role": role, "content": text})
|
| 116 |
+
|
| 117 |
+
messages.append({"role": "user", "content": user_message})
|
| 118 |
+
|
| 119 |
+
fallback_response = groq_client.chat.completions.create(
|
| 120 |
+
model="llama-3.1-8b-instant",
|
| 121 |
+
messages=messages
|
| 122 |
+
)
|
| 123 |
+
return fallback_response.choices[0].message.content
|
| 124 |
+
except Exception:
|
| 125 |
+
st.error("Both Gemini and Groq are unavailable. Please try again later.")
|
| 126 |
+
return None
|