diff --git a/README.md b/README.md index 3e61b7a2..191790bc 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ - + 控制家电开关 @@ -95,6 +95,11 @@ + + + 播报新闻 + + @@ -185,7 +190,6 @@ server: | LLM | FastgptLLM | fastgpt 接口调用 | 免费/消耗 token | 本地化部署,注意配置提示词需在 Fastgpt 控制台设置 | | LLM | GeminiLLM | gemini 接口调用 | 免费 | [点击申请密钥](https://aistudio.google.com/apikey) | | LLM | CozeLLM | coze 接口调用 | 消耗 token | 需提供 bot_id、user_id 及个人令牌 | -| LLM | Home Assistant | homeassistant语音助手接口调用 | 免费 | 需提供home assistant令牌 | 实际上,任何支持 openai 接口调用的 LLM 均可接入使用。 @@ -370,9 +374,41 @@ VAD: ### 6、我想通过小智控制电灯、空调、远程开关机等操作 💡 -方法1:在配置文件中将 `LLM` 设置为 `HomeAssistant`,通过 调用`HomeAssistant`接口实现相关控制。 +本项目,支持以工具调用的方式控制HomeAssistant设备 -方法2:以工具调用的方式控制HomeAssistant设备,LLM中配置HomeAssistant的api_key、base_url,不选择HomeAssistant做LLM,但必须配置一款支持tool调用的LLM,最后在提示词prompt里以示例的格式设置需要控制的设备,音乐播放需要在homeassistant中对接好musicassistant,同时音乐有削刮出媒体信息,对应的媒体播放器配置在设备列表中即可。 +1、首先选择一款支持function call支持的LLM,例如`ChatGLMLLM`。 + +2、在配置文件中,将 `selected_module.Intent` 设置为 `function_call`。 + +3、登录`HomeAssistant`,点击`左下角个人`,切换`安全`导航栏,划到底部`长期访问令牌`生成api_key。 + +在配置文件中,配置好你的home assistant的`devices`(被控制的设备)和`api_key`和`base_url`等信息。例如: + +``` yaml +plugins + home_assistant: + devices: + - 客厅,玩具灯,switch.cuco_cn_460494544_cp1_on_p_2_1 + - 卧室,台灯,switch.iot_cn_831898993_socn1_on_p_2_1 + base_url: http://你的homeassistant地址:8123 + api_key: 你的home assistant api访问令牌 +``` + +最后,允许function_call 插件在配置文件中启用`hass_get_state`(必须)、`hass_set_state`(必须)、`hass_play_music`(不想用ha听音乐可以不启动),例如: + +``` yaml +Intent: + ... + function_call: + type: nointent + functions: + - change_role + - get_weather + - get_news + - hass_get_state + - hass_set_state + - hass_play_music +``` ### 7、更多问题,可联系我们反馈 💬 diff --git a/docs/images/demo0.png b/docs/images/demo0.png new file mode 100644 index 00000000..99592be6 Binary files /dev/null and b/docs/images/demo0.png differ diff --git a/docs/images/demo5.png b/docs/images/demo5.png index 8e03685b..c6da2997 100644 Binary files a/docs/images/demo5.png and b/docs/images/demo5.png differ diff --git a/main/xiaozhi-server/plugins_func/functions/hass_init.py b/main/xiaozhi-server/plugins_func/functions/hass_init.py new file mode 100644 index 00000000..e09eb67f --- /dev/null +++ b/main/xiaozhi-server/plugins_func/functions/hass_init.py @@ -0,0 +1,35 @@ +from config.logger import setup_logging +from core.utils.util import check_model_key + +TAG = __name__ +logger = setup_logging() + +HASS_CACHE = {} + + +def append_devices_to_prompt(conn): + if conn.use_function_call_mode: + funcs = conn.config["Intent"]["function_call"].get("functions", []) + if "hass_get_state" in funcs or "hass_get_state" in funcs: + prompt = "下面是我家智能设备,可以通过homeassistant控制\n" + devices = conn.config["plugins"]["home_assistant"].get("devices", []) + if len(devices) == 0: + return + for device in devices: + prompt += device + "\n" + conn.prompt += prompt + # 更新提示词 + conn.dialogue.update_system_message(conn.prompt) + + +def initialize_hass_handler(conn): + global HASS_CACHE + if HASS_CACHE == {}: + if conn.use_function_call_mode: + funcs = conn.config["Intent"]["function_call"].get("functions", []) + if "hass_get_state" in funcs or "hass_get_state" in funcs: + HASS_CACHE['base_url'] = conn.config["plugins"]["home_assistant"].get("base_url") + HASS_CACHE['api_key'] = conn.config["plugins"]["home_assistant"].get("api_key") + + check_model_key("home_assistant", HASS_CACHE['api_key']) + return HASS_CACHE