在這個範例中,我們將讓 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 請求,是時候探索更多可能性了: