hit-cxf
2026-04-02 a64ae95eb8235c8b9a6d299cc842ea6b9e1a7ace
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
import os
import logging
 
try:
    from tool.llm_gpt import GPT
    from tool.llm_gemini import Gemini
    from tool.llm_deepseek import DeepSeek
    from tool.llm_dashscope import QwenLLM
except ImportError:
    from llm_gpt import GPT
    from llm_gemini import Gemini
    from llm_deepseek import DeepSeek
    from llm_dashscope import QwenLLM
 
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 os.getenv("GOOGLE_GEMINI_BASE_URL", "")
        self.gemini_api_key = gemini_api_key or os.getenv("GEMINI_API_KEY", "")
        self.gpt_base_url = gpt_base_url or os.getenv("OPENAI_BASE_URL", "")
        self.gpt_api_key = gpt_api_key or os.getenv("OPENAI_API_KEY", "")
        self.deepseek_base_url = deepseek_base_url or os.getenv("DEEPSEEK_BASE_URL", "")
        self.deepseek_api_key = deepseek_api_key or os.getenv("DEEPSEEK_API_KEY", "")
        self.dashscope_api_key = dashscope_api_key or os.getenv("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="gemini-3-flash-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 = "gemini-3-flash-preview"
            
        if Config.PRINT_MODEL_INPUT:
            print("---- LLM QUERY REQUEST ----")
            print(f"Model: {model}")
            if task_id:
                print(f"Task ID: {task_id}")
            if image_urls:
                print(f"Images: {len(image_urls)}")
                for u in image_urls:
                    print(f"  - {u}")
            print(f"Prompt: {prompt[:200]}{'...' if len(prompt) > 200 else ''}")
            print("-" * 30)
            
        result = ""
        model_lower = model.lower()
        if model_lower.startswith("gemini"):
            # Gemini client handles its own credentials internally in the current implementation,
            # but we pass args for consistency/future compatibility.
            # Note: Gemini doesn't have built-in web search parameter, user needs to use Function Calling
            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 model_lower.startswith("deepseek"):
            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)
        elif "qwen" in model_lower:
            # Qwen models via DashScope Generation API (text-only mode)
            client = QwenLLM(api_key=self.dashscope_api_key)
            result = client.query(prompt, image_urls=image_urls, model=model, web_search=web_search)
        else:
            # OpenAI 系列: gpt-4, gpt-4o, gpt-5, gpt-5.1, o3 等
            client = GPT(base_url=self.gpt_base_url, api_key=self.gpt_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() != ''])