はじめに
以前の記事では、AWSブログの情報収集、日本語での要約、受信Webhookに対応したコラボレーションツールへの投稿を行うソリューション What’s New Summary Notifier
をSlackフリープランのWebhookで受信する方法を紹介しました。
このソリューションは、AWS BedrockのInvoke APIで Anthropic Claude 3 Sonnetを呼び出す前提となっています。そのため、他のモデルを使うためにはそのモデルの出力に合わせた処理が必要です。そこで、この記事ではInvoke API呼び出しをConverse API呼び出しに置き換える修正を紹介します。これにより、以下の利点が得られます。
- 様々なモデルを試すことで、好みやコストに合わせたモデルが選択可能になる
- マーケットプレイスに依らないAWSのモデルが利用可能になる
- Amazon NovaなどAWSのモデルを選択することで、AWSクレジットの活用が可能になる
特にAWSクレジットを持っているユーザーには、3番目の利点が魅力ではないでしょうか。
参考情報
構築手順
What’s New Summary Notifier
の構築手順は、AWSブログもしくは以前の記事を参考にしてください。どちらをもとに構築した場合でも変更箇所は同じです。
コードの修正とデプロイ
modelIdの変更とConverse API 呼び出し
モデルIDの指定は cdk.json
内の値を変更します。今回は、Nova Microを使用します。
"context": {
"modelRegion": "us-east-1",
- "modelId": "anthropic.claude-3-5-sonnet-20240620-v1:0",
+ "modelId": "amazon.nova-micro-v1:0",
"summarizers": {
"AwsSolutionsArchitectEnglish": {
"outputLanguage": "English.",
Invoke API 呼び出しから Converse API 呼び出しへの変更は ./lambda/notify-to-app/index.py
で行います。Converse API の呼び出しに合わせて、システムプロンプトのテンプレートやAPIパラメータ、出力結果の受け取り部分を変更します。
assumed_role=os.environ.get("BEDROCK_ASSUME_ROLE", None),
region=MODEL_REGION,
)
- beginning_word = "<output>"
+
prompt_data = f"""
-<input>{blog_body}</input>
<persona>You are a professional {persona}. </persona>
<instruction>
Describe a new update in <input></input> tags in bullet points to describe "What is the new feature?", and "Who is this update good for". The description shall be output in <thinking></thinking> tags, and each thinking sentence must start with the bullet point "- " and end with "\n".
max_tokens = 4096
- user_message = {
- "role": "user",
- "content": [
- {
- "type": "text",
- "text": prompt_data,
- }
- ],
- }
-
- assistant_message = {
- "role": "assistant",
- "content": [{"type": "text", "text": f"{beginning_word}"}],
- }
-
- messages = [user_message, assistant_message]
+ system_prompts = [
+ {
+ "text": prompt_data
+ }
+ ]
- body = json.dumps(
+ messages = [
{
- "anthropic_version": "bedrock-2023-05-31",
- "max_tokens": max_tokens,
- "messages": messages,
- "temperature": 0.5,
- "top_p": 1,
- "top_k": 250,
+ "role": "user",
+ "content": [
+ {
+ "text": f"<input>{blog_body}</input>"
+ }
+ ]
}
- )
+ ]
- accept = "application/json"
- contentType = "application/json"
- outputText = "\n"
+ inference_config = {
+ "maxTokens": max_tokens,
+ "temperature": 0.5,
+ "topP": 1,
+ }
+ additional_model_request_fields = {
+ "inferenceConfig": {
+ "topK": 125
+ }
+ }
try:
- response = boto3_bedrock.invoke_model(
- body=body, modelId=MODEL_ID, accept=accept, contentType=contentType
+ response = boto3_bedrock.converse(
+ system=system_prompts,
+ messages=messages,
+ modelId=MODEL_ID,
+ inferenceConfig=inference_config,
+ additionalModelRequestFields=additional_model_request_fields
)
- response_body = json.loads(response.get("body").read().decode())
- outputText = beginning_word + response_body.get("content")[0]["text"]
+
+ outputText = response["output"]["message"]["content"][0]["text"]
+
print(outputText)
# extract contant inside <summary> tag
summary = re.findall(r"<summary>([\s\S]*?)</summary>", outputText)[0]
デプロイ
上記のとおり変更後、デプロイを行います。
cdk deploy
Anthropic Claude 3 SonnetとAmazon Novaとの比較
モデルの変更前後で同じAWSブログをクロールし、内容の比較を行いました。サマリーとしては両者の内容に大きな差はないため、コストで比較するならばトークンあたりのコストが低い Amazon Nova が有利と言えそうです。
anthropic.claude-3-5-sonnet-20240620-v1:0 | amazon.nova-micro-v1:0 | |
---|---|---|
Achieve ~2x speed-up in LLM inference with Medusa-1 on Amazon SageMaker AI
|
Achieve ~2x speed-up in LLM inference with Medusa-1 on Amazon SageMaker AI
|
|
Amazon RDS for PostgreSQL supports minor versions 17.3, 16.7, 15.11, 14.16, 13.19
|
Amazon RDS for PostgreSQL supports minor versions 17.3, 16.7, 15.11, 14.16, 13.19 この機能はPostgreSQLデータベースを管理する開発者や運用者に有益です。
|
|
Amazon Q Developer now supports upgrade to Java 21
|
Amazon Q Developer now supports upgrade to Java 21
|
まとめ
AWSが提供する What’s New Summary Notifier
を Converse API 呼び出しに対応するよう変更し、モデルにAmazon Nova を使用してみました。Converse API 呼び出しに変更したことで、モデルの切替が容易になり、クロスリージョン推論なども試しやすくなりました。以前の記事では、AWSブログ以外のブログやニュースサイトから記事を取得する実装を紹介しました。取得する記事の分野に合わせて、その分野を得意とするモデルに切り替えるといったこともできそうです。