MrArray22 commited on
Commit
75cbcb0
·
verified ·
1 Parent(s): be83acf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -39
app.py CHANGED
@@ -3,6 +3,9 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
 
 
 
6
  from dotenv import load_dotenv
7
  from openai import OpenAI
8
  from tenacity import retry, stop_after_attempt, wait_exponential
@@ -13,7 +16,9 @@ load_dotenv()
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
- OPENAI_MODEL = "openai/gpt-4.1-nano" # or "gpt-3.5-turbo" based on your preference
 
 
17
 
18
 
19
  # --- Basic Agent Definition ---
@@ -22,32 +27,39 @@ class BasicAgent:
22
  def __init__(self):
23
  """Initialize the agent with OpenAI client and setup."""
24
  print("BasicAgent initializing...")
25
- self.client = OpenAI(api_key="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",base_url="https://models.github.ai/inference")
 
26
  print("BasicAgent initialized successfully.")
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @retry(
29
  stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10)
30
  )
31
- def _get_completion(self, prompt: str) -> str:
32
  """Get completion from OpenAI with retry logic."""
33
  try:
34
  response = self.client.chat.completions.create(
35
  model=OPENAI_MODEL,
36
- messages=[
37
- {
38
- "role": "system",
39
- "content": """You are a helpful AI assistant designed to answer questions from the GAIA benchmark.
40
- Follow these guidelines:
41
- 1. Provide clear, concise, and accurate answers
42
- 2. If a question requires specific steps or calculations, show them clearly
43
- 3. Format your response in a clean, readable way
44
- 4. Be precise and avoid ambiguity
45
- 5. If you're not completely sure about an answer, state your confidence level
46
- Remember: Your answers will be evaluated through exact matching.""",
47
- },
48
- {"role": "user", "content": prompt},
49
- ],
50
- temperature=0.2, # Lower temperature for more consistent outputs
51
  max_tokens=1000,
52
  )
53
  return response.choices[0].message.content.strip()
@@ -55,37 +67,70 @@ class BasicAgent:
55
  print(f"Error in OpenAI API call: {e}")
56
  raise
57
 
58
- def _preprocess_question(self, question: str) -> str:
59
- """Preprocess the question to enhance clarity and context."""
60
- enhanced_prompt = f"""Please analyze and answer the following question from the GAIA benchmark.
61
- Question: {question}
 
 
 
 
62
 
63
- Provide a clear, specific answer that can be evaluated through exact matching.
64
- If the question requires multiple steps, please show your reasoning but ensure the final answer is clearly stated.
65
- """
66
- return enhanced_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  def __call__(self, question: str) -> str:
69
  """Process the question and return an answer."""
70
  print(f"Agent received question (first 50 chars): {question[:50]}...")
71
 
72
  try:
73
- # Preprocess the question
74
- enhanced_prompt = self._preprocess_question(question)
75
-
76
- # Get completion from OpenAI
77
- response = self._get_completion(enhanced_prompt)
78
 
79
- # Extract the final answer
80
- # If the response contains multiple lines or explanations,
81
- # we'll try to extract just the final answer
82
- answer_lines = response.strip().split("\n")
83
- final_answer = answer_lines[-1].strip()
84
 
85
- # Log the response for debugging
86
- print(f"Agent generated answer: {final_answer[:100]}...")
87
 
88
- return final_answer
89
 
90
  except Exception as e:
91
  print(f"Error processing question: {e}")
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ import json
7
+ import re
8
+ from typing import Dict, Any
9
  from dotenv import load_dotenv
10
  from openai import OpenAI
11
  from tenacity import retry, stop_after_attempt, wait_exponential
 
16
  # (Keep Constants as is)
17
  # --- Constants ---
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
+ OPENAI_MODEL = (
20
+ "gpt-4-turbo-preview" # Using OpenAI's latest model for better performance
21
+ )
22
 
23
 
24
  # --- Basic Agent Definition ---
 
27
  def __init__(self):
28
  """Initialize the agent with OpenAI client and setup."""
29
  print("BasicAgent initializing...")
30
+ self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
31
+ self.question_history: Dict[str, Any] = {} # Store question context
32
  print("BasicAgent initialized successfully.")
33
 
34
+ def _format_answer(self, raw_answer: str) -> str:
35
+ """Format the answer to improve exact matching success."""
36
+ # Remove any explanations or reasoning
37
+ if "Answer:" in raw_answer:
38
+ answer = raw_answer.split("Answer:")[-1].strip()
39
+ elif "Final answer:" in raw_answer:
40
+ answer = raw_answer.split("Final answer:")[-1].strip()
41
+ else:
42
+ answer = raw_answer.strip()
43
+
44
+ # Clean up formatting
45
+ answer = re.sub(
46
+ r"\s+", " ", answer
47
+ ) # Replace multiple spaces with single space
48
+ answer = answer.strip("\"'") # Remove quotes
49
+ answer = answer.strip(".") # Remove trailing periods
50
+
51
+ return answer.strip()
52
+
53
  @retry(
54
  stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10)
55
  )
56
+ def _get_completion(self, messages: list) -> str:
57
  """Get completion from OpenAI with retry logic."""
58
  try:
59
  response = self.client.chat.completions.create(
60
  model=OPENAI_MODEL,
61
+ messages=messages,
62
+ temperature=0.1, # Lower temperature for more consistent outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  max_tokens=1000,
64
  )
65
  return response.choices[0].message.content.strip()
 
67
  print(f"Error in OpenAI API call: {e}")
68
  raise
69
 
70
+ def _analyze_question(self, question: str) -> dict:
71
+ """Analyze the question to determine its type and required approach."""
72
+ system_msg = """You are an expert at analyzing questions. For the given question:
73
+ 1. Identify the question type (e.g., factual, calculation, reasoning)
74
+ 2. Identify key entities and concepts
75
+ 3. Determine if external knowledge is needed
76
+ 4. Suggest the best approach to answer it
77
+ Provide your analysis in JSON format."""
78
 
79
+ messages = [
80
+ {"role": "system", "content": system_msg},
81
+ {"role": "user", "content": f"Analyze this question: {question}"},
82
+ ]
83
+
84
+ try:
85
+ analysis = self._get_completion(messages)
86
+ return json.loads(analysis)
87
+ except:
88
+ return {"type": "unknown", "approach": "direct"}
89
+
90
+ def _get_answer(self, question: str, analysis: dict) -> str:
91
+ """Get the answer based on question analysis."""
92
+ system_prompt = f"""You are an AI assistant specialized in answering GAIA benchmark questions.
93
+ Your task is to provide EXACT, PRECISE answers that can be matched against a ground truth.
94
+
95
+ Guidelines:
96
+ 1. Provide ONLY the final answer, no explanations
97
+ 2. Be extremely precise and consistent in formatting
98
+ 3. For numerical answers, use digits (e.g., "42" not "forty-two")
99
+ 4. For lists, use comma-separated values without spaces after commas
100
+ 5. For yes/no questions, answer only with "Yes" or "No"
101
+ 6. Remove any punctuation from the end of your answer
102
+ 7. Keep your answer as concise as possible while being complete
103
+
104
+ Question type: {analysis.get('type', 'unknown')}
105
+ Approach: {analysis.get('approach', 'direct')}
106
+
107
+ Remember: Your answer will be compared EXACTLY with the ground truth. Format matters!"""
108
+
109
+ messages = [
110
+ {"role": "system", "content": system_prompt},
111
+ {"role": "user", "content": question},
112
+ ]
113
+
114
+ raw_answer = self._get_completion(messages)
115
+ return self._format_answer(raw_answer)
116
 
117
  def __call__(self, question: str) -> str:
118
  """Process the question and return an answer."""
119
  print(f"Agent received question (first 50 chars): {question[:50]}...")
120
 
121
  try:
122
+ # Analyze the question
123
+ analysis = self._analyze_question(question)
124
+ print(f"Question analysis: {json.dumps(analysis, indent=2)}")
 
 
125
 
126
+ # Get and format the answer
127
+ answer = self._get_answer(question, analysis)
128
+ print(f"Generated answer: {answer}")
 
 
129
 
130
+ # Store question context
131
+ self.question_history[question] = {"analysis": analysis, "answer": answer}
132
 
133
+ return answer
134
 
135
  except Exception as e:
136
  print(f"Error processing question: {e}")