From 2385893893fc041ec0694757ce9b45578f8cd8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?7934-=E6=96=B9=E9=9C=B2=E5=AE=87?= Date: Mon, 24 Feb 2025 17:38:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E7=81=AB?= =?UTF-8?q?=E5=B1=B1=E5=BC=95=E6=93=8E=E7=9A=84=E9=80=89=E6=8B=A9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- llm_adapters.py | 34 ++++++++++++++++++++++++++++++++++ ui/config_tab.py | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/llm_adapters.py b/llm_adapters.py index f919062..ba65947 100644 --- a/llm_adapters.py +++ b/llm_adapters.py @@ -8,6 +8,8 @@ from google.genai import types from azure.ai.inference import ChatCompletionsClient from azure.core.credentials import AzureKeyCredential from azure.ai.inference.models import SystemMessage, UserMessage +from openai import OpenAI + def check_base_url(url: str) -> str: """ @@ -266,6 +268,36 @@ class AzureAIAdapter(BaseLLMAdapter): logging.error(f"Azure AI Inference API 调用失败: {e}") return "" +# 火山引擎实现 +class VolcanoEngineAIAdapter(BaseLLMAdapter): + def __init__(self, api_key: str, base_url: str, model_name: str, max_tokens: int, temperature: float = 0.7, timeout: Optional[int] = 600): + self.base_url = check_base_url(base_url) + self.api_key = api_key + self.model_name = model_name + self.max_tokens = max_tokens + self.temperature = temperature + self.timeout = timeout + + self._client = OpenAI( + # 此为默认路径,您可根据业务所在地域进行配置 + base_url="https://ark.cn-beijing.volces.com/api/v3/bots", + # 从环境变量中获取您的 API Key + api_key=api_key + ) + def invoke(self, prompt: str) -> str: + response = self._client.chat.completions.create( + model=self.model_name, # bot-20250223190248-2bq5k 为您当前的智能体的ID,注意此处与Chat API存在差异。差异对比详见 SDK使用指南 + messages=[ + {"role": "system", "content": "你是DeepSeek,是一个 AI 人工智能助手"}, + {"role": "user", "content": prompt}, + ], + ) + # response = self._client.invoke(prompt) + if not response: + logging.warning("No response from DeepSeekAdapter.") + return "" + return response.choices[0].message.content + def create_llm_adapter( interface_format: str, base_url: str, @@ -296,5 +328,7 @@ def create_llm_adapter( return GeminiAdapter(api_key, model_name, max_tokens, temperature, timeout) elif fmt == "阿里云百炼": return OpenAIAdapter(api_key, base_url, model_name, max_tokens, temperature, timeout) + elif fmt == "火山引擎": + return VolcanoEngineAIAdapter(api_key, base_url, model_name, max_tokens, temperature, timeout) else: raise ValueError(f"Unknown interface_format: {interface_format}") diff --git a/ui/config_tab.py b/ui/config_tab.py index 3f3200a..1c51539 100644 --- a/ui/config_tab.py +++ b/ui/config_tab.py @@ -108,7 +108,7 @@ def build_ai_config_tab(self): # 3) 接口格式 create_label_with_help(self, parent=self.ai_config_tab, label_text="LLM 接口格式:", tooltip_key="interface_format", row=2, column=0, font=("Microsoft YaHei", 12)) - interface_options = ["DeepSeek", "阿里云百炼", "OpenAI", "Azure OpenAI", "Azure AI", "Ollama", "ML Studio", "Gemini"] + interface_options = ["DeepSeek", "阿里云百炼", "OpenAI", "Azure OpenAI", "Azure AI", "Ollama", "ML Studio", "Gemini","火山引擎"] interface_dropdown = ctk.CTkOptionMenu(self.ai_config_tab, values=interface_options, variable=self.interface_format_var, command=on_interface_format_changed, font=("Microsoft YaHei", 12)) interface_dropdown.grid(row=2, column=1, padx=5, pady=5, columnspan=2, sticky="nsew") From fa29b3b7b9773a56cd22ced410ab4675860b8dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?7934-=E6=96=B9=E9=9C=B2=E5=AE=87?= Date: Tue, 25 Feb 2025 09:15:09 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=86=99?= =?UTF-8?q?=E6=AD=BB=E7=81=AB=E5=B1=B1=E5=BC=95=E6=93=8E=E7=9A=84url?= =?UTF-8?q?=E7=9A=84bug=EF=BC=8C=E7=81=AB=E5=B1=B1=E5=BC=95=E6=93=8E?= =?UTF-8?q?=E7=9A=84=E6=A8=A1=E5=9E=8B=E5=A6=82=E6=9E=9C=E5=8A=A0=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=9A=84=E8=AF=9D=EF=BC=8Curl=E6=98=AF=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- llm_adapters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llm_adapters.py b/llm_adapters.py index ba65947..30ff1e6 100644 --- a/llm_adapters.py +++ b/llm_adapters.py @@ -280,7 +280,7 @@ class VolcanoEngineAIAdapter(BaseLLMAdapter): self._client = OpenAI( # 此为默认路径,您可根据业务所在地域进行配置 - base_url="https://ark.cn-beijing.volces.com/api/v3/bots", + base_url=base_url, # 从环境变量中获取您的 API Key api_key=api_key ) From 954f5f8ad7aec7e99e22848ec223dd031e7a435a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?7934-=E6=96=B9=E9=9C=B2=E5=AE=87?= Date: Tue, 25 Feb 2025 15:18:54 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AEtab?= =?UTF-8?q?=EF=BC=8C=E5=B0=86apiKey=E9=9A=90=E8=97=8F=E3=80=82=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=9B=A0=E4=B8=BA=E6=88=AA=E5=9B=BE=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=B8=8D=E5=B0=8F=E5=BF=83=E5=B0=B1=E6=B3=84=E9=9C=B2?= =?UTF-8?q?=E4=BA=86apiKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/config_tab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/config_tab.py b/ui/config_tab.py index 1c51539..0ca6f69 100644 --- a/ui/config_tab.py +++ b/ui/config_tab.py @@ -98,7 +98,7 @@ def build_ai_config_tab(self): # 1) API Key create_label_with_help(self, parent=self.ai_config_tab, label_text="LLM API Key:", tooltip_key="api_key", row=0, column=0, font=("Microsoft YaHei", 12)) - api_key_entry = ctk.CTkEntry(self.ai_config_tab, textvariable=self.api_key_var, font=("Microsoft YaHei", 12)) + api_key_entry = ctk.CTkEntry(self.ai_config_tab, textvariable=self.api_key_var, font=("Microsoft YaHei", 12),show="*") api_key_entry.grid(row=0, column=1, padx=5, pady=5, columnspan=2, sticky="nsew") # 2) Base URL