在 Delphi 应用中集成 Anthropic Claude API
sgcWebSockets 提供了一个生产就绪的 Delphi 组件,覆盖完整的 Anthropic Claude API——从文本生成、图像理解到工具调用、深度思考等全部功能。
Anthropic Claude 是当今最先进的 AI 模型系列之一,以其卓越的推理能力、安全优先的设计以及在文本、代码、图像和结构化数据方面的多样化能力而享誉业界。为帮助 Delphi 开发者充分利用这些能力,sgcWebSockets 提供了 TsgcHTTP_API_Anthropic——一个封装了全部 Anthropic API 接口的综合性原生组件。
无论您是构建智能聊天机器人、自动化文档分析、编排复杂工具流水线,还是批量处理数千个请求,该组件都能让您通过简洁的类型安全 Delphi 代码直接访问 Claude 的全部功能。无需编写 REST 样板代码,无需手动处理 JSON。只需拖放组件、设置 API 密钥,即可开始构建。
完整 API 覆盖
Anthropic Claude API 的所有主要功能均开箱即用。
| 消息与流式传输 发送带有系统提示的消息,同步或通过 Server-Sent Events 实时流式接收响应。 |
视觉理解 分析 JPEG、PNG、GIF 和 WebP 格式的图像。Claude 能描述、解读并推理视觉内容。 |
工具调用 使用 JSON Schema 定义自定义工具,让 Claude 按需调用,构建基于函数调用的智能代理工作流。 |
| 深度思考 为复杂任务启用逐步推理,非常适合数学计算、分析研究和多步骤问题求解。 |
文档处理 发送 PDF 文档和文本文件进行分析、摘要和问答,并支持引用。 |
结构化输出 强制 Claude 返回符合您定义 Schema 的有效 JSON,确保响应可解析且类型安全。 |
| 网页搜索 让 Claude 在对话中使用内置的服务端工具实时搜索网络信息。 | 提示缓存 缓存系统提示、内容块和工具定义,在重复上下文场景下最多可降低 90% 的成本。 | 消息批处理 异步处理数千个请求,非常适合批量内容生成和数据处理流水线。 |
| 文件 API 在 Anthropic 服务器上上传、列出、下载和管理文件,可在多个对话中引用。 | MCP 连接器 将 Claude 连接到外部模型上下文协议服务器,通过第三方工具和服务扩展能力。 | Token 计数 在发送请求前计算 token 数量,精确估算成本并管理上下文窗口预算。 |
快速入门
不到一分钟即可将 Claude 集成到 Delphi 项目中。拖放组件、配置 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;
消息与流式传输
消息 API 是所有 Claude 交互的基础。发送带有可选系统提示的文本,同步或实时流式接收响应。
系统提示通过提供系统提示来控制 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 逐 token 流式接收 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、停止序列、元数据——请使用类型化请求和响应类。
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 能够分析和推理图像。发送照片、截图、图表或图形,即可获得详细描述、数据提取或视觉问答。
支持的格式包括 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;
文档处理与引用
向 Claude 发送 PDF 文档和文本文件进行分析、摘要和问答。启用引用功能,可获得指向原始材料的可验证引用。
// 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 连接到外部模型上下文协议(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 token。
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';
