Structured Outputs zwingen Claude, Antworten zurückzugeben, die einem JSON-Schema entsprechen. Dies garantiert gültige, parsbare JSON-Ausgabe, die mit Ihrer Schemadefinition übereinstimmt.
Verwenden Sie die Convenience-Methode, um eine Nachricht mit JSON-Schema-Ausgabe zu erstellen.
Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';
vSchema := '{"type":"object","properties":{"name":{"type":"string"},' +
'"age":{"type":"integer"}},"required":["name","age"],' +
'"additionalProperties":false}';
WriteLn(Anthropic._CreateMessageJSON('claude-sonnet-4-20250514',
'Extract the name and age: John is 30 years old.', vSchema));
Verwenden Sie die typisierten Klassen, um strukturierte Ausgabe mit dem effort-Parameter zu kombinieren.
Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';
oRequest := TsgcAnthropicClass_Request_Messages.Create;
Try
oRequest.Model := 'claude-sonnet-4-20250514';
oRequest.MaxTokens := 4096;
// JSON schema for structured output
oRequest.OutputFormatSchema :=
'{"type":"object","properties":{"sentiment":{"type":"string",' +
'"enum":["positive","negative","neutral"]},"confidence":' +
'{"type":"number"}},"required":["sentiment","confidence"],' +
'"additionalProperties":false}';
// Set effort level (low, medium, high, max)
oRequest.Effort := 'medium';
oMessage := TsgcAnthropicClass_Request_Message.Create;
oMessage.Role := 'user';
oMessage.Content := 'Analyze the sentiment: I love this product!';
oMessages := oRequest.Messages;
SetLength(oMessages, 1);
oMessages[0] := oMessage;
oRequest.Messages := oMessages;
oResponse := Anthropic.CreateMessage(oRequest);
Try
for i := 0 to Length(oResponse.Content) - 1 do
begin
if oResponse.Content[i].ContentType = 'text' then
WriteLn(oResponse.Content[i].Text);
end;
Finally
oResponse.Free;
End;
Finally
oMessage.Free;
oRequest.Free;
End;
Aktiviert den strikten Modus für Tool-Definitionen, um zu garantieren, dass Tool-Eingaben genau dem input_schema entsprechen.
oTool := TsgcAnthropicClass_Request_Tool.Create;
oTool.Name := 'get_weather';
oTool.Description := 'Get the current weather for a location';
oTool.Strict := True; // Guarantee schema conformance
oTool.InputSchema := '{"type":"object","properties":{"location":' +
'{"type":"string"}},"required":["location"],"additionalProperties":false}';