Using free AI power from our Harbour apps !!!

Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Fri Mar 31, 2023 7:07 pm

Dear friends,

This is the fastest free AI available at the moment (GPT4All):
https://wormhole.app/p4LxB#Do1u8TMntAycXyxfg2X5vA

I do appreciate if you download it and check if the EXE is running for you.

You can also, alternatively, download the same files from the github repo:
https://github.com/nomic-ai/gpt4all/blo ... -win64.exe
https://the-eye.eu/public/AI/models/nom ... ntized.bin (I use an uncensored version in my download)

If you can, I prefeer if you use my files, so we asure we are all using the same files (for the start)

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 great processes functions developed by Przemek!

Next we are going to use it as a process from Harbour:
go.bat
Code: Select all  Expand view
@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
Code: Select all  Expand view
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


We need your help to fine tune it, so we can use our own data from our Harbour apps to query the free AI :-D
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sat Apr 01, 2023 7:21 am

Enhanced version. You can already see the output of the AI from the Harbour app!

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


gpt4all.prg
Code: Select all  Expand view
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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sat Apr 01, 2023 8:38 am

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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sat Apr 01, 2023 9:32 am

random sample from the GPT-J model (that you can use commercially!) we will be releasing tonight tomorrow morning

just readed in discord chat
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Verhoven » Sat Apr 01, 2023 7:28 pm

Verhoven
 
Posts: 521
Joined: Sun Oct 09, 2005 7:23 pm

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sat Apr 01, 2023 7:54 pm

el navegador opera permite usar un vpn gratuito, por lo que es tan sencillo como elegir otro pais...
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sat Apr 01, 2023 11:00 pm

Versión funcionando !!!

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


gpt4all.prg
Code: Select all  Expand view
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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sun Apr 02, 2023 12:18 am

Enhanced version:

gpt4all.prg
Code: Select all  Expand view
function Main()

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

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

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

   ?? RemoveANSIEscapeCodes( AllTrim( cBuffer ) ), "What is a galaxy ?"
   ?

   FWrite( hStdIn, "What is a galaxy ?" + hb_Eol() )

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

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

   while .t.
      cQuestion = ""
      while cChar != Chr( 13 )
         cChar = InKey( 0 )
         cQuestion += cChar
      end  
      if Empty( cQuestion )
         exit
      endif    

      FWrite( hStdIn, cQuestion + hb_Eol() )

      Buffer = "x"
      while ! Empty( cBuffer )  
         cBuffer = Space( nSize )
         nReaded = 0
         while ( nReaded += hb_PRead( hStdOut, @cBuffer, nSize, 500 ) ) == 0
         end

      ?? RemoveANSIEscapeCodes( SubStr( cBuffer, 1, nReaded ) )
      end  
   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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sun Apr 02, 2023 7:49 am

Trying to converting it into a Class. Not working yet:

gpt4all.prg
Code: Select all  Expand view
#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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sun Apr 02, 2023 9:02 am

Enhanced Class version, still we need some fine tunning

gpt4all.prg
Code: Select all  Expand view
#include "hbclass.ch"
#include "FileIO.ch"

function Main()

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

   oAI:Read()
   ?? "Which is the largest city in the world ?"
   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 nTotal := 0, nReaded := 1, cBuffer, cResult, nTries

   cBuffer = "x"

   while nReaded > 0
      cBuffer = Space( BUFFER_SIZE )
      nReaded = 0
      nTries = 0
      while ( nReaded += hb_PRead( ::hStdOut, @cBuffer, BUFFER_SIZE, 1000 ) ) == 0
         if ++nTries > 10
            exit
         endif  
      end

      ?? RemoveANSIEscapeCodes( SubStr( cBuffer, 1, nReaded ) )
      ::Log( 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sun Apr 02, 2023 2:30 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Sun Apr 02, 2023 2:57 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby anserkk » Mon Apr 03, 2023 4:39 am

Will this work with Borland C or just with Microsoft C ONLY ?
User avatar
anserkk
 
Posts: 1332
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Mon Apr 03, 2023 6:06 am

Dear Anser,

It must be a 64 bits EXE only thats why we prefer to use MSVC

The process to create is a 64 bits process, thats why our app must be a 64 bits one
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Using free AI power from our Harbour apps !!!

Postby Antonio Linares » Mon Apr 03, 2023 10:25 am

Look how simple is the new code of gpt4all.prg: You can query the oAI without user intervention too :-)
Code: Select all  Expand view
#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  
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42099
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: betoncu and 51 guests