foropenai icon indicating copy to clipboard operation
foropenai copied to clipboard

Problem with http-client using multipart/form-data

Open gha3mi opened this issue 2 years ago • 1 comments

In test5 for translation:

This curl command functions correctly:

curl https://api.openai.com/v1/audio/translations \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F file="@test/audio_de.mp3" \
  -F model="whisper-1" \
  -F prompt="" \
  -F response_format="json" \
  -F temperature=0.0

The equivalent HTTP request using http-client doesn't work when there is a temperature parameter:

program test_translation

   use http, only: response_type, request, HTTP_POST, pair_type

   type(pair_type),    allocatable :: req_header(:), form_data(:), file_data
   character(len=:),   allocatable :: api_key
   type(response_type)             :: response

   api_key = 'OPENAI_API_KEY'

   req_header = [&
      pair_type('Authorization', 'Bearer '//trim(api_key)),&
      pair_type('Content-Type', 'multipart/form-data')&
      ]

   form_data = [&
      pair_type('model'          , "whisper-1"),&
      pair_type('prompt'         , ""),&
      pair_type('response_format', "json"),&
      pair_type('temperature'    , "0.0")&
      ]

   file_data = pair_type('file', 'test/audio_de.mp3')

   response = request(&
      url    = "https://api.openai.com/v1/audio/translations",&
      method = HTTP_POST,&
      header = req_header,&
      form   = form_data,&
      file   = file_data)

   print*, response%content

end program test_translation

Edit: Updated test program.

gha3mi avatar Sep 19 '23 15:09 gha3mi

This is the error message when using "temperature":

 {
  "error": {
    "message": "1 validation error for Request\nbody -> temperature\n  value is not a valid float (type=type_error.float)",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

@rajkumardongre, In this case, it seems that a real number (float) is required for the "temperature" field instead of a string value.

gha3mi avatar Sep 21 '23 13:09 gha3mi