From e965b3c9af3b46e4794321fd7e870c2908578e5d Mon Sep 17 00:00:00 2001 From: Karasukaigan <80465610+Karasukaigan@users.noreply.github.com> Date: Mon, 17 Feb 2025 23:09:28 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9C=AC=E5=9C=B0Ollama?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=BD=BF=E7=94=A8=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户在使用本地Ollama接口时无需再填写API Key,API Key字段可以留空。 --- ui/generation_handlers.py | 15 +++++++++++++++ ui/main_window.py | 3 +++ 2 files changed, 18 insertions(+) diff --git a/ui/generation_handlers.py b/ui/generation_handlers.py index 681e2e9..944fe3a 100644 --- a/ui/generation_handlers.py +++ b/ui/generation_handlers.py @@ -35,6 +35,9 @@ def generate_novel_architecture_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) + if interface_format == 'Ollama' and api_key == '': + api_key = 'ollama' + topic = self.topic_text.get("0.0", "end").strip() genre = self.genre_var.get().strip() num_chapters = self.safe_get_int(self.num_chapters_var, 10) @@ -80,6 +83,9 @@ def generate_chapter_blueprint_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) + if interface_format == 'Ollama' and api_key == '': + api_key = 'ollama' + self.safe_log("开始生成章节蓝图...") Chapter_blueprint_generate( interface_format=interface_format, @@ -116,6 +122,9 @@ def generate_chapter_draft_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) + if interface_format == 'Ollama' and api_key == '': + api_key = 'ollama' + chap_num = self.safe_get_int(self.chapter_num_var, 1) word_number = self.safe_get_int(self.word_number_var, 3000) user_guidance = self.user_guide_text.get("0.0", "end").strip() @@ -246,6 +255,9 @@ def finalize_chapter_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) + if interface_format == 'Ollama' and api_key == '': + api_key = 'ollama' + embedding_api_key = self.embedding_api_key_var.get().strip() embedding_url = self.embedding_url_var.get().strip() embedding_interface_format = self.embedding_interface_format_var.get().strip() @@ -326,6 +338,9 @@ def do_consistency_check(self): max_tokens = self.max_tokens_var.get() timeout = self.timeout_var.get() + if interface_format == 'Ollama' and api_key == '': + api_key = 'ollama' + chap_num = self.safe_get_int(self.chapter_num_var, 1) chap_file = os.path.join(filepath, "chapters", f"chapter_{chap_num}.txt") chapter_text = read_file(chap_file) diff --git a/ui/main_window.py b/ui/main_window.py index 382223e..ca7f3e6 100644 --- a/ui/main_window.py +++ b/ui/main_window.py @@ -187,6 +187,9 @@ class NovelGeneratorGUI: max_tokens = self.max_tokens_var.get() timeout = self.timeout_var.get() + if interface_format == 'Ollama' and api_key == '': + api_key = 'ollama' + test_llm_config( interface_format=interface_format, api_key=api_key, From 0a24217eb533dccfdd3ff01933ec800c26c53707 Mon Sep 17 00:00:00 2001 From: Karasukaigan <80465610+Karasukaigan@users.noreply.github.com> Date: Tue, 18 Feb 2025 00:46:31 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E9=98=BF?= =?UTF-8?q?=E9=87=8C=E4=BA=91=E7=99=BE=E7=82=BC=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了对阿里云百炼的支持。接口格式里增加了“阿里云百炼”选项,并设置默认模型为“qwen-plus”。已经过测试,可以使用。 此外,还添加了DeepSeek的默认模型“deepseek-chat”和OpenAI的默认模型“gpt-4o-mini”。 --- llm_adapters.py | 2 ++ ui/config_tab.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/llm_adapters.py b/llm_adapters.py index 52012ea..c771bb2 100644 --- a/llm_adapters.py +++ b/llm_adapters.py @@ -291,5 +291,7 @@ def create_llm_adapter( elif fmt == "gemini": # base_url 对 Gemini 暂无用处,可忽略 return GeminiAdapter(api_key, model_name, max_tokens, temperature, timeout) + elif fmt == "阿里云百炼": + return OpenAIAdapter(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 3d724ac..3f3200a 100644 --- a/ui/config_tab.py +++ b/ui/config_tab.py @@ -76,14 +76,19 @@ def build_ai_config_tab(self): self.base_url_var.set("http://localhost:1234/v1") elif new_value == "OpenAI": self.base_url_var.set("https://api.openai.com/v1") + self.model_name_var.set("gpt-4o-mini") elif new_value == "Azure OpenAI": self.base_url_var.set("https://[az].openai.azure.com/openai/deployments/[model]/chat/completions?api-version=2024-08-01-preview") elif new_value == "DeepSeek": self.base_url_var.set("https://api.deepseek.com/v1") + self.model_name_var.set("deepseek-chat") elif new_value == "Gemini": self.base_url_var.set("") elif new_value == "Azure AI": self.base_url_var.set("https://.services.ai.azure.com/models/chat/completions?api-version=2024-05-01-preview") + elif new_value == "阿里云百炼": + self.base_url_var.set("https://dashscope.aliyuncs.com/compatible-mode/v1") + self.model_name_var.set("qwen-plus") for i in range(7): self.ai_config_tab.grid_rowconfigure(i, weight=0) @@ -103,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 fd7f97b0e82445e55d5a0847090c0fc5897d30eb Mon Sep 17 00:00:00 2001 From: Karasukaigan <80465610+Karasukaigan@users.noreply.github.com> Date: Wed, 19 Feb 2025 14:42:37 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9Ollama=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在调用本地Ollama接口时,将api_key默认值设置为“ollama”。 --- llm_adapters.py | 3 +++ ui/generation_handlers.py | 15 --------------- ui/main_window.py | 3 --- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/llm_adapters.py b/llm_adapters.py index c771bb2..f919062 100644 --- a/llm_adapters.py +++ b/llm_adapters.py @@ -172,6 +172,9 @@ class OllamaAdapter(BaseLLMAdapter): self.temperature = temperature self.timeout = timeout + if self.api_key == '': + self.api_key= 'ollama' + self._client = ChatOpenAI( model=self.model_name, api_key=self.api_key, diff --git a/ui/generation_handlers.py b/ui/generation_handlers.py index 944fe3a..681e2e9 100644 --- a/ui/generation_handlers.py +++ b/ui/generation_handlers.py @@ -35,9 +35,6 @@ def generate_novel_architecture_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) - if interface_format == 'Ollama' and api_key == '': - api_key = 'ollama' - topic = self.topic_text.get("0.0", "end").strip() genre = self.genre_var.get().strip() num_chapters = self.safe_get_int(self.num_chapters_var, 10) @@ -83,9 +80,6 @@ def generate_chapter_blueprint_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) - if interface_format == 'Ollama' and api_key == '': - api_key = 'ollama' - self.safe_log("开始生成章节蓝图...") Chapter_blueprint_generate( interface_format=interface_format, @@ -122,9 +116,6 @@ def generate_chapter_draft_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) - if interface_format == 'Ollama' and api_key == '': - api_key = 'ollama' - chap_num = self.safe_get_int(self.chapter_num_var, 1) word_number = self.safe_get_int(self.word_number_var, 3000) user_guidance = self.user_guide_text.get("0.0", "end").strip() @@ -255,9 +246,6 @@ def finalize_chapter_ui(self): max_tokens = self.max_tokens_var.get() timeout_val = self.safe_get_int(self.timeout_var, 600) - if interface_format == 'Ollama' and api_key == '': - api_key = 'ollama' - embedding_api_key = self.embedding_api_key_var.get().strip() embedding_url = self.embedding_url_var.get().strip() embedding_interface_format = self.embedding_interface_format_var.get().strip() @@ -338,9 +326,6 @@ def do_consistency_check(self): max_tokens = self.max_tokens_var.get() timeout = self.timeout_var.get() - if interface_format == 'Ollama' and api_key == '': - api_key = 'ollama' - chap_num = self.safe_get_int(self.chapter_num_var, 1) chap_file = os.path.join(filepath, "chapters", f"chapter_{chap_num}.txt") chapter_text = read_file(chap_file) diff --git a/ui/main_window.py b/ui/main_window.py index ca7f3e6..382223e 100644 --- a/ui/main_window.py +++ b/ui/main_window.py @@ -187,9 +187,6 @@ class NovelGeneratorGUI: max_tokens = self.max_tokens_var.get() timeout = self.timeout_var.get() - if interface_format == 'Ollama' and api_key == '': - api_key = 'ollama' - test_llm_config( interface_format=interface_format, api_key=api_key,