Example of use:
METHOD SendStream( cPrompt, bWriteFunction ) CLASS TOLlama
local aHeaders, cJson, hRequest := { => }, hMessage1 := { => }
curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, .T. )
curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cUrl )
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_SSL_VERIFYPEER, .F. )
hRequest[ "model" ] = ::cModel
hMessage1[ "role" ] = "user"
hMessage1[ "content" ] = cPrompt
hRequest[ "messages" ] = { hMessage1 }
hRequest[ "stream" ] = .T.
hRequest[ "temperature" ] = 0.5
cJson = hb_jsonEncode( hRequest )
curl_easy_setopt( ::hCurl, HB_CURLOPT_POSTFIELDS, cJson )
curl_easy_setopt( ::hCurl, HB_CURLOPT_WRITEFUNCTION, bWriteFunction )
::nError = curl_easy_perform( ::hCurl )
curl_easy_getinfo( ::hCurl, HB_CURLINFO_RESPONSE_CODE, @::nHttpCode )
if ::nError != HB_CURLE_OK
::cResponse := "Error code " + Str( ::nError )
endif
return ::cResponse
oChat:SendStream( AllTrim( cPrompt ), { | cBuffer | ShowTokens( cBuffer ) } )
function ShowTokens( cBuffer )
local hResponse
hb_jsonDecode( cBuffer, @hResponse )
oOutput:Append( hResponse[ "message" ][ "content" ] )
return nil
I was thinking about creating a new contrib/ai folder for Class TOpenAI, TDeepSeek and TOllama. I appreciate your comments. I have those classes working properly.
best regards
Antonio