80 lines
2.8 KiB
Python
80 lines
2.8 KiB
Python
from zhipuai import ZhipuAI
|
||
client = ZhipuAI(api_key="78dc1dfe37e04f29bd4ca9a49858a969.gn7TIZTfzpY35nx9") # 填写您自己的APIKey
|
||
|
||
from utils.image_utils import encode_image
|
||
|
||
reasoning_content = "" # 定义完整思考过程
|
||
answer_content = "" # 定义完整回复
|
||
is_answering = False # 判断是否结束思考过程并开始回复
|
||
|
||
|
||
base64_image = encode_image(r"D:\ocr\QQ截图20250814093022.jpg")
|
||
|
||
prompt = """
|
||
你是一个初中数学图片的描述专家,请将图片中的内容转换为文本。
|
||
1、注意数学公式需要用latex格式输出。注意只输出文本内容,不要输出任何解释。
|
||
2、如果图片中有图形,请结合题干内容对图形进行描述,只要描述不需要任何解释。
|
||
3、如果图片中有几何图形,识别图片中的几何图形,如三角形、圆形、矩形等。请结合题干内容对图形进行详细描述,包括形状、大小、位置关系等。
|
||
3、如果图片中是坐标系,分析抛物线的开口方向和经过的象限,并详细描述。识别图片中的坐标系类型,如直角坐标系、极坐标系等。
|
||
4、要以markdown格式输出
|
||
输出格式为
|
||
【题干】
|
||
【图形描述】
|
||
"""
|
||
|
||
|
||
|
||
|
||
completion = client.chat.completions.create(
|
||
model="glm-4.1v-thinking-flash", # 填写需要调用的模型名称
|
||
messages=[
|
||
{
|
||
"role": "user",
|
||
"content": [
|
||
{
|
||
"type": "text",
|
||
"text": prompt
|
||
},
|
||
{
|
||
"type": "image_url",
|
||
"image_url": {
|
||
# "url" : "https://dsideal.obs.cn-north-1.myhuaweicloud.com/wb/math.jpg"
|
||
'url': f'data:image/jpeg;base64,{base64_image}'
|
||
}
|
||
}
|
||
]
|
||
}
|
||
],
|
||
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(response.choices[0].message) |