Using free AI power from our Harbour apps !!!

537 views
Skip to first unread message

Antonio Linares

unread,
Mar 31, 2023, 3:49:39 PM3/31/23
to Harbour Users
Dear friends,

This is the fastest free AI available at the moment (GPT4All):

I do appreciate if you download it and check if the EXE is running for you.
Its over 3 gigs. Be patient, it will take time :-)

You can also download the same included files from github directly:

I wanted to share mines just to asure that we all test the same EXE :-)

This morning meanwhile I was testing and helping on some python code of GPT4All dev team I realized (I saw and debugged the code) that they just were creating a process with the EXE and routing stdin and stdout, so I thought it is a perfect ocassion to use the geat processes functions developed by Prezmek! 

So here you have a very early gpt4all.prg to do it. IT DOES NOT WORK YET but I guess
you may want to participate in this amazing adventure.

go.bat
@setlocal
@call "%ProgramFiles%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
c:\harbour\bin\win\msvc64\hbmk2 gpt4all.prg -comp=msvc64
gpt4all.exe
@endlocal

gpt4all.prg (we only reach to show the prompt ">" routed from GPT4All EXE)
function Main()

   local hProcess, hStdIn, hStdOut, hStdErr, cBuffer, nSize := 1024, cMsg := ""

   hProcess = hb_processOpen( "gpt4all-lora-quantized-win64.exe", @hStdIn, @hStdOut, @hStdErr, .t. )
   ? "GPT4All running..."

   cBuffer = Space( 1024 )
   while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
      InKey( 0.1 )
   end
   ? nReaded, AllTrim( cBuffer ), "Hi my friend, how are you ?"

   FWrite( hStdIn, "Hi my friend, how are you ?" + Chr( 13 ) )  

   while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
      InKey( 0.1 )
   end
   ? nReaded, AllTrim( cBuffer )

   while .t.
      cBuffer = Space( nSize )
      nReaded = hb_PRead( hStdOut, @cBuffer, nSize, 500 )

      if nReaded > 0
         cMsg += Left( cBuffer, nReaded )
      endif

      if nReaded < 0  
         exit  
      endif  
   end

   ? cMsg

   hb_processClose( hProcess, .t. )
   FClose( hStdIn )
   FClose( hStdOut )
   FClose( hStdErr )
   
return nil

You may take the data of your DBFs, SQL values, external PDFs,
your email account, github repos, etc... and connect that data with your local
and free GPT4All EXE and make any query you or your user may need. This concept
is named "langChain" as it is the foundation of the chatGPT plugins.

Think "langChain" as "senses" for the "artificial brain" :-) This is being developed now
for GPT4All too. So we may have all that power quite soon. In the meantime we should
be able to send large queries to GPT4All. If you want to submit another line, end your input in '\' (explained by GPT4All)

I assume you may want to participate in this amazing Harbour new adventure and I do appreciate your time and cooperation.

best regards

Antonio Linares

unread,
Apr 1, 2023, 1:58:15 AM4/1/23
to Harbour Users
Enhanced gpt4all.prg version (still buggy)

function Main()

   local hProcess, hStdIn, hStdOut, hStdErr, cBuffer, nSize := 1024

   hProcess = hb_processOpen( "gpt4all-lora-quantized-win64.exe", @hStdIn, @hStdOut, @hStdErr, .T. )

   ? "GPT4All running..."

   cBuffer = Space( 1024 )
   while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
      InKey( 0.1 )
   end
   ? RemoveANSIEscapeCodes( AllTrim( cBuffer ) ), "Hi my friend, how are you ?"
   ?

   FWrite( hStdIn, "Hi my friend, how are you ?" + hb_Eol() )

   while ! Empty( cBuffer )  
      while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
         InKey( 0.1 )
      end

      ?? RemoveANSIEscapeCodes( AllTrim( cBuffer ) )      
   end  

   hb_processClose( hProcess, .t. )
   FClose( hStdIn )
   FClose( hStdOut )
   FClose( hStdErr )

   InKey( 0 )

return nil

function RemoveANSIEscapeCodes( cString )
    local cCleanString := ""
    local lInEscapeSeq := .F.
    local cEscapeChars := Chr( 27 ) + "["
    local cEndChars := "mK", n

    for n := 1 to Len( cString )
        cChar := Substr( cString, n, 1 )
        if lInEscapeSeq
            if At( cChar, cEndChars ) > 0 .or. ( cChar == "0" .and. Right( cCleanString, 1 ) == Chr( 27 ) )
                lInEscapeSeq := .F.
            endif
        elseif At( cChar, cEscapeChars ) > 0
            lInEscapeSeq := .T.
        else
            cCleanString += cChar
        endif
    next

return cCleanString

best regards

Antonio Linares

unread,
Apr 1, 2023, 3:12:22 AM4/1/23
to Harbour Users
Enhanced version (still buggy)

function Main()

   local hProcess, hStdIn, hStdOut, hStdErr, cBuffer, nSize := 1024

   hProcess = hb_processOpen( "gpt4all-lora-quantized-win64.exe", @hStdIn, @hStdOut, @hStdErr, .T. )
   ? "GPT4All running..."

   cBuffer = Space( 1024 )
   while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
      InKey( 0.1 )
   end

   ?? RemoveANSIEscapeCodes( AllTrim( cBuffer ) ), "Hi my friend, how are you ?"

   ?

   FWrite( hStdIn, "Hi my friend, how are you ?" + hb_Eol() )

   while ! Empty( cBuffer )  
      while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
         InKey( 0.1 )
      end

      ?? RemoveANSIEscapeCodes( AllTrim( cBuffer ) )
      Alert( RemoveANSIEscapeCodes( AllTrim( cBuffer ) ) )

   end  

   hb_processClose( hProcess, .t. )
   FClose( hStdIn )
   FClose( hStdOut )
   FClose( hStdErr )

   InKey( 0 )

return nil

function RemoveANSIEscapeCodes( cString )

   local cCleanString := "", n

   for n := 1 TO Len( cString )
      cChar := SubStr( cString, n, 1 )
      if cChar == Chr( 27 )
         while cChar != "m"
            n++
            cChar = SubStr( cString, n, 1 )
         end      
         cChar = ""
      endif  

      cCleanString := cCleanString + cChar
   next

return cCleanString

Antonio Linares

unread,
Apr 1, 2023, 4:38:00 AM4/1/23
to Harbour Users
I just read this in the discord chat for GPT4All:

gpt4all is much better than llama 7b (facebook data) because it's been fine tuned. That's the whole point

Regardless of all this, be aware that the llama models are released under a non-commercial license, so you are not meant to ever use them (or any models derived from them) in any commercial production setting

best regards

Antonio Linares

unread,
Apr 1, 2023, 5:36:42 AM4/1/23
to Harbour Users
random sample from the GPT-J model (that you can use commercially!) we will be releasing tonight tomorrow morning

just readed in discord chat

Antonio Linares

unread,
Apr 1, 2023, 6:59:46 PM4/1/23
to Harbour Users
Working version!

I don't understand why it can't close/kill the process. Any hints are apreciated.

GPT4All running...

> Hi my friend, how are you ?
I ' m doing great thanks for asking ! How about yourself ?
>

gpt4all.prg
function Main()

   local hProcess, hStdIn, hStdOut, hStdErr, cBuffer, nSize := 1024

   hProcess = hb_processOpen( "gpt4all-lora-quantized-win64.exe", @hStdIn, @hStdOut, @hStdErr, .F. )
   ? "GPT4All running..."

   cBuffer = Space( nSize )

   while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
      InKey( 0.1 )
   end

   ?? RemoveANSIEscapeCodes( AllTrim( cBuffer ) ), "Hi my friend, how are you ?"
   ?

   FWrite( hStdIn, "Hi my friend, how are you ?" + hb_Eol() )

   while ! Empty( cBuffer )  
      cBuffer = Space( nSize )
      while ( nReaded := hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
      end

      ?? RemoveANSIEscapeCodes( AllTrim( cBuffer ) )
      ?? " "
   end  

   if ! hb_processClose( hProcess, .T. )
      ? "can't close GPT4All process..."
   end

   FClose( hStdIn )
   FClose( hStdOut )
   FClose( hStdErr )

return nil

function RemoveANSIEscapeCodes( cString )

   local cCleanString := "", n

   for n := 1 TO Len( cString )
      cChar := SubStr( cString, n, 1 )
      if cChar == Chr( 27 )
         while cChar != "m"
            n++
            cChar = SubStr( cString, n, 1 )
         end      
         cChar = ""
      endif  

      cCleanString := cCleanString + cChar
   next

return cCleanString

Antonio Linares

unread,
Apr 1, 2023, 8:20:39 PM4/1/23
to Harbour Users

Antonio Linares

unread,
Apr 2, 2023, 3:51:47 AM4/2/23
to Harbour Users
Converting it into a Class. Not working yet (previous version works fine)

#include "hbclass.ch"
#include "FileIO.ch"

function Main()

   local dummy := QOut( "GPT4All running..." )
   local oAI := GPT4All():New()

   oAI:Read()
   oAI:Write( "Which is the largest city in the world ?" )
   // oAI:Read()

   oAI:End()

return nil  

#define BUFFER_SIZE  1024
#define LOGFILE_NAME "GPT4All.log"

CLASS GPT4All

   DATA   hProcess
   DATA   hStdIn, hStdOut, hStdErr

   METHOD New()
   METHOD Write( cText ) INLINE ::Log( "write: " + cText ), FWrite( ::hStdIn, cText + hb_eol() )
   METHOD Read()
   METHOD End()
   METHOD Log( cMsg )

ENDCLASS  

METHOD New( cPrompt ) CLASS GPT4All

   local hStdIn, hStdOut

   ::hProcess = hb_processOpen( "gpt4all-lora-quantized-win64.exe", @hStdIn, @hStdOut, @::hStdErr )

   ::hStdIn = hStdIn
   ::hStdOut = hStdOut

   FErase( LOGFILE_NAME )
   ::Log( "process: " + AllTrim( Str( ::hProcess ) ) )
   ::Log( "stdin: " + AllTrim( Str( ::hStdIn ) ) )
   ::Log( "stdout: " + AllTrim( Str( ::hStdOut ) ) )

return Self

METHOD Read() CLASS GPT4All

   local nReaded, cBuffer, cResult

   cBuffer = "x"


   while ! Empty( cBuffer )  
      cBuffer = Space( BUFFER_SIZE )
      nReaded = 0
      while ( nReaded += hb_PRead( ::hStdOut, @cBuffer, BUFFER_SIZE, 1000 ) ) == 0
      end

      ?? RemoveANSIEscapeCodes( SubStr( cBuffer, 1, nReaded ) )
   end

   ::Log( "after read" )

return

METHOD End() CLASS GPT4All

   local nResult := hb_processClose( ::hProcess )


   FClose( ::hStdIn )
   FClose( ::hStdOut )
   FClose( ::hStdErr )

   ::Log( "End" )

return nResult  

METHOD Log( cMsg ) CLASS GPT4All

   local hLogFile

   if ! File( LOGFILE_NAME )
      FClose( FCreate( LOGFILE_NAME ) )
   endif      

   if( ( hLogFile := FOpen( LOGFILE_NAME, FO_WRITE ) ) != -1 )
      FSeek( hLogFile, 0, FS_END )
      FWrite( hLogFile, cMsg + hb_eol(), Len( cMsg ) + Len( hb_eol() ) )
      FClose( hLogFile )
   endif


return nil  

function RemoveANSIEscapeCodes( cString )

   local cCleanString := "", n

   for n := 1 TO Len( cString )
      cChar := SubStr( cString, n, 1 )
      if cChar == Chr( 27 )
         while cChar != "m"
            n++
            cChar = SubStr( cString, n, 1 )
         end      
         cChar = ""
      endif  

      cCleanString := cCleanString + cChar
   next

return cCleanString

Antonio Linares

unread,
Apr 2, 2023, 5:22:07 AM4/2/23
to Harbour Users
Already working fine as a Class. There is a delay after the prompt ">" is shown that is a bug in my code. Hopefully we solve it soon :-)
The response speed is really good :-)

Antonio Linares

unread,
Apr 2, 2023, 10:35:27 AM4/2/23
to Harbour Users

José M. C. Quintas

unread,
Apr 2, 2023, 12:08:25 PM4/2/23
to harbou...@googlegroups.com


What about it?


José M. C. Quintas

--
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: https://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/c3d5bdd2-0162-4b9a-b79d-ac2ba346800dn%40googlegroups.com.

Antonio Linares

unread,
Apr 3, 2023, 2:01:12 AM4/3/23
to Harbour Users
Dear Jose,

Try cloning the repo to download it:

best regards,

Message has been deleted

Antonio Linares

unread,
Apr 3, 2023, 6:15:57 AM4/3/23
to Harbour Users
Modified demo code to work in a loop where the user can write what he wants. The Harbour app can talk to the AI without user intervention too.

#include "hbclass.ch"
#include "FileIO.ch"

function Main()

   local dummy := QOut( "Loading GPT4All... cpu speed: " + AllTrim( CPUSpeed() ),;
                        If( ! CpuHasAvx2(), "not", "" ) + " AVX2 support. Type exit to finish" )
   local oAI := GPT4All():New(), cMsg

   oAI:Read()
   while ! Empty( cMsg := oAI:Input() )
      if cMsg != "exit"
         oAI:Write( cMsg )
         oAI:Read()
      else
         exit
      end      
   end  

   oAI:End()

return nil  

repo updated

best regards

Antonio Linares

unread,
Apr 4, 2023, 5:47:37 AM4/4/23
to Harbour Users
You need at least 6 gigs (better 8) of RAM and a CPU with AVX2 support, so the IA engine works fine

alkresin

unread,
Apr 14, 2023, 6:38:28 AM4/14/23
to Harbour Users
Antonio,
  thanks for sharing this stuff.

 Personally I do not believe in the fundamental possibility of creating a so-called strong ai, at least, with the help of existing technologies and computational methods based on a Turing machine, but, IMO, trying to create such intelligence is the only task truly deserving of a programmer-researcher.
And, of course, for a lot of tasks, imitation of  the intelligence gives a satisfactory result.

Regards, Alexander.
вторник, 4 апреля 2023 г. в 12:47:37 UTC+3, Antonio Linares:
Reply all
Reply to author
Forward
0 new messages