Bỏ qua để đến nội dung
AggregaAggrega
Chọn ngôn ngữ: Tiếng Việt
Trang này được dịch bằng máy. Đọc bản gốc tiếng Anh.

Nhập một workflow n8n

Hướng dẫn từng bước này nhập một workflow được xuất từ n8n — một tác tử AI trả lời tin nhắn chat bằng một mô hình chat của OpenAI và một bộ nhớ — vào một Aggregate, một Release và một Manifest mà, đúng như được viết, build n8n mang theo workflow đó cho Docker Compose, sau đó build chúng và chạy kết quả. Mọi tệp và mọi khối đầu ra dưới đây đều đến từ một lần chạy thực; workflow và từng giai đoạn của hướng dẫn nằm trong ví dụ import-n8n, nơi một script kiểm tra chạy lại quá trình nhập, so sánh những gì nó ghi, những gì nó in ra và các khối mà trang này trích dẫn với các tệp được lưu ở đó, rồi xác thực và build những gì quá trình nhập đã ghi.

Tạo ví dụ

Bạn có thể chạy lần nhập này bằng các lệnh trên trang này, hoặc để trợ lý AI của bạn chạy giúp bạn.

Ghi ví dụ vào một thư mục mới đặt theo tên của nó:

Terminal window
aggrega scaffold --example import-n8n

Sau đó nó in ra các lệnh cần chạy tiếp theo. Lệnh đầu tiên chuyển vào thư mục mới; lệnh tiếp sau đó, ở bên dưới, chuyển vào thư mục source của thư mục mới, nơi chứa những gì trang này nhập và là nơi chạy lệnh nhập:

Terminal window
cd import-n8n

Thư mục source của ví dụ được tạo khung, mở trong Visual Studio Code, hiển thị bản xuất workflow n8n của một agent hỗ trợ, mở ở các node trigger chat và AI Agent của nó.

Workflow

support-agent.json, được viết cho hướng dẫn này theo định dạng mà n8n xuất một workflow, chứa bốn node: một trigger chat, một AI Agent với một thông điệp hệ thống ngắn, một node OpenAI Chat Model chọn gpt-4o-mini, và một node Simple Memory, được nối với nhau bằng các kết nối mà n8n liệt kê theo tên của node mà mỗi kết nối xuất phát. Trigger chat là công khai, nên một tin nhắn chat gửi tới URL của nó sẽ khởi động workflow, và trigger yêu cầu người gửi cung cấp người dùng và mật khẩu, rồi kiểm tra chúng với một thông tin xác thực dùng cho xác thực cơ bản mà n8n lưu trữ:

"parameters": {
"public": true,
"authentication": "basicAuth",
"options": {}
},
[… omitted: the node's identifier, name, type, version, position and webhook identifier]
"credentials": {
"httpBasicAuth": {
"id": "Vx3mQ8rLb2Nk7Tz5",
"name": "Chat basic auth"
}
}

Node mô hình chọn thông tin xác thực của nó theo cùng cách, theo tham chiếu — bản xuất mang định danh và tên của mỗi thông tin xác thực, không bao giờ mang người dùng, mật khẩu hay khóa:

"parameters": {
"model": {
"__rl": true,
"value": "gpt-4o-mini",
"mode": "list",
"cachedResultName": "gpt-4o-mini"
},
"options": {}
},
"id": "6f1d2c3a-7b4e-4a8f-9c21-0d3e5f6a7b03",
"name": "OpenAI Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"typeVersion": 1.2,
"position": [120, 220],
"credentials": {
"openAiApi": {
"id": "Rb7nW2xQ9kT4mZ1c",
"name": "OpenAI account"
}
}

Bên cạnh workflow, một tệp .aggrega-config.yaml biến thư mục thành một không gian làm việc:

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

và import-answers.yaml chứa câu trả lời cho những gì không workflow nào có thể chứa. Một lần nhập cho Docker Compose không cần câu trả lời nào: nó gắn khóa OpenAI bằng một tham chiếu biến mà Docker Compose điền từ shell của bạn. Một lần nhập cho Kubernetes gắn khóa đó bằng tên của một Secret Kubernetes, thứ chỉ bạn mới có thể chỉ định, nên tệp chỉ định Secret đó (xem Trên Kubernetes):

# 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.
release.yaml:
# 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.n8n.uses.model.openai-chat-model.features.credential.infra.origin: "openai-api-key"

Tệp câu trả lời nhập mô tả đầy đủ tệp này.

Quá trình nhập đối chiếu từng nút với các Components trong danh mục của không gian làm việc, và --target soạn nháp một Manifest từ các Recipes của danh mục đó. Không gian làm việc không chỉ định thư mục danh mục nào, vì vậy quá trình nhập đọc danh mục thư viện chuẩn từ registry mà từ đó nó tải trình nhập của mình về: một bản chụp nhanh có chữ ký của danh mục đã phát hành, được lần nhập đầu tiên tải về, xác minh và lưu trong bộ nhớ đệm nội dung của không gian làm việc, và được nêu tên theo digest của nó trong bản ghi mà quá trình nhập ghi bên cạnh các bản nháp. Một không gian làm việc chỉ định các thư mục danh mục riêng — trong AGGREGA_CATALOG_DIRS, hoặc dưới workspace.catalog_dirs — thì thay vào đó nhập dựa trên các thư mục đó. Chuyển vào source:

Terminal window
cd source

Chạy lệnh nhập

Terminal window
aggrega import support-agent.json --target compose

Trên đầu ra chuẩn, nó liệt kê những gì đã ghi:

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

Trên luồng lỗi chuẩn, với <workspace> đại diện cho thư mục nơi bạn đã chạy nó:

[… 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 n8n format, with drafted paths relative to <workspace>
warn: the value of credentials.httpBasicAuth.id of node When chat message received is carried as written in the flow the draft carries: its format marks the field as a credential, and setting empty_credentials to marked or all writes it empty
warn: the value of credentials.openAiApi.id of node OpenAI Chat Model is carried as written in the flow the draft carries: its format marks the field as a credential, and setting empty_credentials to marked or all writes it empty
warn: node OpenAI Chat Model reaches openai-chat-model, wired to the model socket of core/ai-agent/n8n as openai-chat-model, other than through that connection: an n8n @n8n/n8n-nodes-langchain.lmChatOpenAi node reaches the part at the address the stored credential it selects, or its own parameters, give; the platform delivers this connection as OPENAI_BASE_URL and OPENAI_API_KEY, but an n8n workflow reads a variable of the platform's environment only through an expression reading $env, which the platform refuses while values.allow_env_access_in_nodes is false, the value the draft leaves it at; setting it true lets every workflow, and every user who can edit one, read every variable of the platform's environment, the delivered credentials included
made no choice for When chat message received (@n8n/n8n-nodes-langchain.chatTrigger)
made no choice for AI Agent (@n8n/n8n-nodes-langchain.agent)
mapped OpenAI Chat Model (@n8n/n8n-nodes-langchain.lmChatOpenAi) to core/ai-model/openai as a reference, HIGH at 0.9143
made no choice for Simple Memory (@n8n/n8n-nodes-langchain.memoryBufferWindow)
warning: node "n8n" (provider "compose"): artifact "n8n-flow.compose-fragment.yaml": skipped provenance label injection — the fragment declares no compose service. Compose carries build provenance as service labels, so a fragment contributing only top-level networks, volumes or configs carries none; the services this component emits elsewhere carry the full set.
Pull complete: <workspace>/release.lock (entities=8, source_artifacts=0, external_inputs=0)

Lần nhập chạy từ trình soạn thảo: các bản nháp Aggregate, Release và Manifest xuất hiện bên cạnh bản xuất workflow, đầu ra Aggrega liệt kê cách ánh xạ từng node và các tệp đã ghi, và một thông báo cho biết lần nhập đã soạn nháp 3 tệp, được nhận diện là n8n.

  • Dòng mapped cho biết node OpenAI Chat Model đã trở thành mô hình OpenAI của thư viện chuẩn, core/ai-model/openai, được tham chiếu thay vì được soạn thành bản nháp, vì nó khớp ở cấp cao, với 0.9143: một thành phần riêng, được nối tới socket model của nền tảng.
  • Các dòng made no choice cho biết trigger, tác tử và bộ nhớ vẫn nằm bên trong workflow, thứ mà chính n8n chạy.
  • Hai dòng warn đầu tiên nêu định danh của các thông tin xác thực đã lưu mà trigger và node mô hình chọn, những thứ mà n8n đánh dấu là thông tin xác thực. Đó là các tham chiếu, không phải bí mật, và được mang theo đúng như đã viết.
  • Dòng warn thứ ba là dòng cần đọc kỹ: node mô hình tiếp cận OpenAI bằng thông tin xác thực đã lưu mà nó chọn, chứ không qua kết nối mà bản nháp nối. Nền tảng cung cấp kết nối vào môi trường của n8n, và n8n từ chối việc một workflow đọc môi trường của nó chừng nào Aggregate còn để values.allow_env_access_in_nodes ở false, như bản nháp đang làm. Đặt nó thành true cho phép mọi workflow, và mọi người có thể chỉnh sửa một workflow, đọc mọi biến của nền tảng, kể cả khóa được cung cấp. Vì vậy khóa mà workflow sử dụng là khóa do n8n lưu trữ, như Chạy thử cho thấy.
  • Hai dòng cuối là bước pull. Không còn gì cần trả lời, nên quá trình nhập đã kéo các phụ thuộc của Release được soạn nháp, như aggrega pull làm, và ghi release.lock; bản build bên dưới không cần gì thêm. Dòng warning là dòng mà bản build cũng in ra, và Xác thực và build giải thích nó.

Tệp câu trả lời nêu tên một Secret mà lần nhập này không yêu cầu: quá trình nhập không để trống gì cho nó, nên không áp dụng câu trả lời nào và giữ nguyên tệp.

Những gì được ghi

Aggregate, Release và Manifest được đặt tên imported/support-agent, theo tên riêng của workflow, Support agent, rút gọn thành support-agent — một workflow không có tên được đặt tên theo tệp của nó — dưới namespace mà thiết lập target_namespace của trình nhập đặt ra, là imported trừ khi bạn đặt namespace khác (Thiết lập).

aggregate.yaml — một instance của nền tảng n8n trong thư viện chuẩn, core/ai-agent/n8n, mang workflow đúng như khi được xuất, phần lớn được lược bỏ ở đây, và mô hình được nối tới socket model của nó:

apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: imported/support-agent
version: 0.1.0
description: Draft Aggregate imported from a source in the n8n format.
spec:
components:
n8n:
type: core/ai-agent/n8n
values:
workflow_id: 'sQ4vT8nKp2LmX7bR'
flow_definition: |
{
"id": "sQ4vT8nKp2LmX7bR",
"name": "Support agent",
[… omitted: the rest of the workflow, as exported]
}
uses:
model:
openai-chat-model:
target: openai-chat-model
# For the compose target, the lines below declare credential on openai-chat-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
# swap to core/ai-model/azure-openai (score 0.4125) by setting type: core/ai-model/azure-openai
openai-chat-model:
type: core/ai-model/openai
values:
model: |-
gpt-4o-mini

workflow_id là định danh mà bản xuất gán cho workflow. Mô hình là core/ai-model/openai, với mô hình mà node chọn, gpt-4o-mini; chú thích phía trên nó nêu tên một Component khác trong danh mục mà node giống, cùng với điểm số của nó. Khóa OpenAI mà nền tảng cung cấp cho mô hình được khai báo trên kết nối cùng với biến mà nó được đưa vào môi trường của n8n, OPENAI_API_KEY; Release cho biết nó đến từ đâu.

release.yaml — Release, triển khai Aggregate với Manifest:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: imported/support-agent
# 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 n8n format.
spec:
aggregate: imported/support-agent
manifest: imported/support-agent
components:
n8n:
infra:
# The image running core/ai-agent/n8n, at the version this importer drafts, which the catalog's Recipes for it are known to run; the import does not raise or lower it to follow the source.
image: "n8nio/n8n:1.110.1"
# 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: 5678
uses:
model:
openai-chat-model:
features:
credential:
type: core/secret-value
infra:
# Attached by reference, never as a value: the credential n8n presents to openai-chat-model on its model connection, 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}"
openai-chat-model: {}
  • environment là development, một giá trị mặc định mà chú thích phía trên nó bảo hãy đổi thành môi trường mà Release triển khai tới.
  • Image là n8n 1.110.1, phiên bản mà các Recipes của danh mục cho n8n chạy.
  • published_port là 5678, cổng mà n8n phục vụ, nên trình soạn thảo trả lời tại http://127.0.0.1:5678 trên máy của bạn. Như chú thích của nó nói, mọi tiến trình trên máy này đều có thể truy cập nó ở đó và không gì khác có thể; hãy xóa dòng này để chỉ giữ n8n trên mạng của stack. Một published_port khác 5678 cần có infra.external_base_url: http://127.0.0.1:<published_port> bên cạnh, nếu không n8n sẽ báo các địa chỉ trình soạn thảo và webhook của nó trên localhost:5678.
  • Khóa là một tham chiếu biến, ${OPENAI_API_KEY}, mà Docker Compose điền từ shell của bạn khi stack khởi động. Mô hình OpenAI của thư viện chuẩn là một dịch vụ được lưu trữ, và kết nối tới nó phải mang thông tin xác thực, nên Release đính kèm một thông tin xác thực dù workflow đọc khóa của nó từ kho lưu trữ của n8n.

manifest.yaml — các Recipes Compose của nền tảng và mô hình, và cách hiện thực hóa ghi tham chiếu biến của khóa vào tệp Compose đúng như đã viết:

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

.aggrega-import.json là bản ghi những gì quá trình nhập đã ghi, được một lệnh aggrega import --sync sau đó đọc, và release.lock ghim mọi thực thể danh mục mà Release phân giải tới, theo phiên bản và digest.

Xác thực và dựng

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

Bản nháp Release mở trong trình soạn thảo sau khi xác thực, với đầu ra Aggrega và một thông báo đều cho biết không tìm thấy vấn đề nào.

Bản build của bản nháp Release: một thư mục dist xuất hiện trong Explorer, đầu ra Aggrega liệt kê tệp Docker Compose đã ghi, và một thông báo cho biết đã ghi 1 tệp.

Bước xác thực in ra:

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)

và bản dựng, với <workspace> đại diện cho thư mục chứa các bản nháp:

warning: node "n8n" (provider "compose"): artifact "n8n-flow.compose-fragment.yaml": skipped provenance label injection — the fragment declares no compose service. Compose carries build provenance as service labels, so a fragment contributing only top-level networks, volumes or configs carries none; the services this component emits elsewhere carry the full set.
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

Dòng warning nói về phần của tệp Compose mang workflow, một mục configs cấp cao nhất, không có service nào để gắn các nhãn của bản build. Dòng notice mang tính khuyến nghị: Feature bí mật chấp nhận origin của nó từ values của Aggregate hoặc infra của Release, và thông báo cho biết một lá được khai báo ở cả hai. Không dòng nào thay đổi phán quyết.

dist/compose/docker-compose.yaml chứa n8n và một service nhập workflow trước khi n8n khởi động:

services:
n8n:
image: n8nio/n8n:1.110.1
environment:
OPENAI_BASE_URL: https://api.openai.com/v1
OPENAI_API_KEY: ${OPENAI_API_KEY}
N8N_BLOCK_ENV_ACCESS_IN_NODE: "true"
ports:
- 127.0.0.1:5678:5678
[… omitted: the labels the build writes on every service, naming the Aggregate, the component, the environment and the compiler's version]
volumes:
- type: volume
source: n8n--data
target: /home/node/.n8n
networks:
- compose
depends_on:
n8n--import-flow:
condition: service_completed_successfully
[… omitted: the service's health check]
n8n--import-flow:
image: n8nio/n8n:1.110.1
command:
- import:workflow
- --input=/flows/workflow.json
entrypoint:
- n8n
[… omitted: the labels the build writes on every service]
volumes:
- type: volume
source: n8n--data
target: /home/node/.n8n
networks:
- compose
restart: "no"
configs:
- source: n8n-flow
target: /flows/workflow.json

Workflow đến được n8n thông qua n8n import:workflow, được chạy trên cùng một volume dữ liệu mỗi khi stack khởi động, trước chính n8n. N8N_BLOCK_ENV_ACCESS_IN_NODE là "true", cơ chế chặn của n8n đối với việc một workflow đọc môi trường của nó, vì Aggregate để allow_env_access_in_nodes ở false. Vì vậy node OpenAI Chat Model đọc khóa của nó từ kho thông tin xác thực riêng của n8n, giống như trigger chat đọc tên người dùng và mật khẩu của nó, và bước chạy bên dưới lưu trữ cả hai. Recipe Compose của n8n chỉ công bố một cổng trên máy của bạn khi Release đặt published_port trong infra của n8n, và khi đó chỉ trên địa chỉ loopback, 127.0.0.1; nếu không có nó, dịch vụ chỉ truy cập được trên mạng của stack.

Với trợ lý AI

Một trợ lý AI được thiết lập với các công cụ của Aggrega có thể làm điều tương tự: dán prompt này vào nó.

Chỉ dùng các công cụ của Aggrega, và không tự tạo thư mục nào. Nếu thư mục Workspace chưa có thư mục `import-n8n`, hãy gọi công cụ `scaffold` một lần với `example` đặt thành `import-n8n` và không có `dir`: công cụ này ghi ví dụ vào một thư mục `import-n8n` mới, trong đó thư mục `source` chứa những gì trang này nhập. Sau đó gọi công cụ `import` với `workspace_root` đặt thành `import-n8n/source`, `source` đặt thành `support-agent.json` và `target` đặt thành `compose`: công cụ này đọc các câu trả lời cần thiết từ `import-answers.yaml` bên cạnh `support-agent.json`, ghi các bản nháp vào `import-n8n/source` và kéo về những gì chúng cần. Sau đó gọi công cụ `validate` với `workspace_root` đặt thành `import-n8n/source` và `release` đặt thành `release.yaml`, và công cụ `build` với `workspace_root` đặt thành `import-n8n/source`, `release` đặt thành `release.yaml` và `write` đặt thành true. Cho tôi biết bản build đã ghi những tệp nào; chúng nằm dưới `import-n8n/source/dist/`.

Mở trong VS Code

Khi đã cài tiện ích mở rộng Aggrega cho Visual Studio Code, hãy mở thư mục import-n8n/source trong VS Code, nơi lần nhập đã ghi các bản nháp. Thư mục chứa một Release, chính là release.yaml đã soạn nháp, nên trình soạn thảo chọn nó cho bạn, và kiểm tra các bản nháp đối chiếu với Release đó khi bạn chỉnh sửa, đánh dấu những gì nó tìm thấy trong tệp và trong bảng Problems. Aggrega: Show topology graph, trong Command Palette, vẽ các thành phần của Release và các kết nối giữa chúng. Aggrega: Build, Aggrega: Preview build và Aggrega: Pull hoạt động trên cùng Release đó, và mỗi lệnh cũng là một nút trên bảng Commands của chế độ xem Aggrega trong thanh hoạt động; Build ghi các tệp của nó vào dist/ của thư mục. Dùng Aggrega trong Visual Studio Code trình bày phần còn lại.

Chạy thử

n8n đọc cả hai thông tin xác thực mà workflow chọn từ kho riêng của nó, theo định danh của chúng: khóa OpenAI dưới Rb7nW2xQ9kT4mZ1c, và tên người dùng cùng mật khẩu của chat dưới Vx3mQ8rLb2Nk7Tz5. Dòng lệnh riêng của n8n lưu chúng, dưới các định danh đó, từ một tệp. Ghi chúng vào credentials.json, với các giá trị của riêng bạn:

[
{"id": "Rb7nW2xQ9kT4mZ1c", "name": "OpenAI account", "type": "openAiApi", "data": {"apiKey": "your OpenAI key"}},
{"id": "Vx3mQ8rLb2Nk7Tz5", "name": "Chat basic auth", "type": "httpBasicAuth", "data": {"user": "a user of your own", "password": "a password of your own"}}
]

Đặt khóa trong shell của bạn, theo cách Release bản nháp đọc nó, và khởi động stack:

Terminal window
export OPENAI_API_KEY='your OpenAI key'
docker compose -p support-agent -f dist/compose/docker-compose.yaml up -d --wait

-p đặt tên cho dự án, như bước chạy của Cách một lần nhập diễn ra giải thích. --wait trả về ngay khi n8n báo cáo trạng thái khỏe mạnh, sau khi dịch vụ import workflow đã chạy và thoát. Sao chép thông tin xác thực vào container của n8n, import chúng, rồi xóa bản sao:

Terminal window
docker compose -p support-agent -f dist/compose/docker-compose.yaml exec -T n8n sh -c 'umask 077; cat > /tmp/credentials.json' < credentials.json
docker compose -p support-agent -f dist/compose/docker-compose.yaml exec n8n n8n import:credentials --input=/tmp/credentials.json
docker compose -p support-agent -f dist/compose/docker-compose.yaml exec n8n rm /tmp/credentials.json
[… omitted: n8n's notices about its settings file's permissions, error tracking and deprecated settings]
Successfully imported 2 credentials.

Giờ đây n8n đã giữ cả hai thông tin xác thực, vì vậy hãy xóa credentials.json:

Terminal window
rm credentials.json

Sau đó kích hoạt workflow, và khởi động lại n8n để việc kích hoạt có hiệu lực:

Terminal window
docker compose -p support-agent -f dist/compose/docker-compose.yaml exec n8n n8n update:workflow --id=sQ4vT8nKp2LmX7bR --active=true
docker compose -p support-agent -f dist/compose/docker-compose.yaml restart n8n

Khi docker compose -p support-agent -f dist/compose/docker-compose.yaml ps n8n lại hiển thị n8n (healthy), docker compose -p support-agent -f dist/compose/docker-compose.yaml logs n8n sẽ hiển thị Activated workflow "Support agent" (ID: sQ4vT8nKp2LmX7bR).

Kiểm thử

Mở http://127.0.0.1:5678, địa chỉ mà published_port công bố: trình soạn thảo của n8n yêu cầu một tài khoản chủ sở hữu ở lần truy cập đầu tiên, rồi hiển thị Support agent.

Trigger chat chỉ nhận một tin nhắn khi có tên người dùng và mật khẩu đã lưu ở trên; không có chúng, nó trả lời HTTP/1.1 401 Unauthorized và Authorization is required!, còn với mật khẩu sai thì HTTP/1.1 403 Forbidden. Với hai giá trị nằm trong CHAT_USER và CHAT_PASSWORD, curl đọc chúng từ đầu vào chuẩn của nó, nên chúng không xuất hiện trên dòng lệnh nào:

Terminal window
printf 'user = "%s:%s"\n' "$CHAT_USER" "$CHAT_PASSWORD" | curl --config - -sS -i -X POST \
http://127.0.0.1:5678/webhook/a3c9e1f2-4b5d-4e6f-8a7b-9c0d1e2f3a04/chat -H 'Content-Type: application/json' \
-d '{"action": "sendMessage", "sessionId": "s1", "chatInput": "In one short sentence: what does a support plan cover?"}'
HTTP/1.1 500 Internal Server Error
[… omitted: the response's other headers]
{"message":"Error in workflow"}

Lần chạy đã lưu một giá trị giữ chỗ cho khóa OpenAI, nên workflow dừng lại ở lời gọi tới mô hình. n8n giữ lại lần thực thi đó, được trình soạn thảo liệt kê trong các lần thực thi của workflow, cùng với lời từ chối của OpenAI: Incorrect API key provided: sk-place*****************-key. Với khóa của bạn, câu trả lời là HTTP/1.1 200 OK và câu trả lời của agent, dưới dạng {"output": "…"}.

Trên Kubernetes, khi đã áp dụng bản build mà Trên Kubernetes mô tả, n8n giữ dữ liệu của nó trong một volume tồn tại chừng nào pod của nó còn tồn tại, nên một pod mới khởi động mà không có những gì bạn đã lưu; thay vào đó, hãy lưu thông tin xác thực và kích hoạt workflow trên instance đang chạy. Viết credentials.json như Chạy đã làm, nhập nó qua kubectl exec, từ đầu vào chuẩn, trong namespace mà bạn đã áp dụng vào, rồi xóa nó sau đó:

Terminal window
kubectl exec -i -n <namespace> deploy/n8n -- n8n import:credentials --input=/dev/stdin < credentials.json
Defaulted container "main" out of: main, import-flow (init)
[… omitted: n8n's notices about its settings file's permissions, error tracking and deprecated settings]
Successfully imported 2 credentials.

Chuyển tiếp một cổng cục bộ tới Service của n8n, n8n-default, và để lệnh tiếp tục chạy:

Terminal window
kubectl port-forward -n <namespace> svc/n8n-default 5678:5678

Từ một terminal khác, hãy thiết lập tài khoản chủ sở hữu qua API của n8n, API này đăng nhập cho bạn và giữ phiên trong cookies.txt, rồi kích hoạt workflow:

Terminal window
curl -sS -c cookies.txt -X POST http://127.0.0.1:5678/rest/owner/setup -H 'Content-Type: application/json' \
-d '{"email": "owner@example.com", "firstName": "Your", "lastName": "Name", "password": "a password of your own"}'
curl -sS -b cookies.txt -X PATCH http://127.0.0.1:5678/rest/workflows/sQ4vT8nKp2LmX7bR \
-H 'Content-Type: application/json' -d '{"active": true}'

Câu trả lời thứ hai là workflow, với "active":true. Tin nhắn chat ở trên khi đó đến được nó tại cùng địa chỉ, không thay đổi; lần chạy nhận được cùng {"message":"Error in workflow"} và cùng lời từ chối trong lần thực thi.

Dừng stack bằng lệnh bên dưới. -v cũng xóa volume dữ liệu của n8n, và cùng với nó là các thông tin xác thực đã lưu và việc kích hoạt:

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

Trên Kubernetes

Cùng workflow đó được nhập cho Kubernetes với đích helm. Các bản nháp cho Docker Compose đã nằm sẵn trong source, và lần nhập không bao giờ ghi đè một Aggregate ở đó, vì vậy hãy tạo các bản nháp này vào một thư mục riêng bằng --output-dir, rồi xác thực và build từ thư mục đó:

Terminal window
aggrega import support-agent.json --target helm --output-dir helm
cd helm
aggrega validate --release release.yaml
aggrega build --release release.yaml

Các bản nháp khác nhau ở chỗ target khác nhau. Manifest ánh xạ nền tảng tới helm/n8n, mô hình tới helm/openai, và khóa tới helm/k8s-existing-secret, thứ đọc khóa từ một Secret đã có sẵn trong namespace. Release đính kèm khóa dưới dạng core/referenced-secret, nêu tên Secret đó, openai-api-key, từ tệp câu trả lời.

Bản build ghi các manifest Kubernetes thuần vào dist/helm/ tại thư mục gốc của workspace, source, mà từ helm là ../dist/helm/. Tạo Secret trong namespace mà bạn áp dụng vào, chứa khóa dưới khóa dữ liệu value, rồi áp dụng thư mục như bước chạy của Cách một lần nhập vận hành giải thích. Workflow vẫn đọc thông tin xác thực của nó từ kho lưu trữ riêng của n8n, kho mà Kiểm thử điền trên Kubernetes.