Files
QingLong/AI/WxMini/Test/QVQ_1.py
2025-08-15 09:13:13 +08:00

87 lines
3.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
from WxMini.Milvus.Config.MulvusConfig import *
# 几何题,计算面积和体积
image_url = "https://img.alicdn.com/imgextra/i1/O1CN01gDEY8M1W114Hi3XcN_!!6000000002727-0-tps-1024-406.jpg"
# image_url = 'https://hzkc.oss-cn-beijing.aliyuncs.com/Upload/wuli.png'
# 初始化OpenAI客户端
client = OpenAI(
api_key=MODEL_API_KEY,
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
reasoning_content = "" # 定义完整思考过程
answer_content = "" # 定义完整回复
is_answering = False # 判断是否结束思考过程并开始回复
prompt = """
请以标准的 Markdown 格式返回答案,具体要求如下:
1. 标题使用 `#` 或 `##`。
2. 列表使用 `-` 或 `*`,并确保缩进一致。
3. 数学公式使用 `$...$` 表示行内公式。
4. 数学公式使用 `$$...$$` 表示块级公式。
5. 在 Markdown 文件中,使用 $$ 标记数学公式,并确保 align* 环境中的公式正确对齐。
(1). 使用 $$ 包裹多行数学公式。
(2). 在 align* 环境中,每行公式的开头使用 & 符号对齐。
(3). 确保公式内容清晰、易读,并正确使用 LaTeX 语法。
示例:
markdown> ### 表面积> - **公式**$S = 2(ab + ah + bh)$ > 其中 $a=4\,\text{cm}$(长)、$b=3\,\text{cm}$(宽)、$h=2\,\text{cm}$(高)。> > - **计算过程**> $$> \begin{align*}> S &= 2(4 \times 3 + 4 \times 2 + 3 \times 2) \\> &= 2(12 + 8 + 6) \\> &= 2 \times 26 \\> &= 52\,\text{cm}^2> \end{align*}> $$>
注意事项:
- 确保 $$ 包裹的公式块独立成段,不与普通文本混排。
- 在 align* 环境中,每行公式的开头使用 & 符号对齐,例如 S &= ...。
- 使用 \text{} 包裹单位,例如 \text{cm}^2。
目标:
生成的 Markdown 文件应能够通过 pandoc 正确转换为 HTML并使用 MathJax 渲染数学公式。
"""
# 创建聊天完成请求
completion = client.chat.completions.create(
model="qvq-max", # 此处以 qvq-max 为例,可按需更换模型名称
messages=[
{
"role": "system",
"content": [{"type": "text", "text": "You are a helpful assistant."}],
},
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": image_url
},
},
{"type": "text", "text": "这道题怎么解答?" + prompt},
],
},
],
stream=True,
)
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
# 将完整回复保存到指定文件
output_file = "c:/1.md" # 指定保存文件的路径
with open(output_file, "w", encoding="utf-8") as file:
file.write(answer_content)
print(f"\n完整回复已保存到文件:{output_file}")