diff --git a/bot/dify/dify_bot.py b/bot/dify/dify_bot.py index fc0ed9a27..8c117a13a 100644 --- a/bot/dify/dify_bot.py +++ b/bot/dify/dify_bot.py @@ -94,7 +94,7 @@ def _handle_chatbot(self, query: str, session: DifySession, context: Context): chat_client = ChatClient(api_key, api_base) response_mode = 'blocking' payload = self._get_payload(query, session, response_mode) - files = self._get_upload_files(session) + files = self._get_upload_files(session, context) response = chat_client.create_chat_message( inputs=payload['inputs'], query=payload['query'], @@ -229,7 +229,7 @@ def _handle_agent(self, query: str, session: DifySession, context: Context): chat_client = ChatClient(api_key, api_base) response_mode = 'streaming' payload = self._get_payload(query, session, response_mode) - files = self._get_upload_files(session) + files = self._get_upload_files(session, context) response = chat_client.create_chat_message( inputs=payload['inputs'], query=payload['query'], @@ -314,15 +314,15 @@ def _handle_workflow(self, query: str, session: DifySession, context: Context): reply = Reply(ReplyType.TEXT, rsp_data['data']['outputs']['text']) return reply, None - def _get_upload_files(self, session: DifySession): + def _get_upload_files(self, session: DifySession, context: Context): session_id = session.get_session_id() img_cache = memory.USER_IMAGE_CACHE.get(session_id) - if not img_cache or not conf().get("image_recognition"): + if not img_cache or not self._get_dify_conf(context, "image_recognition", False): return None # 清理图片缓存 memory.USER_IMAGE_CACHE[session_id] = None - api_key = conf().get('dify_api_key', '') - api_base = conf().get("dify_api_base", "https://api.dify.ai/v1") + api_key = self._get_dify_conf(context, "dify_api_key", '') + api_base = self._get_dify_conf(context, "dify_api_base", "https://api.dify.ai/v1") dify_client = DifyClient(api_key, api_base) msg = img_cache.get("msg") path = img_cache.get("path") diff --git a/plugins/custom_dify_app/README.md b/plugins/custom_dify_app/README.md index 515eda7e0..8c311285f 100644 --- a/plugins/custom_dify_app/README.md +++ b/plugins/custom_dify_app/README.md @@ -24,6 +24,7 @@ config.json 配置说明 "api_base": "https://api.dify.ai/v1", # Dify API 基础URL "api_key": "app-xx", # Dify应用的API密钥 "use_on_single_chat": false, # 是否用于单聊,true/false + "image_recognition": false, # 是否启用图片识别,true/false "group_name_keywords": [ # 群名关键词列表,当群名包含其中任一关键词时,使用该配置 "测试群" ] @@ -35,5 +36,6 @@ config.json 配置说明 1. 插件会根据配置文件中的顺序逐一匹配群名关键词,匹配到第一个符合条件的配置就会停止并使用该配置。 2. 对于单聊,插件会使用配置文件中第一个设置了 `"use_on_single_chat": true` 的配置。 -3. 如果没有找到匹配的配置,插件不会修改上下文,将使用默认的 Dify 配置(如果有的话)。 -4. 确保在项目的主配置文件中正确设置了 Dify 相关的配置项,以便插件可以正常工作。 +3. `image_recognition` 默认为false,当设置为true后,请确保在对应的Dify应用中已开启图片识别功能。 +4. 如果没有找到匹配的配置,插件不会修改上下文,将使用默认的 Dify 配置(如果有的话)。 +5. 确保在项目的主配置文件中正确设置了 Dify 相关的配置项,以便插件可以正常工作。 diff --git a/plugins/custom_dify_app/config.json.template b/plugins/custom_dify_app/config.json.template index 6b570b696..963c77714 100644 --- a/plugins/custom_dify_app/config.json.template +++ b/plugins/custom_dify_app/config.json.template @@ -4,6 +4,7 @@ "app_type": "chatbot", "api_base": "https://api.dify.ai/v1", "api_key": "app-xx", + "image_recognition": false, "use_on_single_chat": false, "group_name_keywords": [ "测试群" diff --git a/plugins/custom_dify_app/custom_dify_app.py b/plugins/custom_dify_app/custom_dify_app.py index 6328957cc..2b65385c6 100644 --- a/plugins/custom_dify_app/custom_dify_app.py +++ b/plugins/custom_dify_app/custom_dify_app.py @@ -36,7 +36,7 @@ def __init__(self): def _init_single_chat_conf(self): # 遍历配置,找到用于单聊的配置 for dify_app_dict in self.config: - if "use_on_single_chat" in dify_app_dict and dify_app_dict["use_on_single_chat"]: + if dify_app_dict.get("single_chat", False): self.single_chat_conf = dify_app_dict break @@ -74,5 +74,6 @@ def on_handle_context(self, e_context: EventContext): context["dify_app_type"] = dify_app_conf["app_type"] context["dify_api_base"] = dify_app_conf["api_base"] context["dify_api_key"] = dify_app_conf["api_key"] + context["image_recognition"] = dify_app_conf.get("image_recognition", False) except Exception as e: logger.error(f"[CustomDifyApp] on_handle_context error: {e}")