"""手工视觉 API 联调;自动测试导入本模块时不会请求真实模型。""" import unittest def main() -> None: import base64 import os import sys import requests 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 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) 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", } print(f"图片大小: {len(image_bytes)} bytes") print(f"Base64 长度: {len(encoded)} 字符") print(f"API URL: {url}") print(f"模型: {AI_MODEL}") print("-" * 50) 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]}") 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()