admin管理员组

文章数量:1303389

I'm building a react native app that pass as form data an audio registration to a backend lambda function written in golang.

I'm using react-native-audio-recorder-player:

const audioSet: AudioSet = {
  AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
  AudioSourceAndroid: AudioSourceAndroidType.MIC,
  AVModeIOS: AVModeIOSOption.measurement,
  AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
  AVNumberOfChannelsKeyIOS: 2,
  AVFormatIDKeyIOS: AVEncodingOption.wav,
};

This is the form data in the frontend:

formData.append('file', {
  uri: audioFilePath,
  type: 'audio/wav',
  name: 'audio.wav',
});

This is the backend code, keep in mind that I have setup the BinaryMediaTypes in the APi gateway parameters in the template.yaml, so the request.Body actually exists.

This is the openai library:

fileReader := strings.NewReader(request.body)

client := openai.NewClient(
    option.WithAPIKey(OPEN_AI_KEY),
)

response, err := client.Audio.Transcriptions.New(context.Background(), openai.AudioTranscriptionNewParams{
    Model: openai.F(openai.AudioModelWhisper1),
    File:  openai.FileParam(fileReader, "audio.wav", "audio/wav"),
})

if err != nil {
    logger.Error("Error transcribing audio", zap.Error(err))
    return "", err
}

The problem seems to be related to the fileReader since if I print it this is the result:

{
    "level": "info",
    "ts": 1739222783.0462933,
    "caller": "openai/transcribe.go:18",
    "msg": "Transcribing audio",
    "fileReader": {}
}

Thereby the open ai api return the following error:

"error": "POST \"\": 400 Bad Request {\n  \"error\": {\n    \"message\": \"Invalid file format. Supported formats: ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm']\",\n    \"type\": \"invalid_request_error\",\n    \"param\": null,\n    \"code\": null\n  }\n}"

Thank you

本文标签: goLambda function read audio file coming from frontend as form dataStack Overflow