如何使用 LiveCode 应用程序与 chatGPT 交互

问题描述 投票:0回答:1

我想在 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"&quote into tModel 
  put "application/json" into tContentType
  put "json" into tEncode
  put "model: "&tModel&","&cr&"messages: [{role:"&quote&"user"&quote&",content:"&quote&tQuestion&quote&"}]" 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中创建/修复代码,但这也失败了。

openai-api livecode
1个回答
0
投票

这是我为使其与 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("{"&quote&"role"&quote&": "&quote&"[[tRole]]"&quote&", "& \
         quote&"content"&quote&": "&quote&"[[tContent]]"&quote&"}") into tMessage
   
   put merge("{ "&quote&"model"&quote&": "&quote&"[[tModel]]"&quote&","& \
         quote&"messages"&quote&": [ [[tMessage]] ], "&quote&"temperature"& \
         quote&": 1, "&quote&"max_tokens"&quote&": 256, "&quote&"top_p"&quote&": 1, "& \
         quote&"frequency_penalty"&quote&": 0, "&quote&"presence_penalty"&quote&": 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
© www.soinside.com 2019 - 2024. All rights reserved.