## Installing corresponding python packages
# !pip install openai pyarrow pandas pillow matplotlib
## accessing the api_key of qwen model following the below link
# https://help.aliyun.com/zh/model-studio/get-api-key
data loading
import os
import re
import json
import random
from openai import OpenAI
from base_prompt import *
import pyarrow.parquet as pq
import pandas as pd
from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt
import copy
42) random.seed(
def load_data(data_root='data/scienceqa_json/', test_number=10, shot_number=3):
print(f"loading data {'*'*50}")
= json.load(open(f"{data_root}problems.json"))
problems = json.load(open(f"{data_root}pid_splits.json"))
pid_splits = json.load(open(f"{data_root}instruct_captions.json"))["captions"]
captions
for qid in problems:
'caption'] = captions[qid] if qid in captions else ""
problems[qid][
= pid_splits['test']
qids = qids[:test_number] if test_number > 0 else qids
qids print(f"number of test problems: {len(qids)}")
# pick up shot examples from the training set
= pid_splits['train']
train_qids = random.sample(train_qids, shot_number) # random sample
shot_qids print(f"training question ids for prompting: {shot_qids}")
return problems, qids, shot_qids
def visualize_qestion_img(q_idx, img_caption):
= 'data/scienceqa_parquet/test-00000-of-00001-f0e719df791966ff.parquet'
parquet_path = pq.ParquetFile(parquet_path)
parquet_file = parquet_file.read().to_pandas()
data
# get corresponding index
='data/scienceqa_json/'
data_root= json.load(open(f"{data_root}pid_splits.json"))
pid_splits = pid_splits['test'].index(q_idx)
index_
= data.iloc[index_, 0]
image_content = data.iloc[index_, 1], data.iloc[index_, 2], data.iloc[index_, 3], data.iloc[index_, 4]
question, choices, answer, txt_context if image_content == None:
print('this question has no image context')
else:
= image_content['bytes'], image_content['path']
image_bytes, image_path = Image.open(BytesIO(image_bytes))
image print(f"Question: {question}\n\nText context:{txt_context}\n\nImage caption:{img_caption}\n\nChoices: {choices}\n\nAnswer:{answer}\n\n")
= plt.subplots(1, 1, figsize=(7, 6))
fig, axes
axes.imshow(image)'image')
axes.set_title('off')
axes.axis(
plt.show()
def qwen_predict(prompt):
= OpenAI(
client # 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:api_key="sk-xxx",
="sk-def5c782defa4696a21653a5b6c8af76",
api_key="https://dashscope.aliyuncs.com/compatible-mode/v1",
base_url
)
= client.chat.completions.create(
output ="qwen-plus", # 模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
model=[
messages'role': 'system', 'content': 'You are a helpful assistant.'},
{'role': 'user', 'content': prompt}
{
]
)=output.choices[0].message.content
response# extract the answer
= re.compile(r'The answer is ([A-Z]).')
pattern = pattern.findall(response)
res if len(res) == 1:
= res[0] # 'A', 'B', ...
predict_answer else:
= "FAILED"
predict_answer return response, predict_answer
def get_pred_idx(prediction, choices):
"""
Get the index (e.g. 2) from the prediction (e.g. 'C')
"""
= ["A", "B", "C", "D", "E"]
options
if prediction in options[:len(choices)]:
return options.index(prediction)
else:
return random.choice(range(len(choices)))
= load_data() problems, qids, shot_qids
loading data **************************************************
number of test problems: 10
training question ids for prompting: ['17525', '3065', '688']
comparing two kinds of prompt: QCM-ALE vs. QCM-A
= '389'
qid 'caption']) visualize_qestion_img(qid, problems[qid][
Question: Which rhetorical appeal is primarily used in this ad?
Text context:
Image caption:The image features a mother and daughter sitting on a couch, with the mother holding the daughter close to her chest. The daughter's head is resting on the mother's shoulder, while the mother's hand is placed on the daughter's back. There are two pillows visible in the scene, one on the left side of the couch and another on the right side of the couch. These pillows provide additional comfort and support for the mother and daughter as they sit together. In addition to the mother and daughter, there are two other people present in the image. One person is located on the left side of the couch, while the other person is situated on the right side of the couch.
Choices: ['logos (reason)' 'pathos (emotion)' 'ethos (character)']
Answer:1
= problems[qid]["choices"], problems[qid]["answer"]
choices, gt_answer = build_prompt(problems, shot_qids, qid, prompt_format='QCM-A')
prompt print(f"{'*'*150}\nthe model prompt for qwen:\n{prompt}")
******************************************************************************************************************************************************
the model prompt for qwen:
Question: Does this passage describe the weather or the climate?
The wind was blowing from the west in Ensenada, Mexico, last week.
Context: Hint: Weather is what the atmosphere is like at a certain place and time. Climate is the pattern of weather in a certain place.
Options: (A) climate (B) weather
Answer: The answer is B.
Question: Which is a run-on sentence?
Context: N/A
Options: (A) The little boy popped a big bubble. (B) Nora just moved here, she's new to our country.
Answer: The answer is B.
Question: Which property matches this object?
Context: Select the better answer. The image features a white background with a roll of tin foil on it. The tin foil is positioned in the center of the image, taking up a significant portion of the space. There are also several other items placed around the tin foil, such as a bottle, a cup, and a spoon. These additional objects add to the overall composition of the scene, making it more visually appealing.
Options: (A) yellow (B) shiny
Answer: The answer is B.
Question: Which rhetorical appeal is primarily used in this ad?
Context: The image features a mother and daughter sitting on a couch, with the mother holding the daughter close to her chest. The daughter's head is resting on the mother's shoulder, while the mother's hand is placed on the daughter's back. There are two pillows visible in the scene, one on the left side of the couch and another on the right side of the couch. These pillows provide additional comfort and support for the mother and daughter as they sit together. In addition to the mother and daughter, there are two other people present in the image. One person is located on the left side of the couch, while the other person is situated on the right side of the couch.
Options: (A) logos (reason) (B) pathos (emotion) (C) ethos (character)
Answer:
= qwen_predict(prompt)
response, predict_answer print(f"model_response:\n {response}")
model_response:
The answer is B.
= problems[qid]["choices"], problems[qid]["answer"]
choices, gt_answer = build_prompt(problems, shot_qids, qid, prompt_format='QCM-ALE')
prompt print(f"{'*'*150}\nthe model prompt for qwen:\n{prompt}")
******************************************************************************************************************************************************
the model prompt for qwen:
Question: Does this passage describe the weather or the climate?
The wind was blowing from the west in Ensenada, Mexico, last week.
Context: Hint: Weather is what the atmosphere is like at a certain place and time. Climate is the pattern of weather in a certain place.
Options: (A) climate (B) weather
Answer: The answer is B. BECAUSE: The atmosphere is the layer of air that surrounds Earth. Both weather and climate tell you about the atmosphere.\nWeather is what the atmosphere is like at a certain place and time. Weather can change quickly. For example, the temperature outside your house might get higher throughout the day.\nClimate is the pattern of weather in a certain place. For example, summer temperatures in New York are usually higher than winter temperatures. Read the text carefully.\nThe wind was blowing from the west in Ensenada, Mexico, last week.\nThis passage tells you about the wind direction in Ensenada last week. It describes the atmosphere at a certain place and time. So, this passage describes the weather.
Question: Which is a run-on sentence?
Context: N/A
Options: (A) The little boy popped a big bubble. (B) Nora just moved here, she's new to our country.
Answer: The answer is B. BECAUSE: A sentence is a group of words that forms a complete thought. It has both a subject and a verb.\nMy friends walk along the path.\nA run-on sentence is made up of two sentences that are joined without end punctuation or with just a comma.\nI knocked on the door it opened.\nIt started raining, we ran inside.\nTo fix a run-on sentence, separate it into two sentences. Add end punctuation after the first sentence, and capitalize the second sentence.\nI knocked on the door. It opened.\nIt started raining. We ran inside.\nYou can also fix a run-on sentence by rewriting it as a compound sentence. A compound sentence is two sentences joined by a comma and a conjunction such as and, but, or, or so.\nI knocked on the door, and it opened.\nIt started raining, so we ran inside. Nora just moved here, she's new to our country is a run-on sentence. It has two sentences that are joined by just a comma: Nora just moved here and She's new to our country.
Question: Which property matches this object?
Context: Select the better answer. The image features a white background with a roll of tin foil on it. The tin foil is positioned in the center of the image, taking up a significant portion of the space. There are also several other items placed around the tin foil, such as a bottle, a cup, and a spoon. These additional objects add to the overall composition of the scene, making it more visually appealing.
Options: (A) yellow (B) shiny
Answer: The answer is B. BECAUSE: An object has different properties. A property of an object can tell you how it looks, feels, tastes, or smells. Look at the object.\nThink about each property.\nA shiny object reflects a lot of light. The tin foil is shiny.\nYellow is a color.\nThis color is yellow. The tin foil is not yellow.
Question: Which rhetorical appeal is primarily used in this ad?
Context: The image features a mother and daughter sitting on a couch, with the mother holding the daughter close to her chest. The daughter's head is resting on the mother's shoulder, while the mother's hand is placed on the daughter's back. There are two pillows visible in the scene, one on the left side of the couch and another on the right side of the couch. These pillows provide additional comfort and support for the mother and daughter as they sit together. In addition to the mother and daughter, there are two other people present in the image. One person is located on the left side of the couch, while the other person is situated on the right side of the couch.
Options: (A) logos (reason) (B) pathos (emotion) (C) ethos (character)
Answer:
= qwen_predict(prompt)
response, predict_answer print(f"model_response: {response}\n")
model_response: The answer is B. BECAUSE: Rhetorical appeals are strategies used to persuade an audience. There are three main types: logos, ethos, and pathos.
- Logos (reason) appeals to logic and uses facts, data, or clear reasoning to convince the audience.
- Ethos (character) appeals to credibility or trustworthiness, often showing that the speaker is reliable or knowledgeable.
- Pathos (emotion) appeals to the audience’s feelings, such as love, sadness, fear, or happiness.
In this ad, the image shows a mother holding her daughter closely on a couch, with their heads touching and hands gently placed for comfort. The presence of pillows adds to the sense of warmth and care. Two other people nearby suggest a close-knit family or support system. This scene is designed to evoke feelings of love, safety, and emotional connection.
There are no statistics, logical arguments, or claims about credibility. Instead, the ad focuses on creating an emotional response. Therefore, the primary rhetorical appeal used is pathos (emotion).
preparation on multiple questions
def evaluate_on_dataset(prompt_format='CQM-A'):
= {}, 0
result, correct for qid in qids:
= problems[qid]["choices"], problems[qid]["answer"]
choices, gt_answer = build_prompt(problems, shot_qids, qid, prompt_format='CQM-A')
prompt
= qwen_predict(prompt)
response, predict_answer = get_pred_idx(predict_answer, choices) # 0, 1, ..., 4
pred_idx if pred_idx == gt_answer:
+= 1
correct
=copy.deepcopy(problems[qid])
result[qid]'predict'], result[qid]['response']=pred_idx, response
result[qid][= correct/len(result)
acc return result, correct, acc
= evaluate_on_dataset(prompt_format='QCM-A')
result_a, correct_a, acc_a = evaluate_on_dataset(prompt_format='QCM-ALE')
result_ale, correct_ale, acc_ale
print(f"the acc of format 'QCM-A' is: {acc_a}\nthe acc of format 'QCM-ALE' is: {acc_ale}")
the acc of format 'QCM-A' is: 0.9
the acc of format 'QCM-ALE' is: 1.0
Reuse
Citation
For attribution, please cite this work as:
Li, Zeju. n.d. “Data Loading.” https://zerojumpline.github.io//teaching/2025-08-15-Undergraduate
Project/reasoning.html.