本文将继续为大家介绍如何在亚马逊云科技平台上的Amazon Bedrock上利用Claude 3.7 Sonnet的混合推理能力,并通过Python示例代码段对比标准模式和扩展思维模式的不同。
Anthropic在2/24推出的Claude 3.7 Sonnet模型目前已经登陆亚马逊的Amazon Bedrock上,并带来了突破性的新功能:混合推理。这一创新能够使模型在回答问题之前执行详细的逐步推理,为用户提供前所未有的透明度,并同时深入了解其问题解决过程。
在本文中,我们将通过实际的Python代码示例探索Claude 3.7 Sonnet的推理能力。大家将学习如何启用推理功能,对比不同的标准模式与扩展思维模式,并了解如何将推理与工具使用相结合。
Claude 3.7 Sonnet的优势
Claude 3.7 Sonnet代表了如今生成式AI行业的一大重要进步。作为Claude模型系列中的首个使用混合推理模型,它既能够通过细致的逐步推理解决复杂问题,同时也可以在适当情况下提供极为快速响应。
该模型的主要特点包括:
- 混合推理 - 单一模型可以在标准响应与详细推理之间自由切换
- 扩展思维模式 - 通过透明的逐步思考深入分析问题
- 可调的整推理成本 - 控制分配给思考过程的token数量
- 超大输出容量 - 相比前代模型,输出内容最长可达15倍(最高128K tokens)
- 增强的编码能力 - 在编码基准测试中表现卓越(同行业第一,遥遥领先)
实操前提条件
在开始本文中的代码示例实操之前,请确保大家已具备以下的条件:
- 具有访问Amazon Bedrock权限的亚马逊云科技账户
- 已安装并配置命令行工具AWS CLI,并具有相应权限
- 安装了Python 3.x
- 已安装最新版本的boto3和AWS CLI
安装或升级boto3和AWS CLI的方法如下:
pip install --upgrade boto3 awscli
最重要的是,大家需要在亚马逊云科技账户中开启访问Claude 3.7 Sonnet的权限:
- 进入Amazon Bedrock控制台
- 在“Bedrock配置”下找到“模型访问”
- 选择“修改模型访问”,然后请求开启Claude 3.7 Sonnet的访问权限
当前,Claude 3.7 Sonnet可在以下区域使用:
- us-east-1(北弗吉尼亚)
- us-east-2(俄亥俄)
- us-west-2(俄勒冈)
代码示例 1:对比标准模式与扩展思维模式
我们的第一个代码示例将对比Claude 3.7 Sonnet在启用和未启用推理功能时的API响应上的不同。这有助于我们理解标准模式与扩展思维模式之间的区别。
import boto3
from botocore.exceptions import ClientError
def compare_thinking_modes():
"""
Compares Claude 3.7 Sonnet's responses with and without reasoning enabled.
Shows the difference in responses, thinking process, latency, and token usage.
"""
# Create the Amazon Bedrock runtime client
client = boto3.client("bedrock-runtime", region_name="us-east-1")
# Specify the model ID for Claude 3.7 Sonnet
model_id = "us.anthropic.claude-3-7-sonnet-20250219-v1:0"
# Example prompt to test
prompt = "What would be the impact on global sea levels if all ice in Greenland melted?"
print(f"PROMPT: {prompt}\n")
try:
# Get standard response (without reasoning)
print("Getting standard response...")
standard_response = client.converse(
modelId=model_id,
messages=[
{
"role": "user",
"content": [{"text": prompt}]
}
],
inferenceConfig={"maxTokens": 8000} # We use default values for other parameters
)
# Get response with reasoning enabled
print("Getting extended thinking response...")
reasoning_response = client.converse(
modelId=model_id,
messages=[
{
"role": "user",
"content": [{"text": prompt}]
}
],
inferenceConfig={"maxTokens": 8000}, # Max tokens must be higher than budget_tokens
additionalModelRequestFields={
"thinking": {
"type": "enabled",
"budget_tokens": 4000 # Set a budget for the thinking process
}
}
)
# Extract standard response content
standard_content_blocks = standard_response["output"]["message"]["content"]
standard_text = None
for block in standard_content_blocks:
if "text" in block:
standard_text = block["text"]
break
# Extract reasoning response content
reasoning_content_blocks = reasoning_response["output"]["message"]["content"]
reasoning_text = None
thinking = None
for block in reasoning_content_blocks:
if "text" in block:
reasoning_text = block["text"]
elif "reasoningContent" in block:
thinking = block["reasoningContent"]["reasoningText"]["text"]
# Display results for standard mode
print("\n--- STANDARD MODE ---")
print(f"Tokens used: {standard_response['usage']['inputTokens']} input, {standard_response['usage']['outputTokens']} output")
print("\nRESPONSE:")
print(standard_text)
# Display results for reasoning mode
print("\n\n--- EXTENDED THINKING MODE ---")
print(f"Tokens used: {reasoning_response['usage']['inputTokens']} input, {reasoning_response['usage']['outputTokens']} output")
if thinking:
print("\nTHINKING PROCESS:")
print(thinking)
print("\nRESPONSE:")
print(reasoning_text)
except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke Claude 3.7 Sonnet. Reason: {e}")
exit(1)
if __name__ == "__main__":
compare_thinking_modes()
输出分析:标准模式 vs. 扩展思维模式
当我使用以下提示词问题运行调用Claude 3.7的代码示例时:
如果格陵兰岛的所有冰层都融化,对全球海平面会产生什么影响?
我收到了两种截然不同的回答:
标准模式响应:
# Impact of Greenland Ice Sheet Melting
If all ice in Greenland completely melted, global sea levels would rise by approximately **7.2 meters (24 feet)**.
This significant rise would:
- Inundate coastal cities worldwide
- Displace hundreds of millions of people
- Redraw coastlines globally
- Impact critical infrastructure in low-lying areasFor context, while the Antarctic ice sheet contains much more ice (potential for ~58 meters of sea level rise), Greenland's ice is currently melting at a faster rate due to Arctic warming.
Scientists estimate complete melting of Greenland's ice sheet would take centuries under current climate scenarios, though the process is accelerating with climate change.
扩展思维过程:
This question is asking about the potential impact on global sea levels from a complete melt of Greenland's ice sheet. This is a well-studied scientific question related to climate change impacts.
Greenland contains the second largest ice sheet on Earth (after Antarctica). The Greenland ice sheet contains approximately 2.85-3 million cubic kilometers of ice. If all of this ice melted, it would contribute significantly to sea level rise.
Based on scientific estimates, the complete melting of the Greenland ice sheet would raise global sea levels by approximately 7.2 meters (24 feet) to 7.4 meters. This is a substantial increase that would have dramatic consequences for coastal cities and low-lying areas around the world.
Some key facts about this scenario:
1. Timeframe: Complete melting would likely take centuries or millennia under current warming projections, although the process is already underway.
2. Global impact: A 7+ meter sea level rise would inundate many coastal cities, island nations, and heavily populated river deltas.
3. Current situation: Greenland is already losing ice mass, contributing to observed sea level rise. The rate of ice loss has been accelerating in recent decades due to climate change.
4. Comparison: Antarctica's ice sheet contains much more ice and if it completely melted, it would raise sea levels by about 58 meters (190 feet).
In providing my answer, I'll focus on the estimated sea level rise and briefly mention the impacts this would have, while noting that this scenario represents a long-term projection rather than an immediate threat.
扩展思维模式响应:
# Impact of Complete Greenland Ice Melt on Sea Levels
If all the ice in Greenland melted completely, global sea levels would rise by approximately **7.2 meters (24 feet)**.
## Key context:
- Greenland contains the world's second-largest ice sheet (after Antarctica)
- This amount of sea level rise would have catastrophic consequences for coastal cities and communities worldwide
- Complete melting would likely take centuries to occur under current warming scenarios
- However, partial melting is already happening and accelerating due to climate changeMany major cities like New York, Mumbai, Shanghai, and London would face severe flooding or partial submersion at this level of rise, and some island nations would become uninhabitable.
Would you like me to elaborate on any particular aspect of this scenario?
结果分析:
Claude的扩展思维推理模式最有趣的地方在于,它揭示了Claude的内部推理过程,其中包含了标准模式未包括的多个信息:
- 详细量化信息: 推理过程中引用了“2.85-3百万立方公里的冰”这一具体数据,而该数据并未出现在标准模式的最终回答中。
- 结构化思维: Claude会先将思考过程组织成编号要点(时间范围、全球影响、当前情况、对比分析),然后再综合形成可读的回答。
- 自我指引: Claude会告诉自己“在回答时,我将重点关注估算海平面上升成都……”显示出它是清楚如何规划自己的最终回答的。
- 语气差异: 标准模式的回答更加直接且自信,而扩展思维模式则更倾向于学术性,综合分析各种事实。
- Token使用情况: 扩展思维模式使用了532个输出token,而标准模式仅使用了161个——前者消耗了超过3倍的token。
- 不同的最终格式: 扩展思维模式的回答与传统的AI模型回复相比,采用了不同的回复结构,结尾部分更具对话性,询问了用户是否希望深入了解某个方面。如果用户回答是,模型将继续围绕该问题进行深入挖掘和推理。
这种对推理过程的透明度使我们能够更好地理解Claude AI模型是如何得出结论的,并帮助我们验证其思考过程的准确性。
总结
Claude 3.7 Sonnet在Amazon Bedrock上引入了混合推理功能,使其能够在标准快速响应和详细的逐步推理之间自由切换。本文探讨了如何在Python环境中使用这一能力,并通过代码示例对比了标准模式与扩展思维模式的不同表现。
主要的不同包括:
-
标准模式更简洁直接,适用于快速回答。
-
扩展思维模式能够揭示Claude的完整思考过程,包括详细量化信息、结构化分析、自我指引等,提供更透明、更可信的AI交互体验。
-
启用推理功能后,Claude会消耗更多tokens,但能提供更深入的逻辑推理和更完整的回答框架。
通过调整推理预算和模式,大家可以优化Claude在不同任务中的表现,例如数据分析、推理验证、教育场景等。掌握这一功能后,大家不仅能获取AI的答案,还能理解其完整的思考过程,提升用户与AI交互过程中的透明度和信任度。