78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
from openai import OpenAI
|
||
from utils.image_utils import encode_image
|
||
|
||
# 初始化OpenAI客户端
|
||
client = OpenAI(
|
||
api_key="sk-f6da0c787eff4b0389e4ad03a35a911f",
|
||
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
|
||
)
|
||
|
||
base64_image = encode_image(r"D:\ocr\QQ截图20250814093022.jpg")
|
||
|
||
reasoning_content = "" # 定义完整思考过程
|
||
answer_content = "" # 定义完整回复
|
||
is_answering = False # 判断是否结束思考过程并开始回复
|
||
|
||
prompt = """
|
||
你是一个初中数学图片的描述专家,请将图片中的内容转换为文本。
|
||
1、注意数学公式需要用latex格式输出。注意只输出文本内容,不要输出任何解释。
|
||
2、如果图片中有图形,请根据题干对图形进行描述,只要描述不需要任何解释。
|
||
3、如果图片中是坐标系,一定要说明抛物线的开口和抛物线经过了几个象限。
|
||
4、要以markdown格式输出,注意latex格式两边要加$
|
||
输出格式为
|
||
【题干】
|
||
【图形描述】
|
||
"""
|
||
|
||
# 创建聊天完成请求
|
||
completion = client.chat.completions.create(
|
||
model="qvq-max-latest", # 此处以 qvq-max 为例,可按需更换模型名称
|
||
messages=[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{
|
||
"type": "image_url",
|
||
"image_url": {
|
||
# "url": "https://dsideal.obs.cn-north-1.myhuaweicloud.com/wb/img10.jpg"
|
||
'url': f'data:image/jpeg;base64,{base64_image}'
|
||
},
|
||
},
|
||
{"type": "text",
|
||
"text": prompt},
|
||
],
|
||
},
|
||
],
|
||
stream=True,
|
||
)
|
||
|
||
print("\n" + "=" * 20 + "思考过程" + "=" * 20 + "\n")
|
||
|
||
for chunk in completion:
|
||
# 如果chunk.choices为空,则打印usage
|
||
if not chunk.choices:
|
||
print("\nUsage:")
|
||
print(chunk.usage)
|
||
else:
|
||
delta = chunk.choices[0].delta
|
||
# 打印思考过程
|
||
if hasattr(delta, 'reasoning_content') and delta.reasoning_content != None:
|
||
print(delta.reasoning_content, end='', flush=True)
|
||
reasoning_content += delta.reasoning_content
|
||
else:
|
||
# 开始回复
|
||
if delta.content != "" and is_answering is False:
|
||
print("\n" + "=" * 20 + "完整回复" + "=" * 20 + "\n")
|
||
is_answering = True
|
||
# 打印回复过程
|
||
print(delta.content, end='', flush=True)
|
||
answer_content += delta.content
|
||
|
||
# print("=" * 20 + "完整思考过程" + "=" * 20 + "\n")
|
||
# print(reasoning_content)
|
||
# print("=" * 20 + "完整回复" + "=" * 20 + "\n")
|
||
# 保存成QvqResult.txt
|
||
with open("QvqResult.txt", "w", encoding='utf-8') as f:
|
||
f.write(answer_content)
|
||
print("试题解析文本保存成功!")
|