我想在 LiveCode 中创建一个应用程序,它(通过单击按钮)向 chatGPT 提交问题并将答案放入名为“Response”的字段中。按钮的代码如下:
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
chatGPT 的答案(显示在“响应”字段中)如下:“我们无法解析您请求的 JSON 正文。(提示:这可能意味着您没有正确使用 HTTP 库。OpenAI API 期望JSON 负载,但发送的不是有效的 JSON。”
我要求chatGPT在LiveCode中创建/修复代码,但这也失败了。
这是我为使其与 Livecode 一起使用而编写的代码。我希望这对您的发展有所帮助。
由于我有多个组织,所以我必须添加 OpenAI-Organization,但您可以备注它
您将需要两个字段(问题和答案)
JG
on mouseUp pMouseButton
put "https://api.openai.com/v1/chat/completions" into tURL
put "XXXXXXXXXXXXXXXXXXXXXX" into tKey
put "Content-Type: application/json;charset=utf-8" & return into tHeader
put "Authorization: Bearer " & tKey & return after tHeader
--put "OpenAI-Organization: XXXXXXXXXXXXXXXXX" & return after tHeader
set the httpHeaders to tHeader
put "gpt-3.5-turbo" into tModel
put "user" into tRole
put fld"Question" into tContent
put merge("{""e&"role""e&": ""e&"[[tRole]]""e&", "& \
quote&"content""e&": ""e&"[[tContent]]""e&"}") into tMessage
put merge("{ ""e&"model""e&": ""e&"[[tModel]]""e&","& \
quote&"messages""e&": [ [[tMessage]] ], ""e&"temperature"& \
quote&": 1, ""e&"max_tokens""e&": 256, ""e&"top_p""e&": 1, "& \
quote&"frequency_penalty""e&": 0, ""e&"presence_penalty""e&": 0 }") into tPayload
post tPayload to url tURL
put textDecode(it,"UTF-8") into it
put jsonimport(it) into tArray
put tArray["choices"][1]["message"]["content"] into BotAnswer
put BotAnswer into fld"answer"
end mouseUp