From b3e8b446793f127fdb0b0f62c20a826ad759cb9d Mon Sep 17 00:00:00 2001 From: ck3 Date: Wed, 19 Feb 2025 22:09:31 +0800 Subject: [PATCH 1/3] add home assistant --- .../llm/homeassistant/homeassistant.py | 62 +++++++++++++++++++ requirements.txt | 3 +- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 core/providers/llm/homeassistant/homeassistant.py diff --git a/core/providers/llm/homeassistant/homeassistant.py b/core/providers/llm/homeassistant/homeassistant.py new file mode 100644 index 000000000..115e85e95 --- /dev/null +++ b/core/providers/llm/homeassistant/homeassistant.py @@ -0,0 +1,62 @@ +import requests +from requests.exceptions import RequestException +from config.logger import setup_logging +from core.providers.llm.base import LLMProviderBase + +TAG = __name__ +logger = setup_logging() + + +class LLMProvider(LLMProviderBase): + def __init__(self, config): + self.agent_id = config.get("agent_id") # 对应 agent_id + self.api_key = config.get("api_key") + self.base_url = config.get("base_url", config.get("url")) # 默认使用 base_url + self.api_url = f"{self.base_url}/api/conversation/process" # 拼接完整的 API URL + + def response(self, session_id, dialogue): + print(dialogue) + try: + # home assistant语音助手自带意图,无需使用xiaozhi ai自带的,只需要把用户说的话传递给home assistant即可 + + # 提取最后一个 role 为 'user' 的 content + input_text = None + if isinstance(dialogue, list): # 确保 dialogue 是一个列表 + # 逆序遍历,找到最后一个 role 为 'user' 的消息 + for message in reversed(dialogue): + if message.get("role") == "user": # 找到 role 为 'user' 的消息 + input_text = message.get("content", "") + break # 找到后立即退出循环 + + # 构造请求数据 + payload = { + "text": input_text, + "agent_id": self.agent_id, + "conversation_id": session_id # 使用 session_id 作为 conversation_id + } + # 设置请求头 + headers = { + "Authorization": f"Bearer {self.api_key}", + "Content-Type": "application/json" + } + + # 发起 POST 请求 + response = requests.post(self.api_url, json=payload, headers=headers) + + # 检查请求是否成功 + response.raise_for_status() + + # 解析返回数据 + data = response.json() + speech = data.get("response", {}).get("speech", {}).get("plain", {}).get("speech", "") + + # 返回生成的内容 + if speech: + yield speech + else: + logger.bind(tag=TAG).warning("API 返回数据中没有 speech 内容") + + except RequestException as e: + logger.bind(tag=TAG).error(f"HTTP 请求错误: {e}") + except Exception as e: + logger.bind(tag=TAG).error(f"生成响应时出错: {e}") diff --git a/requirements.txt b/requirements.txt index be5663068..bfa9ed803 100755 --- a/requirements.txt +++ b/requirements.txt @@ -15,4 +15,5 @@ aiohttp==3.9.3 aiohttp_cors==0.7.0 ormsgpack==1.7.0 ruamel.yaml==0.18.10 -loguru==0.7.3 \ No newline at end of file +loguru==0.7.3 +requests>=2.0.0 \ No newline at end of file From 7c15964c0143564897c38f773d6dde81e39cb4c8 Mon Sep 17 00:00:00 2001 From: ck3 Date: Wed, 19 Feb 2025 23:11:54 +0800 Subject: [PATCH 2/3] update home assistant remark --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e29c35cf3..5d8122835 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ server: | LLM | DifyLLM | dify 接口调用 | 消耗 token | 本地化部署,注意配置提示词需在 Dify 控制台设置 | | LLM | GeminiLLM | gemini 接口调用 | 免费 | [点击申请密钥](https://aistudio.google.com/apikey) | | LLM | CozeLLM | coze 接口调用 | 消耗 token | 需提供 bot_id、user_id 及个人令牌 | +| LLM | Home Assistant | homeassistant语音助手接口调用 | 免费 | 需提供home assistant令牌 | 实际上,任何支持 openai 接口调用的 LLM 均可接入使用。 From 5daeed5ead41445dbea5223cb408fee922cede80 Mon Sep 17 00:00:00 2001 From: ck3 Date: Wed, 19 Feb 2025 23:14:17 +0800 Subject: [PATCH 3/3] update config --- config.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config.yaml b/config.yaml index bf4eea011..a0ce2e1b4 100644 --- a/config.yaml +++ b/config.yaml @@ -128,6 +128,12 @@ LLM: user_id: 你的user_id base_url: "https://api.coze.cn/open_api/v2/chat" # 服务地址 personal_access_token: 你的coze个人令牌 + HomeAssistant: + # 定义LLM API类型 + type: homeassistant + base_url: http://homeassistant.local:8123 + agent_id: conversation.chatgpt + api_key: 你的home assistant api访问令牌 TTS: # 当前支持的type为edge、doubao,可自行适配 EdgeTTS: