IA with LLM https://console.groq.com

Post Reply
csincuir
Posts: 419
Joined: Sat Feb 03, 2007 6:36 am
Location: Guatemala
Has thanked: 3 times
Been thanked: 4 times
Contact:

IA with LLM https://console.groq.com

Post by csincuir »

Hello,
For those who want to work with this other LLM https://console.groq.com/login, which provides APIs to work for free, I copy the class I made following the base of the TGemini class of FWH25.01 made by Antonio:

Code: Select all | Expand

//----------------------------------------------------------------------------//
// Clase: TGroq
// Desarrollado por: Carlos Sincuir
// Version FWH: 25.01
// Obtenga su API key desde https://console.groq.com/login
//----------------------------------------------------------------------------//
#include "FiveWin.ch"
#include "hbcurl.ch"

function Main()

   local oChat := TGroq():New("tuApiKey")

   oChat:Send( "Hola, quien eres ?" )
   ? oChat:GetValue() 

   oChat:End()

return nil   

//----------------------------------------------------------------------------//

CLASS TGroq

   DATA   cKey   INIT ""
   DATA   cModel INIT "llama3-70b-8192" 
   DATA   cResponse
   DATA   cUrl   INIT "" 
   DATA   cUploadUrl INIT "" 
   DATA   hCurl
   DATA   nError INIT 0
   DATA   nHttpCode INIT 0
   DATA   nTemperature INIT 0

   METHOD New( cKey, cModel )
   METHOD Send( cPrompt )
   METHOD End()
   METHOD GetValue()

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( cKey, cModel ) CLASS TGroq

   if Empty( cKey )
      ::cKey = GetEnv( "GROQ_API_KEY" )
   else
      ::cKey = cKey
   endif

   if ! Empty( cModel )
      ::cModel = cModel
   endif

   if Val( SubStr( Curl_Version_Info()[ 1 ], 1, RAt( ".", Curl_Version_Info()[ 1 ] ) - 1 ) ) - 8.10 > 0.2
      MsgAlert( "Please use an updated curl DLL" )
   endif    
   
   ::cUrl = "https://api.groq.com/openai/v1/chat/completions"
   ::hCurl = curl_easy_init()

return Self

//----------------------------------------------------------------------------//

METHOD End() CLASS TGroq

   curl_easy_cleanup( ::hCurl )
   ::hCurl = nil

return nil

//----------------------------------------------------------------------------//

METHOD GetValue() CLASS TGroq

   local uValue, hResponse

   if ! Empty( ::cResponse )
      uValue = hb_jsonDecode( ::cResponse, @hResponse )
   endif

   if hb_isHash( hResponse )
      TRY
         uValue = hResponse[ "choices" ][ 1 ][ "message" ][ "content" ]
      CATCH
         TRY
            uValue = hResponse[ "error" ][ "message" ]
         CATCH
            uValue = "Error, no response"
         END
      END
   endif

return uValue

//----------------------------------------------------------------------------//
METHOD Send( cPrompt ) CLASS TGroq

   local aHeaders, cJson, hRequest, hMessages

   curl_easy_setopt( ::hCurl, HB_CURLOPT_POST, .T. )
   curl_easy_setopt( ::hCurl, HB_CURLOPT_URL, ::cUrl )

   aHeaders := { "Content-Type: application/json",;
                 "Authorization: Bearer " + ::cKey }  // Tu API key de Groq

   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. )

   // Estructura del JSON para Groq
   hMessages := { { "role" => "user", "content" => cPrompt } }
   hRequest := { "model" => ::cModel,;  
                 "temperature" => ::nTemperature,;
                 "messages" => hMessages }

   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

//----------------------------------------------------------------------------//
At the moment this LLM does not allow sending documents, but at least it is an option to test AI without having to pay for the API Keys

I hope it helps you.

Best regards.

Carlos Sincuir
User avatar
Antonio Linares
Site Admin
Posts: 42716
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 93 times
Been thanked: 103 times
Contact:

Re: IA with LLM https://console.groq.com

Post by Antonio Linares »

Dear Carlos,

very good! :)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply