Langchain家庭助手Agent

1. 系统概述

1.1 功能需求
  • 感知:接收用户的口头指令,并通过传感器获取环境信息(如温度、湿度等)。
  • 行动:控制家庭电器(灯、窗帘、加湿器、电视、冰箱、空调等)。
  • 思考:根据用户习惯和当前状态,提供优化建议(如时间管理、节能建议等),并在必要时请求用户确认。
1.2 技术栈
  • LangChain:用于构建对话 Agent 和工具链。
  • OpenAI 或其他大语言模型:作为核心推理引擎。
  • 硬件接口:模拟或实际的家庭设备 API。
  • 传感器数据:通过模拟或真实传感器获取环境信息。

2. 架构设计

2.1 感知模块

感知模块负责接收用户输入和获取环境信息:

  • 用户输入可以通过语音转文字工具(如 Whisper API)转换为文本。
  • 环境信息可以通过传感器获取(如温度、湿度、光线强度等)。
2.2 行动模块

行动模块封装了对家庭电器的控制逻辑:

  • 每个设备都有对应的控制函数(如 toggle_lightcontrol_curtains 等)。
  • 这些函数被封装为 LangChain 的 Tool
2.3 思考模块

思考模块负责以下任务:

  • 分析用户习惯(如记录用户在特定时间的操作)。
  • 提供优化建议(如提醒用户关闭不必要的电器以节省能源)。
  • 根据环境信息调整设备状态(如根据湿度自动调节加湿器)。

3. 示例代码

3.1 定义硬件接口

我们首先定义一个模拟的家庭硬件接口:

class HomeHardwareAPI:
    def __init__(self):
        self.light_status = "off"
        self.curtain_status = "closed"
        self.humidifier_status = "off"
        self.tv_status = "off"
        self.fridge_temperature = 4.0  # 摄氏度
        self.air_conditioner_status = "off"
        self.temperature = 22.5  # 当前室内温度
        self.humidity = 50.0  # 当前湿度

    def toggle_light(self, action):
        if action.lower() == "on":
            self.light_status = "on"
            return "Light turned on."
        elif action.lower() == "off":
            self.light_status = "off"
            return "Light turned off."
        else:
            return "Invalid action."

    def control_curtains(self, action):
        if action.lower() == "open":
            self.curtain_status = "open"
            return "Curtains opened."
        elif action.lower() == "close":
            self.curtain_status = "closed"
            return "Curtains closed."
        else:
            return "Invalid action."

    def control_humidifier(self, action):
        if action.lower() == "on":
            self.humidifier_status = "on"
            return "Humidifier turned on."
        elif action.lower() == "off":
            self.humidifier_status = "off"
            return "Humidifier turned off."
        else:
            return "Invalid action."

    def control_tv(self, action):
        if action.lower() == "on":
            self.tv_status = "on"
            return "TV turned on."
        elif action.lower() == "off":
            self.tv_status = "off"
            return "TV turned off."
        else:
            return "Invalid action."

    def adjust_fridge_temperature(self, temperature):
        self.fridge_temperature = temperature
        return f"Fridge temperature set to {temperature}°C."

    def control_air_conditioner(self, action):
        if action.lower() == "on":
            self.air_conditioner_status = "on"
            return "Air conditioner turned on."
        elif action.lower() == "off":
            self.air_conditioner_status = "off"
            return "Air conditioner turned off."
        else:
            return "Invalid action."

    def get_temperature(self):
        return f"Current temperature is {self.temperature}°C."

    def get_humidity(self):
        return f"Current humidity is {self.humidity}%."

3.2 封装为 LangChain 工具

将上述硬件接口封装为 LangChain 的 Tool

from langchain.agents import Tool

home_hardware_api = HomeHardwareAPI()

def toggle_light(action):
    return home_hardware_api.toggle_light(action)

def control_curtains(action):
    return home_hardware_api.control_curtains(action)

def control_humidifier(action):
    return home_hardware_api.control_humidifier(action)

def control_tv(action):
    return home_hardware_api.control_tv(action)

def adjust_fridge_temperature(temperature):
    return home_hardware_api.adjust_fridge_temperature(temperature)

def control_air_conditioner(action):
    return home_hardware_api.control_air_conditioner(action)

def get_temperature():
    return home_hardware_api.get_temperature()

def get_humidity():
    return home_hardware_api.get_humidity()

tools = [
    Tool(
        name="Toggle Light",
        func=toggle_light,
        description="Use this tool to turn the light on or off. Input should be 'on' or 'off'."
    ),
    Tool(
        name="Control Curtains",
        func=control_curtains,
        description="Use this tool to open or close the curtains. Input should be 'open' or 'close'."
    ),
    Tool(
        name="Control Humidifier",
        func=control_humidifier,
        description="Use this tool to turn the humidifier on or off. Input should be 'on' or 'off'."
    ),
    Tool(
        name="Control TV",
        func=control_tv,
        description="Use this tool to turn the TV on or off. Input should be 'on' or 'off'."
    ),
    Tool(
        name="Adjust Fridge Temperature",
        func=adjust_fridge_temperature,
        description="Use this tool to adjust the fridge temperature. Input should be a number representing the desired temperature in Celsius."
    ),
    Tool(
        name="Control Air Conditioner",
        func=control_air_conditioner,
        description="Use this tool to turn the air conditioner on or off. Input should be 'on' or 'off'."
    ),
    Tool(
        name="Get Temperature",
        func=get_temperature,
        description="Use this tool to get the current room temperature."
    ),
    Tool(
        name="Get Humidity",
        func=get_humidity,
        description="Use this tool to get the current room humidity."
    )
]

3.3 初始化 Agent

初始化 LangChain 的 Agent,并添加对话记忆功能:

from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent
from langchain.chains.conversation.memory import ConversationBufferMemory

# 初始化对话记忆
memory = ConversationBufferMemory(memory_key="chat_history")

# 使用 OpenAI 的 GPT-3.5-turbo 模型
llm = ChatOpenAI(temperature=0)

# 初始化 Agent
agent_chain = initialize_agent(
    tools, llm, agent="chat-conversational-react-description", verbose=True, memory=memory
)

3.4 添加思考模块

思考模块可以根据用户习惯和环境信息提供建议。例如:

def provide_suggestions():
    suggestions = []

    # 根据温度建议调整空调
    if home_hardware_api.temperature > 28:
        suggestions.append("It's quite hot. Do you want me to turn on the air conditioner?")

    # 根据湿度建议调整加湿器
    if home_hardware_api.humidity < 30:
        suggestions.append("The air is dry. Do you want me to turn on the humidifier?")

    # 节能建议
    if home_hardware_api.light_status == "on" and home_hardware_api.get_temperature().split()[2] > "25":
        suggestions.append("The light is on and it's warm. You might save energy by turning it off.")

    return suggestions

将建议集成到 Agent 中:

def run_agent(user_input):
    response = agent_chain.run(user_input)
    suggestions = provide_suggestions()
    if suggestions:
        response += "\n\nSuggestions for you:\n" + "\n".join(suggestions)
    return response

3.5 主人口头指令处理

假设主人口头指令通过语音转文字工具转换为文本,我们可以直接调用 run_agent 函数:

# 示例用户输入
user_input = "Turn on the light and open the curtains."
response = run_agent(user_input)
print(response)  # 输出: Light turned on. Curtains opened.

4. 用户习惯学习

为了根据用户习惯做出调整,可以记录用户的操作历史并分析模式。例如:

import datetime

class UserHabits:
    def __init__(self):
        self.history = []

    def log_action(self, action, timestamp=None):
        if timestamp is None:
            timestamp = datetime.datetime.now()
        self.history.append((action, timestamp))

    def analyze_habits(self):
        # 简单分析用户的日常习惯
        habits = {}
        for action, timestamp in self.history:
            time_of_day = timestamp.strftime("%H:%M")
            if time_of_day not in habits:
                habits[time_of_day] = []
            habits[time_of_day].append(action)
        return habits

user_habits = UserHabits()

# 记录用户操作
user_habits.log_action("Turned on the light")
user_habits.log_action("Opened the curtains")
print(user_habits.analyze_habits())

5. 总结

通过上述设计,我们实现了一个具备感知、行动和思考能力的家庭管家 Agent。它能够:

  • 接收主人口头指令并控制家庭电器。
  • 根据环境信息提供建议。
  • 学习用户习惯并优化操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值