目录

Smart Home Agent - OpenVela 智能家居控制系统

基于 openvela 平台与 openvela 智能体 AI Agent 框架的自然语言驱动智能家居控制原型系统。

架构概览

用户输入(自然语言)
        |
        v
  +------------+
  | Agent Core |  (ReAct 推理引擎)
  |  Reason    |  ← 思考:理解用户意图
  |  Act       |  ← 行动:调用工具
  |  Observe   |  ← 观察:获取结果
  +-----+------+
        |
   +----+----+
   |         |
   v         v
+------+  +--------+     +-------+
| Tool |  |  LLM   |     |  MCP  |
| Reg. |  | Backend |     |Client |
+------+  +--------+     +-------+
   |         |               |
   v         v               v
+------+  +--------+     +-------+
|Light |  | HTTP   |     |MCP    |
|Temp  |  | Client |     |Server |
|Dev.  |  +--------+     +-------+
+------+
   |
   v
+------------+
| Security   |
| (ACL)      |
+------------+

功能特性

  • ReAct 推理引擎:Thought-Action-Observation 循环,智能决策
  • 工具注册表:动态工具注册,支持参数 schema 定义
  • 多 LLM 后端:Kimi(月之暗面)和 DeepSeek,统一接口切换
  • 安全模块:命令黑白名单策略和文件访问权限控制
  • MCP 协议:JSON-RPC 2.0 的 Model Context Protocol 客户端
  • Skill 系统:Markdown 格式的多步骤家居自动化场景
  • 零动态内存分配:所有数据结构使用固定大小缓冲区,总内存占用 < 32KB

快速开始

环境要求

  • 操作系统:Ubuntu 22.04+ / Windows 10+ (WSL2) / macOS
  • 编译器:GCC 10+ (支持 C99 标准)
  • CMake 3.10+(可选)
  • 网络连接(用于调用 LLM API)

1. 获取源码

# 克隆项目
git clone <repository_url> smart_home_agent
cd smart_home_agent

2. 编译

Linux / macOS

# 使用 CMake
mkdir build && cd build
cmake ..
make

# 或使用 Makefile
make

Windows (MinGW)

mkdir build && cd build
cmake -G "MinGW Makefiles" ..
mingw32-make

Windows (MSVC)

mkdir build && cd build
cmake -G "Visual Studio 17 2022" ..
cmake --build . --config Release

3. 配置 LLM 后端

方式一:修改配置文件

编辑 config/llm_deepseek.json,替换 API Key:

{
    "provider": "deepseek",
    "model": "deepseek-chat",
    "api_key": "sk-your-actual-api-key",
    "base_url": "https://api.deepseek.com"
}

或编辑 config/llm_kimi.json

{
    "provider": "kimi",
    "model": "moonshot-v1-8k",
    "api_key": "sk-your-actual-api-key",
    "base_url": "https://api.moonshot.cn"
}

切换 LLM 后端:编辑 config/agent_config.json,修改 llm_config 字段:

{
    "llm_config": "config/llm_kimi.json",
    "security_config": "config/security_policy.json",
    "system_prompt": "..."
}

方式二:环境变量

# Linux / macOS
export LLM_API_KEY="sk-your-api-key"
export LLM_BASE_URL="https://api.deepseek.com"
export LLM_MODEL="deepseek-chat"

# Windows PowerShell
$env:LLM_API_KEY="sk-your-api-key"
$env:LLM_BASE_URL="https://api.deepseek.com"
$env:LLM_MODEL="deepseek-chat"

4. 运行

./smart_home_agent [config_path]

# 指定配置文件
./smart_home_agent config/agent_config.json

# 使用环境变量配置
LLM_API_KEY=sk-xxx ./smart_home_agent

5. 交互命令

命令 说明
自然语言输入 控制智能家居设备
status 显示所有设备状态
tools 列出可用工具
mcp_list 列出 MCP 工具
reset 重置对话
help 显示帮助
quit 退出程序

示例交互

You> 把客厅的灯打开
Agent> 已为您打开客厅的灯。

You> 现在卧室温度多少?
Agent> 卧室当前温度为 23.5°C,湿度为 45.2%。

You> 我要出门了
Agent> 离家模式已激活,所有灯光已关闭。

You> 家里所有设备的状态
Agent> 当前设备状态:
  灯光 - 客厅:关, 卧室:关, 厨房:关, 卫生间:关
  传感器 - 客厅:22.5°C/50%, 卧室:23.0°C/45%, 室外:15.0°C/60%

项目结构

smart_home_agent/
├── src/                          # C 语言源码
│   ├── main.c                    # 主程序入口,交互式命令行界面
│   ├── agent_core.c/h            # ReAct 推理引擎(Reason→Act→Observe 循环)
│   ├── tool_registry.c/h         # 工具注册表(动态注册/注销/执行)
│   ├── tools/                    # 自定义工具
│   │   ├── light_control.c/h     # 灯光控制(4房间,on/off/query)
│   │   ├── temperature_sensor.c/h # 温湿度传感器(3位置,模拟数据)
│   │   └── device_status.c/h     # 设备状态汇总(JSON格式)
│   ├── llm/                      # LLM 后端
│   │   ├── llm_backend.c/h       # LLM 后端抽象层(Kimi/DeepSeek 切换)
│   │   └── http_client.c/h       # HTTP 客户端(Winsock/POSIX 双平台)
│   ├── security/                 # 安全模块
│   │   └── access_control.c/h    # 命令黑白名单、文件访问权限控制
│   └── mcp/                      # MCP 协议
│       └── mcp_client.c/h        # MCP 客户端(JSON-RPC 2.0)
├── skills/                       # Skill 文件(Markdown 格式)
│   ├── morning_scene.md          # 早晨起床场景
│   ├── leave_home.md             # 离家模式场景
│   └── good_night.md             # 晚安模式场景
├── config/                       # 配置文件(JSON 格式)
│   ├── agent_config.json         # Agent 配置
│   ├── llm_kimi.json             # Kimi LLM 配置
│   ├── llm_deepseek.json         # DeepSeek LLM 配置
│   └── security_policy.json      # 安全策略配置
├── CMakeLists.txt                # CMake 构建文件
├── Makefile                      # Make 构建文件
└── README.md                     # 项目说明

自定义工具开发

工具接口

typedef int (*tool_execute_fn)(const char *args, char *result, int result_size);

int tool_registry_register(tool_registry_t *registry,
                            const char *name,
                            const char *description,
                            const char *param_schema,
                            tool_execute_fn execute);

开发新工具示例

// my_tool.c
#include "tool_registry.h"

static int my_tool_execute(const char *args, char *result, int result_size)
{
    // 解析 args(JSON 格式)
    // 执行工具逻辑
    // 将结果写入 result
    snprintf(result, result_size, "{\"status\":\"ok\"}");
    return 0;
}

int my_tool_register(tool_registry_t *registry)
{
    return tool_registry_register(registry,
        "my_tool",
        "Description of my tool",
        "{\"param1\":\"string\",\"param2\":\"number\"}",
        my_tool_execute);
}

注册工具

main.c 中添加:

extern int my_tool_register(tool_registry_t *registry);

// 在 main() 函数中:
my_tool_register(&registry);

Skill 文件格式

Skill 文件使用 Markdown 格式,定义多步骤家居场景:

# 场景名称

## Description
场景描述

## Trigger
- 语音触发:"关键词"

## Dependencies
- tool_name_1
- tool_name_2

## Steps

### Step 1: 步骤描述
- Tool: `tool_name`
- Args: `{"param": "value"}`
- Condition: 执行条件

## Expected Outcome
- 预期结果

安全策略配置

config/security_policy.json 支持 4 种策略模式:

模式 说明
allow_all 允许所有命令(排除黑名单)
deny_all 拒绝所有命令(仅允许白名单)
whitelist 仅允许白名单中的命令
blacklist 允许所有命令(排除黑名单中的)

openvela 模拟器部署

环境搭建(Ubuntu 22.04)

# 安装依赖
sudo apt update
sudo apt install -y git python3-pip ninja-build cmake gcc-arm-none-eabi \
    libncurses-dev libssl-dev build-essential git-lfs
pip3 install kconfiglib pyelftools cxxfilt

# 克隆 openvela 源码
repo init -u https://gitee.com/open-vela/manifests.git -b trunk -m openvela.xml \
    --repo-url=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/ --git-lfs
repo sync -c -j8

# 编译 QEMU goldfish-arm64 目标
./build.sh vendor/openvela/boards/vela/configs/goldfish-arm64-v8a-ap/ --cmake -j8

# 运行模拟器
./emulator.sh

将智能家居应用集成到 openvela

  1. smart_home_agent/src/ 目录复制到 apps/smart_home_agent/
  2. 创建 apps/smart_home_agent/KconfigMakefile
  3. 在 defconfig 中启用应用
  4. 重新编译系统镜像

LLM 后端对比

特性 DeepSeek Kimi (Moonshot)
模型 deepseek-chat moonshot-v1-8k
API 兼容 OpenAI 格式 OpenAI 格式
工具调用 function_calling + 文本标记 function_calling
中文理解 优秀 优秀
响应速度
价格

内存占用

系统专为嵌入式平台设计:

模块 内存占用
agent_core_t ~28KB
tool_registry_t ~4KB
llm_backend_t ~2KB
access_control_t ~4KB
工具数据 ~1KB
总计 ~32KB

License

Apache 2.0

关于

Natural Language Smart Home Control System based on OpenVela AI Agent Framework

70.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802032778号