From 831fedc65ae911c1709a043a23f18607e2a20695 Mon Sep 17 00:00:00 2001 From: vonweller <154695311+vonweller@users.noreply.github.com> Date: Tue, 4 Mar 2025 17:44:49 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E7=BB=99=E6=9C=8D=E5=8A=A1=E7=AB=AF?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=86=E8=A7=89=E8=AF=86=E5=88=AB=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为服务端添加视觉识别的能力 客户端只需要发送的消息类型: vl_message = { "type": "VL", "msg": base64(你的图片) "version": 1, "transport": "websocket", "audio_params": { "format": "opus", "sample_rate": 16000, "channels": 1, "frame_duration": 60 } --- config.yaml | 6 ++++-- core/connection.py | 2 ++ core/handle/VLHandle.py | 12 ++++++++++++ core/handle/textHandle.py | 5 +++++ core/providers/llm/openai/openai.py | 5 ++++- requirements.txt | 1 + 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 core/handle/VLHandle.py diff --git a/config.yaml b/config.yaml index d0217deb0..130125c66 100644 --- a/config.yaml +++ b/config.yaml @@ -77,7 +77,7 @@ selected_module: ASR: FunASR VAD: SileroVAD # 将根据配置名称对应的type调用实际的LLM适配器 - LLM: ChatGLMLLM + LLM: AliLLM # TTS将根据配置名称对应的type调用实际的TTS适配器 TTS: EdgeTTS @@ -104,8 +104,10 @@ LLM: # 定义LLM API类型 type: openai # 可在这里找到你的 api_key https://bailian.console.aliyun.com/?apiKey=1#/api-key + #qwen-omni-turbo + #qwen-omni-turbo 这个模型支持视觉识别 base_url: https://dashscope.aliyuncs.com/compatible-mode/v1 - model_name: qwen-turbo + model_name: qwen-omni-turbo #qwen-omni-turbo api_key: 你的deepseek api key DeepSeekLLM: # 定义LLM API类型 diff --git a/core/connection.py b/core/connection.py index 8b6d12854..65dfc16bc 100644 --- a/core/connection.py +++ b/core/connection.py @@ -167,6 +167,8 @@ class ConnectionHandler: async def _route_message(self, message): """消息路由""" if isinstance(message, str): + print('str') + print(message) await handleTextMessage(self, message) elif isinstance(message, bytes): await handleAudioMessage(self, message) diff --git a/core/handle/VLHandle.py b/core/handle/VLHandle.py new file mode 100644 index 000000000..b861741d7 --- /dev/null +++ b/core/handle/VLHandle.py @@ -0,0 +1,12 @@ +import json +from config.logger import setup_logging + +logger = setup_logging() + +async def handleVLMessage(conn, text): + messages = [ + {"type": "image_url","image_url": {"url": f"data:image/png;base64,{text}"},}, + {"type": "text", "text": "图中描绘的是什么景象,请细致查看并描述"}, + ] + conn.executor.submit(conn.chat, messages) + await conn.websocket.send(json.dumps(conn.welcome_msg)) diff --git a/core/handle/textHandle.py b/core/handle/textHandle.py index 2eb7304d1..6dff02303 100644 --- a/core/handle/textHandle.py +++ b/core/handle/textHandle.py @@ -2,6 +2,7 @@ from config.logger import setup_logging import json from core.handle.abortHandle import handleAbortMessage from core.handle.helloHandle import handleHelloMessage +from core.handle.VLHandle import handleVLMessage from core.handle.receiveAudioHandle import startToChat from core.handle.iotHandle import handleIotDescriptors @@ -40,5 +41,9 @@ async def handleTextMessage(conn, message): elif msg_json["type"] == "iot": if "descriptors" in msg_json: await handleIotDescriptors(conn, msg_json["descriptors"]) + ####视觉识别添加能力 msg中应该包含图片数据 + elif msg_json["type"] == "VL": + await handleVLMessage(conn, msg_json['msg']) + except json.JSONDecodeError: await conn.websocket.send(message) diff --git a/core/providers/llm/openai/openai.py b/core/providers/llm/openai/openai.py index e060248f9..71af80589 100644 --- a/core/providers/llm/openai/openai.py +++ b/core/providers/llm/openai/openai.py @@ -23,7 +23,10 @@ class LLMProvider(LLMProviderBase): responses = self.client.chat.completions.create( model=self.model_name, messages=dialogue, - stream=True + # 设置输出数据的模态,当前支持["text"] + modalities=["text"], + stream=True, + stream_options={"include_usage": True} ) is_active = True diff --git a/requirements.txt b/requirements.txt index 4d75889dc..afcd9456e 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +#pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple pyyml==0.0.2 torch==2.2.2 silero_vad==5.1.2 From e487362bbcc2238c27b62043c9dc0739ba44d2cc Mon Sep 17 00:00:00 2001 From: vonweller <154695311+vonweller@users.noreply.github.com> Date: Tue, 4 Mar 2025 17:48:25 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20config.yaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index 130125c66..fbe9dd9fd 100644 --- a/config.yaml +++ b/config.yaml @@ -104,10 +104,9 @@ LLM: # 定义LLM API类型 type: openai # 可在这里找到你的 api_key https://bailian.console.aliyun.com/?apiKey=1#/api-key - #qwen-omni-turbo - #qwen-omni-turbo 这个模型支持视觉识别 + #qwen-omni-turbo 可使用一系列阿里的视觉识别模型 base_url: https://dashscope.aliyuncs.com/compatible-mode/v1 - model_name: qwen-omni-turbo #qwen-omni-turbo + model_name: qwen-turbo api_key: 你的deepseek api key DeepSeekLLM: # 定义LLM API类型 From d60787331c3e93968dbf1ccb18a384eff79c9fdc Mon Sep 17 00:00:00 2001 From: vonweller <154695311+vonweller@users.noreply.github.com> Date: Tue, 4 Mar 2025 18:00:18 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8F=91=E9=80=81?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/handle/VLHandle.py | 1 - 1 file changed, 1 deletion(-) diff --git a/core/handle/VLHandle.py b/core/handle/VLHandle.py index b861741d7..966a2f226 100644 --- a/core/handle/VLHandle.py +++ b/core/handle/VLHandle.py @@ -9,4 +9,3 @@ async def handleVLMessage(conn, text): {"type": "text", "text": "图中描绘的是什么景象,请细致查看并描述"}, ] conn.executor.submit(conn.chat, messages) - await conn.websocket.send(json.dumps(conn.welcome_msg)) From 48f11b47998b624b2a5a6c928966f4c8c88779f8 Mon Sep 17 00:00:00 2001 From: vonweller <154695311+vonweller@users.noreply.github.com> Date: Tue, 4 Mar 2025 18:01:07 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20connection.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/connection.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/connection.py b/core/connection.py index 65dfc16bc..8b6d12854 100644 --- a/core/connection.py +++ b/core/connection.py @@ -167,8 +167,6 @@ class ConnectionHandler: async def _route_message(self, message): """消息路由""" if isinstance(message, str): - print('str') - print(message) await handleTextMessage(self, message) elif isinstance(message, bytes): await handleAudioMessage(self, message) From ab45342349474e53e5e276f35a3ad73f5ce29b32 Mon Sep 17 00:00:00 2001 From: vonweller <154695311+vonweller@users.noreply.github.com> Date: Wed, 5 Mar 2025 14:54:31 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E8=A7=86=E8=A7=89=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=98=AF=E5=90=A6=E6=98=AFOpenai=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 视觉识别添加是否是Openai协议的判断,当前测试openai全系使用无问题 --- config.yaml | 11 +++++++---- core/handle/VLHandle.py | 12 ++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/config.yaml b/config.yaml index fbe9dd9fd..26ee93ae3 100644 --- a/config.yaml +++ b/config.yaml @@ -58,6 +58,8 @@ prompt: | 请注意,要像一个人一样说话,请不要回复表情符号、代码、和xml标签。 当前时间是:{date_time},现在我正在和你进行语音聊天,我们开始吧。 如果用户希望结束对话,请在最后说“拜拜”或“再见”。 + 如果用户希望打开摄像头,等操作时等类型的要求时,你只需要回复正在打开 + 如果用户希望识别画面,识别图片,识别场景在干什么等类型的要求时 你只需要回复正在识别 # 使用完声音文件后删除文件(Delete the sound file when you are done using it) delete_audio: true @@ -77,7 +79,7 @@ selected_module: ASR: FunASR VAD: SileroVAD # 将根据配置名称对应的type调用实际的LLM适配器 - LLM: AliLLM + LLM: ChatGLMLLM # TTS将根据配置名称对应的type调用实际的TTS适配器 TTS: EdgeTTS @@ -106,7 +108,7 @@ LLM: # 可在这里找到你的 api_key https://bailian.console.aliyun.com/?apiKey=1#/api-key #qwen-omni-turbo 可使用一系列阿里的视觉识别模型 base_url: https://dashscope.aliyuncs.com/compatible-mode/v1 - model_name: qwen-turbo + model_name: qwen-omni-turbo api_key: 你的deepseek api key DeepSeekLLM: # 定义LLM API类型 @@ -119,10 +121,11 @@ LLM: # 定义LLM API类型 type: openai # glm-4-flash 是免费的,但是还是需要注册填写api_key的 + # glm-4v-plus 是视觉识别模型 # 可在这里找到你的api key https://bigmodel.cn/usercenter/proj-mgmt/apikeys - model_name: glm-4-flash + model_name: glm-4v-plus url: https://open.bigmodel.cn/api/paas/v4/ - api_key: 你的chat-glm api key + api_key: 你的deepseek api key OllamaLLM: # 定义LLM API类型 type: ollama diff --git a/core/handle/VLHandle.py b/core/handle/VLHandle.py index 966a2f226..3431e789c 100644 --- a/core/handle/VLHandle.py +++ b/core/handle/VLHandle.py @@ -4,8 +4,12 @@ from config.logger import setup_logging logger = setup_logging() async def handleVLMessage(conn, text): - messages = [ - {"type": "image_url","image_url": {"url": f"data:image/png;base64,{text}"},}, - {"type": "text", "text": "图中描绘的是什么景象,请细致查看并描述"}, - ] + #print(text) + messages =[] + if conn.config["LLM"][conn.config["selected_module"]["LLM"]]['type']=='openai': + #print('当前openai接口协议LLM') + messages = [ + {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{text}"}, }, + {"type": "text", "text": "图中描绘的是什么景象,请细致查看并描述"}, + ] conn.executor.submit(conn.chat, messages) From 3f9242aceecfea9e649c09d6d9fbdab0773087c9 Mon Sep 17 00:00:00 2001 From: vonweller <154695311+vonweller@users.noreply.github.com> Date: Wed, 5 Mar 2025 18:34:54 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20config.yaml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 设置正确的回复频率 --- config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.yaml b/config.yaml index 26ee93ae3..0fc9b513f 100644 --- a/config.yaml +++ b/config.yaml @@ -58,8 +58,8 @@ prompt: | 请注意,要像一个人一样说话,请不要回复表情符号、代码、和xml标签。 当前时间是:{date_time},现在我正在和你进行语音聊天,我们开始吧。 如果用户希望结束对话,请在最后说“拜拜”或“再见”。 - 如果用户希望打开摄像头,等操作时等类型的要求时,你只需要回复正在打开 - 如果用户希望识别画面,识别图片,识别场景在干什么等类型的要求时 你只需要回复正在识别 + 如果用户希望打开摄像头等类型的要求时,你只需要回复“好的正在打开” + 如果用户希望识别画面,识别图片,识别场景等类型的要求时“你只需要回复正在识别” # 使用完声音文件后删除文件(Delete the sound file when you are done using it) delete_audio: true