create_transcription Subroutine

private impure elemental subroutine create_transcription(this, file, prompt)

Uses

    • http
    • json_module
  • proc~~create_transcription~~UsesGraph proc~create_transcription foropenai_Transcription::Transcription%create_transcription http http proc~create_transcription->http json_module json_module proc~create_transcription->json_module

Type Bound

Transcription

Arguments

Type IntentOptional Attributes Name
class(Transcription), intent(inout) :: this
character(len=*), intent(in) :: file
character(len=*), intent(in), optional :: prompt

Calls

proc~~create_transcription~~CallsGraph proc~create_transcription foropenai_Transcription::Transcription%create_transcription deserialize deserialize proc~create_transcription->deserialize destroy destroy proc~create_transcription->destroy get get proc~create_transcription->get initialize initialize proc~create_transcription->initialize pair_type pair_type proc~create_transcription->pair_type proc~set_file foropenai_Transcription::Transcription%set_file proc~create_transcription->proc~set_file proc~set_prompt foropenai_Transcription::Transcription%set_prompt proc~create_transcription->proc~set_prompt request request proc~create_transcription->request

Called by

proc~~create_transcription~~CalledByGraph proc~create_transcription foropenai_Transcription::Transcription%create_transcription program~test_transcription test_Transcription program~test_transcription->proc~create_transcription

Source Code

   elemental impure subroutine create_transcription(this, file, prompt)
      use http,        only: response_type, request, HTTP_POST, pair_type
      use json_module, only: json_file

      class(Transcription), intent(inout)        :: this
      character(len=*),     intent(in)           :: file
      character(len=*),     intent(in), optional :: prompt
      type(pair_type),      allocatable          :: req_header(:), form_data(:), file_data
      type(response_type)                        :: response
      type(json_file)                            :: json
      character(len=1024)                        :: temperature_str
      character(len=:), allocatable              :: assistant_response
      logical                                    :: found

      call this%set_file(file=file)

      if (present(prompt)) then
         call this%set_prompt(prompt=prompt)
      else
         call this%set_prompt(prompt='')
      end if

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

      write(temperature_str,'(f3.1)') this%temperature

      form_data = [&
         pair_type('model', trim(this%model)),&
         pair_type(' language', trim(this%language)),&
         pair_type(' response_format', trim(this%response_format)),&
         pair_type(' prompt', trim(this%prompt)),&
         pair_type(' temperature', trim(temperature_str))&
         ]

      file_data =  pair_type('file', trim(this%file))

      response = request(url = this%url, method = HTTP_POST , header = req_header, form=form_data, file=file_data)

      if (response%ok) then
         call json%initialize()
         call json%deserialize(response%content)
         call json%get("text", assistant_response, found=found)
         if (.not. found) then
            call json%get("error.message", assistant_response)
         end if
         this%assistant_response = trim(assistant_response)
         call json%destroy()
      else
         print '(A)', 'Sorry, an error occurred while processing your request.'
         print '(A)', 'Error message:', response%err_msg
      end if

   end subroutine create_transcription