Page 1 of 1

Nueva Clase TDeepSeek en FWH 24.12

Posted: Sun Jan 12, 2025 6:32 am
by Antonio Linares
El próximo FWH 24.12 incluye la Clase TDeepSeek

DeepSeek es mucho más económico que OpenAI y ofrece la misma calidad en Inteligencia Artificial.
Tienes que regitrarte en la web de DeepSeek y obtener tu clave API. Una vez la tengas desde una ventana cmd haz:
set DEEPSEEK_API_KEY=sk-...

Ejemplo de uso:

Code: Select all | Expand

#include "FiveWin.ch"

function Main()

   local oChat := TDeepSeek():New()
   
   oChat:Send( "Using MySQL how to list all tables ? write just the simplest SQL sentence, no explanations" )

   ? oChat:GetValue()

   oChat:End()

return nil   

Re: Nueva Clase TDeepSeek en FWH 24.12

Posted: Sun Jan 12, 2025 7:54 pm
by Antonio Linares

Code: Select all | Expand

// Based on https://api-docs.deepseek.com
// Remember to register in https://deepseek.com/ and get your API key

#include "FiveWin.ch"
#include "c:\harbour\contrib\hbcurl\hbcurl.ch"

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

CLASS TDeepSeek
    
    DATA   cKey   INIT ""
    DATA   cModel INIT "deepseek-chat"
    DATA   cResponse
    DATA   cUrl
    DATA   hCurl
    DATA   nError INIT 0
    DATA   nHttpCode INIT 0

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

ENDCLASS        

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

METHOD New( cKey, cModel ) CLASS TDeepSeek

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

    if ! Empty( cModel )
       ::cModel = cModel
    endif
    
    ::cUrl = "https://api.deepseek.com/chat/completions"
    ::hCurl = curl_easy_init()
    
return Self    

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

METHOD End() CLASS TDeepSeek

    curl_easy_cleanup( ::hCurl )
    ::hCurl = nil

return nil    

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

METHOD GetValue( cHKey ) CLASS TDeepSeek

    local aKeys := hb_AParams(), cKey
    local uValue := hb_jsonDecode( ::cResponse )

    hb_default( @cHKey, "content" )

    if cHKey == "content"
       TRY 
          uValue = uValue[ "choices" ][ 1 ][ "message" ][ "content" ]
       CATCH
          uValue = uValue[ "error" ][ "message" ]
       END   
    endif

    TRY
       for each cKey in aKeys
          if ValType( uValue[ cKey ] ) == "A"
             uValue = uValue[ cKey ][ 1 ][ "choices" ][ 1 ][ "message" ][ "content" ]
          else
             uValue = uValue[ cKey ]
          endif
       next
    CATCH
       XBrowser( uValue )
    END

return uValue

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

METHOD Send( cPrompt ) CLASS TDeepSeek 

   local aHeaders, cJson, hRequest := { => }, hMessage1 := { => }, hMessage2 := { => }

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

   aHeaders := { "Content-Type: application/json", ;
                 "Authorization: Bearer " + ::cKey }

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

   hRequest[ "model" ] = ::cModel
   hMessage1[ "role" ] = "system"
   hMessage1[ "content" ] = "You are a helpfull assistant."
   hMessage2[ "role" ] = "user"
   hMessage2[ "content" ] = cPrompt
   hRequest[ "messages" ] = { hMessage1, hMessage2 }
   hRequest[ "stream" ] = .F.

   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

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