-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_fallback.py
More file actions
82 lines (71 loc) · 2.84 KB
/
Copy pathai_fallback.py
File metadata and controls
82 lines (71 loc) · 2.84 KB
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
"""
ai_fallback.py
--------------
This file talks to Google's Gemini AI when the FAQ database doesn't
have a good match for the student's question.
How it works:
- We load the API key safely from the .env file (never hardcoded)
- We give Gemini a short "system instruction" so it knows it's a
university helpdesk assistant and should stay on-topic
- We send the student's question and return Gemini's answer
- If anything goes wrong (no internet, bad key, timeout), we catch
the error and return a safe fallback message instead of crashing
"""
import os
from dotenv import load_dotenv
from google import genai
from google.genai import types
# Load the .env file so we can read GEMINI_API_KEY from it
load_dotenv()
API_KEY = os.getenv("GEMINI_API_KEY")
# This instruction shapes how Gemini behaves for every question
SYSTEM_INSTRUCTION = (
"You are a helpful university helpdesk assistant for IUB "
"(The Islamia University of Bahawalpur). Answer student questions "
"clearly and briefly, in 2-4 sentences. If a question is not "
"related to university life, politely say you can only help with "
"university-related questions."
)
# ``google.generativeai`` and gemini-2.5-flash-lite are retired for new users.
# The current Google Gen AI SDK uses a client object and this supported model.
client = genai.Client(api_key=API_KEY) if API_KEY else None
def ask_ai(user_question):
"""
Sends the student's question to Gemini and returns the answer.
Returns a tuple: (answer_text, success_flag)
success_flag is True if it worked, False if there was an error.
"""
if not API_KEY:
print("Gemini API error: GEMINI_API_KEY is not configured.")
return (
"Sorry, the AI help service is not configured right now. "
"Please contact the help desk directly for assistance.",
False,
)
try:
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=user_question,
config=types.GenerateContentConfig(
system_instruction=SYSTEM_INSTRUCTION,
),
)
answer = (response.text or "").strip()
if not answer:
raise RuntimeError("Gemini returned an empty response.")
return answer, True
except Exception as error:
# Something went wrong (bad key, no internet, quota, etc.)
print(f"Gemini API error: {error}")
safe_message = (
"Sorry, I couldn't process that right now. "
"Please contact the help desk directly for assistance."
)
return safe_message, False
if __name__ == "__main__":
# Quick manual test
test_question = "Can I bring my own laptop to campus?"
answer, success = ask_ai(test_question)
print(f"Question: {test_question}")
print(f"Success: {success}")
print(f"Answer: {answer}")