log
2026-06-25 7c4c956be0c3c4c2f76f181e356779003bc8f998
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
"""
OpenAI GPT 多模态大模型 API 客户端
支持 gpt-4o, gpt-5.4 (未来版本) 等视觉模型
 
代理由 provider 的 enable_proxy 决定,通过 httpx.Client 注入到 OpenAI SDK。
"""
 
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 time
import base64
import httpx
import logging
from openai import OpenAI
from typing import Dict, List, Optional
from config import Config
 
logger = logging.getLogger(__name__)
 
 
class GPTVLClient:
    """
    GPT VLM 客户端,支持多模态对话
    """
    def __init__(self,
                 api_key: Optional[str] = None,
                 base_url: Optional[str] = None,
                 proxy: Optional[str] = None,
                 timeout: float = 60.0):
        """
        GPT 多模态客户端
        :param api_key: OpenAI API Key
        :param base_url: 自定义 Base URL(如果传入,则不使用本地代理)
        :param timeout: 超时时间
        """
        # 优先使用传入的 api_key,否则从 config.yaml 读取
        self.api_key = api_key or Config.OPENAI_API_KEY
        self.timeout = timeout
        
        kwargs = {"api_key": self.api_key, "timeout": self.timeout}
        
        self.base_url = base_url
        if proxy is None:
            proxy = Config.provider_proxy("openai")
        if proxy:
            kwargs["http_client"] = httpx.Client(
                proxy=proxy,
                timeout=self.timeout,
            )
        if self.base_url:
            kwargs["base_url"] = self.base_url
        self.client = OpenAI(**kwargs)
        self.max_attempts = 5
        self.max_tokens = 4096
 
    def _encode_image(self, image_path: str) -> str:
        """将本地图片编码为 base64"""
        abs_path = os.path.abspath(image_path)
        with open(abs_path, "rb") as f:
            return base64.b64encode(f.read()).decode("utf-8")
 
    def _get_mime_type(self, image_path: str) -> str:
        """根据文件扩展名获取 MIME 类型"""
        ext = os.path.splitext(image_path)[1].lower()
        mime_types = {
            ".jpg": "image/jpeg",
            ".jpeg": "image/jpeg",
            ".png": "image/png",
            ".webp": "image/webp",
            ".gif": "image/gif"
        }
        return mime_types.get(ext, "image/jpeg")
 
    def chat(self, text: str, images: List[str], model: str = "gpt-5.4",
             parameters: Optional[Dict] = None) -> str:
        """
        使用 GPT 进行多模态对话(文本+图片)
        :param text: 文本内容
        :param images: 图片路径列表(支持本地路径或URL)
        :param model: 模型名(如 gpt-4o, gpt-5.4)
        :param parameters: 其他API参数
        :return: API响应内容
        """
        if not self.api_key:
            raise ValueError("OPENAI_API_KEY 未设置")
 
        # 构建消息格式
        content: list = [{"type": "text", "text": text}]
 
        # 处理图片
        if images:
            for img_path in images:
                if img_path.startswith("data:"):
                    # Base64 数据 URL
                    content.append({
                        "type": "image_url",
                        "image_url": {"url": img_path}
                    })
                elif img_path.startswith("http"):
                    # URL 图片
                    content.append({
                        "type": "image_url",
                        "image_url": {"url": img_path}
                    })
                else:
                    # 本地图片 - 转为 base64
                    mime_type = self._get_mime_type(img_path)
                    base64_data = self._encode_image(img_path)
                    data_url = f"data:{mime_type};base64,{base64_data}"
                    content.append({
                        "type": "image_url",
                        "image_url": {"url": data_url}
                    })
 
        messages = [{"role": "user", "content": content}]
 
        attempts = 0
        while attempts < self.max_attempts:
            try:
                response = self.client.chat.completions.create(
                    model=model,
                    messages=messages,
                    max_tokens=self.max_tokens,
                    temperature=parameters.get("temperature", 0.7) if parameters else 0.7
                )
 
                if response.choices and len(response.choices) > 0:
                    return response.choices[0].message.content
 
            except Exception as e:
                logger.warning("GPT VLM request failed: %s", e)
                attempts += 1
                if attempts < self.max_attempts:
                    time.sleep(2 * attempts)  # 指数退避
 
        raise Exception("GPTVL: 达到最大重试次数,仍未获得有效响应。")
 
 
if __name__ == "__main__":
    import sys
    sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    from config import Config
 
    print("=== GPT VL 多模态可用性测试 ===")
    if not Config.OPENAI_API_KEY:
        print("✗ 未设置 OPENAI_API_KEY,跳过")
        sys.exit(1)
    client = GPTVLClient(api_key=Config.OPENAI_API_KEY, base_url=Config.OPENAI_BASE_URL, proxy=Config.provider_proxy("openai"))
    
    # 支持的 VLM 模型列表,包含用户要求的 gpt-5.4
    MODELS = ["gpt-5.4"]
    
    img_path = "code/result/image/test_avail/test_input.png"
    if not os.path.exists(img_path):
        print(f"✗ 测试图片不存在: {img_path}")
    else:
        text = "请描述这张图片的内容"
        for model in MODELS:
            print(f"\n--- 测试模型: {model} ---")
            print(f"输入文本: {text}")
            print(f"输入图片: {img_path}")
            t0 = time.time()
            try:
                result = client.chat(text=text, images=[img_path], model=model)
                elapsed = time.time() - t0
                if result:
                    print(f"✓ 返回结果 ({elapsed:.1f}s): {result[:200]}")
                else:
                    print(f"✗ 返回空结果 ({elapsed:.1f}s)")
            except Exception as e:
                print(f"✗ 失败: {e}")