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

匯入 CrewAI 專案

本逐步說明匯入 CrewAI 專案的代理程式與任務宣告——由兩個代理程式組成、回答客戶關於支援方案問題的 crew——並為 Docker Compose 建置該 crew。匯入會草擬建置所讀取的每個檔案:兩個代理程式與它們的模型、每個代理程式以變數參照呈現給模型的金鑰,以及兩者各自的實現。它只詢問一次任何宣告都無法提供的那個值,也就是 crew 的程式執行所用的映像檔。下面的每個檔案與每一段輸出都來自一次真實的執行;宣告與逐步說明的每個階段都在 import-crewai 範例中,其中有一個檢查指令碼會再次執行匯入,把它寫出的內容、它印出的內容,以及本頁所引用的區塊,與保存在那裡的檔案比較,並驗證與建置匯入所寫出的內容。

草擬範例

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

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

Terminal window
aggrega draft --example import-crewai

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

Terminal window
cd import-crewai

在 Visual Studio Code 中開啟的草擬範例 source 資料夾,顯示 crew 的代理程式設定,其中有 researcher 與 writer。

專案

CrewAI 專案把它 crew 的行為保存在程式中,並在設定檔中宣告它的代理程式與任務。這一個是為本逐步說明而寫的,採用 CrewAI 的經典結構,而匯入會讀取位於 src/support_brief/config/ 之下的兩個宣告檔。程式——經典專案的 crew.py、它所定義的工具——絕不會被讀取,所以範例只包含宣告。

agents.yaml 宣告兩個代理程式,兩者都使用 OpenAI 的 gpt-4o-mini:

researcher:
role: >
Support plan researcher
goal: >
Find what each support plan of the company covers, and what it costs
backstory: >
You keep the company's support plans at your fingertips and answer from
them alone.
llm: openai/gpt-4o-mini
verbose: true
writer:
role: >
Support reply writer
goal: >
Turn the researcher's findings into a short, friendly reply to the customer
backstory: >
You write replies customers can act on, in plain words.
llm: openai/gpt-4o-mini

tasks.yaml 為每個代理程式指派一個任務:

research_task:
description: >
Find the support plans that answer the customer's question: {question}
expected_output: >
The plans that apply, each with what it covers and what it costs
agent: researcher
reply_task:
description: >
Write a reply to the customer's question from the researcher's findings
expected_output: >
A reply of at most five sentences
agent: writer

在專案的根目錄,一個 .aggrega-config.yaml 讓資料夾成為工作區:

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

匯入會參照目錄中的代理程式與模型 Components,而 --target 會從它們的 Recipes 草擬 Manifest。工作區沒有指名目錄資料夾,所以匯入會從它擷取匯入器的登錄庫讀取標準程式庫目錄:已發布目錄的簽署快照,由第一次匯入擷取、驗證並保存在工作區的內容快取中,並在匯入寫於草稿旁的記錄中以其摘要來指名。指名自己目錄資料夾的工作區——在 AGGREGA_CATALOG_DIRS 中,或在 workspace.catalog_dirs 之下——會先讀取那些資料夾,再讀取其後的已發布快照。

匯入要詢問什麼

宣告會指名每個代理程式的模型,但絕不會指名 crew 的程式執行所用的映像檔:你要從你的專案建置那個映像檔。所以匯入會詢問它,每個代理程式各問一次。在終端機上,它會為每一個提出問題,並把答案寫入草稿,以及它所匯入目錄 src/support_brief/ 內的答案檔 import-answers.yaml,所以下一次匯入會從那裡讀取它們,什麼也不問。沒有終端機時——在持續整合中,或透過管道——它什麼也不問:它會寫出鍵為空的檔案,然後停下來,並指出該檔案。範例已經有這個檔案,兩個代理程式使用同一個映像檔 ghcr.io/example/support-brief: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.
release.yaml:
# Container image running the CrewAI application code.
spec.components.researcher.infra.image: "ghcr.io/example/support-brief: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.researcher.uses.model.gpt-4o-mini.features.credential.infra.origin: "openai-api-key"
# Container image running the CrewAI application code.
spec.components.writer.infra.image: "ghcr.io/example/support-brief: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.writer.uses.model.gpt-4o-mini.features.credential.infra.origin: "openai-api-key"

兩個 origin 答案是用於針對 Kubernetes 的建置,也就是 --target helm:此時每個代理程式從 Kubernetes Secret 讀取 OpenAI 金鑰,而答案是該 Secret 的名稱 openai-api-key,絕不是金鑰。針對 Docker Compose 的建置則改從你的 shell 讀取金鑰,所以這次匯入不使用那些答案。匯入答案檔說明該檔案、問題,以及機密答案的規則。

aggrega import 列出該命令接受的每個旗標,其中包括 --answers,它可指名保存在別處的答案檔。

切換到 source:

Terminal window
cd source

執行匯入

指名 crew 的目錄。匯入會讀取其中的宣告,不讀取其他東西:

Terminal window
aggrega import src/support_brief --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 crewai format, with drafted paths relative to <workspace>
info: the answers file <workspace>/src/support_brief/import-answers.yaml also answers spec.components.researcher.uses.model.gpt-4o-mini.features.credential.infra.origin in release.yaml, spec.components.writer.uses.model.gpt-4o-mini.features.credential.infra.origin in release.yaml, which these drafts do not leave empty; those answers are kept there and not applied
mapped researcher (agent) to core/ai-agent/crewai as a reference, NONE
mapped openai/gpt-4o-mini (llm) to core/ai-model/openai as a reference, NONE
mapped writer (agent) to core/ai-agent/crewai as a reference, NONE
made no choice for research_task (task)
made no choice for reply_task (task)
Pull complete: <workspace>/release.lock (entities=8, source_artifacts=0, external_inputs=0)

從編輯器執行的匯入:草擬的 Aggregate、Release 與 Manifest 出現在 crew 的專案旁,Aggrega 輸出列出每個代理程式、模型與任務是如何對應的以及所寫出的檔案,通知則說明匯入草擬了 3 個檔案,辨識為 crewai。

  • info 那一行指出 Docker Compose 草稿不使用的答案,它們被保存在答案檔中,供針對 Kubernetes 的建置使用。
  • mapped 那幾行說明每個代理程式都是標準程式庫 CrewAI 代理程式 core/ai-agent/crewai 的實例,而兩者所指名的模型 openai/gpt-4o-mini 是 core/ai-model/openai 的實例。它們的層級顯示為 NONE:CrewAI 匯入是依據宣告所指名的內容——代理程式與模型提供者——來參照這些 Components,而不是把每一個與目錄比較評分。
  • made no choice 那幾行說明任務不會被草擬成任何東西:任務是 crew 的程式所執行的邏輯。
  • Pull complete 那一行是匯入在擷取草擬 Release 所依賴的內容,就像 aggrega pull 所做的那樣,寫入 release.lock,讓草稿立即就能驗證與建置。在 --offline 下,匯入不拉取任何東西,並指出該執行的 aggrega pull。

它寫出什麼

Aggregate、Release 與 Manifest 都命名為 imported/support-brief:專案的 config/agents.yaml 與 config/tasks.yaml 沒有指名任何 crew——它的名稱存在於程式中,而匯入不讀取程式——所以對它們的目錄匯入會以目錄命名,也就是 support_brief,寫成名稱區段,位於匯入器的 target_namespace 設定所設的命名空間之下,除非你設定別的,否則為 imported(設定)。

aggregate.yaml — 每個代理程式一個實例,各自連到它所指名的模型,並在連線上宣告它所呈現的金鑰,以及模型,開頭的註解說明讀取了什麼、沒讀取什麼:

# CrewAI declarations read:
# - config/agents.yaml
# - config/tasks.yaml
# Not read: the crew's program, which runs its agents and its tasks and defines its tools; it runs in the image the Release names for each agent.
# Tasks the crew's program runs, drafted as no Component:
# - research_task, assigned to researcher
# - reply_task, assigned to writer
apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: imported/support-brief
version: 0.1.0
description: Draft Aggregate imported from a source in the crewai format.
spec:
components:
researcher:
type: core/ai-agent/crewai
values:
role: "Support plan researcher\u000A"
goal: "Find what each support plan of the company covers, and what it costs\u000A"
backstory: "You keep the company's support plans at your fingertips and answer from them alone.\u000A"
allow_delegation: false
log_level: DEBUG
uses:
model:
gpt-4o-mini:
target: gpt-4o-mini
# For the compose target, the lines below declare credential on gpt-4o-mini: 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
writer:
type: core/ai-agent/crewai
values:
role: "Support reply writer\u000A"
goal: "Turn the researcher's findings into a short, friendly reply to the customer\u000A"
backstory: "You write replies customers can act on, in plain words.\u000A"
allow_delegation: false
log_level: INFO
uses:
model:
gpt-4o-mini:
target: gpt-4o-mini
# For the compose target, the lines below declare credential on gpt-4o-mini: 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 model openai/gpt-4o-mini.
gpt-4o-mini:
type: core/ai-model/openai
values:
model: "gpt-4o-mini"

role、goal 與 backstory 照宣告的內容帶入,包括每個摺疊區塊結尾的換行。log_level 對 researcher 是 DEBUG(它宣告了 verbose),對 writer 是 INFO(它沒有)。沒有宣告 allow_delegation 的代理程式會被草擬為 false。模型是託管服務,所以代理程式的 Component 會拒絕不帶認證而連到它的連線;每個連線都會宣告一個,鍵為 OPENAI_API_KEY。

release.yaml — 環境、每個代理程式的映像檔與發布的連接埠,以及每個金鑰的來源:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: imported/support-brief
# 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 crewai format.
spec:
aggregate: imported/support-brief
manifest: imported/support-brief
components:
researcher:
infra:
# Left for the Release, since the source cannot supply it: infra.image, the container image running the crew's program
image: "ghcr.io/example/support-brief: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:
model:
gpt-4o-mini:
features:
credential:
type: core/secret-value
infra:
# Attached by reference, never as a value: the credential researcher presents to gpt-4o-mini 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}"
writer:
infra:
# Left for the Release, since the source cannot supply it: infra.image, the container image running the crew's program
image: "ghcr.io/example/support-brief: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: 8001
uses:
model:
gpt-4o-mini:
features:
credential:
type: core/secret-value
infra:
# Attached by reference, never as a value: the credential writer presents to gpt-4o-mini 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}"
gpt-4o-mini: {}

環境 development 是匯入寫入的預設值,因為沒有任何宣告指名環境;若 Release 要部署到別處,請更改它。每個映像檔都是匯入從 import-answers.yaml 讀取的答案。每個代理程式都發布在你機器的回送位址上,轉送到它的程式所監聽的連接埠 infra.port,除非你另外設定,否則為 8000:researcher 用 8000,writer 用 8001,因為同一個 Release 的兩個服務不能發布相同的連接埠。宣告沒有為金鑰指名任何變數,所以每個連線都從 ${OPENAI_API_KEY}(OpenAI 的用戶端程式庫所讀取的變數)讀取它,作為變數參照,絕不是值。

manifest.yaml — 每個型別的 Compose Recipe,以及把金鑰的變數參照照原樣寫入 Compose 檔案的實現:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: imported/support-brief
version: 0.1.0
spec:
mappings:
components:
type:
core/ai-agent/crewai: compose/crewai
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 輸出與通知都回報沒有發現問題。

草擬 Release 的建置:Explorer 中出現 dist 資料夾,Aggrega 輸出列出所寫出的 Docker Compose 檔案,通知說明寫出了 1 個檔案。

驗證會印出:

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 從 Aggregate 的 values 或 Release 的 infra 接受它的 origin,而該通知回報有一個葉節點同時在兩者中宣告。它不改變任何裁定。

researcher 在 dist/compose/docker-compose.yaml 中的服務;writer 的服務只在名稱、它自己的變數所帶的值,以及它所發布的連接埠上有所不同:

services:
researcher:
image: ghcr.io/example/support-brief:1.0.0
environment:
OPENAI_BASE_URL: https://api.openai.com/v1
MODEL: gpt-4o-mini
CREWAI_ALLOW_DELEGATION: "false"
CREWAI_BACKSTORY: |
You keep the company's support plans at your fingertips and answer from them alone.
CREWAI_GOAL: |
Find what each support plan of the company covers, and what it costs
CREWAI_LOG_LEVEL: DEBUG
CREWAI_ROLE: |
Support plan researcher
OPENAI_API_KEY: ${OPENAI_API_KEY}
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

代理程式會以 OPENAI_BASE_URL 與 MODEL 收到模型的位址與名稱,這是 CrewAI 讀取它們所用的變數,並以 OPENAI_API_KEY 收到金鑰。它的 role、goal、backstory、委派開關與記錄層級,會以 CrewAI Component 預設賦予它們的名稱送達,供 crew 的程式讀取;元件本身上的 inject: 區塊可以改以程式所讀取的名稱傳遞其中任何一個。匯入所草擬的每個值都保留其宣告結尾的換行,所以檔案把它寫成區塊。模型沒有自己的服務:代理程式在它自己的位址觸及 OpenAI API。金鑰是變數參照,在堆疊啟動時從你的 shell 填入。每個服務都只在 127.0.0.1 上發布它的連接埠:researcher 的 8000 與 writer 的 8001,各自轉送到其容器內程式的連接埠 8000。

搭配 AI 助理

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

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

在 VS Code 中開啟

安裝了 Aggrega 的 Visual Studio Code 擴充功能之後,請在 VS Code 中開啟資料夾 import-crewai/source,匯入就把草稿寫在那裡。該資料夾只有一個 Release,也就是草擬的 release.yaml,所以編輯器會替你選取它,並在你編輯草稿時依據該 Release 檢查它們,在檔案與 Problems 面板中標示它所發現的問題。命令選擇區(Command Palette)中的 Aggrega: Show topology graph 會繪出該 Release 的元件及它們之間的連線。Aggrega: Build、Aggrega: Preview build 與 Aggrega: Pull 都作用於同一個 Release,而且每一個也都是活動列中 Aggrega 檢視的 Commands 面板上的按鈕;Build 會把它的檔案寫入該資料夾的 dist/。在 Visual Studio Code 中使用 Aggrega 涵蓋其餘部分。

執行

你要從專案以自己的 Dockerfile 建置映像檔。Docker Compose 會把映像檔自己的命令當作每個代理程式的服務來執行,因為 Recipe 沒有設定任何命令,所以那個命令必須持續執行,而且若你想觸及 crew,它就必須能處理請求。位址、路由與回覆,由你的程式決定。

下面的執行使用了這樣的映像檔:採用 CrewAI 經典配置的 crew 程式——由 crew.py 依匯入所讀取的兩個宣告檔建構 crew——以及作為映像檔命令、該程式自己在連接埠 8000 上的小型 HTTP 伺服器,它對每個 POST /kickoff 執行 crew 一次。在專案根目錄建置你的映像檔,把它的名稱寫在 src/support_brief/import-answers.yaml 中兩個代理程式的 ghcr.io/example/support-brief:1.0.0 的位置,再以 --sync 匯入一次,它會把新的答案寫入草稿並再次拉取;然後建置:

Terminal window
docker build -t support-brief:local .
aggrega import src/support_brief --sync
aggrega build --release release.yaml

以 support-brief:local 作為兩個答案時,同步會在它印出的那幾行中,指出它所更改的那兩個值:

changed spec.components.researcher.infra.image in release.yaml
changed spec.components.writer.infra.image in release.yaml

然後在你的 shell 中設定金鑰並啟動堆疊:

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

-p 會指名專案,如匯入如何執行的執行步驟所說明。

測試

下面的請求會觸及本逐步說明的映像檔所執行的程式:在連接埠 8000 上的小型 HTTP 伺服器後方的 crew,回應 POST /kickoff。你自己的程式會自行決定路由與回覆。請在程式開始監聽之後(堆疊啟動後幾秒鐘),把它傳送給位於 http://127.0.0.1:8000(也就是 published_port 所發布的位址)的 researcher:

Terminal window
curl -sS -X POST http://127.0.0.1:8000/kickoff -H 'Content-Type: application/json' \
-d '{"question": "Which support plan covers a weekend outage, and what does it cost?"}'
{
"error": "the crew failed: AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-place************-key. [… omitted: where to find an API key, and the error's type and code]"
}

該次執行把 OPENAI_API_KEY 設為預留位置,所以回覆是 OpenAI 對它的拒絕,由程式轉達。crew 的請求帶著建置所傳遞的金鑰抵達了 OpenAI API。兩個代理程式執行同一個映像檔,所以請求可以送到任何一個:writer 在 http://127.0.0.1:8001 的回應相同。

以下列命令停止堆疊:

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

在 Kubernetes 上,請以 --target helm 匯入,並把映像檔推送到你的叢集可從中拉取的登錄庫,在 src/support_brief/import-answers.yaml 中指名它。然後建立 Secret openai-api-key 並套用建置,如匯入如何執行的執行步驟所述。每個代理程式的 Deployment 都沒有 Service,所以請把本機連接埠轉送給 researcher 的那一個。該命令會一直執行到你停止為止:

Terminal window
kubectl port-forward deploy/researcher 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/kickoff -H 'Content-Type: application/json' \
-d '{"question": "Which support plan covers a weekend outage, and what does it cost?"}'
{
"error": "the crew failed: AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-place************-key. [… omitted: where to find an API key, and the error's type and code]"
}