From 06cbffdea4d314df9e1b0af0913c1b0a7351d734 Mon Sep 17 00:00:00 2001 From: Miyokiss Date: Fri, 21 Mar 2025 03:12:18 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Add=EF=BC=9AAliAppLLM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config.yaml | 12 +++++ .../core/providers/llm/AliBL/AliBL.py | 52 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 587543dc..75426572 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -165,6 +165,18 @@ LLM: top_p: 1 top_k: 50 frequency_penalty: 0 # 频率惩罚 + AliAppLLM: + # 定义LLM API类型 + type: AliBL + base_url: https://dashscope.aliyuncs.com/compatible-mode/v1 + app_id: 你的app_id + # 可在这里找到你的 api_key https://bailian.console.aliyun.com/?apiKey=1#/api-key + api_key: 你的api_key + # 是否不使用本地prompt:true|false (默不用请在百练应用中设置prompt) + is_No_prompt: true + # Ali_memory_id:false(不使用)|你的memory_id(请在百练应用中设置中获取) + # Tips!:Ali_memory未实现多用户存储记忆(记忆按id调用) + Ali_memory_id: false DoubaoLLM: # 定义LLM API类型 type: openai diff --git a/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py new file mode 100644 index 00000000..074d8f8d --- /dev/null +++ b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py @@ -0,0 +1,52 @@ +from config.logger import setup_logging +from http import HTTPStatus +from dashscope import Application +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"] + self.app_id = config["app_id"] + self.base_url = config.get("base_url") + self.is_No_prompt = config.get("is_No_prompt") + self.memory_id = config.get("Ali_memory_id") + + def response(self, session_id, dialogue): + try: + # 处理dialogue + if self.is_No_prompt: + dialogue.pop(0) + 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 + } + 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}") + + responses = Application.call(**call_params) + if responses.status_code != HTTPStatus.OK: + logger.bind(tag=TAG).error( + f"code={responses.status_code}, " + f"message={responses.message}, " + f"请参考文档:https://help.aliyun.com/zh/model-studio/developer-reference/error-code" + ) + yield "【阿里百练API服务响应异常】" + else: + 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服务响应异常】" \ No newline at end of file From 8bb0cf02ad2340d3de9631f88ef6b627ab1957c6 Mon Sep 17 00:00:00 2001 From: Huang <32005838+openrz@users.noreply.github.com> Date: Fri, 21 Mar 2025 21:08:50 +0800 Subject: [PATCH 2/4] =?UTF-8?q?update:=E9=85=8D=E7=BD=AE=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py index 074d8f8d..ec0fb939 100644 --- a/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py +++ b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py @@ -12,7 +12,7 @@ class LLMProvider(LLMProviderBase): self.app_id = config["app_id"] self.base_url = config.get("base_url") self.is_No_prompt = config.get("is_No_prompt") - self.memory_id = config.get("Ali_memory_id") + self.memory_id = config.get("ali_memory_id") def response(self, session_id, dialogue): try: @@ -49,4 +49,4 @@ class LLMProvider(LLMProviderBase): except Exception as e: logger.bind(tag=TAG).error(f"【阿里百练API服务】响应异常: {e}") - yield "【LLM服务响应异常】" \ No newline at end of file + yield "【LLM服务响应异常】" From 8e605dd1d4cfb8e74ee8f82ab2d0f9d6b93fd5f0 Mon Sep 17 00:00:00 2001 From: Huang <32005838+openrz@users.noreply.github.com> Date: Fri, 21 Mar 2025 21:09:32 +0800 Subject: [PATCH 3/4] =?UTF-8?q?update:=E9=85=8D=E7=BD=AE=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 75426572..ac62f988 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -173,10 +173,10 @@ LLM: # 可在这里找到你的 api_key https://bailian.console.aliyun.com/?apiKey=1#/api-key api_key: 你的api_key # 是否不使用本地prompt:true|false (默不用请在百练应用中设置prompt) - is_No_prompt: true + is_no_prompt: true # Ali_memory_id:false(不使用)|你的memory_id(请在百练应用中设置中获取) # Tips!:Ali_memory未实现多用户存储记忆(记忆按id调用) - Ali_memory_id: false + ali_memory_id: false DoubaoLLM: # 定义LLM API类型 type: openai From 69630c2be044500c2af35d6ee7bf93ad671f5706 Mon Sep 17 00:00:00 2001 From: Huang <32005838+openrz@users.noreply.github.com> Date: Fri, 21 Mar 2025 21:09:54 +0800 Subject: [PATCH 4/4] =?UTF-8?q?update:=E9=85=8D=E7=BD=AE=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py index ec0fb939..51efdc06 100644 --- a/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py +++ b/main/xiaozhi-server/core/providers/llm/AliBL/AliBL.py @@ -11,7 +11,7 @@ class LLMProvider(LLMProviderBase): self.api_key = config["api_key"] self.app_id = config["app_id"] self.base_url = config.get("base_url") - self.is_No_prompt = config.get("is_No_prompt") + self.is_No_prompt = config.get("is_no_prompt") self.memory_id = config.get("ali_memory_id") def response(self, session_id, dialogue):