AI: Using free ollama from Harbour using curl
Posted: Wed Sep 11, 2024 10:11 am
Thanks to Kwon Oh Chul:
Code: Select all | Expand
//EEVE-Korean-10.8B:latest
#include "hbclass.ch"
#define HB_CURLOPT_URL 2
#define HB_CURLOPT_POST 47
#define HB_CURLOPT_POSTFIELDS 15
#define HB_CURLOPT_HTTPHEADER 23
#define HB_CURLOPT_WRITEFUNCTION 11
#define HB_CURLINFO_RESPONSE_CODE 2
#define HB_CURLOPT_DL_BUFF_SETUP 1008
FUNCTION MAIN()
LOCAL oPost
LOCAL cUrl := "http://192.168.2.236:11434/api/chat"
LOCAL hPost := { "model" => "llama3.1:8b", "messages" => { { "role" => "user", "content" => "" } }, "stream" => .F., "temperature"=>0, "format" => "json" }
hPost["messages"][1]["content"] := getText()
hPost["model"] := "llama3.1:8b"
// hPost["model"] := "llama3"
oPost := TDrCURLPost():New( cUrl, hPost )
IF oPost:Execute()
? '<meta charset="utf-8">'
? "Response Code: ", oPost:GetHttpCode()
? "Response: ", oPost:GetResponse()
ELSE
? "Error: ", oPost:GetError()
ENDIF
oPost:Destroy()
RETURN NIL
FUNCTION getText()
LOCAL cText
TEXT INTO cText
What is harbour project for xBase?
ENDTEXT
RETURN cText
CLASS TDrCURLPost
DATA hCurl
DATA cUrl
DATA hPost
DATA cResponse
DATA nHttpCode
DATA cError
METHOD New(cUrl, hPost)
METHOD Execute()
METHOD GetResponse()
METHOD GetHttpCode()
METHOD GetError()
METHOD Destroy()
METHOD BuildPostFields()
ENDCLASS
METHOD New( cUrl, hPost ) CLASS TDrCURLPost
::cUrl := cUrl
::hPost := hPost
::hCurl := curl_easy_init()
RETURN Self
METHOD Execute() CLASS TDrCURLPost
LOCAL nError
LOCAL aHeaders
LOCAL cPostFields
IF ::hCurl == NIL
::cError := "Failed to initialize cURL"
RETURN .F.
ENDIF
cPostFields := ::BuildPostFields()
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_POST, .T.)
curl_easy_setopt(::hCurl, HB_CURLOPT_POSTFIELDS, cPostFields)
curl_easy_setopt(::hCurl, HB_CURLOPT_WRITEFUNCTION, NIL)
curl_easy_setopt(::hCurl, HB_CURLOPT_DL_BUFF_SETUP)
nError := curl_easy_perform(::hCurl)
IF nError == 0
curl_easy_getinfo(::hCurl, HB_CURLINFO_RESPONSE_CODE, @::nHttpCode)
::cResponse := curl_easy_dl_buff_get(::hCurl)
RETURN .T.
ELSE
::cError := "cURL error: " + hb_ntos(nError)
RETURN .F.
ENDIF
RETURN NIL
METHOD GetResponse() CLASS TDrCURLPost
RETURN ::cResponse
METHOD GetHttpCode() CLASS TDrCURLPost
RETURN ::nHttpCode
METHOD GetError() CLASS TDrCURLPost
RETURN ::cError
METHOD Destroy() CLASS TDrCURLPost
IF ::hCurl != NIL
curl_easy_cleanup(::hCurl)
::hCurl := NIL
ENDIF
RETURN NIL
METHOD BuildPostFields() CLASS TDrCURLPost
RETURN hb_jsonEncode(::hPost)