ข้ามไปยังเนื้อหา
AggregaAggregaAggregaAggrega
เลือกภาษา: ไทย
หน้านี้ถูกแปลด้วยเครื่อง อ่านต้นฉบับภาษาอังกฤษ

นำเข้าแอปพลิเคชัน LangGraph

บทแนะนำทีละขั้นตอนนี้นำเข้าการกำหนดค่าของแอปพลิเคชัน LangGraph ซึ่งเป็นกราฟหนึ่งกราฟที่ตอบคำถามของลูกค้าเกี่ยวกับคำสั่งซื้อของพวกเขา และบิลด์แอปพลิเคชันสำหรับ Docker Compose การนำเข้าร่างทุกไฟล์ที่บิลด์อ่าน ได้แก่ กราฟ ฐานข้อมูล PostgreSQL ที่ checkpointer ของกราฟเก็บสถานะไว้ โมเดลที่กราฟเรียก รหัสผ่านและคีย์ในรูปแบบการอ้างอิงตัวแปร และการทำให้เป็นรูปธรรมของแต่ละอย่าง การนำเข้าถามเพียงครั้งเดียวถึงสองค่าที่ไม่มีการกำหนดค่าใดเก็บไว้ คือโมเดลที่โค้ดของกราฟเรียก และอิมเมจที่แอปพลิเคชันรันอยู่ ทุกไฟล์และทุกบล็อกเอาต์พุตด้านล่างมาจากการรันจริง การกำหนดค่าและแต่ละขั้นตอนของบทแนะนำอยู่ในตัวอย่าง import-langgraph ซึ่งสคริปต์ตรวจสอบรันการนำเข้าอีกครั้ง เปรียบเทียบสิ่งที่การนำเข้าเขียน สิ่งที่พิมพ์ และบล็อกที่หน้านี้อ้างถึงกับไฟล์ที่เก็บไว้ที่นั่น แล้วตรวจสอบความถูกต้องและบิลด์สิ่งที่การนำเข้าเขียน

ร่างตัวอย่าง

คุณรันการนำเข้านี้ด้วยคำสั่งในหน้านี้ได้ หรือ ให้ผู้ช่วย AI ของคุณรันให้

เขียนตัวอย่างลงในโฟลเดอร์ใหม่ที่ตั้งชื่อตามตัวอย่าง:

Terminal window
aggrega draft --example import-langgraph

จากนั้นจะพิมพ์คำสั่งที่ต้องรันต่อ คำสั่งแรกเปลี่ยนเข้าโฟลเดอร์ใหม่ ส่วนคำสั่งถัดไปด้านล่างเปลี่ยนเข้าโฟลเดอร์ source ซึ่งมีสิ่งที่หน้านี้นำเข้าและเป็นที่ที่การนำเข้ารัน:

Terminal window
cd import-langgraph

โฟลเดอร์ source ของตัวอย่างที่ร่างไว้เปิดอยู่ใน Visual Studio Code แสดงการกำหนดค่า LangGraph ของตัวอย่าง ซึ่งระบุกราฟหนึ่งกราฟคือ support

โปรเจกต์

แอปพลิเคชัน LangGraph เก็บกราฟไว้ในโค้ดและประกาศกราฟในไฟล์ langgraph.json ซึ่งเป็นไฟล์การกำหนดค่าของเครื่องมือบรรทัดคำสั่งของ LangGraph ไฟล์นี้ซึ่งเขียนขึ้นสำหรับบทแนะนำนี้ ประกาศกราฟหนึ่งกราฟคือ support ด้วยเส้นทางนำเข้าของกราฟในโค้ดของแอปพลิเคชันและคำอธิบาย:

{
"$schema": "https://langgra.ph/schema.json",
"dependencies": ["."],
"python_version": "3.12",
"graphs": {
"support": {
"path": "./src/support_agent/graph.py:graph",
"description": "Answers a question from a customer about their order, and hands it to a person when it cannot."
}
}
}

การนำเข้าอ่าน langgraph.json และไม่อ่านโค้ดของแอปพลิเคชันเลย ตัวอย่างจึงมีเฉพาะการกำหนดค่า ข้างไฟล์นั้น .aggrega-config.yaml ทำให้โฟลเดอร์เป็นพื้นที่ทำงาน:

# The workspace key marks this directory as the workspace root, so an import run
# here writes its drafts beside langgraph.json.
workspace: {}
# The registry the importer is fetched from the first time an import needs it.
default_registry: hub.aggrega.ai

การนำเข้าอ้างอิง Components เอเจนต์ LangGraph PostgreSQL และโมเดล OpenAI ของแค็ตตาล็อก และ --target ร่าง Manifest จาก Recipes ของแค็ตตาล็อก พื้นที่ทำงานไม่ระบุโฟลเดอร์แค็ตตาล็อก การนำเข้าจึงอ่านแค็ตตาล็อกไลบรารีมาตรฐานจากรีจิสทรีที่ดึงตัวนำเข้ามา คือสแนปช็อตที่ลงลายเซ็นของแค็ตตาล็อกที่เผยแพร่ ซึ่งการนำเข้าครั้งแรกดึงมา ยืนยัน และเก็บไว้ในแคชเนื้อหาของพื้นที่ทำงาน และระบุด้วยไดเจสต์ในบันทึกที่การนำเข้าเขียนไว้ข้างร่าง พื้นที่ทำงานที่ระบุโฟลเดอร์แค็ตตาล็อกของตนเอง ใน AGGREGA_CATALOG_DIRS หรือใต้ workspace.catalog_dirs จะอ่านโฟลเดอร์เหล่านั้นก่อน โดยมีสแนปช็อตที่เผยแพร่รองรับอยู่ข้างหลัง

สิ่งที่การนำเข้าถาม

การกำหนดค่า LangGraph ไม่ระบุโมเดล เพราะโค้ดของกราฟเป็นผู้เลือก และไม่ระบุอิมเมจ เพราะคุณบิลด์อิมเมจนั้นจากโปรเจกต์ของคุณ การนำเข้าจึงถามหาทั้งสองอย่าง คือชื่อของโมเดลที่กราฟเรียกสำหรับโมเดล OpenAI ที่ร่าง และอิมเมจที่แอปพลิเคชันรันอยู่ ที่เทอร์มินัลการนำเข้าจะถามและเขียนคำตอบลงในร่างและลงในไฟล์คำตอบ import-answers.yaml ภายในไดเรกทอรีที่นำเข้า ซึ่งในที่นี้คือรากของโปรเจกต์ . การนำเข้าครั้งถัดไปจึงอ่านคำตอบจากที่นั่นและไม่ถามอะไร เมื่อไม่มีเทอร์มินัล เช่นในการรวมระบบต่อเนื่อง หรือผ่านไปป์ การนำเข้าไม่ถามอะไร แต่เขียนไฟล์โดยปล่อยคีย์ว่างแล้วหยุด:

2 values the drafts leave empty are still owed, listed in the answers file <workspace>/import-answers.yaml: write each beside its key there, then run the import again

ตัวอย่างมีไฟล์นี้อยู่แล้วพร้อมคำตอบ: กราฟเรียก gpt-4o-mini ของ OpenAI และ ghcr.io/example/support-agent:1.0.0 แทนอิมเมจที่คุณบิลด์:

# Values the import could not draft from the source, keyed by the drafted file and the key path inside
# it. Write each one beside its key; an import of the same source, and a sync, read them from here and
# ask nothing. A secret is never written here: its key asks where the secret comes from — a variable
# name, ${NAME}, or the name of a Kubernetes Secret.
aggregate.yaml:
# Left for the author, since the configuration names no model: values.model, the name of the model
# core/ai-model/openai serves.
spec.components.model.values.model: "gpt-4o-mini"
release.yaml:
# Container image running the LangGraph application code. The Docker Compose Recipe runs the catalog's
# quickstart agent when it is unset.
spec.components.support.infra.image: "ghcr.io/example/support-agent:1.0.0"
# The origin of the secret value, written by the operator in the Release. Read exactly as
# values.origin is. Write it here or in values.origin, never in both. A secret: name where it comes
# from, never the value.
spec.components.support.uses.checkpointer_db.postgres.features.credential.infra.origin: "support-db-password"
# The origin of the secret value, written by the operator in the Release. Read exactly as
# values.origin is. Write it here or in values.origin, never in both. A secret: name where it comes
# from, never the value.
spec.components.support.uses.model.model.features.credential.infra.origin: "openai-api-key"
# The origin of the secret value, written by the operator in the Release. Read exactly as
# values.origin is. Write it here or in values.origin, never in both. A secret: name where it comes
# from, never the value.
spec.components.postgres.features.password.infra.origin: "support-db-password"

คำตอบ origin สามรายการใช้สำหรับบิลด์สำหรับ Kubernetes ด้วย --target helm ซึ่งรหัสผ่านของฐานข้อมูลและคีย์ OpenAI อ่านจาก Secrets ของ Kubernetes และคำตอบแต่ละรายการคือชื่อของ Secret ได้แก่ รหัสผ่านของฐานข้อมูลจาก support-db-password ทั้งสองฝั่ง และคีย์จาก openai-api-key ไม่ใช่รหัสผ่านหรือคีย์ บิลด์สำหรับ Docker Compose อ่านค่าเหล่านี้จาก shell ของคุณแทน การนำเข้านี้จึงไม่ใช้คำตอบเหล่านั้น ไฟล์คำตอบของการนำเข้า อธิบายไฟล์ คำถาม และกฎสำหรับคำตอบของข้อมูลลับ

aggrega import ระบุทุกแฟล็กที่คำสั่งรับ รวมถึง --answers ซึ่งระบุไฟล์คำตอบที่เก็บไว้ที่อื่น

เปลี่ยนเข้าโฟลเดอร์ source:

Terminal window
cd source

รันการนำเข้า

ระบุไดเรกทอรีของแอปพลิเคชัน การนำเข้าอ่าน langgraph.json ของไดเรกทอรีนั้นและไม่อ่านไฟล์อื่นของไดเรกทอรีเลย:

Terminal window
aggrega import . --target compose

ที่เอาต์พุตมาตรฐาน การนำเข้าแสดงสิ่งที่เขียน:

wrote aggregate.yaml
wrote release.yaml
wrote manifest.yaml
wrote .aggrega-import.json

ที่ข้อผิดพลาดมาตรฐาน โดยที่ <workspace> แทนโฟลเดอร์ที่คุณรัน:

[… omitted: a warning that the importer runs unverified, printed where its module is read from beside its Plugin entity in a catalog folder rather than fetched]
read as the langgraph format, with drafted paths relative to <workspace>
info: the answers file <workspace>/import-answers.yaml also answers spec.components.postgres.features.password.infra.origin in release.yaml, spec.components.support.uses.checkpointer_db.postgres.features.credential.infra.origin in release.yaml, spec.components.support.uses.model.model.features.credential.infra.origin in release.yaml, which these drafts do not leave empty; those answers are kept there and not applied
mapped support (graph) to core/ai-agent/langgraph as a reference, NONE
mapped checkpointer (checkpointer) to core/datastore/postgres as a reference, NONE
mapped model (model) to core/ai-model/openai as a reference, NONE
Pull complete: <workspace>/release.lock (entities=10, source_artifacts=0, external_inputs=0)

การนำเข้าที่รันจากตัวแก้ไข: Aggregate, Release และ Manifest ที่ร่างไว้ปรากฏข้างการกำหนดค่า LangGraph Aggrega output แสดงรายการวิธีที่กราฟ checkpointer และโมเดลของกราฟถูกแมปและไฟล์ที่เขียน และการแจ้งเตือนระบุว่าการนำเข้าร่างไฟล์ 3 ไฟล์ โดยรู้จักเป็น langgraph

  • บรรทัด info ระบุคำตอบที่ฉบับร่างสำหรับ Docker Compose ไม่ใช้ ซึ่งเก็บไว้ในไฟล์คำตอบสำหรับบิลด์สำหรับ Kubernetes
  • บรรทัด mapped ระบุว่ากราฟเป็นอินสแตนซ์ของเอเจนต์ LangGraph ในไลบรารีมาตรฐาน คือ core/ai-agent/langgraph ว่า checkpointer ของกราฟ ซึ่งเป็นที่ที่ LangGraph เก็บสถานะของแต่ละกราฟ เป็นอินสแตนซ์ของ core/datastore/postgres เพราะการกำหนดค่าไม่ระบุ checkpointer และ LangGraph จึงเก็บสถานะใน PostgreSQL และว่าโมเดลที่ทุกกราฟเรียกเป็นอินสแตนซ์ของ core/ai-model/openai ระดับของบรรทัดเหล่านี้อ่านว่า NONE: การนำเข้า LangGraph อ้างอิง Components เหล่านี้ตามสิ่งที่การกำหนดค่าประกาศ แทนที่จะชั่งน้ำหนักแต่ละรายการเทียบกับแค็ตตาล็อก การกำหนดค่าที่ checkpointer.backend เป็น mongo จะร่างด้วย core/datastore/ferretdb ซึ่งใช้โปรโตคอลของ MongoDB บน core/datastore/documentdb โดยเชื่อมโยงทั้งสองตามที่ Aggregate FerretDB ของแค็ตตาล็อกเชื่อมโยง และใช้รหัสผ่านเดียวสำหรับทั้งสอง
  • บรรทัด Pull complete คือการนำเข้าดึงสิ่งที่ Release ที่ร่างพึ่งพา เหมือนที่ aggrega pull ทำ ลงใน release.lock ฉบับร่างจึงตรวจสอบความถูกต้องและบิลด์ได้ทันที ภายใต้ --offline การนำเข้าไม่ pull สิ่งใดและระบุ aggrega pull ที่ต้องรัน

สิ่งที่การนำเข้าเขียน

Aggregate, Release และ Manifest ตั้งชื่อว่า imported/support ตามกราฟเดียวที่ langgraph.json ให้บริการ คือ support เพราะการกำหนดค่าไม่มีชื่อของตัวเอง (การกำหนดค่าที่ให้บริการหลายกราฟจะตั้งชื่อตามไฟล์ หรือตามไดเรกทอรีเมื่อการนำเข้าได้รับไดเรกทอรี ดังกรณีนี้) ภายใต้เนมสเปซที่การตั้งค่า target_namespace ของตัวนำเข้ากำหนด คือ imported เว้นแต่คุณตั้งค่าอื่น (การตั้งค่า)

aggregate.yaml ประกอบด้วยกราฟ ฐานข้อมูลที่กราฟเก็บสถานะไว้ และโมเดลที่กราฟเรียก โดยแต่ละการเชื่อมต่อประกาศข้อมูลรับรองที่ยื่นให้ และขึ้นต้นด้วยความคิดเห็นที่บอกว่าอ่านอะไรและไม่ได้อ่านอะไร:

# LangGraph configurations read:
# - langgraph.json
# Not read: the application's code, which defines each graph; it runs in the image the Release names for each graph.
# Not carried, since they configure the server image langgraph build builds, and the image the Release names is the application's own: dependencies, python_version.
apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: imported/support
version: 0.1.0
description: Draft Aggregate imported from a source in the langgraph format.
spec:
components:
support:
type: core/ai-agent/langgraph
values:
graph_entrypoint: "./src/support_agent/graph.py:graph"
description: "Answers a question from a customer about their order, and hands it to a person when it cannot."
uses:
checkpointer_db:
postgres:
target: postgres
# For the compose target, the lines below declare credential on postgres: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none.
features:
credential:
type: core/secret-value
values:
key: POSTGRES_PASSWORD
model:
model:
target: model
# For the compose target, the lines below declare credential on model: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none.
features:
credential:
type: core/secret-value
values:
key: OPENAI_API_KEY
# The database the configuration's checkpointer keeps each graph's state in, PostgreSQL.
postgres:
type: core/datastore/postgres
features:
# For the compose target, the lines below declare password on postgres: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none.
password:
type: core/secret-value
values:
key: POSTGRES_PASSWORD
# The model every graph calls, which the configuration does not name.
model:
type: core/ai-model/openai
values:
# Left for the author, since the configuration names no model: values.model, the name of the model core/ai-model/openai serves.
model: "gpt-4o-mini"

graph_entrypoint คือเส้นทางนำเข้าของกราฟตามที่การกำหนดค่าเขียน การเชื่อมต่อ checkpointer_db ของกราฟเชื่อมโยงกับฐานข้อมูล และการเชื่อมต่อ model เชื่อมโยงกับโมเดล ซึ่งชื่อของโมเดลคือคำตอบที่การนำเข้าอ่านจาก import-answers.yaml ไม่เขียน recursion_limit ค่าเริ่มต้นของ Component คือ 25 จึงมีผล ฐานข้อมูลได้รับรหัสผ่าน Recipe ของ PostgreSQL จึงไม่เขียนรหัสผ่านของตัวเอง และกราฟยื่นรหัสผ่านเดียวกันบนการเชื่อมต่อไปยังฐานข้อมูล

release.yaml ประกอบด้วยสภาพแวดล้อม อิมเมจ พอร์ตที่เผยแพร่ และที่มาของคีย์และรหัสผ่าน พร้อมความคิดเห็นเหนือกราฟที่บอกว่าอิมเมจของกราฟคืออะไรและกราฟได้รับอะไร:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: imported/support
# environment is a drafted default, since no source names the environment this Release deploys to; change it to name the one it deploys to.
environment: development
version: 0.1.0
description: Draft Release imported from a source in the langgraph format.
spec:
aggregate: imported/support
manifest: imported/support
components:
# Each graph's image is the application's own, not the server image langgraph build builds: its code, with LangGraph installed.
# It is handed, for its code to read, the graph to run as LANGGRAPH_GRAPH_ENTRYPOINT, the model's name as OPENAI_MODEL, the checkpointer store's host and port as LANGGRAPH_CHECKPOINTER_HOST and LANGGRAPH_CHECKPOINTER_PORT, its user and database, where the store publishes them, as LANGGRAPH_CHECKPOINTER_USER and LANGGRAPH_CHECKPOINTER_DATABASE, and a password attached on the checkpointer_db connection as LANGGRAPH_CHECKPOINTER_PASSWORD.
# It is also handed the model's address as OPENAI_BASE_URL and a key attached on the model connection as OPENAI_API_KEY, which the OpenAI client library reads, and recursion_limit as LANGGRAPH_DEFAULT_RECURSION_LIMIT, which LangGraph reads.
# An inject: block on the graph's own instance, through an entry reading graph_entrypoint alone, replaces LANGGRAPH_GRAPH_ENTRYPOINT with the name it writes; one on the model connection replaces OPENAI_MODEL, and one on the checkpointer_db connection the store's host, port, user and database, with the names it writes.
support:
infra:
# Left for the Release, since the source cannot supply it: infra.image, the container image running the application
image: "ghcr.io/example/support-agent:1.0.0"
# Published on this machine's loopback address, 127.0.0.1: reachable from this machine only, by any process running on it. Delete this line to keep the service internal.
published_port: 8000
uses:
checkpointer_db:
postgres:
features:
credential:
type: core/secret-value
infra:
# Attached by reference, never as a value: the credential support presents to postgres on its checkpointer_db connection, the password of postgres (the configuration names no variable for it), read from the variable POSTGRES_PASSWORD, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value.
origin: "${POSTGRES_PASSWORD}"
model:
model:
features:
credential:
type: core/secret-value
infra:
# Attached by reference, never as a value: the credential support presents to model on its model connection (the source names no variable for it, so it is read from the one OpenAI's client libraries read the API key from), read from the variable OPENAI_API_KEY, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value.
origin: "${OPENAI_API_KEY}"
postgres:
features:
password:
type: core/secret-value
infra:
# Attached by reference, never as a value: the password of postgres (the configuration names no variable for it), read from the variable POSTGRES_PASSWORD, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value.
origin: "${POSTGRES_PASSWORD}"
model: {}

สภาพแวดล้อม development เป็นค่าเริ่มต้นที่การนำเข้าเขียนเพราะไม่มีการกำหนดค่าใดระบุไว้ เปลี่ยนค่านี้ถ้า Release ปรับใช้ที่อื่น อิมเมจคือคำตอบที่การนำเข้าอ่านจาก import-answers.yaml กราฟเผยแพร่บนที่อยู่ลูปแบ็กของเครื่องคุณที่พอร์ต 8000 โดยส่งต่อไปยังพอร์ตที่โปรแกรมของกราฟรับฟัง คือ infra.port ซึ่งเป็น 8000 เว้นแต่คุณตั้งค่า คีย์และรหัสผ่านเป็นการอ้างอิงตัวแปร คือ ${OPENAI_API_KEY} และ ${POSTGRES_PASSWORD} ไม่ใช่ค่า

manifest.yaml ประกอบด้วย Recipe สำหรับ Compose ของแต่ละชนิด และการทำให้เป็นรูปธรรมที่เขียนการอ้างอิงตัวแปรแต่ละรายการลงในไฟล์ Compose ตามที่เขียนไว้:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: imported/support
version: 0.1.0
spec:
mappings:
components:
type:
core/ai-agent/langgraph: compose/langgraph
core/datastore/postgres: compose/postgres
core/ai-model/openai: compose/openai
features:
type:
core/secret-value: compose/dotenv-secret

release.lock บันทึกเอนทิตีในแค็ตตาล็อกที่ Release หาค่าได้ ตามไดเจสต์ และ .aggrega-import.json เป็นบันทึกของสิ่งที่การนำเข้าเขียน ซึ่ง aggrega import --sync ครั้งต่อไปอ่าน

ตรวจสอบความถูกต้องและบิลด์

Terminal window
aggrega validate --release release.yaml
aggrega build --release release.yaml

Release ที่ร่างไว้เปิดอยู่ในตัวแก้ไขหลังตรวจสอบความถูกต้อง โดย Aggrega output และการแจ้งเตือนต่างรายงานว่าไม่พบปัญหา

บิลด์ของ Release ที่ร่างไว้: โฟลเดอร์ dist ปรากฏใน Explorer โดยเอาต์พุตของ Aggrega แสดงรายการไฟล์ Docker Compose ที่เขียน และการแจ้งเตือนระบุว่าเขียนไฟล์แล้ว

การตรวจสอบความถูกต้องพิมพ์:

notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
release dependency validation passed: release.yaml (schema-pillar + dependency pillars)

และบิลด์ โดยที่ <workspace> แทนโฟลเดอร์ที่คุณรัน:

notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
[… omitted: a total build time, which differs per run]
Build completed: 1 file written to <workspace>/dist

บรรทัด notice เป็นเชิงแนะนำ: Feature ของข้อมูลลับรับ origin จาก values ของ Aggregate หรือ infra ของ Release และการแจ้งให้ทราบรายงานว่ามีค่าระดับใบหนึ่งค่าที่ประกาศไว้ทั้งสองที่ ซึ่งไม่เปลี่ยนคำตัดสิน

บิลด์เขียน dist/compose/docker-compose.yaml ซึ่งมีบริการสำหรับกราฟ บริการสำหรับฐานข้อมูล และบริการที่รอฐานข้อมูล บริการของกราฟ:

support:
image: ghcr.io/example/support-agent:1.0.0
environment:
LANGGRAPH_DEFAULT_RECURSION_LIMIT: "25"
OPENAI_BASE_URL: https://api.openai.com/v1
OPENAI_MODEL: gpt-4o-mini
LANGGRAPH_CHECKPOINTER_HOST: postgres
LANGGRAPH_CHECKPOINTER_PORT: "5432"
LANGGRAPH_CHECKPOINTER_USER: appuser
LANGGRAPH_CHECKPOINTER_DATABASE: appdb
LANGGRAPH_GRAPH_ENTRYPOINT: ./src/support_agent/graph.py:graph
OPENAI_API_KEY: ${OPENAI_API_KEY}
LANGGRAPH_CHECKPOINTER_PASSWORD: ${POSTGRES_PASSWORD}
ports:
- 127.0.0.1:8000:8000
[… omitted: the labels the build writes on every service, naming the Aggregate, the component, the environment and the compiler's version]
networks:
- compose
depends_on:
support--wait-for-store:
condition: service_completed_successfully
postgres:
condition: service_started

กราฟได้รับที่อยู่ของโมเดล ชื่อของโมเดล และคีย์ของโมเดล รวมถึงโฮสต์ พอร์ต ผู้ใช้ ชื่อฐานข้อมูล และรหัสผ่านของฐานข้อมูล และจุดเริ่มต้นของกราฟเอง ผู้ใช้และชื่อฐานข้อมูลเป็นค่าเริ่มต้นของ Component PostgreSQL คือ appuser และ appdb ซึ่งบริการของฐานข้อมูลด้านล่างสร้างขึ้นด้วยค่าเหล่านี้ กราฟได้รับค่าทั้งสองเพราะ Recipe ของ PostgreSQL เผยแพร่ทั้งสองค่า ส่วนที่จัดเก็บข้อมูลที่ไม่เผยแพร่ค่าใดเลย เช่น Valkey จะไม่ส่งค่าใดให้ ชื่อของโมเดล โฮสต์ พอร์ต ผู้ใช้ และชื่อฐานข้อมูลของฐานข้อมูล และจุดเริ่มต้นของกราฟมาถึงใต้ชื่อที่ Component LangGraph ส่งมอบโดยค่าเริ่มต้น ซึ่งบล็อก inject: บนการเชื่อมต่อ หรือบนส่วนประกอบเองสำหรับค่าของตัวเอง จะแทนที่ด้วยชื่อที่โค้ดของคุณอ่าน ที่อยู่ของโมเดล คีย์ของโมเดล และรหัสผ่านของฐานข้อมูลคงเป็นชื่อใดก็ตามที่บล็อกระบุ และ LANGGRAPH_DEFAULT_RECURSION_LIMIT ก็เช่นกัน ซึ่ง LangGraph อ่านเองเป็นขีดจำกัดขั้นตอนของการรันที่การกำหนดค่าไม่ตั้งค่าใดเลย บริการของฐานข้อมูลเองใช้รหัสผ่านเดียวกัน:

postgres:
image: postgres:16
environment:
POSTGRES_USER: appuser
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: appdb
[… omitted: the labels the build writes on every service, the network, and a health check running PostgreSQL's pg_isready]

บริการของกราฟเริ่มทำงานเมื่อบริการที่สามเสร็จสมบูรณ์แล้วเท่านั้น บริการที่สามรันครั้งเดียวในอิมเมจเอเจนต์ quickstart ของแค็ตตาล็อก ไม่ใช่ในอิมเมจของกราฟเอง และรอจนกว่าที่อยู่ของฐานข้อมูลรับการเชื่อมต่อ TCP โดยลองทุก 2 วินาทีนานสูงสุด 300 วินาที หากไม่ได้การเชื่อมต่อภายในเวลานั้น บริการจะออกพร้อมระบุที่อยู่ และไม่เริ่มกราฟ บริการนี้ได้รับโฮสต์และพอร์ตของฐานข้อมูลเท่านั้น ไม่มีอย่างอื่น:

support--wait-for-store:
image: hub.aggrega.ai/tools/quickstart-agent:1.0.1@sha256:a5f8dcfbb670d86a0cae0aadd2844bec93221a651238f8be3863873e261ee168
[… omitted: the command and the entrypoint, a bash loop that opens a connection to the address]
environment:
STORE_HOST: postgres
STORE_PORT: "5432"
[… omitted: the labels the build writes on every service, and the network]
restart: "no"

คีย์และรหัสผ่านเป็นการอ้างอิงตัวแปรที่เติมจาก shell ของคุณเมื่อสแตกเริ่มทำงาน โมเดลไม่มีบริการของตัวเอง: กราฟเข้าถึง OpenAI API ที่ที่อยู่ของ OpenAI API เอง บริการของกราฟเผยแพร่พอร์ตบน 127.0.0.1 เท่านั้น และเข้าถึงพอร์ตของฐานข้อมูลเองได้บนเครือข่ายของสแตก โดยไม่เผยแพร่อะไรเลย

กับผู้ช่วย AI

ผู้ช่วย AI ที่ ตั้งค่าด้วยเครื่องมือของ Aggrega ทำสิ่งเดียวกันได้: วางพรอมต์นี้ลงไป

ใช้เฉพาะเครื่องมือของ Aggrega และอย่าสร้างโฟลเดอร์เอง ถ้าโฟลเดอร์ Workspace ยังไม่มีโฟลเดอร์ `import-langgraph` เรียกเครื่องมือ `draft` หนึ่งครั้ง โดยตั้ง `example` เป็น `import-langgraph` และไม่ระบุ `dir`: เครื่องมือจะเขียนตัวอย่างลงในโฟลเดอร์ `import-langgraph` ใหม่ ซึ่งโฟลเดอร์ `source` ของโฟลเดอร์นั้นเก็บสิ่งที่หน้านี้นำเข้า จากนั้นเรียกเครื่องมือ `import` โดยตั้ง `workspace_root` เป็น `import-langgraph/source` ตั้ง `source` เป็น `.` และตั้ง `target` เป็น `compose`: เครื่องมืออ่านคำตอบที่ต้องใช้จาก `import-answers.yaml` ข้าง `langgraph.json` เขียนร่างลงใน `import-langgraph/source` และ pull สิ่งที่ร่างเหล่านั้นต้องใช้ จากนั้นเรียกเครื่องมือ `validate` โดยตั้ง `workspace_root` เป็น `import-langgraph/source` และตั้ง `release` เป็น `release.yaml` และเรียกเครื่องมือ `build` โดยตั้ง `workspace_root` เป็น `import-langgraph/source` ตั้ง `release` เป็น `release.yaml` และตั้ง `write` เป็น true บอกฉันว่าบิลด์เขียนไฟล์ใดบ้าง ไฟล์เหล่านั้นอยู่ใต้ `import-langgraph/source/dist/`

เปิดใน VS Code

เมื่อติดตั้ง ส่วนขยาย Aggrega สำหรับ Visual Studio Code แล้ว เปิดโฟลเดอร์ import-langgraph/source ใน VS Code ซึ่งเป็นที่ที่การนำเข้าเขียนร่างไว้ โฟลเดอร์มี Release หนึ่งรายการคือ release.yaml ที่ร่างไว้ ตัวแก้ไขจึงเลือก Release นั้นให้คุณ และตรวจสอบร่างเทียบกับ Release นั้นขณะที่คุณแก้ไข โดยทำเครื่องหมายสิ่งที่พบในไฟล์และในแผง Problems Aggrega: Show topology graph ใน Command Palette วาดส่วนประกอบของ Release และการเชื่อมต่อระหว่างส่วนประกอบเหล่านั้น Aggrega: Build, Aggrega: Preview build และ Aggrega: Pull ทำงานกับ Release เดียวกัน และแต่ละคำสั่งยังเป็นปุ่มบนแผง Commands ของมุมมอง Aggrega ในแถบกิจกรรม Build เขียนไฟล์ลงใน dist/ ของโฟลเดอร์ ใช้ Aggrega ใน Visual Studio Code ครอบคลุมส่วนที่เหลือ

วิธีรัน

คุณบิลด์อิมเมจจากโปรเจกต์ด้วย Dockerfile ของคุณเอง ซึ่งมีโค้ดของกราฟและติดตั้ง LangGraph ไว้ Docker Compose รันคำสั่งของอิมเมจเองเป็นบริการของกราฟ เพราะ Recipe ไม่ตั้งคำสั่งใดไว้ คำสั่งนั้นจึงต้องรันต่อเนื่อง และต้องให้บริการคำขอถ้าคุณต้องการเข้าถึงกราฟ คำสั่งนั้นพบกราฟ โมเดล และฐานข้อมูลผ่านตัวแปรข้างต้น โปรแกรมของคุณเป็นผู้กำหนดที่อยู่ เส้นทาง และการตอบกลับ

การรันด้านล่างใช้อิมเมจเช่นนั้น: กราฟที่เส้นทางซึ่ง langgraph.json ระบุ คือ src/support_agent/graph.py โหนดหนึ่งที่ตอบผ่านโมเดลที่ OPENAI_MODEL ระบุ และคำสั่งของอิมเมจเป็นเซิร์ฟเวอร์ HTTP ขนาดเล็กของโปรแกรมเองบนพอร์ต 8000 อิมเมจคงโครงของโปรเจกต์ไว้ที่ไดเรกทอรีทำงาน เส้นทางที่ LANGGRAPH_GRAPH_ENTRYPOINT ระบุ ซึ่งสัมพัทธ์กับไดเรกทอรีนั้น จึงพบกราฟ เซิร์ฟเวอร์โหลดกราฟนั้น เก็บสถานะของแต่ละการสนทนาไว้ในฐานข้อมูลผ่าน PostgreSQL checkpointer ของ LangGraph ซึ่งเปิดด้วยโฮสต์ พอร์ต ผู้ใช้ ชื่อฐานข้อมูล และรหัสผ่านของฐานข้อมูล โดยอ่านแต่ละค่าจากตัวแปร LANGGRAPH_CHECKPOINTER_ ที่ค่านั้นมาถึง โปรแกรมจึงไม่เขียนค่าใดเลยเอง และรันกราฟหนึ่งครั้งสำหรับ POST /threads/<thread>/runs แต่ละครั้ง บริการของกราฟเริ่มทำงานเมื่อฐานข้อมูลรับการเชื่อมต่อแล้ว เซิร์ฟเวอร์ที่เปิด checkpointer ตอนเริ่มทำงานจึงพบว่าฐานข้อมูลรับการเชื่อมต่ออยู่ บิลด์อิมเมจของคุณที่รากของโปรเจกต์ เขียนชื่ออิมเมจแทน ghcr.io/example/support-agent:1.0.0 ใน import-answers.yaml และนำเข้าอีกครั้งด้วย --sync ซึ่งเขียนคำตอบใหม่ลงในร่างและ pull อีกครั้ง จากนั้นบิลด์:

Terminal window
docker build -t order-support:local .
aggrega import . --sync
aggrega build --release release.yaml

จากนั้นตั้งค่าคีย์และรหัสผ่านของฐานข้อมูลใน shell ของคุณ แล้วเริ่มสแตก:

Terminal window
export OPENAI_API_KEY='your OpenAI key'
export POSTGRES_PASSWORD='a password of your own'
docker compose -p order-support -f dist/compose/docker-compose.yaml up -d --wait

-p ระบุชื่อโปรเจกต์ ตามที่ขั้นตอนรันใน วิธีที่การนำเข้าทำงาน อธิบายไว้ --wait คืนการทำงานเมื่อฐานข้อมูลรายงานว่าทำงานปกติ การรอฐานข้อมูลเสร็จสมบูรณ์ และบริการของกราฟรันอยู่

ทดสอบ

คำขอด้านล่างไปถึงโปรแกรมที่อิมเมจของบทแนะนำนี้รัน คือกราฟหลังเซิร์ฟเวอร์ HTTP ขนาดเล็กบนพอร์ต 8000 ที่ตอบ POST /threads/<thread>/runs โปรแกรมของคุณเองเป็นผู้กำหนดเส้นทางและการตอบกลับของตน ส่งคำขอไปที่ http://127.0.0.1:8000 ซึ่งเป็นที่อยู่ที่ published_port เผยแพร่:

Terminal window
curl -sS -X POST http://127.0.0.1:8000/threads/order-1234/runs -H 'Content-Type: application/json' \
-d '{"message": "Where is my order 1234? It has not arrived yet."}'
{
"thread": "order-1234",
"error": "the run failed: OpenAIAuthenticationError: Error code: 401 - Incorrect API key provided: sk-place************-key. [… omitted: where to find an API key]"
}

การรันตั้ง OPENAI_API_KEY เป็นค่าตัวแทน การตอบกลับจึงเป็นการที่ OpenAI ปฏิเสธคีย์นั้น ซึ่งโปรแกรมส่งต่อมา คำขอของกราฟไปถึง OpenAI API พร้อมคีย์ที่บิลด์ส่งมอบ จากโปรแกรมที่เปิด checkpointer ในฐานข้อมูลด้วยข้อมูลรับรองที่บิลด์ส่งมอบ

หยุดสแตกด้วย:

Terminal window
docker compose -p order-support -f dist/compose/docker-compose.yaml down -v

-v ลบวอลุ่มที่ฐานข้อมูลเก็บข้อมูลไว้ รวมถึงการสนทนาด้วย

บน Kubernetes นำเข้าด้วย --target helm และ push อิมเมจไปยังรีจิสทรีที่คลัสเตอร์ของคุณ pull ได้ โดยระบุชื่ออิมเมจใน import-answers.yaml จากนั้นสร้าง Secrets openai-api-key และ support-db-password และนำบิลด์ไปใช้ ตามที่ขั้นตอนรันใน วิธีที่การนำเข้าทำงาน อธิบายไว้ Deployment ของกราฟไม่มี Service pod ของกราฟรีสตาร์ตจนกว่าฐานข้อมูลจะรับการเชื่อมต่อ เพราะโปรแกรมเปิด checkpointer ตอนเริ่มทำงาน ส่งต่อพอร์ตภายในเครื่องไปยัง Deployment นั้น คำสั่งรันต่อไปจนกว่าคุณจะหยุด:

Terminal window
kubectl port-forward deploy/support 8000:8000
Forwarding from 127.0.0.1:8000 -> 8000
Forwarding from [::1]:8000 -> 8000

จากเทอร์มินัลอื่น ส่งคำขอเดียวกันไปยังพอร์ตที่ส่งต่อ:

Terminal window
curl -sS -X POST http://127.0.0.1:8000/threads/order-1234/runs -H 'Content-Type: application/json' \
-d '{"message": "Where is my order 1234? It has not arrived yet."}'
{
"thread": "order-1234",
"error": "the run failed: OpenAIAuthenticationError: Error code: 401 - Incorrect API key provided: sk-place************-key. [… omitted: where to find an API key]"
}