2025-02-03 09:09 UT+0100 Antonio Linares (alinares/at/fivetechsoft.com)

242 views
Skip to first unread message

Antonio Linares

unread,
Feb 3, 2025, 3:13:22 AM2/3/25
to Harbour Developers
2025-02-03 09:09 UT+0100 Antonio Linares (alinares/at/fivetechsoft.com)
  * contrib/hbcurl/core.c
  ! HB_CURLOPT_WRITEFUNCTION implemented support
    Required to use AI Ollama from Harbour so the tokens are visible to the user
    as they are generated

best regards
Antonio 

Antonio Linares

unread,
Feb 3, 2025, 3:16:14 AM2/3/25
to Harbour Developers
Sorry guys if I made some little mistakes

Thank you!

Antonio Linares

unread,
Feb 3, 2025, 3:24:41 AM2/3/25
to Harbour Developers
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

Antonio Linares

unread,
Feb 3, 2025, 4:43:23 AM2/3/25
to Harbour Developers
Here you can see it working (slow because its using deepseek-r1:32b)

Aleksander Czajczynski

unread,
Feb 5, 2025, 9:09:14 AM2/5/25
to harbou...@googlegroups.com
Anotnio,

I'm not super excited by this tech, it's sometimes useful, but can also be pretending to do something useful, resulting only in approximations, stealing your focus. It's even worse if it steals focus of some manager, to tell us humans, we're not useful anymore. Pardon such ranting from me.

It's okay if you put it under /contribs/hbcurl/tests/ai, but IMHO this doesn't add any superpowers to Harbour. Anyway Harbour's code is very straightforward (as in, it mostly does what's written), so the patterns are easier to generate.

Please try to avoid in your samples

curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_VERIFYPEER, .F. )
It's not very good practice, you can use it when connecting to a localhost or test machine, but then others copy it to their production code.
Fresh curl has this set of options, that makes life easier:
curl_easy_setopt( ::hCurl, HB_CURLOPT_SSL_OPTIONS, HB_CURLSSLOPT_NATIVE_CA )
so it gets a list of Valid Certificate Authorites from your OS.

Best regards, Aleksander

Antonio Linares

unread,
Feb 6, 2025, 2:55:04 AM2/6/25
to Harbour Developers
Dear Aleksander,

Thanks for your comments and advice 

best regards

Reply all
Reply to author
Forward
0 new messages