Il pensiero esteso offre a Claude la capacità di ragionare passo dopo passo su problemi complessi prima di fornire una risposta. Quando abilitato, Claude crea un ragionamento interno (blocchi di pensiero) che migliora la qualità delle risposte per matematica, programmazione, analisi e altri compiti complessi.
Invia un messaggio con il ragionamento esteso abilitato utilizzando il metodo di convenienza. La temperatura viene impostata automaticamente a 1,0 (richiesto dall'API quando il ragionamento è abilitato).
Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';
WriteLn(Anthropic._CreateMessageWithThinking('claude-sonnet-4-20250514',
'How many r''s are in the word strawberry?', 10000));
Utilizzare le classi di richiesta/risposta tipizzate per il controllo completo. Impostare ThinkingType su 'enabled' e ThinkingBudgetTokens sul budget di token desiderato (minimo 1024). La risposta conterrà blocchi di contenuto di tipo thinking e testo.
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 := 16384;
oRequest.ThinkingType := 'enabled';
oRequest.ThinkingBudgetTokens := 10000;
oMessage := TsgcAnthropicClass_Request_Message.Create;
oMessage.Role := 'user';
oMessage.Content := 'Explain the proof that there are infinitely many primes.';
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 = 'thinking' then
WriteLn('Thinking: ' + oResponse.Content[i].Thinking)
else if oResponse.Content[i].ContentType = 'text' then
WriteLn('Response: ' + oResponse.Content[i].Text);
end;
Finally
oResponse.Free;
End;
Finally
oMessage.Free;
oRequest.Free;
End;
Quando si utilizza il pensiero esteso nelle conversazioni multi-turno, i blocchi thinking e redacted_thinking delle risposte precedenti devono essere reinseriti nella conversazione. Utilizzare l'array ContentBlocks per includere questi blocchi.
// Pass back thinking blocks from a previous response
oBlock := TsgcAnthropicClass_Request_Content_Block.Create;
oBlock.ContentType := 'thinking';
oBlock.Text := oPrevThinkingBlock.Thinking; // thinking text
oBlock.Signature := oPrevThinkingBlock.Signature; // signature string
// Pass back redacted thinking blocks
oBlock := TsgcAnthropicClass_Request_Content_Block.Create;
oBlock.ContentType := 'redacted_thinking';
oBlock.Data := oPrevRedactedBlock.Data;