new Class TDeepSeek in next FWH 24.12

Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

new Class TDeepSeek in next FWH 24.12

Post by Antonio Linares »

Next FWH 24.12 includes Class TDeepSeek

DeepSeek is much cheaper than OpenAI and it offers the same AI quality.
You have to register in DeepSeek site and get your API key. Then from a cmd window do:
set DEEPSEEK_API_KEY=sk-...

sample of use:

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: new Class TDeepSeek in next FWH 24.12

Post 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

//----------------------------------------------------------------------------//
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Ari
Posts: 239
Joined: Fri Feb 03, 2006 4:21 pm
Location: São Paulo, SP - Brazil
Contact:

Re: new Class TDeepSeek in next FWH 24.12

Post by Ari »

Antonio,

Gracias por compartir TDeepSeek.

Me gustaría obtener una Light para comunicarme con la API REST con certificado y CURL. según el código a continuación. ¿Tienes alguna experiencia sobre cómo hacerlo?

Code: Select all | Expand

#include "fivewin.ch"
#include "hbcurl.ch"

* ========================================================================
Function Teste()
* ========================================================================
  local cURL := "https://cfqasvir.it-cpi019-rt.cfapps.us10-002.hana.ondemand.com/http/PurchaseOrder/v1?CodeERP=4510160192"
  local cCurl
  local nError
  local cResponse

  cCurl := curl_easy_init()
  curl_easy_setopt( cCurl, HB_CURLOPT_PROTOCOLS, HB_CURLPROTO_HTTPS)
  curl_easy_setopt( cCurl, HB_CURLOPT_URL                   , cURL       )
  curl_easy_setopt( cCurl, HB_CURLOPT_DL_BUFF_SETUP                )  
  curl_easy_setopt( cCurl, HB_CURLOPT_HTTPHEADER      , {"Content-Type: application/json"})                                                         
  curl_easy_setopt( cCurl, HB_CURLOPT_CUSTOMREQUEST   ,'GET'       )
  curl_easy_setopt( cCurl, HB_CURLOPT_SSLCERTPASSWD   , "Key_fornec_Int_69133353"                    )
  curl_easy_setopt( cCurl, HB_CURLOPT_SSLCERT         , "C:\sis\fsql901\Key_fornec_Int_69133353.pem" )
  curl_easy_setopt( cCurl, HB_CURLOPT_SSLKEY          , "C:\sis\fsql901\Key_fornec_Int_69133353.key" )
   
  nError := curl_easy_perform( cCurl )
    
  if nError == HB_CURLE_OK
     cResponse := curl_easy_dl_buff_get( cCurl )
  else
     cResponse := "Error code: " + curl_easy_strerror(nError)
  endif
    
  Msg( cResponse )
  
  curl_easy_cleanup(cCurl)

return nil

resposta : Status 401 não autorized
Thanks,
Ari

FWH 2212 - Harbour 3.2.0 - Embarcadero 7.43 - MySQL
São Paulo - SP - Brasil
www.sisrev.com.br
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: new Class TDeepSeek in next FWH 24.12

Post by Antonio Linares »

Has probado a preguntarle a chatgpt ?

En _ preguntar a varias IAs: chatgpt, claude, copilot, deepseek, es la mejor opción
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Ari
Posts: 239
Joined: Fri Feb 03, 2006 4:21 pm
Location: São Paulo, SP - Brazil
Contact:

Re: new Class TDeepSeek in next FWH 24.12

Post by Ari »

Agradecido,

¿Preguntaré?
Thanks,
Ari

FWH 2212 - Harbour 3.2.0 - Embarcadero 7.43 - MySQL
São Paulo - SP - Brasil
www.sisrev.com.br
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: new Class TDeepSeek in next FWH 24.12

Post by Antonio Linares »

Yesterday 2025-01-20 DeekSeek published a new model much more powerfull: DeepSeek R1

To use it from FWH Class TDeepSeek we have to specify model="deepseek-reasoner"

By default we are using

DATA cModel INIT "deepseek-chat"

so in order to use it we have to change this:

DATA cModel INIT "deepseek-reasoner"

Prices: (the cheapest of all and the best AI)
💰 $0.14 / million input tokens (cache hit)
💰 $0.55 / million input tokens (cache miss)
💰 $2.19 / million output tokens
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: new Class TDeepSeek in next FWH 24.12

Post by Antonio Linares »

https://platform.deepseek.com/ to pay five USD using PayPal
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply