Page 1 of 2

Openai (ChatGPT) from PB

Posted: Tue Apr 11, 2023 12:13 am
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)


Re: Openai (ChatGPT) from PB

Posted: Tue Apr 11, 2023 11:21 pm
by idle
Thanks for sharing

Re: Openai (ChatGPT) from PB

Posted: Wed Apr 12, 2023 6:27 am
by Cyllceaux

Re: Openai (ChatGPT) from PB

Posted: Sun Aug 27, 2023 8:08 pm
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"
}
}

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 9:58 am
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.

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 10:04 am
by Kurzer
@jak64: As already mentioned here, you have to buy an API Key to use the Open AI API:
https://openai.com/pricing

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 10:17 am
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...

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 10:23 am
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

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 10:30 am
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)

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 10:37 am
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...

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 11:05 am
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.

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 11:13 am
by jak64
Ok, got it, thanks Janni

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 11:17 am
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...

Re: Openai (ChatGPT) from PB

Posted: Mon Aug 28, 2023 11:42 am
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.

Re: Openai (ChatGPT) from PB

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