create_translation Subroutine

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

Uses

    • http
    • json_module
  • proc~~create_translation~~UsesGraph proc~create_translation foropenai_Translation::Translation%create_translation http http proc~create_translation->http json_module json_module proc~create_translation->json_module

Type Bound

Translation

Arguments

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

Calls

proc~~create_translation~~CallsGraph proc~create_translation foropenai_Translation::Translation%create_translation deserialize deserialize proc~create_translation->deserialize destroy destroy proc~create_translation->destroy get get proc~create_translation->get initialize initialize proc~create_translation->initialize pair_type pair_type proc~create_translation->pair_type proc~set_file~2 foropenai_Translation::Translation%set_file proc~create_translation->proc~set_file~2 proc~set_prompt~3 foropenai_Translation::Translation%set_prompt proc~create_translation->proc~set_prompt~3 request request proc~create_translation->request

Called by

proc~~create_translation~~CalledByGraph proc~create_translation foropenai_Translation::Translation%create_translation program~test_translation test_Translation program~test_translation->proc~create_translation

Source Code

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

      class(Translation), 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('prompt', trim(this%prompt)),&
         pair_type('response_format', trim(this%response_format))&
         ! 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_translation