選擇基礎模型

Amazon Bedrock 入門

Nikhil Rangarajan

Data Scientist

在 Amazon Bedrock 啟用模型存取

  • 存取模型設定——本課程無需另外啟用模型

顯示在 AWS Console 中啟用 Claude 與 Nova 模型的螢幕擷取畫面。

Amazon Bedrock 入門

輕量型 vs. 進階型基礎模型

  • 輕量型模型
    • 適合:問答、摘要
    • 回應快、成本低
    • 範例:Nova Micro、Claude Haiku

Amazon 標誌。

  • 進階型模型
    • 適合:複雜推理、分析
    • 具備完整分析能力
    • 範例:Nova Plus、Claude Sonnet

Anthropic 標誌。

Amazon Bedrock 入門

Amazon Bedrock 的其他基礎模型

Amazon Bedrock 中的其他模型:

  • Meta 的 Llama

    • 📝 開源 LLM,適用一般文字任務
  • Stability AI 的 Stable Diffusion

    • 🖼 專長於影像生成
  • AI21 的 Jurassic

    • 📈 文字生成與分析

Meta 的 Llama 標誌。

Amazon Bedrock 入門

呼叫 Bedrock 模型

  • 基本結構:
import json
bedrock = boto3.client('bedrock-runtime', region_name='us-east-1')
response = bedrock.invoke_model(modelId='amazon.nova-lite-v1:0',

body=json.dumps(input_dictionary)
)
  • 以 JSON 格式回傳模型回應
Amazon Bedrock 入門

呼叫 Claude

  • Anthropic Claude
response = bedrock.invoke_model(
    modelId='anthropic.claude-3-5-sonnet-v2:0',
    body=json.dumps(

{"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 100,
"messages": [{ "role": "user", "content": [{"type": "text", "text": "your prompt here"}], }],
}
))
Amazon Bedrock 入門

呼叫 Nova

  • Amazon Nova
response = bedrock.invoke_model(
    modelId='amazon.nova-lite-v1:0', 
    body=json.dumps(

{"messages":
[{"role": "user", "content": [{"text": "your prompt here"}] }]
}
))
Amazon Bedrock 入門

擷取模型回應

  • 讀取 API 回應的 body
  • 從 JSON 轉成 Python 字典
  • 以索引鍵擷取輸出
print("Claude:",

json.loads( claude_response['body'].read() )
["content"][0]["text"]
)
Hello! It's nice to meet you. How can I assist you today?
Amazon Bedrock 入門

擷取模型回應

  • 讀取 API 回應的 body
  • 從 JSON 轉成 Python 字典
  • 以索引鍵擷取輸出
print("Nova:",

json.loads( nova_response.get("body").read().decode() )
["output"]["message"]["content"][0]["text"]
)
Hello! How can I assist you today?
Amazon Bedrock 入門

一起來練習吧!

Amazon Bedrock 入門

Preparing Video For Download...