Anthropic | Messages

텍스트 콘텐츠가 포함된 입력 메시지의 구조화된 목록을 보내면 모델이 대화에서 다음 메시지를 생성합니다.

간단한 예제

Claude에 Hello 메시지를 보냅니다.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';
WriteLn(Anthropic._CreateMessage('claude-sonnet-4-20250514', 'Hello!'));

System Prompt 예제

Claude의 동작을 제어하기 위해 system prompt가 있는 메시지를 보냅니다.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';
WriteLn(Anthropic._CreateMessageWithSystem('claude-sonnet-4-20250514',
  'You are a helpful assistant that responds in Spanish.',
  'What is the capital of France?'));

고급 예제

메시지 매개변수에 대한 완전한 제어를 위해 형식화된 request/response 클래스를 사용하십시오.


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 := 1024;
  oRequest.System := 'You are a helpful assistant.';
  oRequest.Temperature := 0.7;

  oMessage := TsgcAnthropicClass_Request_Message.Create;
  oMessage.Role := 'user';
  oMessage.Content := 'Hello!';
  oMessages := oRequest.Messages;
  SetLength(oMessages, 1);
  oMessages[0] := oMessage;
  oRequest.Messages := oMessages;

  oResponse := Anthropic.CreateMessage(oRequest);
  Try
    if Length(oResponse.Content) > 0 then
      WriteLn(oResponse.Content[0].Text);
  Finally
    oResponse.Free;
  End;
Finally
  oMessage.Free;
  oRequest.Free;
End;

스트리밍 예제

Server-Sent Events (SSE)를 사용하여 응답을 실시간으로 스트리밍합니다. 스트리밍 이벤트를 수신하려면 OnHTTPAPISSE 이벤트 핸들러를 할당하십시오.


Anthropic := TsgcHTTP_API_Anthropic.Create(nil);
Anthropic.AnthropicOptions.ApiKey := 'API_KEY';
Anthropic.OnHTTPAPISSE := OnSSEEvent;
Anthropic._CreateMessageStream('claude-sonnet-4-20250514', 'Tell me a story.');

procedure TForm1.OnSSEEvent(Sender: TObject; const aEvent, aData: string;
  var Cancel: Boolean);
begin
  // aEvent contains the event type (e.g. content_block_delta)
  // aData contains the JSON data for this event
  Memo1.Lines.Add(aData);
end;