Files
dsProject/dsLightRag/Test/S4_QVQLanguage.py
2025-08-14 15:45:08 +08:00

76 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from openai import OpenAI
import os
# 初始化OpenAI客户端
client = OpenAI(
api_key="sk-01d13a39e09844038322108ecdbd1bbc",
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
reasoning_content = "" # 定义完整思考过程
answer_content = "" # 定义完整回复
is_answering = False # 判断是否结束思考过程并开始回复
prompt = """
def 几何预处理(text):
# 坐标系标准化
建立参考坐标系(以最长边为x轴基准)
计算各点相对坐标(保留2位小数)
# 约束显式化
"直角在上方"转换为: if 角C是直角
设置C.y = max(A.y, B.y) + offset
添加约束 Angle(A,C,B)=90°
# 指令优化
移除所有文字描述
输出结构化指令元组: return [ ('CreatePoint', 'A', (x1,y1)), ('SetConstraint', 'Perpendicular', ['AC','BC']), ('VisualHint', 'C', {'color':'red','size':8}) ]
"""
# 创建聊天完成请求
completion = client.chat.completions.create(
model="qvq-max", # 此处以 qvq-max 为例,可按需更换模型名称
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "https://dsideal.obs.cn-north-1.myhuaweicloud.com/wb/math.jpg"
},
},
{"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")
# print(answer_content)