在这个示例中,我们将让 Claude 编写一个 Python 函数,用于检查字符串是否为回文。

前提条件

你需要:

Anthropic 提供 Python 和 TypeScript SDK,不过你也可以直接向 API 发送 HTTP 请求。

从 Workbench 开始

无论你执行什么特定任务,任何 API 调用都会向 Anthropic API 发送一个配置良好的提示。在学习如何充分利用 Claude 的过程中,我们建议你在 Workbench(一个基于网络的 Claude 界面)中开始开发过程。

登录 Anthropic Console 并点击 Write a prompt from scratch(从头开始编写提示)。

在中间部分的 User(用户)下,让我们向 Claude 提问。

Why is the ocean salty?

点击 Run(运行)。在右侧,你会看到类似这样的输出:

Response
The ocean is salty due to several factors:

1. Weathering of rocks: Over millions of years, rain, rivers, and streams have eroded rocks containing mineral salts. These salts are carried into the ocean by water runoff.
2. Volcanic activity: Underwater volcanoes and hydrothermal vents release minerals, including salts, into the ocean water.
3. Atmospheric deposition: Salt particles from ocean spray can be carried by wind and deposited back into the ocean.
4. Evaporation: As water evaporates from the surface of the ocean, it leaves behind dissolved salts, increasing the concentration of salt in the remaining water.
5. Biological processes: Some marine organisms contribute to the ocean's salinity by releasing salt compounds as byproducts of their metabolism.

Over time, these processes have continuously added salts to the ocean, while evaporation removes pure water, leading to the ocean's current salinity levels. It's important to note that the total amount of salt in the ocean remains relatively stable because the input of salts is balanced by the removal of salts through processes like the formation of evaporite deposits.

这是一个很好的回答,但假设我们想控制 Claude 给出的确切回答类型。例如,只允许 Claude 用诗歌回答问题。我们可以通过添加系统提示来控制回答的格式、语调和个性。

You are a world-class poet. Respond only with short poems.

再次点击 Run

Response
The ocean's salty brine,
A tale of time and elements combined.
Rocks and rain, a slow erosion,
Minerals carried in solution.
Eons pass, the salt remains,
In the vast, eternal watery domain.

看到 Claude 的回答如何变化了吗?大型语言模型对清晰直接的指令反应良好。你可以将角色指令放在系统提示或用户消息中。我们建议进行测试,看看哪种方式能为你的用例产生最佳结果。

一旦你调整了输入,对输出感到满意,并且对如何使用 Claude 有了很好的理解,就可以将你的 Workbench 转换为集成。

点击 Get Code 复制代表你的 Workbench 会话的生成代码。

安装 SDK

Anthropic 为 Python(3.7+)、TypeScript(4.5+)和 Java(8+)提供 SDK。我们目前还有一个处于测试阶段的 Go SDK。

在你的项目目录中,创建一个虚拟环境。

python -m venv claude-env

使用以下命令激活虚拟环境

  • 在 macOS 或 Linux 上,source claude-env/bin/activate
  • 在 Windows 上,claude-env\Scripts\activate
pip install anthropic

设置你的 API 密钥

每个 API 调用都需要一个有效的 API 密钥。SDK 设计为从环境变量 ANTHROPIC_API_KEY 中获取 API 密钥。你也可以在初始化 Anthropic 客户端时提供密钥。

export ANTHROPIC_API_KEY='your-api-key-here'

调用 API

通过向 /messages 端点传递适当的参数来调用 API。

请注意,Workbench 提供的代码在构造函数中设置 API 密钥。如果你将 API 密钥设置为环境变量,则可以省略下面的那一行。

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-opus-4-20250514",
    max_tokens=1000,
    temperature=1,
    system="You are a world-class poet. Respond only with short poems.",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Why is the ocean salty?"
                }
            ]
        }
    ]
)
print(message.content)

使用 python3 claude_quickstart.pynode claude_quickstart.js 运行代码。

[TextBlock(text="The ocean's salty brine,\nA tale of time and design.\nRocks and rivers, their minerals shed,\nAccumulating in the ocean's bed.\nEvaporation leaves salt behind,\nIn the vast waters, forever enshrined.", type='text')]
Workbench 和代码示例使用默认的模型设置:模型(名称)、温度和要采样的最大令牌数。

这个快速入门展示了如何使用 Console、Workbench 和 API 开发一个基本但功能齐全的 Claude 驱动应用程序。你可以使用这个相同的工作流程作为更强大用例的基础。

下一步

现在你已经发出了第一个 Anthropic API 请求,是时候探索更多可能性了: