Merge pull request #962 from xinnan-tech/update-fix

update:让alibl、等非原生llm使用functional时不卡顿
This commit is contained in:
欣南科技
2025-04-23 21:30:40 +08:00
committed by GitHub
4 changed files with 73 additions and 31 deletions
+27 -3
View File
@@ -1,16 +1,40 @@
# esp32固件编译 # 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) 先按照这个教程配置项目环境[《Windows搭建 ESP IDF 5.3.2开发环境以及编译小智》](https://icnynnzcwou8.feishu.cn/wiki/JEYDwTTALi5s2zkGlFGcDiRknXf)
## 第2步 打开配置文件 ## 第3步 打开配置文件
配置好编译环境后,下载虾哥iaozhi-esp32项目源码, 配置好编译环境后,下载虾哥iaozhi-esp32项目源码,
从这里下载虾哥[xiaozhi-esp32项目源码](https://github.com/78/xiaozhi-esp32)。 从这里下载虾哥[xiaozhi-esp32项目源码](https://github.com/78/xiaozhi-esp32)。
下载后,打开`xiaozhi-esp32/main/Kconfig.projbuild`文件。 下载后,打开`xiaozhi-esp32/main/Kconfig.projbuild`文件。
## 第3步 修改OTA地址 ## 第4步 修改OTA地址
找到`OTA_VERSION_URL``default`的内容,把`https://api.tenclass.net/xiaozhi/ota/` 找到`OTA_VERSION_URL``default`的内容,把`https://api.tenclass.net/xiaozhi/ota/`
改成你自己的地址,例如,我的接口地址是`http://192.168.1.25:8002/xiaozhi/ota/`,就把内容改成这个。 改成你自己的地址,例如,我的接口地址是`http://192.168.1.25:8002/xiaozhi/ota/`,就把内容改成这个。
@@ -6,6 +6,7 @@ from core.providers.llm.base import LLMProviderBase
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
class LLMProvider(LLMProviderBase): class LLMProvider(LLMProviderBase):
def __init__(self, config): def __init__(self, config):
self.api_key = config["api_key"] self.api_key = config["api_key"]
@@ -19,21 +20,25 @@ class LLMProvider(LLMProviderBase):
# 处理dialogue # 处理dialogue
if self.is_No_prompt: if self.is_No_prompt:
dialogue.pop(0) dialogue.pop(0)
logger.bind(tag=TAG).debug(f"【阿里百练API服务】处理后的dialogue: {dialogue}") logger.bind(tag=TAG).debug(
f"【阿里百练API服务】处理后的dialogue: {dialogue}"
)
# 构造调用参数 # 构造调用参数
call_params = { call_params = {
"api_key": self.api_key, "api_key": self.api_key,
"app_id": self.app_id, "app_id": self.app_id,
"session_id": session_id, "session_id": session_id,
"messages": dialogue "messages": dialogue,
} }
if self.memory_id != False: if self.memory_id != False:
# 百练memory需要prompt参数 # 百练memory需要prompt参数
prompt = dialogue[-1].get("content") prompt = dialogue[-1].get("content")
call_params["memory_id"] = self.memory_id call_params["memory_id"] = self.memory_id
call_params["prompt"] = prompt 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) responses = Application.call(**call_params)
if responses.status_code != HTTPStatus.OK: if responses.status_code != HTTPStatus.OK:
@@ -44,9 +49,15 @@ class LLMProvider(LLMProviderBase):
) )
yield "【阿里百练API服务响应异常】" yield "【阿里百练API服务响应异常】"
else: else:
logger.bind(tag=TAG).debug(f"【阿里百练API服务】构造参数: {call_params}") logger.bind(tag=TAG).debug(
f"【阿里百练API服务】构造参数: {call_params}"
)
yield responses.output.text yield responses.output.text
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"【阿里百练API服务】响应异常: {e}") logger.bind(tag=TAG).error(f"【阿里百练API服务】响应异常: {e}")
yield "【LLM服务响应异常】" 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)
@@ -21,37 +21,36 @@ class LLMProvider(LLMProviderBase):
# 发起流式请求 # 发起流式请求
with requests.post( with requests.post(
f"{self.base_url}/chat/completions", f"{self.base_url}/chat/completions",
headers={"Authorization": f"Bearer {self.api_key}"}, headers={"Authorization": f"Bearer {self.api_key}"},
json={ json={
"stream": True, "stream": True,
"chatId": session_id, "chatId": session_id,
"detail": self.detail, "detail": self.detail,
"variables": self.variables, "variables": self.variables,
"messages": [ "messages": [{"role": "user", "content": last_msg["content"]}],
{ },
"role": "user", stream=True,
"content": last_msg["content"]
}
]
},
stream=True
) as r: ) as r:
for line in r.iter_lines(): for line in r.iter_lines():
if line: if line:
try: try:
if line.startswith(b'data: '): if line.startswith(b"data: "):
if line[6:].decode('utf-8') == '[DONE]': if line[6:].decode("utf-8") == "[DONE]":
break break
data = json.loads(line[6:]) data = json.loads(line[6:])
if 'choices' in data and len(data['choices']) > 0: if "choices" in data and len(data["choices"]) > 0:
delta = data['choices'][0].get('delta', {}) delta = data["choices"][0].get("delta", {})
if delta and 'content' in delta and delta['content'] is not None: if (
content = delta['content'] delta
if '<think>' in content: and "content" in delta
and delta["content"] is not None
):
content = delta["content"]
if "<think>" in content:
continue continue
if '</think>' in content: if "</think>" in content:
continue continue
yield content yield content
@@ -62,4 +61,8 @@ class LLMProvider(LLMProviderBase):
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"Error in response generation: {e}") logger.bind(tag=TAG).error(f"Error in response generation: {e}")
yield "【服务响应异常】" 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)
@@ -134,3 +134,7 @@ class LLMProvider(LLMProviderBase):
yield f"JSON解码错误:{e}" yield f"JSON解码错误:{e}"
except Exception as e: except Exception as e:
yield f"发生错误:{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)