Foi postado no harbour-users no dia 03/janeiro/2025, por Antonio Linares - fivewin.
Código: Selecionar todos
#include "c:\harbour\contrib\hbcurl\hbcurl.ch"
//----------------------------------------------------------------------------//
function Main()
local oChatgpt := TOpenAI():New()
// oChatgpt:SendImageURL( "https://forums.fivetechsupport.com/styles/prosilver/imageset/site_logo.gif" )
// oChatgpt:SendImage( "../bitmaps/logo/fivepowergm5.jpg" )
oChatgpt:Send( "What is the capital of France ?" )
MsgInfo( oChatgpt:GetValue() )
oChatgpt:End()
return nil
//----------------------------------------------------------------------------//
CLASS TOpenAI
DATA cKey INIT ""
DATA cModel INIT "gpt-4o-mini"
DATA cResponse
DATA cUrl
DATA hCurl
DATA nError INIT 0
DATA nHttpCode INIT 0
METHOD New( cKey, cModel )
METHOD Send( cPrompt )
METHOD SendImage( cImageFileName )
METHOD SendImageURL( cImageUrl )
METHOD End()
METHOD GetValue( cHKey )
ENDCLASS
//----------------------------------------------------------------------------//
METHOD New( cKey, cModel ) CLASS TOpenAI
if Empty( cKey )
::cKey = GetEnv( "OPENAI_API_KEY" )
endif
if ! Empty( cModel )
::cModel = cModel
endif
::cUrl = "https://api.openai.com/v1/chat/completions"
::hCurl = curl_easy_init()
return Self
//----------------------------------------------------------------------------//
METHOD End() CLASS TOpenAI
curl_easy_cleanup( ::hCurl )
::hCurl = nil
return nil
//----------------------------------------------------------------------------//
METHOD GetValue( cHKey ) CLASS TOpenAI
local aKeys := hb_AParams(), cKey
local uValue := hb_jsonDecode( ::cResponse )
hb_default( @cHKey, "content" )
if cHKey == "content"
uValue = uValue[ "choices" ][ 1 ][ "message" ][ "content" ]
endif
TRY
for each cKey in aKeys
if ValType( uValue[ cKey ] ) == "A"
uValue = uValue[ cKey ][ 1 ][ "choices" ][ 1 ][ "message" ][ "content" ]
else
uValue = uValue[ cKey ]
endif
next
CATCH
XBrowser( uValue )
END
return uValue
//----------------------------------------------------------------------------//
METHOD Send( cPrompt ) CLASS TOpenAI
LOCAL aHeaders, cJson, hRequest := { => }, hMessage := { => }
curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, .T. )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cUrl )
aHeaders := { "Content-Type: application/json", ;
"Authorization: Bearer " + ::cKey }
curl_easy_setopt(::hCurl, HB_CURLOPT_HTTPHEADER, aHeaders)
curl_easy_setopt(::hCurl, HB_CURLOPT_USERNAME, '')
curl_easy_setopt(::hCurl, HB_CURLOPT_DL_BUFF_SETUP)
curl_easy_setopt(::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F.)
hb_HSet( hRequest, "model", ::cModel )
hb_HSet( hMessage, "role", "user" )
hb_HSet( hMessage, "content", cPrompt )
hRequest[ "messages" ] = { hMessage }
cJson = hb_jsonEncode( hRequest )
curl_easy_setopt( ::hCurl, HB_CURLOPT_POSTFIELDS, cJson )
::nError = curl_easy_perform( ::hCurl )
curl_easy_getinfo( ::hCurl, HB_CURLINFO_RESPONSE_CODE, @::nHttpCode )
if ::nError == HB_CURLE_OK
::cResponse = curl_easy_dl_buff_get( ::hCurl )
else
::cResponse := "Error code " + Str( ::nError )
endif
return ::cResponse
//----------------------------------------------------------------------------//
METHOD SendImage( cImageFileName ) CLASS TOpenAI
local aHeaders, cJson, cBase64Image, hMessage := { => }
if ! File( cImageFileName )
MsgAlert( "Image " + cImageFileName + " not found" )
return nil
endif
cBase64Image := hb_base64Encode( memoRead( cImageFileName ) )
curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, .T. )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, "https://api.openai.com/v1/chat/completions" )
aHeaders := { "Content-Type: application/json", ;
"Authorization: Bearer " + ::cKey }
curl_easy_setopt( ::hCurl, HB_CURLOPT_HTTPHEADER, aHeaders )
curl_easy_setopt( ::hCurl, HB_CURLOPT_USERNAME, "" )
curl_easy_setopt( ::hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
hb_HSet( hMessage, "role", "user" )
hb_HSet( hMessage, "content", { ;
{ "type" => "text", "text" => "What is in this image?" }, ;
{ "type" => "image_url", ;
"image_url" => { "url" => "data:image/jpeg;base64," + cBase64Image } } } )
cJson := hb_jsonEncode( { "model" => ::cModel, ;
"messages" => { hMessage } } )
curl_easy_setopt( ::hCurl, HB_CURLOPT_POSTFIELDS, cJson )
::nError = curl_easy_perform( ::hCurl )
curl_easy_getinfo( ::hCurl, HB_CURLINFO_RESPONSE_CODE, @::nHttpCode )
if ::nError == HB_CURLE_OK
::cResponse = curl_easy_dl_buff_get( ::hCurl )
else
::cResponse = "Error code " + Str( ::nError )
endif
return ::cResponse
//----------------------------------------------------------------------------//
METHOD SendImageURL( cImageUrl ) CLASS TOpenAI
local aHeaders, cJson, hRequest := { => }, hMessage := { => }
curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, .T. )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cUrl )
aHeaders := { "Content-Type: application/json", ;
"Authorization: Bearer " + ::cKey }
curl_easy_setopt( ::hCurl, HB_CURLOPT_HTTPHEADER, aHeaders )
curl_easy_setopt( ::hCurl, HB_CURLOPT_USERNAME, "" )
curl_easy_setopt( ::hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
hb_HSet( hRequest, "model", ::cModel )
hb_HSet( hMessage, "role", "user" )
hb_HSet( hMessage, "content", { ;
{ "type" => "text", "text" => "What's in this image?" }, ;
{ "type" => "image_url", "image_url" => { "url" => cImageUrl } } } )
hRequest[ "messages" ] = { hMessage }
cJson = hb_jsonEncode( hRequest )
curl_easy_setopt( ::hCurl, HB_CURLOPT_POSTFIELDS, cJson )
::nError = curl_easy_perform( ::hCurl )
curl_easy_getinfo( ::hCurl, HB_CURLINFO_RESPONSE_CODE, @::nHttpCode )
if ::nError == HB_CURLE_OK
::cResponse = curl_easy_dl_buff_get( ::hCurl )
else
::cResponse = "Error code " + Str( ::nError )
endif
return ::cResponse
Nota: nesta data/horário, o site mostra aviso de manutenção.


