mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
add home assistant
This commit is contained in:
parent
eac2852930
commit
b3e8b44679
2 changed files with 64 additions and 1 deletions
62
core/providers/llm/homeassistant/homeassistant.py
Normal file
62
core/providers/llm/homeassistant/homeassistant.py
Normal file
|
|
@ -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}")
|
||||||
|
|
@ -15,4 +15,5 @@ aiohttp==3.9.3
|
||||||
aiohttp_cors==0.7.0
|
aiohttp_cors==0.7.0
|
||||||
ormsgpack==1.7.0
|
ormsgpack==1.7.0
|
||||||
ruamel.yaml==0.18.10
|
ruamel.yaml==0.18.10
|
||||||
loguru==0.7.3
|
loguru==0.7.3
|
||||||
|
requests>=2.0.0
|
||||||
Loading…
Reference in a new issue