From 06cbffdea4d314df9e1b0af0913c1b0a7351d734 Mon Sep 17 00:00:00 2001 From: Miyokiss Date: Fri, 21 Mar 2025 03:12:18 +0800 Subject: [PATCH] =?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