Anthropic Claude API を Delphi アプリケーションに統合する
sgcWebSockets は、テキスト生成やビジョンからツール利用、拡張思考まで、Anthropic Claude API の全機能に対応した本番運用可能な Delphi コンポーネントを提供します。
Anthropic Claude は現在利用可能な中で最も高度な AI モデルファミリーの 1 つであり、卓越した推論力、安全性を最優先とした設計、テキスト、コード、ビジョン、構造化データにわたる多彩な機能で知られています。これらの機能を活用したい Delphi 開発者向けに、sgcWebSockets は TsgcHTTP_API_Anthropic を提供します。これは Anthropic API のすべての領域をラップする包括的なネイティブコンポーネントです。
インテリジェントなチャットボットの構築、ドキュメント分析の自動化、複雑なツールパイプラインのオーケストレーション、数千件のリクエストのバッチ処理など、用途を問わず、このコンポーネントは Claude のすべての機能にクリーンで型安全な Delphi コードから直接アクセスする手段を提供します。REST のボイラープレートも、JSON の取り回しも不要です。コンポーネントをドロップし、API キーを設定するだけで、すぐに構築を始められます。
完全な API カバレッジ
Anthropic Claude API の主要機能はすべて標準でサポートされています。
| メッセージとストリーミング システムプロンプト付きでメッセージを送信し、同期的に、または Server-Sent Events 経由でリアルタイムにストリーミングされたレスポンスを受信します。 |
ビジョン JPEG、PNG、GIF、WebP 形式の画像を解析します。Claude は視覚コンテンツを記述、解釈、推論します。 |
ツール利用 JSON Schema でカスタムツールを定義し、Claude に呼び出させます。関数呼び出しを使ったエージェント型ワークフローを構築できます。 |
| 拡張思考 複雑なタスクに対する段階的な推論を有効化します。数学、分析、多段階の問題解決に最適です。 |
ドキュメント処理 PDF ドキュメントやテキストファイルを送信して、解析、要約、引用付きの質問応答を行います。 |
構造化出力 Claude にスキーマに準拠した有効な JSON を返させます。パース可能で型安全なレスポンスを保証します。 |
| Web 検索 組み込みのサーバーサイドツールを使用して、会話中に Claude に Web をリアルタイムで検索させます。 | プロンプトキャッシング システムプロンプト、コンテンツブロック、ツール定義をキャッシュします。繰り返し使用するコンテキストでコストを最大 90% 削減します。 | メッセージバッチ 数千件のリクエストを非同期処理します。大量のコンテンツ生成やデータ処理パイプラインに最適です。 |
| Files API Anthropic のサーバー上でファイルをアップロード、一覧表示、ダウンロード、管理します。複数の会話間で参照できます。 | MCP コネクター Claude を外部の Model Context Protocol サーバーに接続します。サードパーティのツールやサービスで機能を拡張できます。 | トークンカウント リクエスト送信前にトークン数をカウントします。コストを正確に見積もり、コンテキストウィンドウの予算を管理できます。 |
はじめに
Claude を Delphi プロジェクトに 1 分以内で統合します。コンポーネントをドロップし、API キーを設定して、最初のメッセージを送信します。
// Create the component and configure the API key
var
Anthropic: TsgcHTTP_API_Anthropic;
vResponse: string;
begin
Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Try
Anthropic.AnthropicOptions.ApiKey := 'YOUR_API_KEY';
// Send a simple message to Claude
vResponse := Anthropic._CreateMessage(
'claude-sonnet-4-20250514', 'Hello, Claude!');
ShowMessage(vResponse);
Finally
Anthropic.Free;
End;
end;
メッセージとストリーミング
Messages API はすべての Claude インタラクションの基礎です。任意でシステムプロンプトを付けてテキストを送信し、同期的または real-time でストリーミングされるレスポンスを受信します。
システムプロンプト会話のコンテキスト、人格、または制約を設定するシステムプロンプトを提供することで、Claude の挙動を制御します。
vResponse := Anthropic._CreateMessageWithSystem( 'claude-sonnet-4-20250514', 'You are a helpful assistant that responds in Spanish.', 'What is the capital of France?'); // Returns: "La capital de Francia es París."
リアルタイムストリーミング
レスポンシブなユーザーインターフェース向けに、Server-Sent Events を使用して Claude のレスポンスをトークン単位でストリーミングします。OnHTTPAPISSE イベントハンドラーを割り当て、_CreateMessageStream を呼び出します。
// Enable streaming via SSE
Anthropic.OnHTTPAPISSE := OnSSEEvent;
Anthropic._CreateMessageStream('claude-sonnet-4-20250514',
'Tell me a story about a brave explorer.');
procedure TForm1.OnSSEEvent(Sender: TObject;
const aEvent, aData: string; var Cancel: Boolean);
begin
// aEvent: event type (e.g., content_block_delta)
// aData: JSON payload for this event
Memo1.Lines.Add(aData);
end;
高度な型付き API
リクエストパラメーター(temperature、top-p、stop シーケンス、メタデータ)を完全に制御するには、型付きリクエスト・レスポンスクラスを使用します。
var
oRequest: TsgcAnthropicClass_Request_Messages;
oMessage: TsgcAnthropicClass_Request_Message;
oResponse: TsgcAnthropicClass_Response_Messages;
begin
oRequest := TsgcAnthropicClass_Request_Messages.Create;
Try
oRequest.Model := 'claude-sonnet-4-20250514';
oRequest.MaxTokens := 1024;
oRequest.System := 'You are a helpful assistant.';
oRequest.Temperature := 0.7;
oMessage := TsgcAnthropicClass_Request_Message.Create;
oMessage.Role := 'user';
oMessage.Content := 'Explain quantum computing in simple terms.';
// ... add message to request, send, and process response
oResponse := Anthropic.CreateMessage(oRequest);
Try
if Length(oResponse.Content) > 0 then
ShowMessage(oResponse.Content[0].Text);
Finally
oResponse.Free;
End;
Finally
oMessage.Free;
oRequest.Free;
End;
end;
ビジョン — 画像理解
Claude は画像を解析し推論できます。写真、スクリーンショット、図表、グラフを送信して、詳細な説明、データ抽出、視覚的な Q&A を受け取れます。
サポート形式は JPEG、PNG、GIF、WebP です。画像は base64 エンコードされたコンテンツブロックとして送信されます。
// Load an image and ask Claude to describe it
var
vBase64: string;
begin
vBase64 := sgcBase64Encode(LoadFileToBytes('product-photo.png'));
ShowMessage(Anthropic._CreateVisionMessage(
'claude-sonnet-4-20250514',
'Describe this product image for an e-commerce listing.',
vBase64, 'image/png'));
end;
ツール利用 — 関数呼び出し
JSON Schema でカスタムツールを定義すると、Claude がいつどのように呼び出すかを決定します。これがエージェント型の多段階ワークフローを構築する基盤になります。
ツール利用のフローは明確なパターンに従います。ツールを定義し、リクエストを送信し、レスポンス内の tool_use を検出し、関数をローカルで実行し、最終回答のために結果を Claude に返します。
// Define a weather tool with JSON Schema input
oTool := TsgcAnthropicClass_Request_Tool.Create;
oTool.Name := 'get_weather';
oTool.Description := 'Get the current weather in a given location';
oTool.InputSchema :=
'{"type":"object","properties":{"location":{"type":"string",' +
'"description":"The city and state"}},"required":["location"]}';
// Send request with tools defined
oResponse := Anthropic.CreateMessage(oRequest);
// Check if Claude wants to call a tool
if oResponse.StopReason = 'tool_use' then
begin
for i := 0 to Length(oResponse.Content) - 1 do
begin
if oResponse.Content[i].ContentType = 'tool_use' then
begin
vToolUseId := oResponse.Content[i].Id;
vToolName := oResponse.Content[i].Name;
vToolInput := oResponse.Content[i].Input;
// Execute the tool and return the result to Claude
end;
end;
end;
ドキュメント処理と引用
PDF ドキュメントやテキストファイルを Claude に送信し、解析、要約、質問応答を行います。引用を有効化すると、元の資料に対する検証可能な参照を受け取れます。
// Send a PDF and ask Claude to summarize it
vBase64 := sgcBase64Encode(LoadFileToBytes('annual-report.pdf'));
vResponse := Anthropic._CreateDocumentMessage(
'claude-sonnet-4-20250514',
'Summarize the key findings of this report.',
vBase64, 'application/pdf');
引用
ドキュメントのコンテンツブロックで引用を有効にすると、Claude のレスポンスにソース参照(ページ番号、文字範囲、引用テキストを含む)を受け取れます。
oDocBlock := TsgcAnthropicClass_Request_Content_Block.Create; oDocBlock.ContentType := 'document'; oDocBlock.SourceType := 'base64'; oDocBlock.MediaType := 'application/pdf'; oDocBlock.Data := vBase64; oDocBlock.Title := 'Annual Report'; oDocBlock.CitationsEnabled := True;
MCP コネクター
Claude を外部の Model Context Protocol(MCP)サーバーに接続して、サードパーティのツールやサービス(データベース、CRM、内部 API など)で機能を拡張します。
Anthropic.AnthropicOptions.BetaHeaders := 'mcp-client-2025-11-20'; // Connect to an MCP server with one method call vResponse := Anthropic._CreateMessageWithMCP( 'claude-sonnet-4-20250514', 'What tools are available?', 'https://my-mcp-server.example.com/sse', 'my-mcp-server');
認証済み MCP サーバー
認証を必要とするサーバーに対しては、型付き API が OAuth Bearer トークンをサポートします。
oServer := TsgcAnthropicClass_Request_MCPServer.Create; oServer.ServerType := 'url'; oServer.Url := 'https://my-mcp-server.example.com/sse'; oServer.Name := 'my-server'; oServer.AuthorizationToken := 'OAUTH_TOKEN'; oTool := TsgcAnthropicClass_Request_Tool.Create; oTool.ToolType := 'mcp_toolset'; oTool.MCPServerName := 'my-server';
