HemulGM / DelphiOpenAI

OpenAI API wrapper for Delphi. Use ChatGPT, DALL-E, Whisper and other products.
MIT License
241 stars 58 forks source link

OpenAI.Audio.CreateTranscription with SRT format causes OpenAIExceptionInvalidResponse #34

Open Himeko7 opened 11 months ago

Himeko7 commented 11 months ago

When using the transcription functionality, if you request SRT format in the response, even though valid data is returned, it results in an OpenAIExceptionInvalidResponse exception. The problem is the returned data from the API is plain text SRT, not JSON data.

procedure TMainUI.Button3Click(Sender: TObject);
Var OpenAI: IOpenAI;
    AIT: TAudioTranscription;
    AIR: TAudioText;
begin
  OpenAI := TOpenAI.Create('***');

  AIR := TAudioText.Create;
  Try
  AIR := OpenAI.Audio.CreateTranscription(
    procedure(Params: TAudioTranscription)
    begin
      Params.&File('D:\test.mp4');
      Params.ResponseFormat('srt');
      Params.Language('en');
    end);
  Except
  on E: OpenAIExceptionInvalidResponse do
    Memo2.Lines.Add('OpenAI Error: ' + E.Message + ' - ' + E.Code.ToString);
  End;
    Memo2.Lines.Add(AIR.Text);

  AIR.Free;
end;

triggers here, even with code 200 and valid SRT text in the response text

function TOpenAIAPI.ParseResponse<T>(const Code: Int64; const ResponseText: string): T;
begin
  case Code of
    200..299:
      try
        Result := TJson.JsonToObject<T>(ResponseText);
      except
        Result := nil;
      end;
  else
    ParseError(Code, ResponseText);
  end;
  if not Assigned(Result) then
    raise OpenAIExceptionInvalidResponse.Create('Empty or invalid response:', '', '', Code);
end;
HemulGM commented 11 months ago

Oh thanks. I'll take a look and fix it soon.