การนำเอเจนต์น้ำหนักเบาไปใช้งานที่เอดจ์
เรียกใช้โมเดลขนาดเล็กบน Raspberry Pi และอุปกรณ์เอดจ์เพื่อการตอบสนองที่หน่วงต่ำ
การนำเอเจนต์น้ำหนักเบาไปใช้งานที่เอดจ์ เป็นบทเรียน AI Agents ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents มีบทเรียนทั้งหมด 4 บทเรียน
Agent AI ที่ขอบเครือข่าย
Agent ที่ขอบเครือข่ายทำงานโดยตรงบนอุปกรณ์ IoT หรือเกตเวย์ภายใน เช่น Raspberry Pi, Jetson Nano หรือ PC สำหรับงานอุตสาหกรรม แทนที่จะทำงานบนคลาวด์ ประโยชน์คือความหน่วงต่ำลง (ไม่ต้องเดินทางไปกลับ) ทำงานแบบออฟไลน์ได้ และใช้แบนด์วิดท์น้อยลง ข้อแลกเปลี่ยนคือข้อจำกัดด้านการประมวลผลและ RAM ทำให้ต้องใช้โมเดลที่เล็กและมีประสิทธิภาพมากขึ้น
การเลือกโมเดลสำหรับการใช้งานที่ขอบเครือข่าย
อุปกรณ์ขอบเครือข่ายไม่สามารถใช้งาน GPT-4o หรือ Claude Opus ได้ โมเดลขนาดเล็กที่ใช้งานได้บน Raspberry Pi 5 (RAM 8 GB) ได้แก่ Phi-3-mini (พารามิเตอร์ 3.8B), Gemma-2B และ TinyLlama-1.1B เมื่อทำให้โมเดลเหล่านี้เป็นควอนไทซ์ 4 บิต จะใช้ RAM 1–3 GB และทำงานที่ความเร็ว 5–15 โทเค็นต่อวินาทีบน CPU
# Model size reference for edge selection:
EDGE_MODELS = {
'tinyllama-1.1b-q4': {
'params': '1.1B', 'quantization': 'Q4_K_M',
'ram_gb': 0.8, 'tokens_per_sec_cpu': 15,
'use_case': 'simple classification, keyword detection'
},
'phi-3-mini-q4': {
'params': '3.8B', 'quantization': 'Q4_K_M',
'ram_gb': 2.5, 'tokens_per_sec_cpu': 8,
'use_case': 'reasoning, multi-step decisions'
},
'gemma-2b-q4': {
'params': '2B', 'quantization': 'Q4_K_M',
'ram_gb': 1.5, 'tokens_per_sec_cpu': 10,
'use_case': 'general assistant tasks'
}
}
for name, info in EDGE_MODELS.items():
print(f'{name}: {info["ram_gb"]}GB RAM, '
f'{info["tokens_per_sec_cpu"]} tok/s — {info["use_case"]}')การติดตั้ง Ollama บน Raspberry Pi
Ollama เป็นวิธีที่ง่ายที่สุดในการเรียกใช้ LLM ขนาดเล็กบนฮาร์ดแวร์ภายในเครื่อง โดยจัดการการดาวน์โหลดโมเดล การทำควอนไทซ์ และ REST API ภายในเครื่องที่เข้ากันได้กับ OpenAI SDK คำสั่งหนึ่งใช้ติดตั้ง และอีกคำสั่งใช้ดึงข้อมูลพร้อมเริ่มโมเดล
# Install Ollama (run on the Raspberry Pi terminal):
# curl -fsSL https://ollama.ai/install.sh | sh
# Pull and run a model:
# ollama pull phi3:mini
# ollama serve (starts API on localhost:11434)
# Python client — uses the OpenAI-compatible endpoint:
from openai import OpenAI
local_client = OpenAI(
base_url='http://localhost:11434/v1',
api_key='ollama' # Ollama ignores this but it is required by the SDK
)
def local_inference(prompt: str, model: str = 'phi3:mini') -> str:
response = local_client.chat.completions.create(
model=model,
messages=[{'role': 'user', 'content': prompt}],
max_tokens=256,
temperature=0.1
)
return response.choices[0].message.content
result = local_inference('Is temperature 45C dangerous for a server room?')
print(result)ควอนไทซ์ GGUF แบบ 4 บิต
GGUF (รูปแบบรวมที่สร้างโดย GPT) คือรูปแบบไฟล์ที่ llama.cpp และ Ollama ใช้สำหรับโมเดลที่ผ่านการทำควอนไทซ์แล้ว Q4_K_M หมายถึงควอนไทซ์แบบผสม 4 บิต ซึ่งมีขนาดเล็กกว่าประมาณ 75% เมื่อเทียบกับรูปแบบ 32 บิต โดยสูญเสียคุณภาพเพียงเล็กน้อยสำหรับงานให้เหตุผลที่ขอบเครือข่าย
# Understanding quantisation quality levels:
QUANTISATION_GUIDE = {
'Q2_K': {'size_multiplier': 0.25, 'quality': 'poor', 'ram': 'minimal'},
'Q4_K_M': {'size_multiplier': 0.45, 'quality': 'good', 'ram': 'low'},
'Q5_K_M': {'size_multiplier': 0.55, 'quality': 'better', 'ram': 'medium'},
'Q8_0': {'size_multiplier': 0.75, 'quality': 'near-original', 'ram': 'high'},
'F16': {'size_multiplier': 1.0, 'quality': 'original', 'ram': 'full'}
}
# For edge: Q4_K_M is the sweet spot
# 7B model: 7B * 4bit/8 = 3.5 GB in Q4 vs 14 GB in F16
def estimate_vram_gb(param_billions: float, quant: str) -> float:
multiplier = QUANT_GUIDE = {
'Q4_K_M': 0.45, 'Q8_0': 0.75, 'F16': 1.0
}
return round(param_billions * multiplier.get(quant, 0.5), 2)
print(f'Phi-3-mini Q4_K_M: {estimate_vram_gb(3.8, "Q4_K_M")} GB')
print(f'Phi-3-mini F16: {estimate_vram_gb(3.8, "F16")} GB')การเพิ่มความเร็วการอนุมานบน CPU
บนอุปกรณ์ขอบเครือข่ายที่ใช้ CPU เท่านั้น ความเร็วการอนุมานขึ้นอยู่กับจำนวนเธรดและการแคชพรอมต์ กำหนดจำนวนเธรดให้ตรงกับจำนวนแกนของ CPU ทำให้พรอมต์ระบบสั้น (ระบบจะประมวลผลซ้ำทุกครั้งที่เรียกใช้หากไม่ได้แคชไว้) และรวมคำขอที่ซ้ำกันเป็นชุดเมื่อทำได้
import os
import time
# Ollama environment variables for performance tuning
# Set before starting the Ollama service:
# OLLAMA_NUM_PARALLEL=1 (single request at a time on small devices)
# OLLAMA_MAX_LOADED_MODELS=1 (only keep one model in RAM)
def timed_inference(prompt: str, client, model: str = 'phi3:mini') -> dict:
start = time.time()
response = client.chat.completions.create(
model=model,
messages=[{'role': 'user', 'content': prompt}],
max_tokens=128
)
elapsed = time.time() - start
text = response.choices[0].message.content
tokens = len(text.split()) # approximate
return {
'text': text,
'elapsed_s': round(elapsed, 2),
'approx_tps': round(tokens / elapsed, 1)
}
result = timed_inference('Classify: temperature=45C, normal range 18-30C. Action?',
local_client)
print(result)สถาปัตยกรรมแบบไฮบริดเอดจ์-คลาวด์
สถาปัตยกรรมที่เหมาะสมที่สุดใช้โมเดลเอดจ์สำหรับการตัดสินใจที่รวดเร็วและมีความเสี่ยงต่ำ (การจำแนกประเภท ตรรกะตามค่าเกณฑ์) และซิงค์ไปยังโมเดลคลาวด์สำหรับการตัดสินใจที่ซับซ้อน เกิดขึ้นไม่บ่อย หรือมีความเสี่ยงสูง เอเจนต์เอดจ์จะจัดคิวคำถามที่ซับซ้อนและส่งเมื่อการเชื่อมต่อเอื้ออำนวย
import anthropic
import time
class HybridAgent:
def __init__(self, edge_client, cloud_api_key: str):
self.edge = edge_client # Ollama local client
self.cloud = anthropic.Anthropic(api_key=cloud_api_key)
self.pending_cloud_queries = []
def decide(self, context: dict) -> str:
# Fast path: edge model for simple binary decisions
simple_prompt = (
f'Sensor: {context}. '
'Respond with exactly one word: NORMAL or ALERT.'
)
edge_result = self.edge.chat.completions.create(
model='phi3:mini',
messages=[{'role': 'user', 'content': simple_prompt}],
max_tokens=5
).choices[0].message.content.strip()
if edge_result == 'ALERT':
# Queue complex analysis for cloud
self.pending_cloud_queries.append(context)
return edge_result
def sync_to_cloud(self):
"""Call when online connectivity is available."""
for ctx in self.pending_cloud_queries:
result = self.cloud.messages.create(
model='claude-opus-4-5', max_tokens=512,
messages=[{'role': 'user', 'content':
f'Full analysis of: {ctx}'}]
)
print('Cloud analysis:', result.content[0].text[:100])
self.pending_cloud_queries.clear()คิวออฟไลน์สำหรับการซิงค์กับคลาวด์
เอเจนต์เอดจ์มักทำงานในสภาพแวดล้อมที่การเชื่อมต่อขาดหายเป็นระยะ ให้บัฟเฟอร์คำถาม ค่าที่อ่านได้จากเซนเซอร์ และบันทึกการดำเนินการไว้ภายในเครื่อง จากนั้นส่งข้อมูลทั้งหมดไปยังคลาวด์เมื่อการเชื่อมต่อกลับคืนมา ใช้ฐานข้อมูล SQLite เป็นบัฟเฟอร์ภายในเครื่อง
import sqlite3
import json
from datetime import datetime
DB_PATH = '/home/pi/agent_buffer.db'
def init_buffer_db():
conn = sqlite3.connect(DB_PATH)
conn.execute("""
CREATE TABLE IF NOT EXISTS sync_queue (
id INTEGER PRIMARY KEY AUTOINCREMENT,
type TEXT NOT NULL,
payload TEXT NOT NULL,
created_at TEXT NOT NULL,
synced INTEGER DEFAULT 0
)
""")
conn.commit()
conn.close()
def buffer_for_sync(record_type: str, payload: dict):
conn = sqlite3.connect(DB_PATH)
conn.execute(
'INSERT INTO sync_queue (type, payload, created_at) VALUES (?, ?, ?)',
(record_type, json.dumps(payload), datetime.utcnow().isoformat())
)
conn.commit()
conn.close()
def flush_to_cloud(cloud_fn):
conn = sqlite3.connect(DB_PATH)
rows = conn.execute(
'SELECT id, type, payload FROM sync_queue WHERE synced=0 LIMIT 100'
).fetchall()
for row_id, r_type, payload in rows:
cloud_fn(r_type, json.loads(payload))
conn.execute('UPDATE sync_queue SET synced=1 WHERE id=?', (row_id,))
conn.commit()
conn.close()
print(f'Synced {len(rows)} records to cloud')
if __name__ == '__main__':
import os, tempfile
DB_PATH = os.path.join(tempfile.gettempdir(), 'agent_buffer_demo.db')
init_buffer_db()
buffer_for_sync('sensor_reading', {'temp': 22.5})
flush_to_cloud(lambda t, p: print(f'Synced to cloud: {t} -> {p}'))
การตรวจสอบสถานะของเอเจนต์เอดจ์
อุปกรณ์เอดจ์อาจร้อนเกินไป มี RAM เหลือน้อย หรือทำให้การอนุมานของโมเดลหยุดชะงัก ให้ใช้ตัวตรวจสอบสถานะที่ตรวจสอบอุณหภูมิ CPU, RAM ที่ว่าง และเวลาแฝงของการอนุมาน แล้วเผยแพร่ตัวชี้วัดสถานะไปยังคลาวด์
import subprocess
import psutil # pip install psutil
def get_edge_health() -> dict:
cpu_percent = psutil.cpu_percent(interval=1)
ram = psutil.virtual_memory()
disk = psutil.disk_usage('/')
# Read CPU temperature (Raspberry Pi specific)
try:
temp_output = subprocess.check_output(
['vcgencmd', 'measure_temp'], text=True
)
cpu_temp = float(temp_output.strip().replace("temp=", "").replace("'C", ""))
except Exception:
cpu_temp = -1.0 # not available on non-Pi hardware
return {
'cpu_percent': cpu_percent,
'cpu_temp_c': cpu_temp,
'ram_used_pct': ram.percent,
'ram_available_mb': round(ram.available / 1024 / 1024),
'disk_used_pct': disk.percent,
'timestamp': datetime.utcnow().isoformat()
}
health = get_edge_health()
print('Edge health:', health)การแคชการอนุมานสำหรับเอเจนต์เอดจ์
โมเดลเอดจ์ทำงานช้า ให้แคชคำตอบสำหรับอินพุตที่เหมือนกันทุกประการ สำหรับงานจำแนกประเภทจากเซนเซอร์ พรอมต์เดียวกัน (เช่น "temperature=23.5, classify") มักปรากฏซ้ำ แคชแบบพจนานุกรมอย่างง่ายที่มี TTL จะช่วยหลีกเลี่ยงการเรียกใช้การอนุมานซ้ำโดยไม่จำเป็น
from datetime import datetime, timedelta
import hashlib
class InferenceCache:
def __init__(self, ttl_seconds: int = 60):
self.ttl = timedelta(seconds=ttl_seconds)
self._cache: dict = {} # hash -> {'result', 'expires'}
def _key(self, prompt: str) -> str:
return hashlib.md5(prompt.encode()).hexdigest()
def get(self, prompt: str):
key = self._key(prompt)
entry = self._cache.get(key)
if entry and datetime.utcnow() < entry['expires']:
return entry['result']
return None
def set(self, prompt: str, result: str):
key = self._key(prompt)
self._cache[key] = {
'result': result,
'expires': datetime.utcnow() + self.ttl
}
cache = InferenceCache(ttl_seconds=60)
def cached_edge_inference(prompt: str, client) -> str:
cached = cache.get(prompt)
if cached:
print('Cache hit')
return cached
result = local_inference(prompt, client)
cache.set(prompt, result)
return resultรายการตรวจสอบก่อนนำไปใช้งาน
ก่อนนำเอเจนต์เอดจ์ไปใช้งานจริง ให้ตรวจสอบว่า โมเดลใช้พื้นที่ใน RAM ที่มีอยู่ได้โดยเหลือสำรอง 20%, เวลาแฝงของการอนุมานเป็นไปตามข้อกำหนดด้านการตอบสนองของเหตุการณ์, บัฟเฟอร์ออฟไลน์ผ่านการทดสอบ, ระบบตรวจสอบสถานะกำลังเผยแพร่ข้อมูล และมีการตั้งค่าให้เริ่มต้นใหม่โดยอัตโนมัติเมื่อเกิดข้อขัดข้อง
# systemd service file for auto-restart (save as /etc/systemd/system/edge-agent.service)
SYSTEMD_SERVICE = '''
[Unit]
Description=IoT Edge Agent
After=network.target ollama.service
Requires=ollama.service
[Service]
User=pi
WorkingDirectory=/home/pi/edge-agent
ExecStart=/usr/bin/python3 /home/pi/edge-agent/agent.py
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
'''
# Enable:
# sudo systemctl enable edge-agent
# sudo systemctl start edge-agent
# sudo journalctl -u edge-agent -f (follow logs)
print('Deployment checklist:')
checklist = [
'Model RAM fits with 20% headroom',
'Inference latency < response time requirement',
'Offline SQLite buffer tested',
'Health metrics publishing to cloud',
'systemd auto-restart configured'
]
for item in checklist:
print(f' [ ] {item}')การตั้งค่าฮาร์ดแวร์ Raspberry Pi
ก่อนนำซอฟต์แวร์ไปใช้งาน ให้เตรียมฮาร์ดแวร์ให้พร้อม Raspberry Pi 5 ที่มี RAM ขนาด 8 GB เป็นรุ่นขั้นต่ำที่แนะนำสำหรับการเรียกใช้ Phi-3-mini เปิดใช้การแบ่งหน่วยความจำ GPU ปิดการใช้สว็อป (เพราะทำให้อุปกรณ์จัดเก็บข้อมูลแบบแฟลชเสื่อมเร็วขึ้น) และกำหนด IP แบบคงที่เพื่อให้เข้าถึงจากระยะไกลได้อย่างน่าเชื่อถือ
# Raspberry Pi setup notes (run manually over SSH):
# sudo raspi-config -> System -> GPU Memory -> 256
# Disable swap to protect SD card:
# sudo dphys-swapfile swapoff && sudo dphys-swapfile uninstall
def check_available_ram_mb():
try:
with open('/proc/meminfo') as f:
for line in f:
if line.startswith('MemAvailable'):
return int(line.split()[1]) // 1024
except FileNotFoundError:
return None
ram_mb = check_available_ram_mb()
if ram_mb is None:
print('Could not read /proc/meminfo on this OS — simulated Pi reading: MemAvailable ~ 850 MB')
else:
print(f'Available RAM: {ram_mb} MB')ตรวจสอบความรู้
การทำควอนไทซ์แบบ Q4_K_M สำหรับโมเดลภาษาหมายความว่าอย่างไร
ทบทวน: การนำเอเจนต์น้ำหนักเบาไปใช้งานบนเอดจ์
สิ่งที่คุณได้เรียนรู้ในบทเรียนนี้:
- การเลือกโมเดล: Phi-3-mini, Gemma-2B, TinyLlama สำหรับเอดจ์ และการทำควอนไทซ์แบบ Q4_K_M
- Ollama: เซิร์ฟเวอร์ LLM ภายในเครื่องที่มี API เข้ากันได้กับ OpenAI บนพอร์ต 11434
- สถาปัตยกรรมแบบไฮบริด: โมเดลเอดจ์สำหรับการตัดสินใจที่รวดเร็ว และคลาวด์สำหรับการวิเคราะห์ที่ซับซ้อน
- บัฟเฟอร์ออฟไลน์: คิว SQLite ที่ส่งข้อมูลไปยังคลาวด์เมื่อเชื่อมต่ออีกครั้ง
- แคชการอนุมาน: แคช TTL ช่วยหลีกเลี่ยงการอนุมานซ้ำสำหรับพรอมต์ที่เหมือนกัน
- การนำไปใช้งาน: บริการ systemd สำหรับเริ่มต้นใหม่โดยอัตโนมัติเมื่อเกิดข้อขัดข้อง
หลักสูตรถัดไป: ตลาดซื้อขายเอเจนต์และระบบปลั๊กอิน
คำถามที่พบบ่อย
บทเรียน “การนำเอเจนต์น้ำหนักเบาไปใช้งานที่เอดจ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การนำเอเจนต์น้ำหนักเบาไปใช้งานที่เอดจ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การนำเอเจนต์น้ำหนักเบาไปใช้งานที่เอดจ์”
เรียกใช้โมเดลขนาดเล็กบน Raspberry Pi และอุปกรณ์เอดจ์เพื่อการตอบสนองที่หน่วงต่ำ คุณปฏิบัติ AI Agents ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การนำเอเจนต์น้ำหนักเบาไปใช้งานที่เอดจ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Agents นี้ได้ไหม
ได้ บทเรียน AI Agents ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โพรโทคอล MQTT สำหรับการผสานเอเจนต์
- การประมวลผลข้อมูลอนุกรมเวลาในเอเจนต์
- การตอบสนองอัตโนมัติต่อเหตุการณ์จากเซนเซอร์
- การนำเอเจนต์น้ำหนักเบาไปใช้งานที่เอดจ์