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