Spaces:
No application file
No application file
File size: 1,891 Bytes
f2393fe |
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 |
import google.generativeai as genai
import logging
from content import get_response_format
from prompts import get_prompt
import os
# Configure logging
logging.basicConfig(level=logging.INFO)
# Set up Gemini API Key (Replace with your API Key)
# GEMINI_API_KEY = "your-api-key"
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
def query_gemini(prompt, query_type="default"):
"""
Queries Gemini AI and returns a formatted response.
"""
try:
model = genai.GenerativeModel(model_name="gemini-2.0-flash-lite")
# Add structured prompt formatting
structured_prompt = get_prompt(prompt.lower())
full_prompt = f"{structured_prompt}\n\nFormat your response clearly.\n\nUser Query: {prompt}"
# Generate response
response = model.generate_content(full_prompt)
response_text = response.text.strip() if response.text else "I couldn't generate a response."
return format_response(response_text, query_type)
except Exception as e:
logging.error(f"Gemini API error: {e}")
return "Sorry, an error occurred while generating a response."
def format_response(response_text, query_type="default"):
"""
Formats AI responses based on the query type using content.py rules.
"""
response_template = get_response_format(query_type)
if not isinstance(response_template, str):
logging.error("Invalid response format: response_templates should be a dictionary string.")
return response_text # Return unformatted response as fallback
formatted_response = response_template.replace("{response}", response_text)
# Preserve new lines for bullet points
if query_type in ["bullet_points", "points_with_paragraph"]:
formatted_response = formatted_response.replace("\\n", "\n")
return formatted_response |