I want to create an app in LiveCode, which (by clicking on a button) submits a question to chatGPT and puts the answer into a field called "Response". The code of the button is the following:
on mouseUp
put "<my personal API key>" into api_key
put "2 + x = 10, what is x?" into tQuestion
put "https://api.openai.com/v1/chat/completions" into tURL
put "Bearer" && api_key into tAuthorization
put quote&"gpt-3.5-turbo""e into tModel
put "application/json" into tContentType
put "json" into tEncode
put "model: "&tModel&","&cr&"messages: [{role:""e&"user""e&",content:""e&tQuestion"e&"}]" into tBody
set the httpHeaders to "Authorization:" && tAuthorization && cr & "Content-Type:" && tContentType
post tBody to tURL
put it into field "Response"
end mouseUp
The answer of chatGPT (shown in field "Response") is the following: "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON."
I asked chatGPT to create/fix the code in LiveCode, but this also failed.
Here is the code I did to make it work with Livecode. I hope this helps you with your development.
Since I have multi-Organizations I had to add the OpenAI-Organization, but you can remark it
You will need two Fields (Question and Answer)
JG