Commit ·
7d410a6
1
Parent(s): f67d6ce
Updated model
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
-
def __init__(self, model="gpt-4
|
| 18 |
self.model = model
|
| 19 |
self.temperature = temperature
|
| 20 |
print("OpenAIAgent initialized.")
|
|
@@ -26,17 +26,22 @@ class BasicAgent:
|
|
| 26 |
model=self.model,
|
| 27 |
temperature=self.temperature,
|
| 28 |
messages=[
|
| 29 |
-
{"role": "system", "content": "You are a helpful AI assistant
|
| 30 |
{"role": "user", "content": question}
|
| 31 |
]
|
| 32 |
)
|
| 33 |
answer = response['choices'][0]['message']['content'].strip()
|
| 34 |
-
print(f"Answer: {answer}")
|
| 35 |
return answer
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Error calling OpenAI API: {e}")
|
| 38 |
return f"ERROR: {e}"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def generate_answers(agent, tasks):
|
| 41 |
answers = []
|
| 42 |
|
|
@@ -44,22 +49,21 @@ def generate_answers(agent, tasks):
|
|
| 44 |
task_id = task.get("task_id", "")
|
| 45 |
question = task.get("question", "").lower()
|
| 46 |
|
| 47 |
-
# Skip
|
| 48 |
if any(x in question for x in ["youtube.com", "audio", "video", ".mp3", ".mp4"]):
|
| 49 |
-
print(f"Skipping task {task_id}: Unsupported media
|
| 50 |
continue
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Handle fallback or API error
|
| 56 |
-
if response.startswith("ERROR"):
|
| 57 |
-
print(f"Skipping task {task_id} due to error.")
|
| 58 |
continue
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
answers.append({
|
| 61 |
"task_id": task_id,
|
| 62 |
-
"submitted_answer":
|
| 63 |
})
|
| 64 |
|
| 65 |
return answers
|
|
|
|
| 14 |
# --- Basic Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 16 |
class BasicAgent:
|
| 17 |
+
def __init__(self, model="gpt-4", temperature=0.2):
|
| 18 |
self.model = model
|
| 19 |
self.temperature = temperature
|
| 20 |
print("OpenAIAgent initialized.")
|
|
|
|
| 26 |
model=self.model,
|
| 27 |
temperature=self.temperature,
|
| 28 |
messages=[
|
| 29 |
+
{"role": "system", "content": "You are a helpful AI assistant. Answer with only the correct option letter (A, B, C, or D)."},
|
| 30 |
{"role": "user", "content": question}
|
| 31 |
]
|
| 32 |
)
|
| 33 |
answer = response['choices'][0]['message']['content'].strip()
|
| 34 |
+
print(f"Raw Answer: {answer}")
|
| 35 |
return answer
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Error calling OpenAI API: {e}")
|
| 38 |
return f"ERROR: {e}"
|
| 39 |
|
| 40 |
+
def extract_option_letter(text):
|
| 41 |
+
"""Extract just A/B/C/D from the answer string."""
|
| 42 |
+
match = re.search(r'\b([A-D])\b', text.upper())
|
| 43 |
+
return match.group(1) if match else "INVALID"
|
| 44 |
+
|
| 45 |
def generate_answers(agent, tasks):
|
| 46 |
answers = []
|
| 47 |
|
|
|
|
| 49 |
task_id = task.get("task_id", "")
|
| 50 |
question = task.get("question", "").lower()
|
| 51 |
|
| 52 |
+
# Skip media questions
|
| 53 |
if any(x in question for x in ["youtube.com", "audio", "video", ".mp3", ".mp4"]):
|
| 54 |
+
print(f"Skipping task {task_id}: Unsupported media.")
|
| 55 |
continue
|
| 56 |
|
| 57 |
+
raw_response = agent(task["question"])
|
| 58 |
+
if raw_response.startswith("ERROR"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
continue
|
| 60 |
|
| 61 |
+
# Extract only the answer letter (A, B, C, D)
|
| 62 |
+
final_answer = extract_option_letter(raw_response)
|
| 63 |
+
|
| 64 |
answers.append({
|
| 65 |
"task_id": task_id,
|
| 66 |
+
"submitted_answer": final_answer
|
| 67 |
})
|
| 68 |
|
| 69 |
return answers
|