Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
|
| 4 |
+
class GeminiAgent:
|
| 5 |
+
"""
|
| 6 |
+
An agent that uses the Gemini-1.5-Pro model to answer questions.
|
| 7 |
+
"""
|
| 8 |
+
def __init__(self):
|
| 9 |
+
"""
|
| 10 |
+
Initializes the agent, configures the Gemini API key, and sets up the model.
|
| 11 |
+
Raises a ValueError if the GEMINI_API_KEY is not found in the environment secrets.
|
| 12 |
+
"""
|
| 13 |
+
print("Initializing GeminiAgent...")
|
| 14 |
+
|
| 15 |
+
# 1. Get API Key from Hugging Face Secrets
|
| 16 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 17 |
+
if not api_key:
|
| 18 |
+
raise ValueError("GEMINI_API_KEY secret not found! Please set it in your Space's settings.")
|
| 19 |
+
|
| 20 |
+
# 2. Configure the Generative AI client
|
| 21 |
+
genai.configure(api_key=api_key)
|
| 22 |
+
|
| 23 |
+
# 3. Initialize the Gemini 1.5 Pro model
|
| 24 |
+
self.model = genai.GenerativeModel('gemini-1.5-pro-latest')
|
| 25 |
+
print("GeminiAgent initialized successfully.")
|
| 26 |
+
|
| 27 |
+
def __call__(self, question: str) -> str:
|
| 28 |
+
"""
|
| 29 |
+
Processes a question by sending it to the Gemini model and returns the stripped text answer.
|
| 30 |
+
|
| 31 |
+
The prompt is engineered to request a direct, exact-match answer as required by the competition.
|
| 32 |
+
"""
|
| 33 |
+
print(f"Agent received question (first 80 chars): {question[:80]}...")
|
| 34 |
+
|
| 35 |
+
# Prompt engineered for the "EXACT MATCH" requirement.
|
| 36 |
+
# It instructs the model to provide only the answer and nothing else.
|
| 37 |
+
prompt = f"""You are an expert problem-solving agent. Your goal is to answer the following question as accurately as possible.
|
| 38 |
+
The evaluation system requires an EXACT MATCH. Therefore, you must provide only the final answer and nothing else.
|
| 39 |
+
Do not include any introductory text, explanations, or the phrase "FINAL ANSWER".
|
| 40 |
+
For example, if the question asks for a specific year, your response should be just "2023". If it's a name, just "John Doe". If it is a number, just "42".
|
| 41 |
+
|
| 42 |
+
Question: {question}
|
| 43 |
+
|
| 44 |
+
Final Answer:"""
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
# 4. Call the model
|
| 48 |
+
response = self.model.generate_content(prompt)
|
| 49 |
+
|
| 50 |
+
# 5. Extract and clean the answer
|
| 51 |
+
final_answer = response.text.strip()
|
| 52 |
+
print(f"Agent returning answer: {final_answer}")
|
| 53 |
+
return final_answer
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"An error occurred while calling the Gemini API: {e}")
|
| 57 |
+
return f"Error processing question: {e}"
|