Openai (ChatGPT) from PB

Share your advanced PureBasic knowledge/code with the community.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Openai (ChatGPT) from PB

Post by ricardo »

Hi,

This is just a very simple way to use the well know Openai API from within PB.

Code: Select all

Procedure SendPrompt(sPrompt.s)
  
  sUrl. s ="https://api.openai.com/v1/chat/completions"
  
  NewMap Header$()
  Header$("content-type") = "application/json"
  Header$("Authorization") = "Bearer [API KEY]"
  
 
  nMaxTokens.l = 3500
  sModel.s = "gpt-3.5-turbo"
  sNumberOfResponses = 1
  
  sTemperature.f =0.7
  
  
  sBody.s = "{"
  sBody.s = sBody.s + Chr(34) + "max_tokens" + Chr(34) + ": " + Str(nMaxTokens) + ", "
  sBody.s = sBody.s + Chr(34) + "model" + Chr(34) + ": " + Chr(34) + sModel + Chr(34) + ", "
  sBody.s = sBody.s + Chr(34) + "temperature" + Chr(34) + ": "  + Str(sTemperature) + ", "
  sBody.s + Chr(34) + "n" + Chr(34) + ": " + Str(sNumberOfResponses) + ", "
  sBody.s = sBody.s + Chr(34) + "messages" + Chr(34) + ": [{" +  Chr(34) + "role" + Chr(34) + ": " + Chr(34) + "user" + Chr(34) + ", " + Chr(34) + "content" + Chr(34) + ": " + Chr(34) +  sPrompt + Chr(34) + "}]"
  sBody.s + "}"
  
  Debug sBody
  
  ; Realizar la solicitud a la API de OpenAI
  Define httpReq
  httpReq = HTTPRequest(#PB_HTTP_Post, sUrl, sBody, 0, Header$())
  
  If httpReq
    Debug "StatusCode: " + HTTPInfo(httpReq, #PB_HTTP_StatusCode)
    
    Response$ = HTTPInfo(httpReq, #PB_HTTP_Response)
    Debug "Response: " + Response$

    FinishHTTP(httpReq)
  Else
    Debug "Request creation failed"
  EndIf
  
  
EndProcedure



sPrompt.s = "please write an article about purebasic"

SendPrompt(sPrompt)

ARGENTINA WORLD CHAMPION
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Openai (ChatGPT) from PB

Post by idle »

Thanks for sharing
Cyllceaux
Enthusiast
Enthusiast
Posts: 510
Joined: Mon Jun 23, 2014 1:18 pm

Re: Openai (ChatGPT) from PB

Post by Cyllceaux »

jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Openai (ChatGPT) from PB

Post by jak64 »

Thank,
I tested, i have error message :

{"max_tokens": 3500, "model": "gpt-3.5-turbo", "temperature": 1, "n": 1, "messages": [{"role": "user", "content": "please write an article about purebasic"}]}
StatusCode: 401
Response: {
"error": {
"message": "Incorrect API key provided: [API KEY]. You can find your API key at https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
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 »

I haven't tested this code, but by that error message, your APIkey is missing or incorrect.
You must use your own APIKey to access ChatGPT.
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 670
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Openai (ChatGPT) from PB

Post by Kurzer »

@jak64: As already mentioned here, you have to buy an API Key to use the Open AI API:
https://openai.com/pricing
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
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 Kurzer,
Thank you for your answer, I understood but I wanted to have a free solution using ChatGPT 3.5 which is free.

Question :
Can't we simply with Purebasic:

1) Open ChatGPT 3.5 website (https://chat.openai.com/)

2) Send him a query (example, "what is the capital of the USA?")

3) Get his answer in a text variable?

So this is not possible without going through an API key?

On other sites, all you have to do is save the source code of the WEB page and then search in this file to find what you want to retrieve...
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 670
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Openai (ChatGPT) from PB

Post by Kurzer »

No.

Already point 1) does not work. the PB web gadget is outdated and cannot display the Open AI page correctly (at least on my computers - Win 7 to Win 11).

Try it for yourself:

Code: Select all

  If OpenWindow(0, 0, 0, 1000, 800, "WebGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
    WebGadget(0, 10, 10, 980, 780, "https://chat.openai.com/") 
    Repeat 
    Until WaitWindowEvent() = #PB_Event_CloseWindow 
  EndIf
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Openai (ChatGPT) from PB

Post by jak64 »

Thanks Kurzer,
Isn't there another way without using the WEB gadget?

In one of my programs, I use the instructions:

url="http:\\.........................................."
RunProgram(url,"","",0)
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Openai (ChatGPT) from PB

Post by jak64 »

HI,
This little program does open the ChatGPT site.

Code: Select all

Global url.s
url="https://chat.openai.com/"
RunProgram(url,"","",0)
On the other hand, I don't know how to send the request in the "Send a message" field or how to retrieve the response in a variable...
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: Openai (ChatGPT) from PB

Post by Janni »

Hi jak64,

It really sounds like using the ChatGPT API is the better way for this.

Elements to learn.
ChatGPT: API documentation (and api key)
PureBasic: http request
PureBasic: response in json
PureBasic: fetch whatever from json and write to file.
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Openai (ChatGPT) from PB

Post by jak64 »

Ok, got it, thanks Janni
jak64
Enthusiast
Enthusiast
Posts: 619
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Openai (ChatGPT) from PB

Post by jak64 »

Actually, I wanted to use ChatGPT 3.5 to program a little MCQ like game. I tested ChatGPT (manually), asking it for MCQs of different levels (easy, medium, hard) and it worked very well.
The questions would change each time, so the game would have an infinite lifespan...
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 10:17 am
Thank you for your answer, I understood but I wanted to have a free solution using ChatGPT 3.5 which is free.

Question :
Can't we simply with Purebasic:

1) Open ChatGPT 3.5 website (https://chat.openai.com/)

2) Send him a query (example, "what is the capital of the USA?")

3) Get his answer in a text variable?
Using ChatGPT would be really complicated. Instead, use the Openai API (the creators of ChatGPT), it is possible with the example that I showed, but you must pay.

Using the Openai API, you only pay for what you use, it is not a monthly subscription and the use is really very cheap (dollar cents), unless the use is intensive.
The best thing is to monitor what is being spent, which can be done manually on the openai site.
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 »

Thanks for the info Ricardo.
Have a nice day (even if the Argentinian beat France at football, I'm French) :D
Post Reply