From 374cd5dbb86f6f841dc093d568c744662de75845 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Fri, 17 Nov 2023 16:27:44 +0800 Subject: [PATCH] feat: support send knowledge base image --- bot/linkai/link_ai_bot.py | 20 +++++++++++++++++--- channel/chat_channel.py | 1 + config-template.json | 3 +-- config.py | 4 ++-- plugins/linkai/README.md | 5 +++-- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/bot/linkai/link_ai_bot.py b/bot/linkai/link_ai_bot.py index 5df89eb..1dc5df2 100644 --- a/bot/linkai/link_ai_bot.py +++ b/bot/linkai/link_ai_bot.py @@ -7,13 +7,12 @@ import requests from bot.bot import Bot from bot.chatgpt.chat_gpt_session import ChatGPTSession -from bot.openai.open_ai_image import OpenAIImage from bot.session_manager import SessionManager from bridge.context import Context, ContextType from bridge.reply import Reply, ReplyType from common.log import logger from config import conf, pconf - +import threading class LinkAIBot(Bot): # authentication failed @@ -64,7 +63,7 @@ class LinkAIBot(Bot): session_id = context["session_id"] session = self.sessions.session_query(query, session_id) - model = conf().get("model") or "gpt-3.5-turbo" + model = conf().get("model") # remove system message if session.messages[0].get("role") == "system": if app_code or model == "wenxin": @@ -104,6 +103,10 @@ class LinkAIBot(Bot): knowledge_suffix = self._fetch_knowledge_search_suffix(response) if knowledge_suffix: reply_content += knowledge_suffix + # image process + if response["choices"][0].get("img_urls"): + thread = threading.Thread(target=self._send_image, args=(context.get("channel"), context, response["choices"][0].get("img_urls"))) + thread.start() return Reply(ReplyType.TEXT, reply_content) else: @@ -262,3 +265,14 @@ class LinkAIBot(Bot): return suffix except Exception as e: logger.exception(e) + + + def _send_image(self, channel, context, image_urls): + if not image_urls: + return + try: + for url in image_urls: + reply = Reply(ReplyType.IMAGE_URL, url) + channel.send(reply, context) + except Exception as e: + logger.error(e) \ No newline at end of file diff --git a/channel/chat_channel.py b/channel/chat_channel.py index 648eaac..8ed5f4f 100644 --- a/channel/chat_channel.py +++ b/channel/chat_channel.py @@ -175,6 +175,7 @@ class ChatChannel(Channel): if e_context.is_break(): context["generate_breaked_by"] = e_context["breaked_by"] if context.type == ContextType.TEXT or context.type == ContextType.IMAGE_CREATE: # 文字和图片消息 + context["channel"] = e_context["channel"] reply = super().build_reply_content(context.content, context) elif context.type == ContextType.VOICE: # 语音消息 cmsg = context["msg"] diff --git a/config-template.json b/config-template.json index 597245e..dd07d25 100644 --- a/config-template.json +++ b/config-template.json @@ -1,7 +1,7 @@ { "channel_type": "wx", + "model": "", "open_ai_api_key": "YOUR API KEY", - "model": "gpt-3.5-turbo", "text_to_image": "dall-e-2", "voice_to_text": "openai", "text_to_voice": "openai", @@ -28,7 +28,6 @@ "speech_recognition": true, "group_speech_recognition": false, "voice_reply_voice": false, - "tts_voice_id": "alloy", "conversation_max_tokens": 2500, "expires_in_seconds": 3600, "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", diff --git a/config.py b/config.py index 53d8bf0..8db08cd 100644 --- a/config.py +++ b/config.py @@ -16,7 +16,7 @@ available_setting = { "open_ai_api_base": "https://api.openai.com/v1", "proxy": "", # openai使用的代理 # chatgpt模型, 当use_azure_chatgpt为true时,其名称为Azure上model deployment名称 - "model": "gpt-3.5-turbo", # 还支持 gpt-3.5-turbo-16k, gpt-4, wenxin, xunfei + "model": "gpt-3.5-turbo", # 还支持 gpt-4, gpt-4-turbo, wenxin, xunfei "use_azure_chatgpt": False, # 是否使用azure的chatgpt "azure_deployment_id": "", # azure 模型部署名称 "azure_api_version": "", # azure api版本 @@ -52,7 +52,7 @@ available_setting = { "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0, - "request_timeout": 60, # chatgpt请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间 + "request_timeout": 180, # chatgpt请求超时时间,openai接口默认设置为600,对于难问题一般需要较长时间 "timeout": 120, # chatgpt重试超时时间,在这个时间内,将会自动重试 # Baidu 文心一言参数 "baidu_wenxin_model": "eb-instant", # 默认使用ERNIE-Bot-turbo模型 diff --git a/plugins/linkai/README.md b/plugins/linkai/README.md index dc3caa7..043d436 100644 --- a/plugins/linkai/README.md +++ b/plugins/linkai/README.md @@ -25,7 +25,8 @@ "summary": { "enabled": true, # 文档总结和对话功能开关 "group_enabled": true, # 是否支持群聊开启 - "max_file_size": 5000 # 文件的大小限制,单位KB,默认为5M,超过该大小直接忽略 + "max_file_size": 5000, # 文件的大小限制,单位KB,默认为5M,超过该大小直接忽略 + "type": ["FILE", "SHARING", "IMAGE"] # 支持总结的类型,分别表示 文件、分享链接、图片 } } ``` @@ -99,7 +100,7 @@ #### 使用 -功能开启后,向机器人发送 **文件** 或 **分享链接卡片** 即可生成摘要,进一步可以与文件或链接的内容进行多轮对话。 +功能开启后,向机器人发送 **文件**、 **分享链接卡片**、**图片** 即可生成摘要,进一步可以与文件或链接的内容进行多轮对话。如果需要关闭某种类型的内容总结,设置 `summary`配置中的type字段即可。 #### 限制