Claude 3.7 Sonnet的新推理功能,前所未有地展现了Claude如何分析问题、分解问题以及得出结论,从而让您一探人工智能的逐步思考过程。
本文将构建一个Python应用程序,通过Amazon Bedrock利用这一强大的新推理功能。您将了解如何启用推理功能,分配适当的计算资源,并处理包含Claude内部推理过程和最终答案的结构化响应。无论您是在调试提示信息、验证计算结果,还是将Claude用作教育工具,了解其推理过程都能极大提升您与AI的交互体验。
推理功能介绍
推理是Claude在给出答案之前,能够明确且逐步解决复杂问题的能力。启用推理功能后,Claude不仅会给出最终答案,还会展示其完整的思考过程,类似于人类解决问题时可能会阐述思考过程。
这一功能赋予了Claude以下优势。
可将复杂问题拆解为易于管理的各个部分。
探索解决问题的多种方法。
系统分析信息并得出合乎逻辑的结论。
展示问题解决过程,对解决数学或逻辑问题尤为重要。
识别自身思考过程中的潜在漏洞。
对于诸如复杂数学运算、逻辑难题或细致分析性问题等需要多步骤深入思考的问题,推理显得尤为关键。通过揭示Claude的内部思考流程,您可以清晰洞察AI是如何逐步推导出结论的,这不仅能建立信任感,还极具教育意义。
启用推理功能后,Claude会根据您的token预算分配相应部分的计算资源,来执行这一明确的思考流程,随后会将此流程与最终答案一并反馈给您,这将让您能够前所未有地洞悉AI解决问题的思路。
准备条件
开始使用之前,请确保您已具备以下条件。
拥有一个可以访问Amazon Bedrock的亚马逊云科技账户。
已安装Python 3.x。
已安装并配置Amazon CLI。
在账户中已启用Claude 3.7 Sonnet功能。
*注:截至撰写本文时,Claude 3.7可在us-east-1、us-east-2和us-west-2区域中使用。
设置依赖项
您需要安装Amazon SDK for Python(boto3),以下是包含所需依赖项的方法。
pip install boto3>=1.37.0
或者可以将以下内容添加到requirements.txt文件中。
boto3>=1.37.0
1
创建基本文件结构
创建一个新的Python文件(如claude_reasoning.py),在其中编写代码,以便与Claude的推理功能进行交互。
import boto3
from botocore.exceptions import ClientError
def reasoning_example():
# We'll implement this function in the next steps
pass
if name == "__main__":
# We'll call our reasoning function here
pass
左右滑动查看完整示意
2
设置Amazon Bedrock客户端
首先需要创建Amazon Bedrock运行时客户端来处理API请求。在reasoning_example()函数中添加以下内容。
# 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"
左右滑动查看完整示意
3
创建信息发送至Claude
接下来创建一条简单信息发送给Claude。
# Create the message with the user's prompt
user_message = "Describe the purpose of a 'hello world' program in one line."
conversation = [
{
"role": "user",
"content": [{"text": user_message}],
}
]
左右滑动查看完整示意
4
配置推理参数
启用推理功能,并为其设置token预算。
# Configure reasoning parameters with a 2000 token budget
reasoning_config = {
"thinking": {
"type": "enabled",
"budget_tokens": 2000
}
}
左右滑动查看完整示意
budget_tokens参数定义了Claude在推理过程中可使用的最大tokens数量,您可根据自身提示复杂程度调整此数值。
5
向Claude发送请求
发送提示和推理配置。
# Send message and reasoning configuration to the model
response = client.converse(
modelId=model_id,
messages=conversation,
additionalModelRequestFields=reasoning_config
)
左右滑动查看完整示意
additionalModelRequestFields参数支持传递推理配置。
6
处理响应
Claude将返回其推理过程和最终响应,响应需要提取。
# Extract the list of content blocks from the model's response
content_blocks = response["output"]["message"]["content"]
reasoning = None
text = None
# Process each content block to find reasoning and response text
for block in content_blocks:
if "reasoningContent" in block:
reasoning = block["reasoningContent"]["reasoningText"]["text"]
if "text" in block:
text = block["text"]
return reasoning, text
左右滑动查看完整示意
7
显示结果
最后将显示推理过程和最终答案,并更新您的主要函数。
if __name__ == "__main__":
# Execute the example and display reasoning and final response
reasoning, text = reasoning_example()
print("\n<thinking>")
print(reasoning)
print("</thinking>\n")
print(text)
左右滑动查看完整示意
完整示例
以下为完整的运行示例。
import boto3
from botocore.exceptions import ClientError
"""
This example demonstrates how to use Anthropic Claude 3.7 Sonnet's reasoning capability
with Amazon Bedrock. It shows how to:
- Set up the Amazon Bedrock runtime client
- Create a message
- Configure reasoning parameters
- Send a request with reasoning enabled
- Process both the reasoning output and final response
"""
def reasoning_example():
# Create the Amazon Bedrock runtime client
client = boto3.client("bedrock-runtime", region_name="us-east-1")
# Specify the model ID. For the latest available models, see:
# https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html
model_id = "us.anthropic.claude-3-7-sonnet-20250219-v1:0"
# Create the message with the user's prompt
user_message = "Describe the purpose of a 'hello world' program in one line."
conversation = [
{
"role": "user",
"content": [{"text": user_message}],
}
]
# Configure reasoning parameters with a 2000 token budget
reasoning_config = {
"thinking": {
"type": "enabled",
"budget_tokens": 2000
}
}
try:
# Send message and reasoning configuration to the model
response = client.converse(
modelId=model_id,
messages=conversation,
additionalModelRequestFields=reasoning_config
)
# Extract the list of content blocks from the model's response
content_blocks = response["output"]["message"]["content"]
reasoning = None
text = None
# Process each content block to find reasoning and response text
for block in content_blocks:
if "reasoningContent" in block:
reasoning = block["reasoningContent"]["reasoningText"]["text"]
if "text" in block:
text = block["text"]
return reasoning, text
except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)
if __name__ == "__main__":
# Execute the example and display reasoning and final response
reasoning, text = reasoning_example()
print("\n<thinking>")
print(reasoning)
print("</thinking>\n")
print(text)
左右滑动查看完整示意
输出示例
运行此代码,可看到类似以下输出。
<thinking>
I need to explain the purpose of a "Hello World" program in a single line.
A "Hello World" program is typically the simplest program that can be written in
a programming language. Its purpose is to:
1. Verify that the programming environment is set up correctly
2. Demonstrate the minimal syntax needed to create a functioning program
3. Provide a simple introduction to the language
4. Output the text "Hello, World!" or a similar message
I need to condense this information into a concise, one-line description that
captures the essence of what a "Hello World" program is for.
</thinking>
A "Hello World" program serves as the simplest introduction to a programming
language by demonstrating basic syntax while verifying the environment works correctly.
左右滑动查看完整示意
总结与展望
推理功能通过揭示模型思考过程,革新了人类与人工智能的交互方式。按照本指南中的步骤操作,您现在可以通过Amazon Bedrock使用Python访问并利用这一强大功能。
推理功能为增强信任、优化结果以及从与人工智能交互中获得更深刻洞见方面,创造了诸多令人振奋的新机遇。针对更高级别的应用场景,建议您考虑以下方面。
根据问题复杂程度灵活调整token预算,更复杂的问题可能需要更高的推理预算。
利用推理输出来验证多步骤计算或复杂分析过程的准确性。
通过调整提示比较不同的推理方法。
将推理与Claude函数调用等其他功能相结合,打造强大且透明的AI解决方案。
将推理功能作为教育工具,帮助理解专家级的问题解决策略。
通过将推理功能融入应用程序,您不仅仅是在获取答案,更是在深入了解问题解决的整个过程。这种透明度有助于建立用户信任,并提供更丰富、更有价值的AI交互体验。
本篇作者
Dennis Traub
亚马逊云科技软件工程师与开发者布道师
诚邀您参与「云上探索实验室」,扫描下方二维码,立即体验Amazon Bedrock,开启大模型选型的实战之旅!
星标不迷路,开发更极速!
关注后记得星标「亚马逊云开发者」
听说,点完下面4个按钮
就不会碰到bug了!
点击阅读原文查看博客!获得更详细内容!