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

前提条件

你需要:

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

从 Workbench 开始

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

登录 Anthropic Console 并点击 Workbench

在中间部分的 User 下,让我们向 Claude 提一个问题。

User
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 用诗歌回答问题。我们可以通过添加系统提示来控制回答的格式、语气和个性。

System prompt
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+) 提供 SDK。

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

Python
python -m venv claude-env

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

  • 在 macOS 或 Linux 上,source claude-env/bin/activate
  • 在 Windows 上,claude-env\Scripts\activate
Python
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 密钥设置为环境变量,则可以省略下面的那行代码。

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

Response
[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 请求,是时候探索更多可能性了:

Was this page helpful?