Skip to content

Commit

Permalink
fix: custom dify app image recognition (hanfangyuan4396#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfangyuan4396 authored Nov 16, 2024
1 parent b5c1f74 commit dee7829
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
12 changes: 6 additions & 6 deletions bot/dify/dify_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions plugins/custom_dify_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [ # 群名关键词列表,当群名包含其中任一关键词时,使用该配置
"测试群"
]
Expand All @@ -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 相关的配置项,以便插件可以正常工作。
1 change: 1 addition & 0 deletions plugins/custom_dify_app/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"测试群"
Expand Down
3 changes: 2 additions & 1 deletion plugins/custom_dify_app/custom_dify_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}")

0 comments on commit dee7829

Please sign in to comment.