from openai import OpenAI
# 一、调用OCR整理出试题
client = OpenAI (
api_key = ' sk-f6da0c787eff4b0389e4ad03a35a911f ' ,
base_url = " https://dashscope.aliyuncs.com/compatible-mode/v1 " ,
)
prompt = " 请提取图片中的试题 "
completion = client . chat . completions . create (
model = " qwen-vl-ocr-latest " ,
messages = [
{
" role " : " user " ,
" content " : [
{
" type " : " image_url " ,
" image_url " : " https://ylt.oss-cn-hangzhou.aliyuncs.com/HuangHai/Test/Source.jpg " ,
# 输入图像的最小像素阈值, 小于该值图像会按原比例放大, 直到总像素大于min_pixels
" min_pixels " : 28 * 28 * 4 ,
# 输入图像的最大像素阈值, 超过该值图像会按原比例缩小, 直到总像素低于max_pixels
" max_pixels " : 28 * 28 * 8192
} ,
# qwen-vl-ocr-latest支持在以下text字段中传入Prompt, 若未传入, 则会使用默认的Prompt: Please output only the text content from the image without any additional descriptions or formatting.
# 如调用qwen-vl-ocr-1028, 模型会使用固定Prompt: Read all the text in the image.不支持用户在text中传入自定义Prompt
{ " type " : " text " ,
" text " : prompt } ,
]
}
] )
print ( completion . choices [ 0 ] . message . content )
# 二、再次调用大模型整理内容
prompt = """
我将提供一份markdown格式的试卷 , 请帮我整理出每道题的以下内容 :
1. 题目序号
2. 题目内容 ( 自动识别并添加 $ 或 $ $ 包裹数学公式 )
3. 选项 ( 如果有 )
4. 答案
5. 解析
要求 :
- 一道题一道题输出 , 不要使用表格
- 自动检测数学表达式并用 $ 或 $ $ 正确包裹
- 确保公式中的特殊字符正确转义
- 除题目内容外 , 不要输出其它无关信息
内容如下 :
"""
text = completion . choices [ 0 ] . message . content
prompt + = text
completion = client . chat . completions . create (
model = " deepseek-v3 " ,
messages = [
{ " role " : " system " , " content " : " You are a helpful assistant. " } ,
{ " role " : " user " ,
" content " : prompt } ,
] ,
)
print ( completion . choices [ 0 ] . message . content )
# 将上面的返回内容写入到文件 整理后的结果.md 中
with open ( " Res/整理后的结果.md " , " w " , encoding = " utf-8 " ) as f :
f . write ( completion . choices [ 0 ] . message . content )
print ( " 保存成功! " )