これを試します。
プロジェクトの初期化
以下コマンドを実行して、プロジェクトを初期化します。
# Node.jsプロジェクトの初期化
npm init -y
npm pkg set type=module
# 依存パッケージの追加
npm i express @strands-agents/sdk @opentelemetry/auto-instrumentations-node
npm i -D typescript tsx @types/node @types/express
# TypeScript設定の初期化
npx tsc --init --outDir dist
# npmスクリプトの追加
npm pkg set scripts.build="tsc"
npm pkg set scripts.start="node dist/index.js"
# デプロイ前にローカルパッケージを削除
rm -rf node_modules
# AIエージェント用スクリプトの作成
touch index.ts
上記によって作成される package.json と tsconfig.json の存在をもって、後続のAgentCoreツールキットがTypeScriptプロジェクトだと判定してくれるようになります。
AIエージェントの開発
メインのAIエージェントをTypeScriptで作成します。
Strands AgentsのTypeScript版を、AgentCoreランタイムの要件に従ってExpress.jsでAPIサーバー化しています。
index.ts
import * as strands from '@strands-agents/sdk'
import express from 'express'
// AIエージェントとAPIサーバーを作成
const agent = new strands.Agent()
const app = express()
// ヘルスチェック用エンドポイントを作成
app.get('/ping', (_, res) => res.sendStatus(200))
// エージェント呼び出し用エンドポイントを作成
const rawBody = express.raw({ type: '*/*' })
app.post('/invocations', rawBody, async (req, res) => {
// プロンプトを取り出してAIエージェントを呼び出し
const prompt = new TextDecoder().decode(req.body)
const result = await agent.invoke(prompt)
res.json({ result })
})
// APIサーバーを起動
app.listen(8080)
デプロイ
スターターツールキットを試してみましょう。こいつ自体はPythonのままですw
# AWS認証
aws login
# ツールキットを最新化
pip install --upgrade bedrock-agentcore-starter-toolkit awscrt
# 設定ファイルを作成
agentcore configure
# ビルドしてデプロイ
agentcore deploy
デプロイ完了したら、呼び出してみましょう!
agentcore invoke '{"prompt": "元気?"}'
ちゃんとClaudeからレスポンスが来ました🎉
Response:
{"result":{"type":"agentResult","stopReason":"endTurn","lastMessage":{"type":"message","role":"assistant","content":
[{"type":"textBlock","text":"{\"response\":
\"はい、元気です!ありがとうございます。あなたはいかがですか?😊\"}"}]}}}
おまけ
StrandsとかAgentCoreがそもそもよく分からない方は、こちらから!