Next ‘26 Developer Keynote: Enhancing Agents with Memory

1. บทนำ

ใน Codelab นี้ คุณจะได้ยกระดับ Agent ของ ADK ไปอีกขั้นด้วยการเพิ่มความรู้ที่คงอยู่และเฉพาะทาง คุณจะได้เรียนรู้วิธีจัดการสถานะการสนทนาด้วยเซสชันแพลตฟอร์ม Agent, เปิดใช้การเรียนรู้ระยะยาวด้วย Memory Bank และผสานรวมข้อมูลกฎที่ซับซ้อนของเมืองโดยใช้ Spark และ AlloyDB สำหรับ RAG (Retrieval-Augmented Generation)

สิ่งที่คุณจะได้ทำ

  • กำหนดค่าเซสชันแพลตฟอร์ม Agent เพื่อให้การสนทนาคงอยู่
  • ติดตั้งใช้งาน Memory Bank เพื่อให้ Agent เรียนรู้จากการโต้ตอบก่อนหน้านี้ได้
  • ใช้ Spark Lightning Engine เพื่อนำเข้าและประมวลผลเอกสารประกอบกฎของเมือง
  • สร้างระบบ RAG โดยใช้ AlloyDB และ Vector Search
  • ติดตั้งใช้งาน Agent ที่ปรับปรุงแล้วในแพลตฟอร์ม Agent

สิ่งที่คุณต้องมี

  • เว็บเบราว์เซอร์ เช่น Chrome
  • โปรเจ็กต์ Google Cloud ที่เปิดใช้การเรียกเก็บเงินแล้ว
  • มีความคุ้นเคยกับ Python และ SQL ในระดับพื้นฐาน

ระยะเวลาโดยประมาณ: 60 นาที

ทรัพยากรที่สร้างขึ้นใน Codelab นี้ควรมีค่าใช้จ่ายไม่เกิน $5

2. ก่อนเริ่มต้น

สร้างโปรเจ็กต์ Google Cloud

  1. ใน Google Cloud Console ในหน้าตัวเลือกโปรเจ็กต์ ให้เลือกหรือสร้างโปรเจ็กต์ Google Cloud
  2. ตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ที่อยู่ในระบบคลาวด์แล้ว ดูวิธีตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินในโปรเจ็กต์แล้ว

เริ่มต้น Cloud Shell

Cloud Shell คือสภาพแวดล้อมบรรทัดคำสั่งที่ทำงานใน Google Cloud ซึ่งโหลดเครื่องมือที่จำเป็นไว้ล่วงหน้า

  1. คลิกเปิดใช้งาน Cloud Shell ที่ด้านบนของคอนโซล Google Cloud
  2. เมื่อเชื่อมต่อกับ Cloud Shell แล้ว ให้ยืนยันการตรวจสอบสิทธิ์โดยทำดังนี้
    gcloud auth list
    
  3. ยืนยันว่าได้กำหนดค่าโปรเจ็กต์แล้ว
    gcloud config get project
    
  4. หากโปรเจ็กต์ไม่ได้ตั้งค่าตามที่คาดไว้ ให้ตั้งค่าดังนี้
    export PROJECT_ID=<YOUR_PROJECT_ID>
    gcloud config set project $PROJECT_ID
    

ยืนยันการตรวจสอบสิทธิ์โดยทำดังนี้

gcloud auth list

ยืนยันโปรเจ็กต์โดยทำดังนี้

gcloud config get project

ตั้งค่าหากจำเป็นโดยทำดังนี้

export PROJECT_ID=<YOUR_PROJECT_ID>
gcloud config set project $PROJECT_ID

เปิดใช้ API

เรียกใช้คำสั่งนี้เพื่อเปิดใช้ API ที่จำเป็นทั้งหมดสำหรับการจัดการเซสชัน การประมวลผล Spark และ AlloyDB

gcloud services enable \
  aiplatform.googleapis.com \
  run.googleapis.com \
  alloydb.googleapis.com \
  dataproc.googleapis.com \
  documentai.googleapis.com \
  storage.googleapis.com \
  secretmanager.googleapis.com

3. ตั้งค่าสภาพแวดล้อม

สำหรับ Codelab นี้ คุณจะใช้สภาพแวดล้อมที่กำหนดค่าไว้ล่วงหน้าในที่เก็บข้อมูล Keynote

  1. โคลนที่เก็บข้อมูลและไปที่โฟลเดอร์โปรเจ็กต์โดยทำดังนี้
git clone https://github.com/GoogleCloudPlatform/next-26-keynotes
cd next-26-keynotes/devkey/enhancing-agents-with-memory
  1. ตั้งค่าสภาพแวดล้อมเสมือนของ Python และติดตั้งแพ็กเกจ ADK ที่จำเป็นโดยทำดังนี้
uv venv
source .venv/bin/activate
uv sync

กำหนดค่าตัวแปรสภาพแวดล้อม

Agent ต้องมีการกำหนดค่าเฉพาะเพื่อเชื่อมต่อกับแพลตฟอร์ม Agent และ AlloyDB

  1. คัดลอกไฟล์สภาพแวดล้อมตัวอย่างโดยทำดังนี้
cp .env.example .env
  1. เปิดไฟล์ .env แล้วอัปเดตช่องต่อไปนี้
    • GOOGLE_CLOUD_PROJECT: รหัสโปรเจ็กต์
    • GOOGLE_CLOUD_LOCATION: us-central1
    • ALLOYDB_CLUSTER_ID: rules-db
GOOGLE_CLOUD_PROJECT=<YOUR_PROJECT_ID>
GOOGLE_CLOUD_LOCATION=global
GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_CLOUD_REGION=us-central1
ALLOYDB_CLUSTER_ID=rules-db
  1. เรียกใช้สคริปต์ตัวช่วยต่อไปนี้เพื่อสร้างอินสแตนซ์ Agent Engine ที่จะใช้สำหรับเซสชันการสนทนาและหน่วยความจำระยะยาว ซึ่งจะป้อนข้อมูล AGENT_ENGINE_ID ในไฟล์ .env โดยอัตโนมัติ
uv run utils/setup_agent_engine.py

เมื่อดำเนินการสำเร็จ คุณควรเห็นข้อความต่อไปนี้

Creating Agent Engine instance...
Successfully created Agent Engine. ID: 1234567890
Updated .env with AGENT_ENGINE_ID=1234567890

4. สร้าง Agent ด้วยการจัดการเซสชัน

ในขั้นตอนนี้ คุณจะเริ่มต้นใช้งาน Marathon Planner Agent ที่สามารถเก็บประวัติการสนทนาไว้ได้หลายรอบ ซึ่งทำได้โดยใช้คลาส App ของ ADK และเซสชันแพลตฟอร์ม Agent

เริ่มต้นใช้งาน Agent และบริการเซสชัน

เปิดไฟล์ planner_agent/agent.py คุณจะเห็นวิธีที่เราเพิ่มคลาส ADK เพื่อผสานรวมเซสชันแพลตฟอร์ม Agent ซึ่งช่วยให้เราสามารถทำให้ Agent มีสถานะเมื่อเวลาผ่านไป และแก้ไขบริบทได้ตามต้องการ

from google.adk.agents import LlmAgent
from google.adk.sessions import VertexAiSessionService
from vertexai.agent_engines import AdkApp

PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
REGION = os.environ.get("GOOGLE_CLOUD_REGION", "us-central1")

# Initialize Vertex AI for regional services
if PROJECT_ID:
    vertexai.init(project=PROJECT_ID, location=REGION)

# Define the agent logic
root_agent = LlmAgent(
    name="planner_agent",
    model="gemini-3-flash-preview",
    instruction="You are a helpful marathon planning assistant...",
    tools=[] # We will add tools in the next steps
)

def session_service_builder():
    """Builder for Agent Platform Sessions."""
    return VertexAiSessionService(project=PROJECT_ID, location=REGION)

# Wrap the agent in an AdkApp to manage stateful context
app = AdkApp(
    agent=root_agent,
    session_service_builder=session_service_builder
)

5. เปิดใช้การเรียนรู้ระยะยาวด้วย Memory Bank

แม้ว่าการจัดการเซสชันจะติดตามการสนทนาแต่ละรายการ แต่คุณก็สามารถทำเช่นเดียวกันกับหน่วยความจำระยะยาวได้ ในขั้นตอนนี้ คุณจะแนบ Agent กับ Memory Bank ของแพลตฟอร์ม Agent ซึ่งเป็นบริการหน่วยความจำที่พร้อมใช้งานระดับองค์กรและมีการจัดการอย่างเต็มรูปแบบ

เริ่มต้นใช้งานบริการ Memory Bank

Memory Bank ช่วยให้ Agent เรียกบริบทจากเซสชันต่างๆ ได้ อัปเดตไฟล์ planner_agent/agent.py เพื่อรวมบริการหน่วยความจำโดยทำดังนี้

from google.adk.memory import VertexAiMemoryBankService

def memory_service_builder():
    """Builder for Agent Platform Memory Bank."""
    return VertexAiMemoryBankService(
        project=PROJECT_ID,
        location=REGION,
        agent_engine_id=AGENT_ENGINE_ID
    )

ติดตั้งใช้งานการนำเข้าหน่วยความจำอัตโนมัติ

เราเพิ่ม after_agent_callback เพื่อให้ Agent เรียนรู้จากทุกๆ รอบ ฟังก์ชันนี้จะทริกเกอร์หลังจากที่ Agent ตอบกลับเสร็จสมบูรณ์ ซึ่งช่วยให้ Agent "ย่อย" เซสชันและบันทึกหน่วยความจำที่เกี่ยวข้องลงใน Bank ได้

  1. กำหนดฟังก์ชัน Callback โดยทำดังนี้
async def auto_save_memories(callback_context):
    """Callback to ingest the session into the memory bank after the turn."""
    # In AdkApp, the memory service is available via the invocation context
    if hasattr(callback_context._invocation_context, 'memory_service') and callback_context._invocation_context.memory_service:
        await callback_context._invocation_context.memory_service.add_session_to_memory(
            callback_context._invocation_context.session
        )
  1. แนบการเรียกกลับกับ LlmAgent โดยทำดังนี้
root_agent = LlmAgent(
    # ... other params
    after_agent_callback=[auto_save_memories],
)

6. การตั้งค่า AlloyDB สำหรับ RAG

ก่อนที่จะนำเข้าข้อมูลกฎของเมือง เราต้องมีฐานข้อมูลประสิทธิภาพสูงเพื่อจัดเก็บข้อมูล ในขั้นตอนนี้ คุณจะสร้างคลัสเตอร์ AlloyDB และเริ่มต้นใช้งานสคีมาฐานข้อมูลสำหรับ Vector Search

1. สร้างคลัสเตอร์ AlloyDB และอินสแตนซ์หลัก

เรียกใช้คำสั่งต่อไปนี้ใน Cloud Shell เพื่อสร้างคลัสเตอร์และอินสแตนซ์หลัก

# Create the cluster
gcloud alloydb clusters create rules-db \
  --password=postgres \
  --region=us-central1

# Create the primary instance with IAM authentication enabled
gcloud alloydb instances create rules-db-primary \
  --instance-type=PRIMARY \
  --cpu-count=2 \
  --region=us-central1 \
  --cluster=rules-db \
  --database-flags=alloydb.iam_authentication=on

2. ให้สิทธิ์บทบาท IAM ที่จำเป็น

คุณต้องมีสิทธิ์เฉพาะจึงจะใช้เซิร์ฟเวอร์ MCP ของ AlloyDB ที่มีการจัดการได้ เรียกใช้คำสั่งต่อไปนี้เพื่อให้สิทธิ์บทบาทที่จำเป็น

export USER_EMAIL=$(gcloud config get-value account)

# Role to use MCP tools
gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="user:$USER_EMAIL" \
  --role="roles/mcp.toolUser"

# Role to execute SQL in AlloyDB
gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="user:$USER_EMAIL" \
  --role="roles/alloydb.admin"

# Role for IAM database authentication
gcloud projects add-iam-policy-binding $PROJECT_ID \
  --member="user:$USER_EMAIL" \
  --role="roles/alloydb.databaseUser"

# Create the IAM-based database user
gcloud alloydb users create "$USER_EMAIL" \
  --cluster=rules-db \
  --region=us-central1 \
  --type=IAM_BASED

3. สร้างฐานข้อมูลและตารางผ่าน AlloyDB Studio

เนื่องจากฐานข้อมูลและตาราง AlloyDB มีการจัดการผ่าน SQL เราจึงจะใช้ AlloyDB Studio ในคอนโซล Google Cloud เพื่อสรุปสคีมา

  1. ไปที่ AlloyDB > คลัสเตอร์ แล้วคลิก rules-db
  2. คลิก AlloyDB Studio ในเมนูการนำทางด้านซ้าย
  3. เข้าสู่ระบบโดยใช้ผู้ใช้ postgres และรหัสผ่านที่คุณตั้งไว้ (postgres)
  4. เรียกใช้ SQL ต่อไปนี้เพื่อสร้างฐานข้อมูล
    CREATE DATABASE city_rules;
    
  5. เปลี่ยนการเชื่อมต่อฐานข้อมูลเป็น city_rules ใน AlloyDB Studio แล้วเรียกใช้ SQL ต่อไปนี้เพื่อติดตั้งส่วนขยายและสร้างตาราง rules
    -- Install extensions for vector search and ML
    CREATE EXTENSION IF NOT EXISTS vector;
    CREATE EXTENSION IF NOT EXISTS google_ml_integration CASCADE;
    
    -- Create the rules table
    CREATE TABLE IF NOT EXISTS rules (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        text TEXT NOT NULL,
        city TEXT NOT NULL,
        embedding vector(3072) DEFAULT NULL
    );
    
    -- Grant your IAM user access to the table (replace with your email)
    GRANT ALL PRIVILEGES ON TABLE rules TO "YOUR_EMAIL_ADDRESS";
    

7. การนำเข้าข้อมูลกฎของเมืองด้วย Spark Lightning Engine

Agent ต้องมีข้อมูลและบริบทขององค์กรเป็นพื้นฐาน จึงจะวางแผนได้อย่างถูกต้องแม่นยำ ไม่ใช่แค่มีพรอมต์ที่สร้างขึ้นมาอย่างดีเท่านั้น ในขั้นตอนนี้ คุณจะใช้ Spark Lightning Engine ใน Dataproc Serverless เพื่อประมวลผล PDF ขนาดใหญ่ที่มีกฎของเมืองและนำเข้าข้อมูลเหล่านั้นลงใน AlloyDB

ทำไมจึงต้องใช้ Spark Lightning Engine

การให้ข้อมูลพื้นฐานแก่ Agent ในวงกว้างต้องอาศัยการประมวลผลข้อมูลที่ไม่มีโครงสร้างจำนวนมาก Spark Lightning Engine เป็นเครื่องมือการดำเนินการประสิทธิภาพสูงสำหรับ Spark ซึ่งช่วยเร่งความเร็วเวิร์กโหลดเหล่านี้ได้อย่างมาก เราใช้เครื่องมือนี้เพื่อทำการแบ่งส่วนเชิงความหมาย ในเอกสารโดยใช้ Document AI ของ Google

สำรวจไปป์ไลน์ Spark

ตรรกะการนำเข้ากำหนดไว้ใน spark-setup/spark_alloydb_processor.py ไปป์ไลน์มีขั้นตอนดังนี้

  1. แสดงรายการ PDF: ดึงข้อมูล URI ของเอกสารจากที่เก็บข้อมูล Google Cloud Storage
  2. การแยกข้อมูลเชิงความหมาย: ใช้ UDF (ฟังก์ชันที่ผู้ใช้กำหนด) เพื่อเรียกใช้ Document AI API
  3. เขียนไปยัง AlloyDB: บันทึกส่วนข้อความที่แยกออกมาลงในตาราง AlloyDB ที่ชื่อว่า rules
# Extract from spark_alloydb_processor.py
def process_document(gcs_uri: str):
    # ... calls Document AI to parse PDF ...
    return chunks

# Parallel processing with Spark Lightning Engine
process_udf = udf(process_document, chunk_schema)
chunked_df = uri_df.withColumn("chunks", process_udf(col("gcs_uri"))) \
                   .select(explode(col("chunks")).alias("chunk")) \
                   .select("chunk.*")

# Save to AlloyDB for Vector Search
chunked_df.write.format("jdbc") \
    .option("url", jdbc_url) \
    .option("dbtable", "rules") \
    .mode("append") \
    .save()

เรียกใช้งานการนำเข้า

ทริกเกอร์กระบวนการนำเข้าโดยใช้สคริปต์ที่ให้ไว้โดยทำดังนี้

./spark-setup/run_dataproc.sh

8. RAG กับ AlloyDB

เมื่อข้อมูลกฎของเมืองอยู่ใน AlloyDB แล้ว Agent ก็จะใช้ข้อมูลดังกล่าวเพื่อทำการดึงข้อมูลมาเสริม (Retrieval-Augmented Generation หรือ RAG) ได้ ซึ่งจะช่วยให้มั่นใจได้ว่าแผนการจัดงานวิ่งมาราธอนเป็นไปตามรหัสเมืองที่เฉพาะเจาะจง

ศักยภาพของ AlloyDB สำหรับ RAG

AlloyDB มีความโดดเด่นในด้าน Vector Search ซึ่งช่วยให้เราจัดเก็บทั้ง Structured Data และการฝังเวกเตอร์ไว้ในที่เดียวกันได้ Agent สามารถใช้ฟังก์ชัน embedding ในตัวใน AlloyDB เพื่อค้นหาข้อมูลกฎที่เกี่ยวข้องมากที่สุดได้

เรามีเครื่องมือที่ค้นหา AlloyDB โดยใช้ความคล้ายคลึงของเวกเตอร์เพื่อให้ Agent เข้าถึงข้อมูลนี้ได้ คุณดูตรรกะนี้ได้ใน hybrid_recall.sql ซึ่งแสดงวิธีคำนวณระยะห่างระหว่างการค้นหากับกฎที่จัดเก็บไว้

SELECT
    text,
    (embedding <=> 
     embedding('gemini-embedding-001', 
               'Restrictions for running a race on the Las Vegas strip')::vector) 
    as distance
FROM
    rules
WHERE city = 'Las Vegas'
ORDER BY
    distance ASC
LIMIT 5;

ให้ข้อมูลพื้นฐานแก่ Agent เกี่ยวกับกฎท้องถิ่นด้วยเครื่องมือ RAG

หากต้องการให้ Agent ใช้เครื่องมือได้ คุณต้องกำหนดเครื่องมือใน planner_agent/tools.py แล้วลงทะเบียนเครื่องมือใน planner_agent/agent.py เราจะใช้เซิร์ฟเวอร์ MCP ของ AlloyDB ระยะไกลที่มีการจัดการ จาก Google Cloud เพื่อเชื่อมต่อกับฐานข้อมูล

  1. กำหนดเครื่องมือใน planner_agent/tools.py โดยใช้รูปแบบ "การเรียกข้อมูลแบบไฮบริด" เราจะใช้โปรโตคอล streamable_http เพื่อเชื่อมต่อกับเซิร์ฟเวอร์ MCP ของ AlloyDB ที่มีการจัดการ
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

async def get_local_and_traffic_rules(query: str) -> str:
    """Uses vector search in AlloyDB via managed MCP server."""
    # Vector search query using built-in AlloyDB embedding functions
    sql = f"SELECT text FROM rules WHERE city = 'Las Vegas' ORDER BY embedding <=> google_ml.embedding('gemini-embedding-001', '{query}')::vector ASC LIMIT 5;"
    
    # Establish a streamable HTTP connection to the MCP server
    async with streamablehttp_client(url, headers=get_auth_headers()) as (read_stream, write_stream, _):
        async with ClientSession(read_stream, write_stream) as session:
            await session.initialize()
            result = await session.call_tool(
                "execute_sql",
                arguments={
                    "instance": full_instance_name,
                    "database": "city_rules",
                    "sqlStatement": sql
                }
            )
            return "\n".join([c.text for c in result.content if hasattr(c, 'text')])
  1. ลงทะเบียนเครื่องมือและสรุปไฟล์ planner_agent/agent.py โดยทำดังนี้
# ... imports ...

# Assemble the Agent
root_agent = LlmAgent(
    name="planner_agent",
    model="gemini-3-flash-preview",
    instruction="You are a helpful marathon planning assistant...",
    tools=[
        get_local_and_traffic_rules,
    ],
    after_agent_callback=[auto_save_memories],
)

# 2. Wrap the agent in an AdkApp to manage the stateful lifecycle
app = AdkApp(
    agent=root_agent,
    session_service_builder=session_service_builder,
    memory_service_builder=memory_service_builder
)

9. คำแนะนำจากผู้เชี่ยวชาญด้วยทักษะของ Agent

ทักษะของ Agent คือโมดูลแบบสแตนด์อโลนที่มีคำแนะนำ คำแนะนำ และแหล่งข้อมูลที่เฉพาะเจาะจงเพื่อช่วยให้ Agent ทำงานได้อย่างมีประสิทธิภาพมากขึ้น แทนที่จะใส่คำแนะนำที่ซับซ้อนสำหรับเครื่องมือทุกรายการลงในพรอมต์ของระบบ คุณสามารถสรุปความเชี่ยวชาญนั้นไว้ในทักษะที่จะโหลดเมื่อจำเป็นเท่านั้น

Google มีทักษะที่สร้างไว้ล่วงหน้าสำหรับผลิตภัณฑ์ของ Google (เช่น AlloyDB และ BigQuery) เพื่อให้ Agent ปฏิบัติตามแนวทางปฏิบัติแนะนำของอุตสาหกรรมสำหรับการค้นหาข้อมูลและการจัดการทรัพยากร คุณสามารถสำรวจทักษะและรูปแบบเฉพาะทางอื่นๆ ได้ที่ Google Skills Depot คุณจะพบทักษะพื้นฐานของ AlloyDB ได้ที่นี่

1. สำรวจไฟล์ทักษะ

เปิดไฟล์ทักษะที่กำหนดค่าไว้ล่วงหน้าที่ planner_agent/skills/get-local-and-traffic-rules/SKILL.md ไฟล์จะมีลักษณะดังนี้

---
name: get-local-and-traffic-rules
description: Retrieve local rules and traffic information for a specific jurisdiction.
---
# get_local_and_traffic_rules Skill

This skill provides guidelines on how to effectively use the `get_local_and_traffic_rules` tool.

## Overview
The `get_local_and_traffic_rules` tool interfaces with an AlloyDB database to perform vector similarity searches on a corpus of rules and traffic information using a provided natural language query.

## Usage Guidelines
1. **Query Specificity**: When calling the tool, provide specific details in the `query` argument. For example, instead of querying "food rules", use "rules regarding food vendors during public events".
2. **Contextual Use**: Use the tool when planning events or activities that require adherence to local municipal or state rules (e.g., street closures, noise ordinances, environmental rules).
3. **Handling Results**: The tool returns a string containing the text of the top 5 most relevant rules. If no error occurs, parse the returned string to inform your planning tasks.
4. **Error Handling**: If an error string is returned (e.g., "Error querying rules: ..."), you must report this failure or attempt an alternative approach if applicable.

## Underlying Mechanism
- The tool uses `google_ml.embedding` to convert the query into a vector representation.
- It calculates distance (`<=>`) against the `embedding` column in the `rules` table on an AlloyDB instance.
- Results are fetched in descending order of similarity, limited to 5 results.

2. วิธีลงทะเบียนทักษะ

ในไฟล์ planner_agent/agent.py ระบบจะโหลดทักษะจากไดเรกทอรีและเพิ่มลงในเครื่องมือของ Agent โค้ดจะมีลักษณะดังนี้

import pathlib
from google.adk.skills import load_skill_from_dir
from google.adk.tools import skill_toolset

# Load the AlloyDB skill from its directory
alloydb_skill = load_skill_from_dir(pathlib.Path(__file__).parent / "skills" / "get-local-and-traffic-rules")

# Assemble the Agent with the Skill Toolset
root_agent = LlmAgent(
    name="planner_agent",
    model="gemini-3-flash-preview",
    instruction="You are a helpful marathon planning assistant...",
    tools=[
        get_local_and_traffic_rules,
        skill_toolset.SkillToolset(skills=[alloydb_skill])
    ],
    after_agent_callback=[auto_save_memories],
)

10. ทดสอบ Agent

  1. เริ่ม Agent ในเครื่องโดยทำดังนี้
uv run adk run planner_agent
  1. ถามคำถามเกี่ยวกับกฎของเมือง เช่น [user]: What are the rules for running a race on the Las Vegas strip?

Agent จะเรียกใช้เครื่องมือ get_local_and_traffic_rules ทำการค้นหาเวกเตอร์ใน AlloyDB และแสดงคำตอบตามส่วนกฎอย่างเป็นทางการที่ประมวลผลโดย Spark

11. ติดตั้งใช้งาน Agent

ติดตั้งใช้งานในแพลตฟอร์ม Agent

uv run adk deploy agent_engine \
  --env_file .env \
  planner_agent

12. ล้างข้อมูล

หากต้องการหลีกเลี่ยงค่าใช้จ่ายที่เกิดขึ้นอย่างต่อเนื่อง ให้ลบทรัพยากรที่สร้างขึ้นระหว่าง Codelab นี้

ลบคลัสเตอร์ AlloyDB

# Delete the AlloyDB Cluster
gcloud alloydb clusters delete rules-db --region=us-central1 --force

ลบแอป Agent Runtime

คุณสามารถลบอินสแตนซ์ Reasoning Engine ผ่านคอนโซลหรือใช้คำสั่ง gcloud (หากมีชื่อทรัพยากร) ใช้คอนโซลเพื่อความสะดวกโดยทำดังนี้

  1. ไปที่หน้าAgent Runtime
  2. เลือก planner_agent -> คลิกปุ่มจุด 3 จุดทางด้านขวา
  3. คลิกลบ

13. ขอแสดงความยินดี

ขอแสดงความยินดี คุณได้ปรับปรุง Agent ของ ADK ให้มีความสามารถในการให้ข้อมูลพื้นฐานและหน่วยความจำขั้นสูงเรียบร้อยแล้ว

สิ่งที่คุณได้เรียนรู้

  • Agent ที่มีสถานะ: การผสานรวมเซสชันแพลตฟอร์ม Agent เพื่อเก็บรักษาบริบทการสนทนา
  • การเรียนรู้ระยะยาว: การแนบ Memory Bank ของแพลตฟอร์ม Agent เพื่อให้ Agent เรียนรู้จากการโต้ตอบของผู้ใช้ได้
  • การนำเข้าข้อมูล: การใช้ Spark Lightning Engine และ Document AI เพื่อประมวลผลเอกสารที่ไม่มีโครงสร้าง
  • RAG: การสร้างระบบ Vector Search ใน AlloyDB เพื่อให้ข้อมูลพื้นฐานแก่ Agent เกี่ยวกับกฎในโลกแห่งความเป็นจริง

ขั้นตอนถัดไป