Using https://platform.openai.com/docs/guides/vision examples.
openai1.prg
Code: Select all | Expand
#include "FiveWin.ch"
#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" )
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 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 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
//----------------------------------------------------------------------------//