hit-cxf
2026-06-02 bb0986daef8db477d90c598ffefe1890715e28f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import sys
 
models_dir = os.path.dirname(os.path.abspath(__file__))
backend_dir = os.path.dirname(models_dir)
if backend_dir not in sys.path:
    sys.path.insert(0, backend_dir)
 
import logging
import os
from typing import List, Optional
from config import Config
 
try:
    from models.vlm_dashscope import QwenVLClient
    from models.vlm_gemini import GeminiVLClient
    from models.vlm_gpt import GPTVLClient
except ImportError:
    from vlm_dashscope import QwenVLClient
    from vlm_gemini import GeminiVLClient
    from vlm_gpt import GPTVLClient
 
logger = logging.getLogger(__name__)
 
 
class VLM:
    def __init__(self,
                 dashscope_api_key: Optional[str] = None,
                 dashscope_base_url: Optional[str] = None,
                 gemini_api_key: Optional[str] = None,
                 gemini_base_url: Optional[str] = None,
                 gpt_api_key: Optional[str] = None,
                 gpt_base_url: Optional[str] = None,
                 proxy: Optional[str] = None):
        """
        Unified VLM (Vision Language Model) Client
        Routes requests to DashScope (QwenVL) or Gemini based on model name.
        """
        # Initialize DashScope Client
        self.dashscope_client = QwenVLClient(
            api_key=dashscope_api_key,
            base_url=dashscope_base_url
        )
        # Initialize Gemini Client
        self.gemini_client = GeminiVLClient(
            api_key=gemini_api_key,
            base_url=gemini_base_url
        )
        # Initialize GPT Client
        self.gpt_client = GPTVLClient(
            api_key=gpt_api_key,
            base_url=gpt_base_url,
            proxy=Config.provider_proxy("openai") if proxy is None else proxy
        )
 
    def query(self,
             prompt: str,
             image_paths: Optional[List[str]] = None,
             model: str = "qwen3.6-plus",
             session_id: Optional[str] = None) -> str:
        if Config.PRINT_MODEL_INPUT:
            lines = [
                "---- VLM REQUEST ----",
                f"Prompt: {prompt}",
            ]
            if image_paths:
                lines.append(f"Images: {len(image_paths)}")
                for p in image_paths:
                    lines.append(" - [Base64图片]" if p.startswith("data:") else f" - {p}")
            lines.append(f"Model: {model}")
            if session_id:
                lines.append(f"Session ID: {session_id}")
            lines.append("-" * 30)
            logger.info("\n%s", "\n".join(lines))
 
        # Determine backend provider
        model_lower = model.lower()
        is_gemini = "gemini" in model_lower
        is_gpt = "gpt" in model_lower
 
        if is_gemini:
            # 处理图片路径
            processed_images = []
            for p in image_paths or []:
                if p.startswith("data:") or p.startswith("http") or p.startswith("file://"):
                    processed_images.append(p)
                else:
                    processed_images.append(p)  # 传递原始路径,内部会处理
            return self.gemini_client.chat(text=prompt, images=processed_images, model=model)
        elif is_gpt:
            return self.gpt_client.chat(text=prompt, images=image_paths or [], model=model)
        else:
            # DashScope (Qwen/Kimi) - 需要将 base64 保存为临时文件
            file_urls = []
            import tempfile
            import base64 as b64
 
            for p in image_paths or []:
                if p.startswith("data:"):
                    # Base64 数据 URL,需要解码并保存为临时文件
                    try:
                        # 解析 data URL: data:image/png;base64,xxxxx
                        header, b64_data = p.split(",", 1)
                        mime_type = header.split(";")[0].replace("data:", "")
                        image_data = b64.b64decode(b64_data)
 
                        # 创建临时文件
                        suffix = f".{mime_type.split('/')[-1]}" if '/' in mime_type else ".png"
                        with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
                            tmp.write(image_data)
                            temp_path = tmp.name
 
                        abs_path = os.path.abspath(temp_path)
                        file_urls.append(f"file://{abs_path}")
                    except Exception as e:
                        logger.exception("Failed to process base64 image")
                        raise ValueError(f"无法解析 base64 图片: {e}")
                elif p.startswith("http") or p.startswith("file://"):
                    file_urls.append(p)
                else:
                    abs_path = os.path.abspath(p)
                    file_urls.append(f"file://{abs_path}")
            return self.dashscope_client.chat(text=prompt, images=file_urls, model=model, stream=False)