-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_core.py
More file actions
50 lines (41 loc) · 1.85 KB
/
ai_core.py
File metadata and controls
50 lines (41 loc) · 1.85 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
from google import genai
import os
API_KEY = os.environ.get("GEMINI_API_KEY")
if not API_KEY:
# If the key isn't set, print a reminder and use a placeholder
print("FATAL ERROR: GEMINI_API_KEY environment variable is NOT set.")
try:
client = genai.Client(api_key=API_KEY)
GEMINI_MODEL = 'gemini-2.5-flash'
except Exception as e:
print(f"Error initializing Gemini Client: {e}")
print("ACTION REQUIRED: Ensure GEMINI_API_KEY environment variable is set.")
client = None
def get_ai_response(prompt: str) -> str:
if not client:
return "I'm sorry, my core intelligence module is offline. Please check my API key."
try:
if not API_KEY:
return "Error: API Key is missing. Check terminal for instructions"
system_instruction = (
"You are a helpful and professional assistant. "
"For all detailed or multi-step answers, use markdown formatting. "
"Break down long explanations into numbered lists, bullet points, and use **bold** text to highlight key concepts. "
"Avoid writing large paragraphs."
)
combined_prompt = system_instruction + prompt
response = client.models.generate_content(
model=GEMINI_MODEL,
contents=combined_prompt,
config={"tools": [{"google_search": {}}]}
)
return response.text
except Exception as e:
print(f"AI Core Error: {e}")
return "I encountered a processing error while fetching the information."
if __name__ == '__main__':
# This block allows you to test the AI Core independently
test_prompt = "What are the three most important things I need for a basic Python voice assistant?"
print(f"Testing AI with: '{test_prompt}'")
ai_answer = get_ai_response(test_prompt)
print(f"\nAI Response:\n{ai_answer}")