更新bug
This commit is contained in:
+85
-102
@@ -1,112 +1,95 @@
|
||||
"""
|
||||
测试视觉模式 API 调用,使用 debug_chat_area.png 作为测试图片。
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import base64
|
||||
import json
|
||||
import requests
|
||||
"""手工视觉 API 联调;自动测试导入本模块时不会请求真实模型。"""
|
||||
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
from ai_config import AI_API_BASE, AI_API_KEY, AI_MODEL, AI_TIMEOUT
|
||||
import unittest
|
||||
|
||||
# 读取调试截图
|
||||
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()
|
||||
def main() -> None:
|
||||
import base64
|
||||
import os
|
||||
import sys
|
||||
|
||||
print(f"图片大小: {len(img_bytes)} bytes")
|
||||
b64 = base64.b64encode(img_bytes).decode("utf-8")
|
||||
print(f"Base64 长度: {len(b64)} 字符")
|
||||
import requests
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {AI_API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
from ai_config import AI_API_KEY, AI_MODEL, AI_TIMEOUT
|
||||
from ai_chat import _completions_url
|
||||
|
||||
from ai_chat import _completions_url
|
||||
url = _completions_url()
|
||||
print(f"API URL: {url}")
|
||||
print(f"模型: {AI_MODEL}")
|
||||
print("-" * 50)
|
||||
img_path = os.path.join(os.path.dirname(__file__), "debug_chat_area.png")
|
||||
if not os.path.exists(img_path):
|
||||
print(f"❌ 找不到测试图片: {img_path}")
|
||||
raise SystemExit(1)
|
||||
|
||||
# 方式 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]}")
|
||||
with open(img_path, "rb") as handle:
|
||||
image_bytes = handle.read()
|
||||
encoded = base64.b64encode(image_bytes).decode("utf-8")
|
||||
url = _completions_url()
|
||||
headers = {
|
||||
"Authorization": f"Bearer {AI_API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
# 方式 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]}")
|
||||
print(f"图片大小: {len(image_bytes)} bytes")
|
||||
print(f"Base64 长度: {len(encoded)} 字符")
|
||||
print(f"API URL: {url}")
|
||||
print(f"模型: {AI_MODEL}")
|
||||
print("-" * 50)
|
||||
|
||||
# 方式 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]}")
|
||||
def request_case(title: str, image_url) -> None:
|
||||
print(f"\n[{title}]")
|
||||
payload = {
|
||||
"model": AI_MODEL,
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": (
|
||||
"这是企业微信聊天消息区域截图。请只依据截图中最末端的客户消息,"
|
||||
"判断是否有新的客户消息并简短回复;不要把我方旧回复当作客户消息。"
|
||||
),
|
||||
},
|
||||
{"type": "image_url", "image_url": image_url},
|
||||
],
|
||||
}
|
||||
],
|
||||
"max_tokens": 200,
|
||||
}
|
||||
try:
|
||||
response = requests.post(
|
||||
url,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
timeout=AI_TIMEOUT,
|
||||
)
|
||||
response.raise_for_status()
|
||||
content = response.json()["choices"][0]["message"]["content"]
|
||||
print(f"✅ 回复: {content[:200]}")
|
||||
except Exception as exc:
|
||||
print(f"❌ 失败: {exc}")
|
||||
response = getattr(exc, "response", None)
|
||||
if response is not None:
|
||||
print(f" 响应: {response.text[:300]}")
|
||||
|
||||
print("\n测试完成。")
|
||||
request_case(
|
||||
"测试 1:标准 OpenAI data URI",
|
||||
{"url": f"data:image/png;base64,{encoded}"},
|
||||
)
|
||||
request_case(
|
||||
"测试 2:纯 base64(兼容性探测)",
|
||||
{"url": encoded},
|
||||
)
|
||||
request_case(
|
||||
"测试 3:data URI + detail=high",
|
||||
{"url": f"data:image/png;base64,{encoded}", "detail": "high"},
|
||||
)
|
||||
print("\n测试完成。")
|
||||
|
||||
|
||||
class ManualVisionIsolationTest(unittest.TestCase):
|
||||
def test_manual_entrypoint_is_import_safe(self) -> None:
|
||||
self.assertTrue(callable(main))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user