Anthropic | Structured Outputs

Yapılandırılmış Çıktılar, Claude'u bir JSON şemasına uygun yanıtlar döndürmeye zorlar. Bu, şema tanımınızla eşleşen geçerli, ayrıştırılabilir JSON çıktısını garanti eder.

Basit Örnek

JSON şema çıktısıyla bir mesaj oluşturmak için kolaylık yöntemini kullanın.


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));

Gelişmiş Örnek

Yapılandırılmış çıktıyı effort parametresiyle birleştirmek için tipli sınıfları kullanın.


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;

Strict Tool Use

Tool girdilerinin input_schema'ya tam olarak uyduğunu garanti etmek için tool tanımlarında strict modu etkinleştirin.


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}';

Özellikler