Files
kefu/wechat_rpa/test_vision.py
T
2026-07-16 12:11:05 +08:00

113 lines
3.7 KiB
Python

"""
测试视觉模式 API 调用,使用 debug_chat_area.png 作为测试图片。
"""
import os
import sys
import base64
import json
import requests
sys.path.insert(0, os.path.dirname(__file__))
from ai_config import AI_API_BASE, AI_API_KEY, AI_MODEL, AI_TIMEOUT
# 读取调试截图
img_path = os.path.join(os.path.dirname(__file__), "debug_chat_area.png")
if not os.path.exists(img_path):
print(f"❌ 找不到测试图片: {img_path}")
sys.exit(1)
with open(img_path, "rb") as f:
img_bytes = f.read()
print(f"图片大小: {len(img_bytes)} bytes")
b64 = base64.b64encode(img_bytes).decode("utf-8")
print(f"Base64 长度: {len(b64)} 字符")
headers = {
"Authorization": f"Bearer {AI_API_KEY}",
"Content-Type": "application/json",
}
from ai_chat import _completions_url
url = _completions_url()
print(f"API URL: {url}")
print(f"模型: {AI_MODEL}")
print("-" * 50)
# 方式 1: 标准 OpenAI 格式 (data URI)
print("\n[测试 1] 标准 OpenAI 格式 (data:image/png;base64,...)")
payload1 = {
"model": AI_MODEL,
"messages": [
{"role": "system", "content": "你是一个真人,你要时刻盯着系统内容,进行回复"},
{"role": "user", "content": [
{"type": "text", "text": (
"这是一个聊天对话窗口的截图。"
"左边的灰色气泡是对方(客户)发的消息,右边的蓝色气泡是我方之前的回复。"
"请只关注对方(客户)发的最后一条消息,针对那条消息直接回复。"
"只输出回复内容,不要描述图片,不要解释,不要加引号。"
)},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}},
]},
],
"max_tokens": 200,
}
try:
resp = requests.post(url, headers=headers, json=payload1, timeout=AI_TIMEOUT)
resp.raise_for_status()
result = resp.json()
content = result["choices"][0]["message"]["content"]
print(f"✅ 回复: {content[:200]}")
except Exception as e:
print(f"❌ 失败: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f" 响应: {e.response.text[:300]}")
# 方式 2: 不带 data URI 前缀
print("\n[测试 2] 纯 base64 (不带 data: 前缀)")
payload2 = {
"model": AI_MODEL,
"messages": [
{"role": "user", "content": [
{"type": "text", "text": "请描述这张图片中的文字内容,用中文回答。"},
{"type": "image_url", "image_url": {"url": b64}},
]},
],
"max_tokens": 200,
}
try:
resp = requests.post(url, headers=headers, json=payload2, timeout=AI_TIMEOUT)
resp.raise_for_status()
result = resp.json()
content = result["choices"][0]["message"]["content"]
print(f"✅ 回复: {content[:200]}")
except Exception as e:
print(f"❌ 失败: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f" 响应: {e.response.text[:300]}")
# 方式 3: detail 参数
print("\n[测试 3] 带 detail 参数")
payload3 = {
"model": AI_MODEL,
"messages": [
{"role": "user", "content": [
{"type": "text", "text": "请描述这张图片中的文字内容,用中文回答。"},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}", "detail": "high"}},
]},
],
"max_tokens": 200,
}
try:
resp = requests.post(url, headers=headers, json=payload3, timeout=AI_TIMEOUT)
resp.raise_for_status()
result = resp.json()
content = result["choices"][0]["message"]["content"]
print(f"✅ 回复: {content[:200]}")
except Exception as e:
print(f"❌ 失败: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f" 响应: {e.response.text[:300]}")
print("\n测试完成。")