Using OpenAI GPT-3 from Harbour
Posted: Tue Aug 25, 2020 7:42 am
Code: Select all
#include "/usr/include/harbour/hbcurl.ch"
function Main()
? gpt3( "time to go there for" )
return nil
function gpt3( cQuery )
local hCurl, cBuffer := ""
local aData := {}
local aResult
local cUrl := "https://api.openai.com/v1/engines/davinci/completions"
curl_global_init()
if ! empty( hCurl := curl_easy_init() )
curl_easy_setopt( hCurl, HB_CURLOPT_URL, cUrl )
curl_easy_setopt( hCurl, HB_CURLOPT_DL_BUFF_SETUP )
curl_easy_setopt( hCurl, HB_CURLOPT_HTTPAUTH, HB_CURLAUTH_BASIC )
curl_easy_setopt( hCurl, HB_CURLOPT_POST, 1 )
curl_easy_setopt( hCurl, HB_CURLOPT_HTTPHEADER, {"Content-Type:application/json"} )
curl_easy_setopt( hCurl, HB_CURLOPT_POSTFIELDS, '{"prompt" : "' + cQuery + '", "temperature": 0, "max_tokens": 30 }' )
curl_easy_setopt( hCurl, HB_CURLOPT_USERPWD, ":sk-fourty_chars_made_of_a-zA-Z0-9" )
if curl_easy_perform( hCurl ) == 0
cBuffer = curl_easy_dl_buff_get( hCurl )
if ! empty( cBuffer )
hb_jsonDecode( cBuffer, @aData )
endif
else
? "error"
endif
endif
curl_global_cleanup()
return If( ! Empty( aData ), aData,)