hit-cxf
2026-05-14 cc5582b71914036e24f8e75c964e320fe64e84be
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
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
 
try:
    from models.llm_gpt import GPT
    from models.llm_gemini import Gemini
    from models.llm_deepseek import DeepSeek
    from models.llm_dashscope import QwenLLM
    from models.vlm_dashscope import QwenVLClient
except ImportError:
    from llm_gpt import GPT
    from llm_gemini import Gemini
    from llm_deepseek import DeepSeek
    from llm_dashscope import QwenLLM
    from vlm_dashscope import QwenVLClient
 
from config import Config
 
logger = logging.getLogger(__name__)
 
class LLM:
    def __init__(self, gemini_base_url="", gemini_api_key="", gpt_base_url="", gpt_api_key="", deepseek_base_url="", deepseek_api_key="", dashscope_api_key=""):
        self.gemini_base_url = gemini_base_url or Config.GOOGLE_GEMINI_BASE_URL
        self.gemini_api_key = gemini_api_key or Config.GEMINI_API_KEY
        self.gpt_base_url = gpt_base_url or Config.OPENAI_BASE_URL
        self.gpt_api_key = gpt_api_key or Config.OPENAI_API_KEY
        self.deepseek_base_url = deepseek_base_url or Config.DEEPSEEK_BASE_URL
        self.deepseek_api_key = deepseek_api_key or Config.DEEPSEEK_API_KEY
        self.dashscope_api_key = dashscope_api_key or Config.DASHSCOPE_API_KEY
 
    def full_to_half(self, text):
        if not isinstance(text, str):
            return text
        
        translation_table = {0x3000: 0x0020}
        for i in range(65281, 65375):
            translation_table[i] = i - 65248
            
        return text.translate(translation_table)
 
    def query(self, prompt, image_urls=[], model="qwen3.6-max-preview", safe_content=True, task_id=None, web_search=False):
        """
        Query the LLM with a prompt and optional image URLs.
        Selects the backend (GPT or Gemini) based on the model name.
 
        :param web_search: Enable web search for supported providers
        """
        if safe_content:
            prompt = self.full_to_half(prompt)
 
        if not model:
            model = "qwen3.6-max-preview"
            
        if Config.PRINT_MODEL_INPUT:
            lines = [
                "---- LLM QUERY REQUEST ----",
                f"Model: {model}",
            ]
            if task_id:
                lines.append(f"Task ID: {task_id}")
            if image_urls:
                lines.append(f"Images: {len(image_urls)}")
                lines.extend(f"  - {u}" for u in image_urls)
            lines.extend([
                f"Web Search: {web_search}",
                f"Prompt: {prompt[:200]}{'...' if len(prompt) > 200 else ''}",
                "-" * 30,
            ])
            logger.info("\n%s", "\n".join(lines))
            
        result = ""
        model_lower = model.lower()
        if model_lower.startswith("gemini"):
            client = Gemini(base_url=self.gemini_base_url, api_key=self.gemini_api_key)
            result = client.query(prompt, image_urls=image_urls, model=model)
        elif "gpt" in model_lower:
            # OpenAI series models
            client = GPT(
                base_url=self.gpt_base_url, 
                api_key=self.gpt_api_key, 
                proxy=Config.provider_proxy("openai"),
            )
            result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
        elif "kimi" in model_lower or "qwen3.6-plus" in model_lower or "qwen3.6-flash" in model_lower:
            # DashScope VLM models (using MultiModalConversation API)
            dashscope_vl_client = QwenVLClient(api_key=self.dashscope_api_key)
            result = dashscope_vl_client.chat(text=prompt, images=[], model=model, stream=False)
        elif "deepseek-v3.2" in model_lower:
            # DeepSeek v3.2 (通过 DashScope Generation API)
            client = QwenLLM(api_key=self.dashscope_api_key)
            result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
        elif model_lower.startswith("deepseek") and "v3.2" not in model_lower:
            # Original DeepSeek provider
            client = DeepSeek(base_url=self.deepseek_base_url, api_key=self.deepseek_api_key)
            result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
        else:
            # Default to Qwen models / deepseek-v3.2 via DashScope Generation API
            client = QwenLLM(api_key=self.dashscope_api_key)
            result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
 
        if safe_content:
            result = self.full_to_half(result)
        
        # Remove empty lines
        return '\n'.join([line for line in result.split('\n') if line.strip() != ''])