IA con 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 con https://console.groq.com

Post by csincuir »

Hola,
Para los que quieran trabajar con este otro LLM https://console.groq.com/login, que proporciona API's para trabajar de manera gratuita, les copio la clase que hice siguiendo la base de la clase TGemini de FWH25.01 hecha por 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

//----------------------------------------------------------------------------//
Por el momento este LLM no permite enviar documentos, pero al menos es una opcion para proabar la IA sin necesidad de pagar por las API Key's

Espero les sirva.

Saludos cordiales.

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 con https://console.groq.com

Post by Antonio Linares »

Estimado Carlos,

Muy bien! :)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply