Upload agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import logging
|
| 4 |
+
from google import genai
|
| 5 |
+
from google.genai.types import GenerateContentConfig
|
| 6 |
+
from ratelimit import limits, sleep_and_retry
|
| 7 |
+
|
| 8 |
+
RPM = 15
|
| 9 |
+
TPM = 1_000_000
|
| 10 |
+
PER_MINUTE = 60
|
| 11 |
+
SYSTEM_PROMPT_GAIA = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."
|
| 12 |
+
|
| 13 |
+
logging.basicConfig(
|
| 14 |
+
level=logging.INFO,
|
| 15 |
+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
| 16 |
+
datefmt="%Y-%m-%d %H:%M:%S"
|
| 17 |
+
)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
class BasicAgent:
|
| 21 |
+
def __init__(self):
|
| 22 |
+
logger.info("BasicAgent initialized.")
|
| 23 |
+
|
| 24 |
+
def __call__(self, question: str) -> str:
|
| 25 |
+
logger.info(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 26 |
+
fixed_answer = "This is a default answer."
|
| 27 |
+
logger.info(f"Agent returning fixed answer: {fixed_answer}")
|
| 28 |
+
return fixed_answer
|
| 29 |
+
|
| 30 |
+
class SimpleGeminiAgent(BasicAgent):
|
| 31 |
+
def __init__(self, model="gemini-2.0-flash"):
|
| 32 |
+
super().__init__()
|
| 33 |
+
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 34 |
+
self.client = genai.Client(api_key=gemini_key)
|
| 35 |
+
self.model = model
|
| 36 |
+
logger.info("AdvancedAgent initialized.")
|
| 37 |
+
self.minute_start = time.time()
|
| 38 |
+
self.tokens_this_minute = 0
|
| 39 |
+
self.token_count = 0
|
| 40 |
+
|
| 41 |
+
@sleep_and_retry
|
| 42 |
+
@limits(calls=RPM, period=PER_MINUTE)
|
| 43 |
+
def __call__(self, question: str) -> str:
|
| 44 |
+
now = time.time()
|
| 45 |
+
if now - self.minute_start >= 60:
|
| 46 |
+
self.tokens_this_minute = 0
|
| 47 |
+
self.minute_start = now
|
| 48 |
+
|
| 49 |
+
# Enforce tokens per minute
|
| 50 |
+
if self.tokens_this_minute + self.token_count > TPM:
|
| 51 |
+
sleep_time = max(0, 60 - (now - self.minute_start))
|
| 52 |
+
time.sleep(sleep_time)
|
| 53 |
+
self.tokens_this_minute = 0
|
| 54 |
+
self.minute_start = time.time()
|
| 55 |
+
|
| 56 |
+
response = self.client.models.generate_content(model=self.model,
|
| 57 |
+
contents=question,
|
| 58 |
+
config=GenerateContentConfig(system_instruction=SYSTEM_PROMPT_GAIA))
|
| 59 |
+
self.tokens_this_minute += response.usage_metadata.total_token_count
|
| 60 |
+
self.token_count += response.usage_metadata.total_token_count
|
| 61 |
+
logger.info(f"AdvancedAgent received question (first 50 chars): {question[:50]}...")
|
| 62 |
+
logger.info(f"AdvancedAgent returning answer: {response.text}")
|
| 63 |
+
return response.text
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
# Example usage
|
| 67 |
+
agent = SimpleGeminiAgent()
|
| 68 |
+
question = "What is the capital of France?"
|
| 69 |
+
answer = agent(question)
|
| 70 |
+
print(f"Question: {question}\nAnswer: {answer}")
|