gpt支持json格式的数据返回(response_format: ‘json_object‘)

      Api.h5
        .chatCreateChatCompletion({
          model: 'gpt-3.5-turbo-1106',
          token: 'sk-f4fe8b67-fcbe-46fd-8cc9-fd1dac5d6d59',
          messages: [
            {
              role: 'user',
              content:
                '使用json格式返回十二生肖,包含中文名和英文名,[{id:"1", enName:"", cnName: ""}]',
            },
          ],
          params: {
            n: 1,
            response_format: { type: 'json_object' },
          },
        })
        .then((res) => {
          if (res.code === 200) {
            console.log(res)
            let content = res.data.completion.choices[0].message.content
            console.log(content)
            try {
              content = JSON.parse(content)
              console.log(content)
            } catch (error) {
              console.log(error)
            }
          }
        })
    }

gpt支持json格式的数据返回(response_format: ‘json_object‘)_第1张图片

接口地址

https://chat.xutongbao.top/api/light/chat/createChatCompletion

请求方式

post

Token获取方式

访问:https://chat.xutongbao.top/

使用邮箱注册账号

点击【我的】

点击【API】

model可选值

“gpt-3.5-turbo-1106”、 “gpt-3.5-turbo-16k” 、 “gpt-4”、“gpt-4-1106-preview”。 默认值为: “gpt-3.5-turbo-1106”

请求参数示例

{
    "model": "gpt-3.5-turbo-1106",
    "token": "sk-3d76d415-dd72-43ff-b7c8-65fb426f1d7b",
    "messages": [
        {
            "role": "user",
            "content": "List of months that have 30 days in json"
        }
    ],
    "params": {
        "n": 1,
        "response_format": {
            "type": "json_object"
        }
    }
}

响应数据

{
    "code": 200,
    "data": {
        "completion": {
            "id": "chatcmpl-8K3TSgnJ31qpVlomvnSYpqKYmEZFR",
            "object": "chat.completion",
            "created": 1699790606,
            "model": "gpt-3.5-turbo-1106",
            "choices": [
                {
                    "index": 0,
                    "message": {
                        "role": "assistant",
                        "content": "{\n  \"months\": [\n    \"April\",\n    \"June\",\n    \"September\",\n    \"November\"\n  ]\n}"
                    },
                    "finish_reason": "stop"
                }
            ],
            "usage": {
                "prompt_tokens": 17,
                "completion_tokens": 25,
                "total_tokens": 42
            },
            "system_fingerprint": "fp_eeff13170a"
        },
        "info": {
            "isSdHide": "2",
            "sdLimitCount": 2000,
            "sdTextToImg": 412,
            "token": "sk-3d76d415-dd72-43ff-b7c8-65fb426f1d7b",
            "numOfOneDayCanCallApi": 50,
            "numOfOneDayAlreadyCallApi": 7,
            "apiDate": "2023-11-12",
            "isHasGPT4Auth": true
        }
    },
    "message": "成功"
}

使用json格式返回十二生肖,包含中文名和英文名,[{id:"1", enName:"", cnName: ""}] 

gpt支持json格式的数据返回(response_format: ‘json_object‘)_第2张图片

gpt支持json格式的数据返回(response_format: ‘json_object‘)_第3张图片

gpt支持json格式的数据返回(response_format: ‘json_object‘)_第4张图片

gpt支持json格式的数据返回(response_format: ‘json_object‘)_第5张图片

你可能感兴趣的:(web前端,gpt,json,javascript)