From 951370eee75b65aa1eedb1c0bf28f0fa7a929c67 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 23 Apr 2025 21:29:42 +0800 Subject: [PATCH] =?UTF-8?q?update:=E8=AE=A9alibl=E3=80=81=E7=AD=89?= =?UTF-8?q?=E9=9D=9E=E5=8E=9F=E7=94=9Fllm=E4=BD=BF=E7=94=A8function?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E5=8D=A1=E9=A1=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/firmware-build.md | 30 +++++++++-- .../core/providers/llm/AliBL/AliBL.py | 19 +++++-- .../core/providers/llm/fastgpt/fastgpt.py | 51 ++++++++++--------- .../core/providers/llm/gemini/gemini.py | 4 ++ 4 files changed, 73 insertions(+), 31 deletions(-) diff --git a/docs/firmware-build.md b/docs/firmware-build.md index f821805a..ebb7413b 100644 --- a/docs/firmware-build.md +++ b/docs/firmware-build.md @@ -1,16 +1,40 @@ # esp32固件编译 -## 第1步 配置环境 +## 第1步 准备你的ota地址 +如果你按照教程使用的是全模块部署,就应该会有ota地址。 + +此刻,请你用浏览器打开你的ota地址,例如我的ota地址 +``` +http://192.168.1.25:8002/xiaozhi/ota/ +``` + +如果显示“OTA接口运行正常,websocket集群数量:X”。那就往下。 + +如果显示“OTA接口运行不正常”,大概是你还没在`智控台`配置`Websocket`地址。那就: + +- 1、使用超级管理员登录智控台 + +- 2、顶部菜单点击`参数管理` + +- 3、在列表中找到`server.websocket`项目,输入你的`Websocket`地址。例如我的就是 + +``` +ws://192.168.1.25:8000/xiaozhi/v1/ +``` + +配置完后,再使用浏览器刷新你的ota接口地址,看看是不是正常了。如果还不正常就,就再次确认一下Websocket是否正常启动,是否配置了Websocket地址。 + +## 第2步 配置环境 先按照这个教程配置项目环境[《Windows搭建 ESP IDF 5.3.2开发环境以及编译小智》](https://icnynnzcwou8.feishu.cn/wiki/JEYDwTTALi5s2zkGlFGcDiRknXf) -## 第2步 打开配置文件 +## 第3步 打开配置文件 配置好编译环境后,下载虾哥iaozhi-esp32项目源码, 从这里下载虾哥[xiaozhi-esp32项目源码](https://github.com/78/xiaozhi-esp32)。 下载后,打开`xiaozhi-esp32/main/Kconfig.projbuild`文件。 -## 第3步 修改OTA地址 +## 第4步 修改OTA地址 找到`OTA_VERSION_URL`的`default`的内容,把`https://api.tenclass.net/xiaozhi/ota/` 改成你自己的地址,例如,我的接口地址是`http://192.168.1.25:8002/xiaozhi/ota/`,就把内容改成这个。 diff --git a/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py index 51efdc06..769f3117 100644 --- a/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py +++ b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py @@ -6,6 +6,7 @@ from core.providers.llm.base import LLMProviderBase TAG = __name__ logger = setup_logging() + class LLMProvider(LLMProviderBase): def __init__(self, config): self.api_key = config["api_key"] @@ -19,21 +20,25 @@ class LLMProvider(LLMProviderBase): # 处理dialogue if self.is_No_prompt: dialogue.pop(0) - logger.bind(tag=TAG).debug(f"【阿里百练API服务】处理后的dialogue: {dialogue}") + logger.bind(tag=TAG).debug( + f"【阿里百练API服务】处理后的dialogue: {dialogue}" + ) # 构造调用参数 call_params = { "api_key": self.api_key, "app_id": self.app_id, "session_id": session_id, - "messages": dialogue + "messages": dialogue, } if self.memory_id != False: # 百练memory需要prompt参数 prompt = dialogue[-1].get("content") call_params["memory_id"] = self.memory_id call_params["prompt"] = prompt - logger.bind(tag=TAG).debug(f"【阿里百练API服务】处理后的prompt: {prompt}") + logger.bind(tag=TAG).debug( + f"【阿里百练API服务】处理后的prompt: {prompt}" + ) responses = Application.call(**call_params) if responses.status_code != HTTPStatus.OK: @@ -44,9 +49,15 @@ class LLMProvider(LLMProviderBase): ) yield "【阿里百练API服务响应异常】" else: - logger.bind(tag=TAG).debug(f"【阿里百练API服务】构造参数: {call_params}") + logger.bind(tag=TAG).debug( + f"【阿里百练API服务】构造参数: {call_params}" + ) yield responses.output.text except Exception as e: logger.bind(tag=TAG).error(f"【阿里百练API服务】响应异常: {e}") yield "【LLM服务响应异常】" + + def response_with_functions(self, session_id, dialogue, functions=None): + logger.bind(tag=TAG).info(f"阿里百练暂未实现完整的工具调用(function call)") + return self.response(session_id, dialogue) diff --git a/main/xiaozhi-server/core/providers/llm/fastgpt/fastgpt.py b/main/xiaozhi-server/core/providers/llm/fastgpt/fastgpt.py index 38f9a2c9..e0e6ccc7 100644 --- a/main/xiaozhi-server/core/providers/llm/fastgpt/fastgpt.py +++ b/main/xiaozhi-server/core/providers/llm/fastgpt/fastgpt.py @@ -21,37 +21,36 @@ class LLMProvider(LLMProviderBase): # 发起流式请求 with requests.post( - f"{self.base_url}/chat/completions", - headers={"Authorization": f"Bearer {self.api_key}"}, - json={ - "stream": True, - "chatId": session_id, - "detail": self.detail, - "variables": self.variables, - "messages": [ - { - "role": "user", - "content": last_msg["content"] - } - ] - }, - stream=True + f"{self.base_url}/chat/completions", + headers={"Authorization": f"Bearer {self.api_key}"}, + json={ + "stream": True, + "chatId": session_id, + "detail": self.detail, + "variables": self.variables, + "messages": [{"role": "user", "content": last_msg["content"]}], + }, + stream=True, ) as r: for line in r.iter_lines(): if line: try: - if line.startswith(b'data: '): - if line[6:].decode('utf-8') == '[DONE]': + if line.startswith(b"data: "): + if line[6:].decode("utf-8") == "[DONE]": break data = json.loads(line[6:]) - if 'choices' in data and len(data['choices']) > 0: - delta = data['choices'][0].get('delta', {}) - if delta and 'content' in delta and delta['content'] is not None: - content = delta['content'] - if '' in content: + if "choices" in data and len(data["choices"]) > 0: + delta = data["choices"][0].get("delta", {}) + if ( + delta + and "content" in delta + and delta["content"] is not None + ): + content = delta["content"] + if "" in content: continue - if '' in content: + if "" in content: continue yield content @@ -62,4 +61,8 @@ class LLMProvider(LLMProviderBase): except Exception as e: logger.bind(tag=TAG).error(f"Error in response generation: {e}") - yield "【服务响应异常】" \ No newline at end of file + yield "【服务响应异常】" + + def response_with_functions(self, session_id, dialogue, functions=None): + logger.bind(tag=TAG).info(f"fastgpt暂未实现完整的工具调用(function call)") + return self.response(session_id, dialogue) diff --git a/main/xiaozhi-server/core/providers/llm/gemini/gemini.py b/main/xiaozhi-server/core/providers/llm/gemini/gemini.py index ca78caa9..a91a6bf0 100644 --- a/main/xiaozhi-server/core/providers/llm/gemini/gemini.py +++ b/main/xiaozhi-server/core/providers/llm/gemini/gemini.py @@ -134,3 +134,7 @@ class LLMProvider(LLMProviderBase): yield f"JSON解码错误:{e}" except Exception as e: yield f"发生错误:{e}" + + def response_with_functions(self, session_id, dialogue, functions=None): + logger.bind(tag=TAG).info(f"gemini暂未实现完整的工具调用(function call)") + return self.response(session_id, dialogue)