log
2026-06-22 2ffbfdcef91bd428c862186ef9fa62e890a86c49
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
# -*- coding: utf-8 -*-
"""
Qwen LLM API 客户端(DashScope Generation API)
支持 qwen3.7-max、qwen3.6-max-preview、qwen3-max 等文本生成模型
"""
 
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 logging
from typing import Optional
from config import Config
 
logger = logging.getLogger(__name__)
 
try:
    import dashscope
    from dashscope import Generation
except ImportError:
    dashscope = None
    Generation = None
 
 
class QwenLLM:
    """
    Qwen LLM 客户端,使用 DashScope Generation API
    支持纯文本生成(可作为 LLM 使用)
    """
    def __init__(self, api_key: Optional[str] = None, base_url: Optional[str] = None):
        """
        :param api_key: DashScope API Key
        :param base_url: DashScope API Base URL (可选)
        """
        self.api_key = api_key or Config.DASHSCOPE_API_KEY
        self.base_url = base_url or Config.DASHSCOPE_BASE_URL
 
        if not self.api_key:
            logger.warning("DASHSCOPE_API_KEY is not set")
 
        if dashscope:
            dashscope.api_key = self.api_key
            # Do not override base_url to avoid "url error" if config contains wrong path
            # if self.base_url:
            #     dashscope.base_http_api_url = self.base_url
 
        self.max_attempts = 3
        self.max_tokens = 8000
 
    def query(self, prompt: str, image_urls: list = None, model: str = "qwen-max", web_search: bool = False):
        """
        Query Qwen model for text generation.
        Note: This is for text-only LLM use. For image+text, use VLM client.
 
        :param prompt: Text prompt
        :param image_urls: Ignored in this LLM implementation (use VLM for multimodal)
        :param model: Model name (e.g., qwen3.7-max, qwen3.6-max-preview, qwen3-max)
        :param web_search: If True, adds enable_search: True to API call
        """
        if dashscope is None:
            raise RuntimeError("dashscope package not installed. Run: pip install dashscope")
 
        if not model or "qwen3.5" in model:
            # 兼容处理遗留的 qwen3.5 传参,避免 url error
            if "max" in model:
                model = "qwen-max"
            elif "turbo" in model:
                model = "qwen-turbo"
            else:
                model = "qwen-plus"
 
 
        messages = [{"role": "system", "content": "You are a helpful assistant."}]
        messages.append({"role": "user", "content": prompt})
 
        attempts = 0
        while attempts < self.max_attempts:
            try:
                # Build request parameters
                request_params = {
                    "model": model,
                    "messages": messages,
                    "result_format": "message",
                    "stream": False,
                    "max_tokens": self.max_tokens
                }
                # Add web search if enabled
                if web_search:
                    request_params["enable_search"] = True
 
                response = Generation.call(api_key=self.api_key, **request_params)
 
                if response.status_code == 200:
                    choice = response.output.choices[0]
                    if choice.message.content:
                        return choice.message.content
                    elif hasattr(choice.message, 'reasoning_content') and choice.message.reasoning_content:
                        return choice.message.reasoning_content
                    else:
                        logger.warning("Qwen returned an empty response; retrying")
                        time.sleep(2)
                else:
                    error_msg = f"Qwen API error: {response.code} - {response.message}"
                    logger.error(error_msg)
                    raise RuntimeError(error_msg)
 
            except Exception as e:
                logger.error(f"Error occurred with Qwen: {e}. Retrying.")
                time.sleep(5)
 
            attempts += 1
 
        raise Exception("Max attempts reached, failed to get a response from Qwen.")
 
 
if __name__ == "__main__":
    import sys
    sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    from config import Config
 
    # 支持的模型列表
 
    MODELS = ["qwen3.7-max", "qwen3.6-max-preview", "qwen3-max", "deepseek-v3.2"]
 
    print("=== DashScope LLM 可用性测试 ===")
    api_key = Config.DASHSCOPE_API_KEY
    if not api_key:
        print("✗ DASHSCOPE_API_KEY 未设置,跳过")
        sys.exit(1)
    print(f"  API Key: {api_key[:6]}***{api_key[-4:]}")
 
    client = QwenLLM(api_key=api_key)
    prompt = "用一句话介绍你自己。"
    print(f"  Prompt: {prompt}")
 
    for model in MODELS:
        print(f"\n--- 测试模型: {model} ---")
        t0 = time.time()
        try:
            resp = client.query(prompt, model=model)
            elapsed = time.time() - t0
            print(f"✓ 响应 ({elapsed:.1f}s): {resp.strip()[:200]}")
        except Exception as e:
            print(f"✗ 失败: {e}")