Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -10,14 +10,32 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- class BasicAgent:
 
 
14
  def __init__(self):
15
- print("BasicAgent initialized.")
 
16
  def __call__(self, question: str) -> str:
17
- print(f"Agent received question (first 50 chars): {question[:50]}...")
18
- fixed_answer = "This is a default answer."
19
- print(f"Agent returning fixed answer: {fixed_answer}")
20
- return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
@@ -40,7 +58,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
- agent = BasicAgent()
44
  except Exception as e:
45
  print(f"Error instantiating agent: {e}")
46
  return f"Error initializing agent: {e}", None
 
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
+ import openai
14
+
15
+ class SmartAgent:
16
  def __init__(self):
17
+ openai.api_key = os.getenv("OPENAI_API_KEY")
18
+
19
  def __call__(self, question: str) -> str:
20
+ try:
21
+ response = openai.ChatCompletion.create(
22
+ model="gpt-3.5-turbo",
23
+ messages=[
24
+ {"role": "system", "content": "You are a helpful assistant that gives exact, factual answers only."},
25
+ {"role": "user", "content": question}
26
+ ],
27
+ max_tokens=200,
28
+ temperature=0.0,
29
+ )
30
+ answer = response["choices"][0]["message"]["content"].strip()
31
+ return self.clean_answer(answer)
32
+ except Exception as e:
33
+ print(f"OpenAI Error: {e}")
34
+ return "ERROR"
35
+
36
+ def clean_answer(self, answer: str) -> str:
37
+ # Remove possible prefixes like "Answer:" or "FINAL ANSWER:"
38
+ return answer.replace("FINAL ANSWER:", "").replace("Answer:", "").strip()
39
 
40
  def run_and_submit_all( profile: gr.OAuthProfile | None):
41
  """
 
58
 
59
  # 1. Instantiate Agent ( modify this part to create your agent)
60
  try:
61
+ agent = SmartAgent()
62
  except Exception as e:
63
  print(f"Error instantiating agent: {e}")
64
  return f"Error initializing agent: {e}", None