Spaces:
Running
Running
Commit
·
afd461f
1
Parent(s):
179cf59
test
Browse files- QCMTool.py +65 -0
- app.py +35 -12
- info/questions.json +13 -0
- prompts.yaml +43 -0
QCMTool.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import random
|
3 |
+
|
4 |
+
class QCMTool:
|
5 |
+
def __init__(self, json_file):
|
6 |
+
"""
|
7 |
+
Initialize the QCM tool with a JSON file containing questions.
|
8 |
+
"""
|
9 |
+
self.json_file = json_file
|
10 |
+
self.questions = self.load_questions()
|
11 |
+
|
12 |
+
def load_questions(self):
|
13 |
+
"""
|
14 |
+
Load questions from the JSON file.
|
15 |
+
"""
|
16 |
+
with open(self.json_file, 'r') as file:
|
17 |
+
questions = json.load(file)
|
18 |
+
return questions
|
19 |
+
|
20 |
+
def pick_random_question(self):
|
21 |
+
"""
|
22 |
+
Pick a random question from the loaded questions.
|
23 |
+
"""
|
24 |
+
return random.choice(self.questions)
|
25 |
+
|
26 |
+
def check_answer(self, question, user_answer):
|
27 |
+
"""
|
28 |
+
Check if the user's answer is correct and provide an explanation.
|
29 |
+
"""
|
30 |
+
if user_answer == question["correct_answer"]:
|
31 |
+
return True, question["explanation"]
|
32 |
+
else:
|
33 |
+
return False, question["explanation"]
|
34 |
+
|
35 |
+
def run(self):
|
36 |
+
"""
|
37 |
+
Run the QCM tool: pick a question, ask the user, and check the answer.
|
38 |
+
"""
|
39 |
+
question = self.pick_random_question()
|
40 |
+
print("Question:", question["question"])
|
41 |
+
for i, option in enumerate(question["options"], 1):
|
42 |
+
print(f"{chr(64 + i)}. {option}") # Use 'A', 'B', 'C', 'D' instead of numbers
|
43 |
+
|
44 |
+
user_answer = input("Your answer (enter the option letter, e.g., A, B, C, D): ").strip().upper()
|
45 |
+
try:
|
46 |
+
# Map user input (A, B, C, D) to the corresponding option index
|
47 |
+
option_index = ord(user_answer) - 65 # Convert 'A' to 0, 'B' to 1, etc.
|
48 |
+
if 0 <= option_index < len(question["options"]):
|
49 |
+
selected_option = question["options"][option_index]
|
50 |
+
is_correct, explanation = self.check_answer(question, selected_option)
|
51 |
+
if is_correct:
|
52 |
+
print("Correct! 🎉")
|
53 |
+
else:
|
54 |
+
print("Incorrect! 😞")
|
55 |
+
print("Explanation:", explanation)
|
56 |
+
else:
|
57 |
+
print("Invalid option letter. Please try again.")
|
58 |
+
except (ValueError, IndexError):
|
59 |
+
print("Invalid input. Please enter a valid option letter (A, B, C, D).")
|
60 |
+
|
61 |
+
|
62 |
+
qcm_tool = QCMTool('info/questions.json')
|
63 |
+
|
64 |
+
# Run the QCM tool
|
65 |
+
qcm_tool.run()
|
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import json
|
|
|
2 |
|
3 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
4 |
import datetime
|
@@ -94,7 +95,7 @@ def provide_my_information(query: str) -> str:
|
|
94 |
return f"I am located in {my_info['location']}."
|
95 |
elif "occupation" in query or "job" in query or "work" in query:
|
96 |
return f"I work as a {my_info['occupation']}."
|
97 |
-
elif "education" in query or "
|
98 |
return f"I have a {my_info['education']}."
|
99 |
elif "skills" in query or "what can you do" in query:
|
100 |
return f"My skills include: {', '.join(my_info['skills'])}."
|
@@ -110,21 +111,43 @@ def provide_my_information(query: str) -> str:
|
|
110 |
)
|
111 |
else:
|
112 |
return "I'm sorry, I don't have information on that. Please ask about my name, location, occupation, education, skills, hobbies, or contact details."
|
113 |
-
|
|
|
114 |
@tool
|
115 |
-
def
|
116 |
-
"""
|
|
|
|
|
117 |
Args:
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
"""
|
120 |
try:
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
except Exception as e:
|
127 |
-
return f"Error
|
128 |
|
129 |
|
130 |
final_answer = FinalAnswerTool()
|
@@ -150,7 +173,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
150 |
|
151 |
agent = CodeAgent(
|
152 |
model=model,
|
153 |
-
tools=[final_answer,calculate_risk_metrics,
|
154 |
max_steps=6,
|
155 |
verbosity_level=1,
|
156 |
grammar=None,
|
|
|
1 |
import json
|
2 |
+
from random import random
|
3 |
|
4 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
5 |
import datetime
|
|
|
95 |
return f"I am located in {my_info['location']}."
|
96 |
elif "occupation" in query or "job" in query or "work" in query:
|
97 |
return f"I work as a {my_info['occupation']}."
|
98 |
+
elif "education" in query or "educational" in query:
|
99 |
return f"I have a {my_info['education']}."
|
100 |
elif "skills" in query or "what can you do" in query:
|
101 |
return f"My skills include: {', '.join(my_info['skills'])}."
|
|
|
111 |
)
|
112 |
else:
|
113 |
return "I'm sorry, I don't have information on that. Please ask about my name, location, occupation, education, skills, hobbies, or contact details."
|
114 |
+
|
115 |
+
|
116 |
@tool
|
117 |
+
def qcm_tool(json_file: str, user_answer: Optional[str] = None) -> str:
|
118 |
+
"""
|
119 |
+
A tool that picks a random QCM (Multiple Choice Question) from a JSON file and checks the user's answer.
|
120 |
+
|
121 |
Args:
|
122 |
+
json_file: Path to the JSON file containing questions.
|
123 |
+
user_answer: The user's answer (optional). If not provided, the tool returns a question and options.
|
124 |
+
|
125 |
+
Returns:
|
126 |
+
If no user_answer is provided:
|
127 |
+
A string containing the question and options.
|
128 |
+
If user_answer is provided:
|
129 |
+
A string indicating whether the answer is correct and providing an explanation.
|
130 |
"""
|
131 |
try:
|
132 |
+
# Load questions from the JSON file
|
133 |
+
with open("info/questions.json", 'r') as file:
|
134 |
+
questions = json.load(file)
|
135 |
+
|
136 |
+
# Pick a random question
|
137 |
+
question = random.choice(questions)
|
138 |
+
|
139 |
+
if user_answer is None:
|
140 |
+
# Return the question and options
|
141 |
+
options_str = "\n".join([f"{chr(65 + i)}. {opt}" for i, opt in enumerate(question['options'])])
|
142 |
+
return f"Question: {question['question']}\nOptions:\n{options_str}"
|
143 |
+
else:
|
144 |
+
# Check the user's answer
|
145 |
+
if user_answer.strip().upper() == question['correct_answer'].strip().upper():
|
146 |
+
return f"Correct! 🎉\nExplanation: {question['explanation']}"
|
147 |
+
else:
|
148 |
+
return f"Incorrect! 😞\nExplanation: {question['explanation']}"
|
149 |
except Exception as e:
|
150 |
+
return f"Error in QCM tool: {str(e)}"
|
151 |
|
152 |
|
153 |
final_answer = FinalAnswerTool()
|
|
|
173 |
|
174 |
agent = CodeAgent(
|
175 |
model=model,
|
176 |
+
tools=[final_answer,calculate_risk_metrics,qcm_tool,visit_webpage,web_search,provide_my_information], ## add your tools here (don't remove final answer)
|
177 |
max_steps=6,
|
178 |
verbosity_level=1,
|
179 |
grammar=None,
|
info/questions.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"question": "A bank’s risk officer is evaluating climate-related risk drivers that could create financial risk for the bank. The risk officer has classified rising global temperatures and wildfires as acute physical risks. The risk officer is correct with respect to:",
|
4 |
+
"options": [
|
5 |
+
"wildfires only.",
|
6 |
+
"rising global temperatures only.",
|
7 |
+
"both wildfires and rising global temperatures.",
|
8 |
+
"neither wildfires nor rising global temperatures."
|
9 |
+
],
|
10 |
+
"correct_answer": "wildfires only.",
|
11 |
+
"explanation": "Wildfires are correctly classified as acute physical risks, which relate to extreme weather events. Rising global temperatures should be classified as chronic physical risks, which relate to climate shifts like rising global temperatures and rising sea levels. (LO 99.b)"
|
12 |
+
}
|
13 |
+
]
|
prompts.yaml
CHANGED
@@ -132,6 +132,49 @@
|
|
132 |
{%- else %}
|
133 |
{%- endif %}
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
Here are the rules you should always follow to solve your task:
|
136 |
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_code>' sequence, else you will fail.
|
137 |
2. Use only variables that you have defined!
|
|
|
132 |
{%- else %}
|
133 |
{%- endif %}
|
134 |
|
135 |
+
---
|
136 |
+
**QCM Tool**
|
137 |
+
You also have access to a QCM (Multiple Choice Question) tool that can fetch questions from a JSON file and check user answers. Here’s how to use it:
|
138 |
+
|
139 |
+
- To fetch a random question, call `qcm_tool(json_file='questions.json')`.
|
140 |
+
- To check a user's answer, call `qcm_tool(json_file='questions.json', user_answer="A")`.
|
141 |
+
|
142 |
+
Example:
|
143 |
+
---
|
144 |
+
Task: "Ask me a practice question on financial risk management."
|
145 |
+
|
146 |
+
Thought: I will use the QCM tool to fetch a random question from the JSON file and display it to the user.
|
147 |
+
Code:
|
148 |
+
```py
|
149 |
+
question_data = qcm_tool(json_file='questions.json')
|
150 |
+
print(question_data)
|
151 |
+
```<end_code>
|
152 |
+
Observation:
|
153 |
+
Question: A bank’s risk officer is evaluating climate-related risk drivers that could create financial risk for the bank. The risk officer has classified rising global temperatures and wildfires as acute physical risks. The risk officer is correct with respect to:
|
154 |
+
Options:
|
155 |
+
A. wildfires only.
|
156 |
+
B. rising global temperatures only.
|
157 |
+
C. both wildfires and rising global temperatures.
|
158 |
+
D. neither wildfires nor rising global temperatures.
|
159 |
+
|
160 |
+
Thought: I will now ask the user to provide their answer.
|
161 |
+
Code:
|
162 |
+
```py
|
163 |
+
user_answer = input("Please enter your answer (A, B, C, or D): ")
|
164 |
+
result = qcm_tool(json_file='questions.json', user_answer=user_answer)
|
165 |
+
print(result)
|
166 |
+
```<end_code>
|
167 |
+
Observation:
|
168 |
+
Correct! 🎉
|
169 |
+
Explanation: Wildfires are correctly classified as acute physical risks, which relate to extreme weather events. Rising global temperatures should be classified as chronic physical risks, which relate to climate shifts like rising global temperatures and rising sea levels. (LO 99.b)
|
170 |
+
|
171 |
+
Thought: I will now provide the final answer with the result.
|
172 |
+
Code:
|
173 |
+
```py
|
174 |
+
final_answer(result)
|
175 |
+
```<end_code>
|
176 |
+
|
177 |
+
---
|
178 |
Here are the rules you should always follow to solve your task:
|
179 |
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```<end_code>' sequence, else you will fail.
|
180 |
2. Use only variables that you have defined!
|