如何使用ChatGPT从我女儿作业照片生成数学作业:第一部分
使用gpt-4-vision-preview和Python将数学工作表的照片转化为额外的练习题。
去年,OpenAI的ChatGPT引入了gpt-4-vision-preview,这是一个可以让你根据上传的图片提问的模型。例如,你可以询问它描述一个场景、一个房间,甚至是你冰箱里的内容。作为一个室内设计师,我曾考虑过使用它,但是我岔开了话题。
今天,我们将使用Python的ChatGPT来获取一个六年级数学练习册的图片,并生成类似的问题集。为什么这样做呢?偶尔,我会编写一些练习题来辅助我的女儿完成数学作业。编写好的题目需要花费一定的时间,虽然我很享受个性化编写它们的过程,但我也不介意省些时间。
这篇博客文章将涵盖以下内容:
- 使用OpenAI Python SDK中的gpt-4-vision-preview从上传的照片生成一份工作表。
- 迭代直到我们对结果满意为止。
在接下来的博客文章中,我们将使用生成的任务来:
- 请要求 gpt-4 输出 LaTeX 代码,以便我们可以生成新作业的 PDF。
- 尝试生成一个非常简单、纯HTML/JS的白板例子。
让我们先解决一个悬而未决的问题。许多家长和教育工作者对学生使用ChatGPT和其他智能AI工具来作弊或代写作业感到担忧,这是正当的。而我对于如何利用它来提升教育更感兴趣。
请注意:本系列示例使用GPT-4,您需要成为“按使用付费”的用户。截至本文撰写时,这意味着您需要至少在您的账户中加载1美元。根据令牌(或像素)计算的各种OpenAI模型的定价可以在这里找到。
迭代 1: 从图像生成新问题
我们将使用以下Python包:
- openai(欲了解更多信息,请参阅ChatGPT开发者快速入门)
- Base64是一种用于将二进制数据转换为可表示的ASCII字符集的编码方法。它常用于在电子邮件中传输、存储或处理二进制数据,特别是在不支持二进制格式的系统中。Base64编码使二进制数据在传输期间能够轻松地被表示和解码,因为所有字符都属于标准的ASCII字符集。这种编码方法将3个字节的二进制数据编码为4个字节的ASCII字符,因此导致编码后的数据会比原始数据略大。它由64种不同的可打印字符组成,包括大小写字母、数字以及'+', '/'两个特殊字符。基于Base64编码,我们可以确保数据在各种系统之间的互操作性。
- dotenv(加载一个包含OpenAI API密钥的.env文件)
首先,您将使用您的API密钥创建一个OpenAI客户端。对于本地开发,将API密钥和密码放在一个.env文件中是常见的做法(但确保只提交一个没有真实值的样本文件到版本控制中:如果您使用Git,请将.env添加到您的.gitignore中)。
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv
import base64
# Load the .env file that contains the OPENAI_API_KEY
# OPENAI_API_KEY=<your key>
load_dotenv(find_dotenv())
# Create an OpenAI client, using the OPENAI_KEY environment variable
# client = OpenAI(api_key=<insert your key>)
client = OpenAI()
客户端的“聊天关闭”API需要很多参数。我们将使用以下参数:
- messages:表示您发送给ChatGPT的自定义指示和提示的字典数组。
- 模型:您正在使用的模型
- max_tokens:您希望在回复中最多有的令牌数。
下面是对thegpt-4-vision模型请求的消息结构。您可以通过“用户”角色提交提示和图像。image_url可以是返回图像的公开可访问URL的链接,也可以是请求中包含的Base64编码图像。我们将对图像进行编码并在请求中发送。(有关如何使用Vision功能的更多详细信息,请参阅此处的文档。)
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": f"{prompt}",
},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
},
],
}
]
为了对图像进行编码,让我们按照他们的建议将图像进行base64编码,然后将字节转换为utf-8编码的字符串以便通过HTTP发送。
# Images can be passed to ChatGPT either via a publicly accessible link, or
# inline as a base64 encoded string
# https://platform.openai.com/docs/guides/vision
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
在我们的第一次尝试中,请问ChatGPT:“请使用这个六年级数学试卷的第一页,生成10个更多的问题。”
# Question we are asking based on the image
prompt = "Using the first page of this 6th grade math test, please generate 10 more problems."
# Encodes a photo taken of a page from a 6th grade math assessment test.
base64_image = encode_image("data/unit-2-assessment.jpg")
# Set the messages to send to model
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": f"{prompt}",
},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
},
],
}
]
# Send request to model
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=messages,
max_tokens=4096)
# Print out the response from ChatGPT
print(response.choices[0].message.content)
worksheet_str = response.choices[0].message.content
在我完全能够思考AI完全接管育儿的想法之前,它给出了以下回答:
Sure! Here are 10 additional math problems suitable for a 6th-grade assessment based on the style of problems on the test page:
**Greatest Common Factor (GCF) and Least Common Multiple (LCM):**
5. a. Find the GCF of (24, 36).
b. Find the LCM of (6, 14).
6. a. Find the GCF of (21, 49).
b. Find the LCM of (15, 20).
**Real-World Application of GCF and LCM:**
7. Two lights start blinking together at 12:00 PM. One light blinks every 6 seconds and the other every 8 seconds. When will they blink together again?
8. A gardener wants to plant flowers that bloom every 7 days and bushes that bloom every 10 days. If she plants them both today, in how many days will they bloom on the same day?
**Fraction Multiplication and Area Models:**
9. Mike has a rope that is 3/4 of a yard long. He cuts it into 2 equal parts. What is the length of each part? Draw a model to represent the problem.
10. A recipe calls for 2/5 cup of sugar. If Sarah is making half of the recipe, how much sugar does she need? Draw a model to represent the problem.
**Word Problems Involving Fractions:**
11. A school has 2/3 of a pizza left over from a party. If they divide this evenly among 3 teachers, how much pizza will each teacher get?
12. A bag contains 3/5 lb of apples. If each apple weighs 1/5 lb, how many apples are in the bag?
**Miscellaneous:**
13. In a class of 30 students, 4/5 of the students brought their homework. How many students brought their homework?
14. If one batch of cookies requires 3/4 cup of chocolate chips, how many cups of chocolate chips are required to make 4 batches?
These problems are designed to reinforce and assess skills in finding GCF and LCM, understanding real-world applications of these concepts, using fractions in context, drawing area models, and solving word problems related to multiplication and division of fractions.
不错的ChatGPT!你通过了我面试的第一轮。然而,当我再次尝试时,它没有生成像“最大公因数和最小公倍数的真实世界应用”这样的标题来表示每个部分。
迭代2:增加更多的提示内容
为了增强一致性,我希望在提示中提供更多说明。ChatGPT倾向于解释它试图做什么,所以或许我们可以利用这一点?请注意第一个回答以以下内容开头:
Sure! Here are 10 additional math problems suitable for a 6th-grade assessment based on the style of problems on the test page:
让我们修改提示为:“使用这张六年级数学试卷的第一页,请根据试卷上的问题风格生成10个额外的问题。” 这样一来,我们得到的回答更接近原始的风格。另外,我错了——原始的练习册上从未有过单独的章节标题,但ChatGPT决定在第一个回答中包含这个。
Sure, I can create additional problems in the style presented in the math test you've shown. Below are 10 new problems across the various types seen in the image:
1. Find the greatest common factor for each pair of numbers.
a. GCF (16, 24) = ___
b. GCF (30, 45) = ___
2. Find the least common multiple for each pair of numbers.
a. LCM (7, 5) = ___
b. LCM (9, 6) = ___
3. The Pencil Factory produces two types of pencils: standard pencils every 5 minutes and colored pencils every 8 minutes. They start operations at 9:00 A.M.
How long until both types of pencils are produced together again? ___ minutes (unit)
4. Ben's family plans to grow flowers on 1/4 of their garden space. Ben and his brother will plant 1/2 of the flower portion of the garden.
How much of the family garden will Ben and his brother plant? ___
Draw an area model and write a number sentence to represent the problem.
5. Find the greatest common factor for each pair of numbers.
a. GCF (14, 21) = ___
b. GCF (35, 50) = ___
6. Find the least common multiple for each pair of numbers.
a. LCM (4, 10) = ___
b. LCM (8, 10) = ___
7. The Gadget Workshop produces two gadgets: a widget every 6 minutes and a gizmo every 9 minutes. Production starts at 7:30 A.M.
When will both gadgets next be produced at the same time? ___ minutes (unit)
8. Emma's family allocated 3/5 of their backyard for a vegetable garden. Emma and her cousin will cultivate 3/4 of the vegetable garden together.
What fraction of the backyard will Emma and her cousin cultivate? ___
Illustrate the situation with an area model and write a number sentence.
9. Find the greatest common factor for each pair of numbers.
a. GCF (18, 27) = ___
b. GCF (20, 30) = ___
10. Find the least common multiple for each pair of numbers.
a. LCM (3, 11) = ___
b. LCM (7, 8) = ___
These problems maintain the format and cover similar math concepts as the original test page, including greatest common factors (GCF), least common multiples (LCM), and fractional parts of a whole, along with real-world applications.
迭代3:添加一个“系统”消息
您可以给ChatGPT赋予一种“虚拟角色”。根据文档:
系统消息有助于设置助手的行为。例如,您可以修改助手的个性,或提供关于它在对话中如何表现的具体指示。
让我们更新消息列表并再次发送请求。初始系统消息需要是第一个元素。
messages[:0] = {
"role": "system",
"content": "You are a helpful 6th grade math school teacher that creates homework assignments for your students."
}
response = client.chat.completions.create(
model="gpt-4-vision-preview",
messages=messages,
max_tokens=4096)
# Print out the response from ChatGPT
print(response.choices[0].message.content)
worksheet_str = response.choices[0].message.content
我们得到了一个与之前相似的回应。嗯,除了以“记住展示你的工作以获得满分!”结尾之外,似乎并没有产生什么不同。
Certainly! Based on the style of problems on the test page, here are 10 more problems for your 6th grade math homework assignment:
1. Find the greatest common factor for each pair of numbers.
a. GCF (24, 36) = ____
b. GCF (9, 27) = ____
2. Find the least common multiple for each pair of numbers.
a. LCM (7, 14) = ____
b. LCM (8, 10) = ____
3. The Happy Hat Shop makes two types of hats. They produce a sun hat every 6 minutes and a baseball cap every 8 minutes. If they start at 10:00 A.M., how long until the sun hat and baseball cap are made together again?
Answer: ________ minutes
4. Mike's family divided up their yard such that 1/4 of the yard will have flowers. Mike and his brother will plant 1/2 of the flower portion of the yard. How much of the yard will Mike and his brother plant with flowers?
Draw an area model and write a number sentence to represent the problem.
Number sentence: ____________
5. Find the greatest common factor for each pair of numbers.
a. GCF (35, 50) = ____
b. GCF (20, 45) = ____
6. Find the least common multiple for each pair of numbers.
a. LCM (3, 11) = ____
b. LCM (6, 14) = ____
7. The Fizzy Soda Company makes two flavors of soda. They produce a cola every 5 minutes and a lemon-lime soda every 7 minutes. They start at 9:00 A.M. How long until both flavors are produced together again?
Answer: ________ minutes
8. Laura's family is installing new carpet in their house such that 3/5 of the floors will be carpeted. Laura and her uncle will carpet 2/3 of the carpeted portion of the floors. How much of the house's floors will Laura and her uncle carpet?
Draw an area model and write a number sentence to represent the problem.
Number sentence: ____________
9. Find the greatest common factor for each pair of numbers.
a. GCF (21, 28) = ____
b. GCF (18, 54) = ____
10. Find the least common multiple for each pair of numbers.
a. LCM (9, 15) = ____
b. LCM (4, 16) = ____
These questions continue the concepts of GCF and LCM as well as real-world application problems involving fractions and repeated scheduling. Remember to show your work for full points!
迭代3:将更多指示添加到提示中
我对生成的格式相当满意,但是提供一些描述性的标题会有助于引导学生,至少在他们刚开始学习时。让我们尝试恢复这些标题!我尝试了几次(未在此处显示),但每次都意外地影响到了生成的问题类型。要么原始工作表中存在的问题类型被省略了,要么包含了本来不存在的内容。下面的提示(改变用粗体显示)对于标题给出了好的结果,但我并不完全满意:它仍然影响到了问题的类型。
使用这份六年级数学测试的第一页,请根据测试页上的问题风格生成10个更多的问题。为每个部分提供标题。
Based on the style of problems on the test page, here are 10 more problems that could serve as additional practice or homework for 6th-grade students:
**Greatest Common Factor (GCF)**
1. Find the greatest common factor for the following pair of numbers.
a. GCF (24, 36) = _______
b. GCF (30, 45) = _______
**Least Common Multiple (LCM)**
2. Find the least common multiple for the following pair of numbers.
a. LCM (7, 5) = _______
b. LCM (9, 6) = _______
**Real-World LCM Problem**
3. The community center offers a yoga class every 9 days and a pilates class every 6 days. If both classes are held today, in how many days will they both be offered on the same day again?
**Fraction of a Group Problem**
4. Pedro's bookshelf has 4 shelves, and \( \frac{3}{4} \) of them are filled with books. If each shelf can hold 16 books, how many books are on Pedro's bookshelf?
5. A recipe calls for \( \frac{1}{2} \) a cup of sugar. If Simon wants to make half a batch, how much sugar will he need?
**Fraction Multiplication Problem**
6. Miranda has \( \frac{3}{5} \) of a yard of fabric. She cuts off \( \frac{2}{3} \) of what she has for a project. How much fabric did she cut off?
**Area Model for Fractions**
7. Create an area model to represent the multiplication of \( \frac{3}{6} \times \frac{1}{4} \). Write the number sentence that represents this problem.
**Mixed Numbers**
8. Convert the mixed number to an improper fraction:
a. \( 3\frac{1}{4} \) = _______
b. \( 2\frac{2}{5} \) = _______
**Comparing Fractions**
9. Which is greater, \( \frac{5}{6} \) or \( \frac{2}{3} \)? Show how you know by either finding a common denominator or converting to decimals.
**Real-World Fraction Problem**
10. A school's soccer team won \( \frac{3}{5} \) of their games last season. If they played 25 games, how many games did they win?
Please make sure to show all your work for each problem and check your answers. Remember to simplify your fractions where possible. Good luck!
我决定再试一次(改变以粗体显示):
使用这个六年级数学测试的第一页,请根据测试页面上的问题风格生成10个更多的问题。请为每个部分提供描述性的标题。 使用该6年级数学测试的第一页,请根据测试页面上的问题风格生成10个更多的问题。请为每个部分提供描述性的标题。
### Greatest Common Factor (GCF)
5. **GCF Practice**
a. GCF (24, 36) = ______
b. GCF (40, 60) = ______
### Least Common Multiple (LCM)
6. **LCM Challenge**
a. LCM (6, 9) = ______
b. LCM (7, 14) = ______
### Real-world Applications of LCM and GCF
7. **Factory Schedule Problem**
The Pencil Factory produces red pencils every 5 hours and blue pencils every 8 hours. They start production at 7:00 A.M. When will both red and blue pencils be produced at the same time next?
### Fraction Multiplication in Real-world Context
8. **Gardening Fractions**
Daniel is planning a rectangular flower bed. He wants 1/4 of the flower bed to be roses and 1/2 of the roses section to consist of red roses. What fraction of the whole flower bed will be red roses?
Draw a rectangular model and write a number sentence to represent the problem.
### Additional Common Factor and Multiple Problems
9. **Common Factors and Multiples**
a. Name all the common factors of 14 and 21.
b. What is the smallest common multiple of 11 and 12?
### Practical Fraction Problem
10. **Baking Fractions**
Maria has 3/4 of a cup of sugar. She uses 2/3 of it to make a batch of cookies. How much sugar does she have left?
Draw a visual model and write a number sentence to represent the problem.
### Rational Number Operations
11. **Fraction Subtraction**
a. Subtract the fraction: 5/8 - 1/4 = ______
b. Subtract the fraction: 7/10 - 2/5 = ______
### Time Interval Problem Using LCM
12. **Meeting Time**
Two friends want to meet for yoga. One friend goes every 6 days, and the other goes every 9 days. If they both went today, after how many days will they both go to yoga on the same day next?
### Area and Fraction Review
13. **Camping Plot**
A family is camping on a rectangular plot that is 1/3 acre. They use 3/5 of this plot to set up their tents. What fraction of an acre did the tents cover?
Sketch a visual representation of the camping plot subdivided accordingly.
### Additional Calculation Practice
14. **Quick Calculations**
a. Multiply the fraction: 3/8 * 2/5 = ______
b. Divide the fraction: 6/7 ÷ 3/4 = ______
### Problem-Solving with Unit Rates
15. **Speedy Snail Trail**
A snail moves at a constant rate of 1/12 of a mile per hour. How far will the snail travel in 4 hours?
Provide a number sentence to calculate the distance traveled.
Please remember to write out your work for each problem and label your answers clearly. Good luck!
结论
它肯定会产生类似于原始工作表的问题,但要获得一致的结果并不容易!我需要尝试添加更多的指示(即进一步明确要求)。
- 尝试不同的提示来生成标题似乎影响了它所生成的问题类型,这完全出乎意料。
- 有时候它包含了原始工作表中没有的问题类型。
- 有时候会漏掉一些原工作表中的问题类型(“区域模型”) 。
- 它周期性地更改输出分数的格式: “1/4”, “¼”, “\(\frac{1}{4}\)” (最后一个是Markdown格式)。
- 准备好被告知你不是个酷爸爸了。¯\_(ツ)_/¯
推荐
- 使用LangChain来协调对ChatGPT(或其他LLM)的调用,创建可重复使用的“提示模板”,并将一系列操作链接在一起。(为了简洁起见,我在本例中没有使用LangChain。)
- 使用Jupyter笔记本进行快速反馈、可复现性和可追踪性:每个笔记本运行一个实验,并将结果保存在适当命名的文件夹和文件中。(这是我处理此事的方式。)
- 不要放弃!可能需要多次尝试才能找到自己喜欢的东西。由于输出内容很容易编辑,我对结果可以有些宽容。
- 尝试使用不同的工作表来查看其性能。