AI聊職場2025.6.27---創建AI Agent (人工智慧代理人)範例

 


Q:A創建I Agent (人工智慧代理人)範例
AI:

AI 代理人 (AI Agent) 是一個能夠感知環境、做出決策並採取行動以達成特定目標的軟體實體。它們可以從簡單的任務自動化到複雜的自主系統。以下將提供一個簡單的 Python AI 代理人範例,並解釋其核心概念。
什麼是 AI 代理人?
一個 AI 代理人通常包含以下核心組件:
  * **感知 (Perception):** 代理人從環境中獲取資訊的能力。這可以是透過感測器、API 呼叫、使用者輸入等方式。
  * **思考/決策 (Thinking/Decision-making):** 代理人根據感知到的資訊和其內建的規則或模型來分析情況並決定下一步的行動。
  * **行動 (Action):** 代理人對環境產生影響的能力。這可以是執行程式碼、發送訊息、控制硬體等。
  * **目標 (Goal):** 代理人希望達成的最終狀態或任務。

簡單的 AI 代理人範例:智能恆溫器
這個範例是一個模擬智能恆溫器的 AI 代理人。它的目標是將室溫維持在一個理想的範圍內。

```python
import time

class SmartThermostatAgent:
    def __init__(self, target_temperature=24, temperature_tolerance=1):
        self.target_temperature = target_temperature  # 目標溫度
        self.temperature_tolerance = temperature_tolerance  # 溫度容忍度
        self.current_temperature = 22  # 模擬當前室溫
        print(f"智能恆溫器代理人已啟動。目標溫度:{self.target_temperature}°C")

    def perceive_temperature(self):
        """感知:模擬獲取當前室溫。"""
        # 在實際應用中,這會是從溫度感測器讀取數據
        # 為了演示,我們隨機模擬溫度變化
        import random
        change = random.uniform(-0.5, 0.5)
        self.current_temperature += change
        print(f"感知到當前室溫:{self.current_temperature:.2f}°C")
        return self.current_temperature

    def decide_action(self, current_temp):
        """決策:根據當前室溫決定下一步行動。"""
        lower_bound = self.target_temperature - self.temperature_tolerance
        upper_bound = self.target_temperature + self.temperature_tolerance

        if current_temp < lower_bound:
            return "TURN_ON_HEATER"
        elif current_temp > upper_bound:
            return "TURN_ON_AC"
        else:
            return "DO_NOTHING"

    def execute_action(self, action):
        """行動:執行決策。"""
        if action == "TURN_ON_HEATER":
            print("執行動作:開啟暖氣。")
            # 實際中會發送指令給暖氣設備
            self.current_temperature += 0.8  # 模擬暖氣升溫
        elif action == "TURN_ON_AC":
            print("執行動作:開啟冷氣。")
            # 實際中會發送指令給冷氣設備
            self.current_temperature -= 0.8  # 模擬冷氣降溫
        elif action == "DO_NOTHING":
            print("執行動作:保持現狀。")
        else:
            print(f"未知動作:{action}")

    def run(self, cycles=10):
        """代理人運行主循環。"""
        print("\n--- 代理人運行中 ---")
        for i in range(cycles):
            print(f"\n--- 週期 {i+1} ---")
            temp = self.perceive_temperature()  # 感知
            action = self.decide_action(temp)  # 決策
            self.execute_action(action)         # 行動
            time.sleep(1) # 模擬時間流逝

創建並運行代理人
if __name__ == "__main__":
    thermostat_agent = SmartThermostatAgent(target_temperature=24, temperature_tolerance=1.5)
    thermostat_agent.run(cycles=15)
```

 程式碼解釋
1.  **`SmartThermostatAgent` 類別:**
      * `__init__`: 初始化代理人,設定目標溫度 (`target_temperature`) 和容忍度 (`temperature_tolerance`)。
      * `perceive_temperature`: 模擬從環境中獲取當前室溫。在真實情況下,這會連接到感測器。
      * `decide_action`: 這是代理人的**決策邏輯**。它根據當前溫度與目標溫度的比較來決定是否開啟暖氣、冷氣或保持現狀。
      * `execute_action`: 根據決策執行相應的動作。這會模擬對環境的影響(例如,升高或降低模擬溫度)。
      * `run`: 這是代理人的主循環。它不斷地重複「感知 -\> 決策 -\> 行動」的過程,直到達到預設的循環次數。

這個範例的 AI 代理人特性
  * **目標導向:** 代理人有明確的目標(將室溫維持在特定範圍內)。
  * **響應式:** 它會根據環境變化(室溫)做出反應。
  * **自主性:** 它在運行時不需要持續的人類干預。

進階考量
這個範例是一個非常基礎的 AI 代理人。更複雜的 AI 代理人可能包含:
  * **學習能力:** 代理人可以透過經驗學習並改進其決策。這可能涉及機器學習模型。
  * **規劃能力:** 代理人能夠預測未來的結果並制定一系列行動來達成長期目標。
  * **多代理人系統:** 多個 AI 代理人可以互相協作或競爭,共同完成任務。
  * **記憶/狀態:** 代理人可以維護內部狀態或記憶,以影響其未來的決策。
這個範例為您提供了一個理解 AI 代理人基本概念的起點。您可以根據此基礎擴展,創建更複雜和智能的代理人應用。

 

文章標籤
全站熱搜
創作者介紹
創作者 AI芯視界 的頭像
AI芯視界

AI芯視界

AI芯視界 發表在 痞客邦 留言(0) 人氣(11)