// Developed by FiveTech Software, using parts by Charles OhChul
#include "FiveWin.ch"
#include "c:\harbour\contrib\hbcurl\hbcurl.ch"
//----------------------------------------------------------------------------//
CLASS TOLlama
DATA cModel
DATA cResponse
DATA cUrl
DATA hCurl
DATA nError INIT 0
DATA nHttpCode INIT 0
METHOD New( cModel )
METHOD Send( cPrompt )
METHOD End()
METHOD GetValue( cHKey )
ENDCLASS
//----------------------------------------------------------------------------//
METHOD New( cModel ) CLASS TOLlama
hb_default( @cModel, "deepseek-r1:32b" )
::cModel = cModel
::cUrl = "http://localhost:11434/api/chat"
::hCurl = curl_easy_init()
return Self
//----------------------------------------------------------------------------//
METHOD End() CLASS TOLlama
curl_easy_cleanup( ::hCurl )
::hCurl = nil
return nil
//----------------------------------------------------------------------------//
METHOD GetValue( cHKey ) CLASS TOLlama
local uValue := hb_jsonDecode( ::cResponse )
hb_default( @cHKey, "content" )
if cHKey == "content"
TRY
uValue = uValue[ "message" ][ "content" ]
CATCH
uValue = uValue[ "error" ][ "message" ]
END
endif
return uValue
//----------------------------------------------------------------------------//
METHOD Send( cPrompt ) 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_DL_BUFF_SETUP )
curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
hRequest[ "model" ] = ::cModel
hMessage1[ "role" ] = "user"
hMessage1[ "content" ] = cPrompt
hRequest[ "messages" ] = { hMessage1 }
hRequest[ "stream" ] = .F.
hRequest[ "temperature" ] = 0.5
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
//----------------------------------------------------------------------------//#include "FiveWin.ch"
function Main()
local oChat := TOLlama():New( "deepseek-r1:32b" )
oChat:Send( "tell me the meaning of life" )
? oChat:GetValue()
oChat:End()
return nilollama run deepseek-r1:32b