CCoE AIチャット構築
概要
CCoEへの問い合わせや申請に対応してくれるAIチャットを作成してみます。
主な機能は以下の通りです。
- AWSに関連する質問に回答
- AWSアカウントの発行
3回に分けて構築していきます。
1.基本的なAIチャットの構築
2.MCPを使用して、AWSドキュメントから情報を回答可能に
3.AWSアカウントの発行機能を追加 ← 今回
今回はStrandsのtoolを使用して、AWSアカウントの発行をできるようにしてみます!
Custom Toolを使用する
今回は、AWSアカウントの発行をするために、Custom Toolを使用します。
参考:https://github.com/strands-agents/samples/blob/main/01-tutorials/01-fundamentals/04-tools/02-custom-tools/custom-tools-with-strands-agents.ipynb
Custom Toolの作成
app/routes/main.py
にAWSアカウントを発行するためのツールを追加します。
(省略)
from strands import Agent, tool # ←tool追加
SYSTEM_PROMPT = """
# AWS Cloud Center of Excellence (CCoE) アシスタント
(省略)
## ツールの使用について
### create_aws_account
AWSアカウントを発行するためのツールです。
必須パラメータ:
- `email`: アカウントのメールアドレス
- `account_name`: アカウント名
"""
@main_bp.route('/chat', methods=['POST'])
@login_required
def chat():
try:
print("Chatting...")
(省略)
with stdio_mcp_client:
tools = stdio_mcp_client.list_tools_sync()
agent = Agent(
model=model,
messages=conversation_history,
system_prompt=SYSTEM_PROMPT,
tools=tools + [create_aws_account], # ←作成したツールを追加
)
response = agent(user_message)
response_text = response.message['content'][0]['text']
(省略)
# ↓ツールを作成
@tool
def create_aws_account(email: str, account_name: str) -> dict:
"""
アカウントの発行
Args:
email (str): アカウントのメールアドレス
account_name (str): アカウント名
Returns:
dict: response
"""
client = boto3.client('organizations')
try:
response = client.create_account(
Email=email,
AccountName=account_name,
RoleName='OrganizationAccountAccessRole',
IamUserAccessToBilling='ALLOW'
)
return response
except Exception as e:
return {'error': str(e)}
たったのこれだけでできてしまいます。すごい、、
ツールの定義は、@tool
デコレーターを使用して行います。
システムプロンプトにツールについて説明を入れてあげるとより確実に動くと思います。
チャットしてみる
では早速チャットをしてみます。
AWSアカウントの作成を依頼してみましょう!
発行がされたようです!
一応確認してみます!
ちゃんと作られていました!
まとめ
今回は、CCoE AIチャットにAWSアカウントの発行機能を追加してみました。
実際には権限を分けたり、承認処理を入れる必要があると思います。
「一部門に一台AIチャット」な時代が来るかもしれないですね!
Appendix
AppRunnerでデプロイする際のapprunner.yamlの一例
version: 1.0
runtime: python311
build:
commands:
pre-build:
- echo "Installing dependencies..."
build:
- pip3 install --upgrade pip
- pip3 install pipenv
- pipenv install -r requirements.txt
post-build:
- echo "Build completed"
run:
runtime-version: 3.11
pre-run:
- echo "Installing dependencies..."
- pip3 install --upgrade pip
- pip3 install pipenv
- pipenv install -r requirements.txt
command: pipenv run waitress-serve --port=8080 --call wsgi:create_app
network:
port: 8080
env: PORT
env:
- name: AWS_REGION
value: "ap-northeast-1"
- name: COGNITO_USER_POOL_ID
value: "ap-northeast-1_xxxxxxxxx"
- name: COGNITO_DOMAIN
value: "ap-northeast-1xxxxxxxxx"
- name: COGNITO_CLIENT_ID
value: "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
- name: COGNITO_CLIENT_SECRET
value: "xxxxxxxxxxxxxxxxxxxxxxxxx"
以下の点に注意してください!
- AppRunner用IAMロールの権限
- MCPクライアントの初期化の際の記述がWindowsの場合はLinuxに変更すること
- CognitoのコールバックURLなどの設定にAppRunnerのドメインを追加すること
弊社では一緒に働く仲間を募集中です!
現在、様々な職種を募集しております。
カジュアル面談も可能ですので、ご連絡お待ちしております!
募集内容等詳細は、是非採用サイトをご確認ください。