I am trying to do a REST request:
uses
REST.Client, REST.Types;
procedure TForm1.ButtonDoRequestClick(Sender: TObject);
var
RESTClient: TRESTClient;
RESTRequest: TRESTRequest;
RESTResponse: TRESTResponse;
AudioStream, FileStream: TStream;
begin
// Create a REST client and request
RESTClient := TRESTClient.Create(nil);
RESTClient.BaseURL := 'https://api.openai.com/v1/';
RESTRequest := TRESTRequest.Create(nil);
RESTRequest.Method := rmPOST;
RESTRequest.Resource := 'synthesize';
RESTRequest.Client := RESTClient;
// Set the API key and request parameters
RESTRequest.AddParameter('engine', 'davinci');
RESTRequest.AddParameter('text', 'Hello, world!');
RESTRequest.AddParameter('voice', 'en-US-Wavenet-A');
RESTRequest.Params.AddItem('Authorization', 'Bearer ' + 'Your API key here', pkHTTPHEADER, [poDoNotEncode]);
// Execute the request and get the audio response
RESTResponse := TRESTResponse.Create(nil);
try
RESTRequest.Execute;
if (RESTResponse.StatusCode >= 200) and (RESTResponse.StatusCode < 300) then
begin
AudioStream := TStringStream.Create(RESTResponse.Content);
try
FileStream := TFileStream.Create('audio.wav', fmCreate);
try
FileStream.CopyFrom(AudioStream, 0);
ShowMessage('Audio saved to file!');
finally
FileStream.Free;
end;
finally
AudioStream.Free;
end;
end
else
begin
ShowMessageFmt('Error (%d): %s', [RESTResponse.StatusCode, RESTResponse.Content]);
end;
finally
RESTClient.Free;
RESTRequest.Free;
RESTResponse.Free;
end;
end;
Unfortunately, I just got an error message.