Spaces:
Runtime error
Runtime error
Add system prompt to SmolAgent and update response parsing logic
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ from smolagents import CodeAgent, InferenceClientModel
|
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 12 |
|
| 13 |
# --- Smol Agent Definition ---
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
@@ -28,21 +29,32 @@ class SmolAgent:
|
|
| 28 |
self._agent = CodeAgent(
|
| 29 |
tools=[],
|
| 30 |
model=model,
|
|
|
|
| 31 |
)
|
| 32 |
print("SmolAgent initialized.")
|
| 33 |
|
| 34 |
def __call__(self, question: str) -> str:
|
| 35 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 36 |
-
# 6. Prompt carefully - optimized for evaluation tasks
|
| 37 |
-
prompt = f"""Answer the following question with a short, direct response. Be concise and accurate:
|
| 38 |
-
|
| 39 |
-
Question: {question}
|
| 40 |
-
|
| 41 |
-
Answer:"""
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
print(f"Error running agent: {e}")
|
| 48 |
return f"AGENT ERROR: {e}"
|
|
|
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 12 |
+
SYSTEM_PROMPT = """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."""
|
| 13 |
|
| 14 |
# --- Smol Agent Definition ---
|
| 15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
| 29 |
self._agent = CodeAgent(
|
| 30 |
tools=[],
|
| 31 |
model=model,
|
| 32 |
+
instructions=SYSTEM_PROMPT,
|
| 33 |
)
|
| 34 |
print("SmolAgent initialized.")
|
| 35 |
|
| 36 |
def __call__(self, question: str) -> str:
|
| 37 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
+
# The system prompt is already set, just pass the question.
|
| 40 |
+
full_response = self._agent.run(question)
|
| 41 |
+
print(f"Agent full response: {full_response}")
|
| 42 |
+
|
| 43 |
+
# Parse the response to extract the final answer
|
| 44 |
+
if "FINAL ANSWER:" in full_response:
|
| 45 |
+
# Split and get the part after "FINAL ANSWER:"
|
| 46 |
+
final_answer_part = full_response.split("FINAL ANSWER:")[1]
|
| 47 |
+
# Remove brackets if they exist and strip whitespace
|
| 48 |
+
final_answer = final_answer_part.strip()
|
| 49 |
+
if final_answer.startswith('[') and final_answer.endswith(']'):
|
| 50 |
+
final_answer = final_answer[1:-1]
|
| 51 |
+
|
| 52 |
+
print(f"Agent returning parsed answer: {final_answer}")
|
| 53 |
+
return final_answer
|
| 54 |
+
else:
|
| 55 |
+
print("Warning: 'FINAL ANSWER:' not found in response. Returning full response.")
|
| 56 |
+
return full_response
|
| 57 |
+
|
| 58 |
except Exception as e:
|
| 59 |
print(f"Error running agent: {e}")
|
| 60 |
return f"AGENT ERROR: {e}"
|