Code: Select all | Expand
// Get your API key from https://aistudio.google.com
#include "FiveWin.ch"
#include "c:\harbour\contrib\hbcurl\hbcurl.ch"
//----------------------------------------------------------------------------//
CLASS TGemini
DATA cKey INIT ""
DATA cModel INIT "gemini-2.0-flash"
DATA cResponse
DATA cUrl
DATA hCurl
DATA nError INIT 0
DATA nHttpCode INIT 0
DATA nTemperature INIT 0
METHOD New( cKey, cModel )
METHOD Send( cPrompt )
METHOD End()
METHOD GetValue()
ENDCLASS
//----------------------------------------------------------------------------//
METHOD New( cKey, cModel ) CLASS TGemini
if Empty( cKey )
::cKey = GetEnv( "GEMINI_API_KEY" )
else
::cKey = cKey
endif
if ! Empty( cModel )
::cModel = cModel
endif
::cUrl = "https://generativelanguage.googleapis.com/v1beta/models"
::hCurl = curl_easy_init()
return Self
//----------------------------------------------------------------------------//
METHOD End() CLASS TGemini
curl_easy_cleanup( ::hCurl )
::hCurl = nil
return nil
//----------------------------------------------------------------------------//
METHOD GetValue() CLASS TGemini
local uValue := hb_jsonDecode( ::cResponse )
TRY
uValue = uValue[ "candidates" ][ 1 ][ "content" ][ "parts" ][ 1 ][ "text" ]
CATCH
uValue = uValue[ "error" ][ "message" ]
END
return uValue
//----------------------------------------------------------------------------//
METHOD Send( cPrompt ) CLASS TGemini
local aHeaders, cJson, hRequest, hContents := { => }, hGenerationConfig
curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, .T. )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cUrl + "/" + ::cModel + ":generateContent?key=" + ::cKey )
aHeaders := { "Content-Type: application/json" }
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. )
hContents[ "role" ] = "user"
hContents[ "parts" ] = { { "text" => cPrompt } }
hGenerationConfig = { "temperature" => ::nTemperature,;
"topK" => 40, "topP" => 0.95, "maxOutputTokens" => 8192,;
"responseMimeType" => "text/plain" }
hRequest = { "contents" => hContents, "generationConfig" => hGenerationConfig }
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
//----------------------------------------------------------------------------//
Code: Select all | Expand
// Get your API key from https://aistudio.google.com
// from cmd: set GEMINI_API_KEY=your_api_key
#include "FiveWin.ch"
//----------------------------------------------------------------------------//
function Main()
local oChat := TGemini():New()
oChat:Send( "cuantas 'r's hay en 'strawberry' ?" )
? oChat:GetValue()
oChat:End()
return nil
//----------------------------------------------------------------------------//