跳到內容
AggregaAggregaAggregaAggrega
選擇語言: 繁體中文
本頁為機器翻譯。閱讀英文原文。

匯入 Dify 應用程式

本逐步說明會將從 Dify 匯出的應用程式——OpenAI 模型上的聊天助理——匯入為 Aggregate、Release 與 Manifest,它們會建置標準程式庫的 Dify 安裝並帶著該應用程式,依原樣用於 Kubernetes,接著建置並執行結果。下方的每個檔案與每段輸出都來自真實執行;該應用程式與逐步說明的每個階段都在 import-dify 範例中,其中的檢查指令碼會再次執行匯入,將其寫出的內容、印出的內容,以及本頁引用的區塊與該處保存的檔案比對,並驗證與建置匯入所寫出的內容。

草擬範例

你可以用本頁的命令執行這個匯入,或是讓你的 AI 助理替你執行。

把範例寫入一個以它命名的新資料夾:

Terminal window
aggrega draft --example import-dify

接著它會印出下一步要執行的命令。第一個切換到新資料夾;其後的那一個(見下方)切換到它的 source 資料夾,其中存放本頁所匯入的內容,也是匯入執行的位置:

Terminal window
cd import-dify

在 Visual Studio Code 中開啟的範例 source 資料夾,顯示聊天模式下客服助理的 Dify 應用程式匯出內容。

應用程式

support-assistant.yml 是為本逐步說明撰寫的聊天應用程式,使用 Dify 的 DSL(Dify 為應用程式匯出的 YAML),DSL 版本為 0.7.0:包含一段開場白、一個簡短的提示,以及它用來回答的模型,並以其提供者命名:

model:
completion_params:
stop: []
temperature: 0.4
mode: chat
name: gpt-4o-mini
provider: langgenius/openai/openai

旁邊的 .aggrega-config.yaml 讓該資料夾成為工作區:

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

而 import-answers.yaml 保存的是任何應用程式都無法保存之內容的答案:安裝的應用程式機密,也就是 Dify 用來簽署工作階段、並用來加密其所儲存之模型提供者金鑰的金鑰;以及匯入應用程式時所用的擁有者帳號,包含其電子郵件與密碼。在 Kubernetes 上,每個機密都是從 Kubernetes Secret 讀取,所以其答案就是該 Secret 的名稱;同一個檔案也適用於 Docker Compose 的匯入,如 On Docker Compose 所示:

# 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:
# Passed down to core/dify's secret_key parameter, which has no default; that parameter's description
# in core/dify says what the value is.
spec.parameters.secret_key: "dify-credentials"
# Email address of the installation's owner account, read only when values.app_definition is set. The
# step that imports the application creates this account when it performs Dify's first-time setup, and
# signs in as it; on an installation whose setup has already run, it names an existing account allowed
# to import applications, and the password attached to the owner_password slot is that account's.
# Required when values.app_definition is set: the build halts when it is empty. It has no default.
spec.boundaries.platform.aggregates.dify.components.api.infra.owner_email: "owner@example.com"
# 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.boundaries.platform.aggregates.dify.components.api.features.owner_password.infra.origin: "dify-owner"

owner@example.com 代表你自己的電子郵件;dify-credentials 與 dify-owner 是 Run it 所建立的 Secret。沒有該檔案時,在終端機進行的匯入會逐一詢問這三個值,一次一個問題,並將你的答案寫入新的答案檔;在無法詢問的情況下,它會寫出帶有這三個鍵與空白答案的該檔案,列出這些鍵,並以狀態 1 結束。匯入答案檔完整說明了該檔案。

工作區沒有指定目錄資料夾,所以匯入會從它取得匯入器的登錄庫讀取標準程式庫目錄:那是已發布目錄的簽署快照,由第一次匯入取得、驗證並保存在工作區的內容快取中,並以其摘要記載於匯入寫在草稿旁的記錄中。若工作區在 AGGREGA_CATALOG_DIRS 或 workspace.catalog_dirs 之下指定了自己的目錄資料夾,則會先讀取那些資料夾,再讀取其後的已發布快照。切換到 source:

Terminal window
cd source

執行匯入

標準程式庫的 Dify 安裝可在 Kubernetes 與 Docker Compose 上實現。本逐步說明指定 helm 目標:

Terminal window
aggrega import support-assistant.yml --target helm

在標準輸出上,它會列出所寫出的內容:

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 dify format, with drafted paths relative to <workspace>
warn: node model_config.model (model) names the model "gpt-4o-mini" of the model provider "langgenius/openai/openai", whose credentials are set inside Dify, and core/ai-agent/dify configures that for itself rather than reaching it through a socket, so the draft wires no part for it
made no choice for model_config.model (model)
Pull complete: <workspace>/release.lock (entities=25, source_artifacts=0, external_inputs=0)

從編輯器執行匯入:草擬的 Aggregate、Release 與 Manifest 出現在應用程式匯出檔旁,Aggrega 輸出列出草稿所連接的內容與寫出的檔案,通知則說明匯入草擬了 3 個檔案,辨識為 dify。

  • warn 行說明模型仍留在 Dify 內:Dify 自行設定其模型提供者及其金鑰,所以草稿不連接任何模型。Dify 執行後,你要安裝其 OpenAI 提供者並設定該提供者的金鑰,如 Test it 所示。
  • Pull complete 行就是拉取。在答案檔回答了每個值之後,匯入像 aggrega pull 一樣拉取了草稿 Release 的相依項目,並寫出 release.lock;下方的建置不需要更多內容。

它寫出什麼

Aggregate、Release 與 Manifest 的名稱為 imported/support-assistant,取自應用程式的名稱——app 下的 Support assistant,簡化為 support-assistant(沒有名稱的應用程式則以其檔案命名)——位於匯入器的 target_namespace 設定所指定的命名空間之下,除非你另行設定,否則為 imported(設定)。

aggregate.yaml——標準程式庫的 Dify 安裝 core/dify,是草稿在 aggregates 之下組合的一個 Aggregate,應用程式依原樣放在其 app_definition 參數中(此處省略大部分內容),而安裝的 secret_key 則從草稿宣告的參數往下傳遞:

apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: imported/support-assistant
version: 0.1.0
description: Draft Aggregate imported from a source in the dify format.
spec:
parameters:
secret_key:
type: string
required: true
minLength: 1
description: "Passed down to core/dify's secret_key parameter, which has no default; that parameter's description in core/dify says what the value is."
aggregates:
# An import sees the Components of the workspace's catalog and none of its Aggregates, so it does not check that the catalog holds core/dify.
dify:
type: core/dify
parameters:
secret_key: "{{ .parameters.secret_key }}"
app_definition: |
app:
description: Answers questions about the support plans of a small software company.
[… omitted: the rest of the application, as exported]
version: 0.7.0

該安裝由十個元件組成——Dify 的 API 伺服器、其背景工作程式、其排程器、其網頁前端、其程式碼沙箱、其外掛程式執行階段、一個輸出 Proxy、PostgreSQL、一個相容 Redis 的快取,以及 Weaviate——彼此連接,而匯入該應用程式是其 API 伺服器的 Recipe 所渲染的一個步驟。它的應用程式機密在 core/dify 中沒有預設值,所以草稿將 secret_key 宣告為自己的參數,不帶預設值,並往下傳遞,交由 Release 來滿足。該安裝的其他認證沿用 core/dify 給它們的預設值,如 Run it 所述。

release.yaml——Release,它將該安裝路由到一個邊界 platform 中,並滿足應用程式無法從答案檔提供的內容:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: imported/support-assistant
# 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 dify format.
spec:
aggregate: imported/support-assistant
manifest: imported/support-assistant
parameters:
# Left for the Release, since the source cannot supply it: secret_key, which the drafted Aggregate declares and passes down to core/dify; that parameter's description there says what the value is
secret_key: "dify-credentials"
boundaries:
platform:
aggregates:
dify:
components:
api:
infra:
# Lets the installation reach Dify's plugin marketplace, which the plugin serving the application's model, such as OpenAI's, is installed from.
marketplace_enabled: true
# Left for the Release, since the source cannot supply it: components.api.infra.owner_email, the email of the installation's owner account the application is imported under, on the api component of core/dify
owner_email: "owner@example.com"
features:
owner_password:
type: core/referenced-secret
infra:
# Left for the Release, since the source cannot supply it: the name of a Kubernetes Secret already in the namespace holding, under the data key value, the owner account's password.
origin: "dify-owner"
worker:
infra:
# Lets the installation reach Dify's plugin marketplace, which the plugin serving the application's model, such as OpenAI's, is installed from.
marketplace_enabled: true
scheduler:
infra:
# Lets the installation reach Dify's plugin marketplace, which the plugin serving the application's model, such as OpenAI's, is installed from.
marketplace_enabled: true
  • environment 是 development,這是預設值,其上方的註解說明要將它改為該 Release 部署的環境。
  • secret_key 是保存應用程式機密之 Secret 的名稱 dify-credentials,位於安裝所指定的資料鍵 secret-key 之下:Manifest 從命名空間中已存在的 Secret 讀取每個認證。
  • 擁有者帳號設定在該安裝的 api 元件上,位於將安裝放入其邊界的路由項目中:包含其電子郵件,以及以 core/referenced-secret 附加的密碼,該項目指名保存密碼的 Secret dify-owner,位於資料鍵 value 之下。已回答之值上方的註解說明了匯入為何無法草擬它們。
  • marketplace_enabled 在 API 伺服器 api、其背景工作程式與其排程器上為 true,適用於每個目標:除非 Release 允許,否則該安裝不會連到 Dify 供應商的任何東西,而應用程式用來回答的 OpenAI 提供者是 Dify 從其外掛程式市集安裝的外掛程式,如 Test it 所示。這三者各自讀取自己的設定。

manifest.yaml——該安裝所組成之每個元件的 Helm Recipe,以及從命名空間中已存在的 Secret 讀取認證的實現:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: imported/support-assistant
version: 0.1.0
spec:
mappings:
components:
type:
core/datastore/postgres: helm/postgres
core/datastore/redis: helm/redis
core/datastore/weaviate: helm/weaviate
core/ai-agent/dify-ssrf-proxy: helm/dify-ssrf-proxy
core/ai-agent/dify-sandbox: helm/dify-sandbox
core/ai-agent/dify-plugin-daemon: helm/dify-plugin-daemon
core/ai-agent/dify: helm/dify
core/ai-agent/dify-worker: helm/dify-worker
core/ai-agent/dify-worker-beat: helm/dify-worker-beat
core/ai-agent/dify-web: helm/dify-web
features:
type:
core/referenced-secret: helm/k8s-existing-secret

--target 會對應該安裝所組成之元件的元件型別,以及它自己的元件型別。當目錄為某個型別提供多個 Helm Recipes 時,如 PostgreSQL,它會對應以該型別命名的那一個,即 helm/postgres。

.aggrega-import.json 是匯入所寫內容的記錄,之後的 aggrega import --sync 會讀取它,而 release.lock 以版本與摘要鎖定該 Release 所解析到的每個目錄實體。

驗證並建置

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

驗證後在編輯器中開啟的草稿 Release,Aggrega 輸出與通知都回報未發現問題。

草稿 Release 的建置:總管中出現 dist 資料夾,Aggrega 輸出列出 Dify 安裝的 Kubernetes 資訊清單,通知指出已寫入 24 個檔案。

驗證會印出:

notice: LINT: Feature "core/referenced-secret" at core/referenced-secret: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/referenced-secret" at core/referenced-secret: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: 24 files written to <workspace>/dist

notice 那一行僅供參考:機密 Feature 從 Aggregate 的 values 或 Release 的 infra 接受它的 origin,而該通知回報有一個葉節點同時在兩者中宣告。它不改變任何裁定。

建置會將該安裝的 Kubernetes 資訊清單寫在 dist/boundaries/platform/helm/ 之下。其中有匯入應用程式的 Job,即 dify.api/dify-app-import.job.yaml。它會等待 Dify 的 API 伺服器回應,在尚未執行首次設定的情況下以 Release 所指名的擁有者帳號執行 Dify 的首次設定,然後匯入應用程式:

spec:
podFailurePolicy:
rules:
- action: FailJob
onExitCodes:
containerName: import
operator: In
values: [2]
template:
spec:
restartPolicy: Never
containers:
- name: import
image: "langgenius/dify-api:1.17.0"
command: ["python", "/seed/import.py"]
env:
- name: DIFY_API_URL
value: "http://dify-zdapi-default:5001"
- name: OWNER_EMAIL
value: "owner@example.com"
- name: OWNER_PASSWORD
valueFrom:
secretKeyRef:
name: "dify-owner"
key: "value"
volumeMounts:
- name: seed
mountPath: /seed
readOnly: true
volumes:
- name: seed
configMap:
name: dify-zdapi-app

應用程式本身在旁邊的 ConfigMap 中。當 Dify 拒絕該步驟傳送的內容時,也就是以請求逾時或速率限制以外的用戶端錯誤回覆,或回報匯入失敗,該步驟會印出 Dify 回覆所帶的訊息並以狀態 2 結束,當定義在 app.name 之下沒有指名任何應用程式時也一樣;Job 的 podFailurePolicy 會在該次嘗試就讓 Job 失敗,因為重試會以同樣方式被拒絕;任何其他失敗,例如伺服器在大約十分鐘內沒有回應,則會重試。

搭配 AI 助理

設定了 Aggrega 工具的 AI 助理也能做同樣的事:把這段提示貼給它。

只使用 Aggrega 工具,且不要自行建立任何資料夾。如果 Workspace 資料夾中還沒有 `import-dify` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `import-dify`,且不帶 `dir`:它會將範例寫入新的 `import-dify` 資料夾,其 `source` 資料夾保存本頁所匯入的內容。接著呼叫 `import` 工具,將 `workspace_root` 設為 `import-dify/source`、`source` 設為 `support-assistant.yml`、`target` 設為 `helm`:它會從 `support-assistant.yml` 旁的 `import-answers.yaml` 讀取所需的答案,將草稿寫入 `import-dify/source`,並拉取它們所需的內容。接著呼叫 `validate` 工具,將 `workspace_root` 設為 `import-dify/source`、`release` 設為 `release.yaml`,並呼叫 `build` 工具,將 `workspace_root` 設為 `import-dify/source`、`release` 設為 `release.yaml`、`write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `import-dify/source/dist/` 之下。

在 VS Code 中開啟

安裝 Aggrega extension for Visual Studio Code 後,在 VS Code 中開啟資料夾 import-dify/source,匯入就是把草稿寫在那裡。該資料夾保存一個 Release,即草稿的 release.yaml,所以編輯器會替你選取它;一旦 On Docker Compose 在 compose 中草擬了第二個 Release,Aggrega: Select Release… 就會選擇編輯器要處理的那一個。你編輯草稿時,編輯器會依所選取的 Release 檢查它們,並在檔案與 Problems 面板中標示所發現的問題。命令選擇區中的 Aggrega: Show topology graph 會繪出所選 Release 的元件以及它們之間的連線。Aggrega: Build、Aggrega: Preview build 與 Aggrega: Pull 都作用於同一個 Release,而且每一個也是活動列中 Aggrega 檢視的 Commands 面板上的按鈕;Build 會將其檔案寫入該資料夾的 dist/。其餘內容請見 Use Aggrega in Visual Studio Code。

執行

套用資訊清單之前,先建立它們所讀取的 Secret。指名了不存在之 Secret 的工作負載不會啟動。其中兩個保存答案檔所指名的內容:dify-owner,在 value 之下保存擁有者的密碼,以及 dify-credentials,在 secret-key 之下保存應用程式機密。該安裝的另外四個認證——其資料庫密碼,以及其元件彼此出示的三把金鑰——沿用 core/dify 給它們的預設值,而在 Kubernetes 上,每個預設值都是資訊清單讀取它的 Secret 名稱:dify-db-password-for-evaluation-only 位於 db-password 之下、dify-code-execution-key-for-evaluation-only 位於 code-execution-key 之下、dify-plugin-daemon-key-for-evaluation-only 位於 plugin-daemon-key 之下,以及 dify-inner-api-key-for-evaluation-only 位於 inner-api-key 之下。這四個也要建立,各自保存你自己的值;若要從名稱不同的 Secret 讀取它們,請在 Aggregate 的 dify 項目中,於 parameters 之下,將名稱傳給 core/dify,作為 db_password、code_execution_key、plugin_daemon_key 與 inner_api_key。

在你的 shell 中設定擁有者的密碼,名為 DIFY_OWNER_PASSWORD,因為你稍後要用它登入。Dify 接受的擁有者密碼至少八個字元,且包含一個字母與一個數字:給予任何其他密碼時,匯入 Job 會在 Dify 拒絕設定時立即失敗,其記錄會顯示 first-time setup was refused with HTTP 422: Validation error for parameter 'payload',這是 Dify 對它拒絕的擁有者帳號所給的回覆。下方的等待會等到 Job 完成,而失敗的 Job 永遠不會完成,所以它會一直執行到逾時;請在旁邊的第二個終端機中執行 kubectl wait --for=condition=failed job/dify-zdapi-app-import --timeout=15m,這個等待就會在 Job 失敗時立即返回。刪除該 Job 與 dify-owner Secret,以 Dify 接受的密碼再次建立該 Secret,然後再次套用。

為其他五個 Secret 各寫一個檔案,各保存一行:資料鍵、=,以及你自己的值——dify-credentials.env 內容為 secret-key=、db-password.env 內容為 db-password=、code-execution-key.env 內容為 code-execution-key=、plugin-daemon-key.env 內容為 plugin-daemon-key=,以及 inner-api-key.env 內容為 inner-api-key=。然後建立這六個 Secret。此時叢集已保存了這些值,所以刪除這些檔案:

Terminal window
printf 'value=%s\n' "$DIFY_OWNER_PASSWORD" > dify-owner.env
kubectl create secret generic dify-owner --from-env-file=dify-owner.env
kubectl create secret generic dify-credentials --from-env-file=dify-credentials.env
kubectl create secret generic dify-db-password-for-evaluation-only --from-env-file=db-password.env
kubectl create secret generic dify-code-execution-key-for-evaluation-only --from-env-file=code-execution-key.env
kubectl create secret generic dify-plugin-daemon-key-for-evaluation-only --from-env-file=plugin-daemon-key.env
kubectl create secret generic dify-inner-api-key-for-evaluation-only --from-env-file=inner-api-key.env
rm dify-owner.env dify-credentials.env db-password.env code-execution-key.env plugin-daemon-key.env inner-api-key.env

套用資訊清單,如 How an import runs 的執行步驟所說明——-R 會涵蓋 dify.api/ 與 dify.proxy/ 中的 Job 與兩個 ConfigMap——然後等待匯入 Job 並讀取其記錄,再等待外掛程式執行階段:

Terminal window
kubectl apply -R -f dist/boundaries/platform/helm/
kubectl wait --for=condition=complete job/dify-zdapi-app-import --timeout=15m
kubectl logs job/dify-zdapi-app-import
kubectl rollout status statefulset/dify-zdplugins
service/dify-zdapi-default created
deployment.apps/dify-zdapi created
[… omitted: a line for each of the other Services, Deployments and StatefulSets]
job.batch/dify-zdapi-app-import created
configmap/dify-zdapi-app created
configmap/dify-zdproxy-config created
job.batch/dify-zdapi-app-import condition met
first-time setup done: owner account owner@example.com
imported 'Support assistant' as application 7e03e85c-b13f-4ddc-b539-040470a61d4c
partitioned roll out complete: 1 new pods have been updated...

記錄會指出應用程式的識別碼,Test it 需要它。

測試

Dify 的 API 在 http://127.0.0.1:5001 回應。在 Kubernetes 上,將本機連接埠轉送到 API 伺服器的 Service dify-zdapi-default,位於你套用到的命名空間中,並讓該命令在第二個終端機中持續執行:

Terminal window
kubectl port-forward -n <namespace> svc/dify-zdapi-default 5001:5001

在 Docker Compose 上,依 On Docker Compose 所示啟動後,Release 已在該位址發布 API,並在 http://127.0.0.1:3001 發布主控台。開啟 http://127.0.0.1:3001/signin,即主控台的登入頁面;/ 會重新導向到那裡。該頁面將其請求傳送到 http://127.0.0.1:5001 的 API,而 Dify 的 API 只對來自草稿 console_url 所指名位址 http://127.0.0.1:3001 的頁面回應瀏覽器,所以請以擁有者身分在那裡登入。若在任何其他位址開啟,主控台的請求不會得到這樣的回應,瀏覽器會拒絕每個回覆;請將主控台發布在另一個連接埠,並隨之變更 console_url。

應用程式也能透過 API 回應,無需瀏覽器。在已設定 DIFY_OWNER_PASSWORD 的終端機中,以擁有者身分登入。主控台 API 接受以 Base64 編碼的密碼,而登入會將工作階段留在 cookies.txt 中。之後的每個主控台請求都會送回該工作階段,並在 X-CSRF-Token 標頭中帶上同一檔案中的 CSRF 權杖;沒有它的請求會被拒絕,並回應 CSRF token is missing or invalid.

Terminal window
printf '{"email": "owner@example.com", "password": "%s"}' "$(printf %s "$DIFY_OWNER_PASSWORD" | base64 | tr -d '\n')" |
curl -sS -c cookies.txt http://127.0.0.1:5001/console/api/login -H 'Content-Type: application/json' -d @-
CSRF=$(awk '$6 == "csrf_token" {print $7}' cookies.txt)
{"result":"success"}

Dify 的模型提供者是外掛程式,而新的安裝沒有任何提供者。安裝 Dify 的 OpenAI 提供者,版本 1.1.0,也就是該次執行所安裝的版本;外掛程式執行階段會從 Dify 的市集取得它,而草稿的 marketplace_enabled 讓安裝可以連到市集,所以該安裝必須能連上網際網路:

Terminal window
curl -sS -b cookies.txt -H "X-CSRF-Token: $CSRF" -H 'Content-Type: application/json' \
http://127.0.0.1:5001/console/api/workspaces/current/plugin/install/marketplace \
-d '{"plugin_unique_identifiers": ["langgenius/openai:1.1.0@1ded02acee56efd5788c86fd447d792e4923e959554a4942f440c7859d164340"]}'
{"all_installed":false,"task_id":"01a1dc0a-653d-756b-a104-e5d6d1e43695","task":null}

若從 Release 中刪除 marketplace_enabled,Dify 會改為拒絕該請求,並回應 {"code":"invalid_param","message":"marketplace is not enabled","status":400}。安裝在背景執行。以回覆所指名的 task_id 詢問其工作,直到其 status 為 success:

Terminal window
curl -sS -b cookies.txt -H "X-CSRF-Token: $CSRF" \
http://127.0.0.1:5001/console/api/workspaces/current/plugin/tasks/<task id>
{"task":{"id":"01a1dc0a-653d-756b-a104-e5d6d1e43695",[… omitted: when it was created and last updated],"status":"success","total_plugins":1,"completed_plugins":1,"plugins":[{"plugin_unique_identifier":"langgenius\/openai:1.1.0@1ded02acee56efd5788c86fd447d792e4923e959554a4942f440c7859d164340",[… omitted: the plugin's status, its icon and its names in four languages]}]}}

將提供者的金鑰寫入 openai.json,內容為 {"credentials": {"openai_api_key": "your OpenAI key"}},設定它,然後刪除該檔案:

Terminal window
curl -sS -b cookies.txt -H "X-CSRF-Token: $CSRF" -H 'Content-Type: application/json' \
http://127.0.0.1:5001/console/api/workspaces/current/model-providers/langgenius/openai/openai/credentials \
-d @openai.json
rm openai.json

Dify 會在保留金鑰之前,以一個對提供者的請求檢查該金鑰,對於提供者接受的金鑰,回應 {"result":"success"}。該次執行設定了預留位置,而 Dify 拒絕了它,並附上 OpenAI 的回答:

{"code":"invalid_param","message":"Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-place*****************-key. You can find your API key at https:\/\/platform.openai.com\/account\/api-keys.', 'type': 'invalid_request_error', 'code': 'invalid_api_key', 'param': None}, 'status': 401}","status":400}

應用程式會回應以其自有 API 金鑰發出的請求。以匯入該應用程式之步驟的記錄中的應用程式識別碼建立一個——在 Docker Compose 上為 docker compose -p support-assistant -f ../dist/boundaries/platform/compose/docker-compose.yaml logs dify-zdapi-app-import:

Terminal window
curl -sS -X POST -b cookies.txt -H "X-CSRF-Token: $CSRF" \
http://127.0.0.1:5001/console/api/apps/<application id>/api-keys
{"id":"8d6c1eb1-cf61-4825-9b04-f32689ed0489","type":"app","token":"[… omitted: the key]","last_used_at":null,"created_at":1794693042}

然後以該金鑰傳送訊息給應用程式,回覆的 token:

Terminal window
curl -sS http://127.0.0.1:5001/v1/chat-messages \
-H 'Authorization: Bearer <app key>' -H 'Content-Type: application/json' \
-d '{"inputs": {}, "query": "In one short sentence: what does a support plan cover?", "response_mode": "blocking", "user": "test-it"}'

預留位置被拒絕後,模型沒有金鑰,Dify 會如此告知:

{"code":"provider_not_initialize","message":"Model gpt-4o-mini credentials is not initialized.","status":400}

在 Kubernetes 與 Docker Compose 上的執行得到相同的回覆。使用提供者接受的金鑰時,回覆會在 answer 帶有模型的答案。

在 Kubernetes 上,停止連接埠轉送,然後移除你套用的內容、Secret 與工作階段檔案:

Terminal window
kubectl delete -R -f dist/boundaries/platform/helm/
kubectl delete secret dify-owner dify-credentials dify-db-password-for-evaluation-only dify-code-execution-key-for-evaluation-only dify-plugin-daemon-key-for-evaluation-only dify-inner-api-key-for-evaluation-only
kubectl delete pvc data-dify-zddb-0 storage-dify-zdplugins-0 data-dify-zdvector-0
rm cookies.txt

刪除 StatefulSet 時,Kubernetes 會保留 StatefulSet 所建立的磁碟區宣告,所以第三個命令會移除它們,連同資料庫、已安裝的外掛程式與向量儲存區。在 Docker Compose 上,docker compose -p support-assistant -f ../dist/boundaries/platform/compose/docker-compose.yaml down -v 會移除該堆疊及其磁碟區。

On Docker Compose

同一個應用程式可用 compose 目標,從同一個答案檔匯入到 Docker Compose。供 Kubernetes 使用的草稿已經在 source 中,所以請用 --output-dir 將 Compose 的草稿草擬到它們自己的資料夾;匯入仍會讀取應用程式旁的答案檔:

Terminal window
aggrega import support-assistant.yml --target compose --output-dir compose

Docker Compose 沒有 Secret 可以指名,所以對 compose 而言,草稿會自行寫出應用程式機密與擁有者的密碼,各自作為變數參照,由 Docker Compose 從啟動堆疊者的環境中填入。那裡只需要擁有者的電子郵件。匯入會保留答案檔中的兩個 Secret 名稱而不套用它們,並在標準錯誤上這麼說:

info: the answers file <workspace>/import-answers.yaml also answers spec.boundaries.platform.aggregates.dify.components.api.features.owner_password.infra.origin in release.yaml, spec.parameters.secret_key in release.yaml, which these drafts do not leave empty; those answers are kept there and not applied

草稿 Release 從 DIFY_SECRET_KEY 讀取應用程式機密,從 DIFY_OWNER_PASSWORD 讀取擁有者的密碼,並在回送位址上將 API 發布於連接埠 5001,並將前端 console 發布於 3001:

parameters:
# Passed by reference, never as a value: secret_key, which the drafted Aggregate declares and passes down to core/dify; that parameter's description there says what the value is; it is read from the variable DIFY_SECRET_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.
secret_key: "${DIFY_SECRET_KEY}"
[… omitted: the boundary, the marketplace switch, and the owner's email, answered as above]
# 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: 5001
console_url: "http://127.0.0.1:3001"
features:
owner_password:
type: core/referenced-secret
infra:
# Attached by reference, never as a value: the owner account's password, read from the variable DIFY_OWNER_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: "${DIFY_OWNER_PASSWORD}"
[… omitted: the worker and the scheduler, each with the marketplace switch, as above]
console:
infra:
# 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: 3001

每個連接埠都只能從你的機器連到,如其註解所說,而刪除某一行會讓該服務只留在堆疊的網路上。前端在其容器內監聽 3000,並發布在 3001,所以它可以和另一個發布 3000 的範例並行執行。API 伺服器的 console_url 是前端發布的位址,所以你為前端變更的連接埠也要在那裡變更。

Manifest 將每個元件對應到其 Compose Recipe,並將 core/referenced-secret 對應到 compose/dotenv-referenced-secret,後者會將每個參照依原樣寫入 Compose 檔案。從新資料夾驗證並建置:

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

建置會寫在工作區根目錄的 dist 之下,而工作區仍然是 source,也就是保存 .aggrega-config.yaml 的資料夾:Compose 檔案是 source/dist/boundaries/platform/compose/docker-compose.yaml,從 compose 看來是 ../dist/boundaries/platform/compose/docker-compose.yaml,而且 compose 之內不會寫出 dist 資料夾。該檔案帶有 SECRET_KEY: ${DIFY_SECRET_KEY} 與 OWNER_PASSWORD: ${DIFY_OWNER_PASSWORD},而不是任一值。啟動堆疊之前,請在你的 shell 中,或在 Compose 檔案旁的 .env 檔案中,設定這兩個變數。

該安裝的另外四個認證沿用 core/dify 給它們的預設值,而在 Docker Compose 上,每個預設值就是值本身,依原樣寫入 Compose 檔案:DB_PASSWORD: dify-db-password-for-evaluation-only,資料庫上的 POSTGRES_PASSWORD 相同,同樣還有 dify-code-execution-key-for-evaluation-only、dify-plugin-daemon-key-for-evaluation-only 與 dify-inner-api-key-for-evaluation-only。目錄的每位讀者都持有這些預設值,所以它們只供評估之用。若要取代其中一個,請在草稿的 Aggregate 中為它宣告一個字串參數,在 dify 項目的 parameters 之下,以 core/dify 對它的名稱——db_password、code_execution_key、plugin_daemon_key 或 inner_api_key——往下傳遞,如同草稿傳遞 secret_key 一樣,並在 Release 的 parameters 之下滿足它。寫有 db_password: "${DIFY_DB_PASSWORD}" 的 Release 會建置出帶有 DB_PASSWORD: ${DIFY_DB_PASSWORD} 與 POSTGRES_PASSWORD: ${DIFY_DB_PASSWORD} 的 Compose 檔案,像上面兩個一樣從環境中填入。

從 compose 啟動堆疊,使用 Dify 接受的擁有者密碼,如 Run it 所述:

Terminal window
export DIFY_SECRET_KEY='a long random value of your own'
export DIFY_OWNER_PASSWORD='a password of your own'
docker compose -p support-assistant -f ../dist/boundaries/platform/compose/docker-compose.yaml up -d --wait

在該次執行中,--wait 在大約一分鐘內返回,那時每個伺服器都回報健康。匯入應用程式的步驟與它們並行執行,並在完成後結束;其記錄接著顯示:

first-time setup done: owner account owner@example.com
imported 'Support assistant' as application 7fae73f4-826b-4918-a623-a3383aa0da8c

Test it 會在兩個已發布的連接埠連到該安裝。