この例では、Claudeに文字列が回文かどうかをチェックするPython関数を書いてもらいます。

前提条件

必要なもの:

AnthropicはAPIに直接HTTPリクエストを行うこともできますが、PythonとTypeScript SDKを提供しています。

Workbenchから始める

APIコールを行う場合—特定のタスクに関係なく—適切に構成されたプロンプトをAnthropic APIに送信します。Claudeを最大限に活用する方法を学ぶ際には、開発プロセスをWorkbenchから始めることをお勧めします。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の応答がどのように変わったか見てください。LLMは明確で直接的な指示に適切に応答します。役割の指示はシステムプロンプトまたはユーザーメッセージのどちらにも配置できます。どちらの方法があなたのユースケースに最適な結果をもたらすかをテストすることをお勧めします。

出力に満足し、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.pyまたはnode 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リクエストを行ったので、他に何が可能かを探索する時間です: