在這個範例中,我們將讓 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?