Openai (ChatGPT) from PB

Share your advanced PureBasic knowledge/code with the community.
User avatar
Caronte3D
Addict
Addict
Posts: 1355
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Openai (ChatGPT) from PB

Post by Caronte3D »

Try to create an APIkey with a free account, I think in the past it was possible (don't know actually).
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Openai (ChatGPT) from PB

Post by ricardo »

jak64 wrote: Mon Aug 28, 2023 12:14 pm Thanks for the info Ricardo.
Have a nice day (even if the Argentinian beat France at football, I'm French) :D
Yoiu are welcome!!

That was a very nice soccer game!!
ARGENTINA WORLD CHAMPION
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Openai (ChatGPT) from PB

Post by jak64 »

Hello Ricardo,
Indeed, good game.
See you soon
User avatar
Fig
Enthusiast
Enthusiast
Posts: 352
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: Openai (ChatGPT) from PB

Post by Fig »

Hi,
I tried it...
How long does it take to get an answer ?

It very very slow... more than 10 minutes !!
Can we get the answer letter by letter like in chatgpt instead of the whole long one ?
There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Openai (ChatGPT) from PB

Post by ricardo »

Fig wrote: Tue Nov 14, 2023 9:22 pm Hi,
I tried it...
How long does it take to get an answer ?

It very very slow... more than 10 minutes !!
Can we get the answer letter by letter like in chatgpt instead of the whole long one ?
}
Now you can connect using

Code: Select all

sModel.s = "gpt-3.5-turbo-1106"
That is fast.
You receive a JSON with the response.

The only problem is that, as happens with ChatGPT, you may receive uncertain information and that often does not follow the prompt instructions to the letter.


You can also use

Code: Select all

sModel = "dall-e-3"
And you will be able to prompt for images. Images are not as good as MidJourney, but its the start for something that may in the future create better images.

In the dall-e-3 model you will receive also a JSON response with the url of the image created.
ARGENTINA WORLD CHAMPION
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Openai (ChatGPT) from PB

Post by ricardo »

Here is the code for generate images.

Code: Select all

          Procedure.s SendPrompt(sPrompt.s)
            sUrl.s = "https://api.openai.com/v1/images/generations"
            
            NewMap Header$()
            Header$("content-type") = "application/json"
            Header$("Authorization") = "Bearer YOURAPIKEY "
            
            nMaxTokens.l = 5000
            sModel.s = "dall-e-3"
            sNumberOfResponses = 1
            
            sTemperature.f = 0.9

            
            sBody.s = "{"
            sBody.s = sBody.s + Chr(34) + "model" + Chr(34) + ": " + Chr(34) + sModel + Chr(34) + ", "
            sBody + Chr(34) + "prompt" + Chr(34) + ": " + Chr(34) + sPrompt + Chr(34) + ", "
            sBody.s + Chr(34) + "n" + Chr(34) + ": " + Str(sNumberOfResponses) + ", "
            sBody + Chr(34) + "quality" + Chr(34) + ": " + Chr(34) + "hd" + Chr(34) + ", "
            sBody + Chr(34) + "size" + Chr(34) + ": " + Chr(34) + "1024x1024" + Chr(34)
            sBody.s + "}"
            
            ; Debug sBody
            

            Define httpReq
            
            httpReq = HTTPRequest(#PB_HTTP_Post, sUrl, sBody, 0, Header$())

            If httpReq

              Response$ = Trim(HTTPInfo(httpReq, #PB_HTTP_Response))
              Debug Response$
            EndIf
            
          EndProcedure
          
          lKeyword$= "Create and hipperrealistic image of a cat in the floor of a beautiful kitchen drinking milk in the sunsent hour HD"
          SendPrompt(lKeyword$)
Image
ARGENTINA WORLD CHAMPION
Post Reply