调用GPT接口使用3.5模型报错:Unrecognized request argument supplied: messages

报错信息:

话不多说,今天我请求chatgpt接口返回这样的信息:

{
  "error": {
    "message": "Unrecognized request argument supplied: messages",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

一看懵逼了,我的请求参数存在问题,然后各种试,各种改,改成json字符串,改http请求方式,都不行,当中我又怀疑是我key的问题,又换了key还是不行,又以为是我代理网站的问题,后面又换海外网络,用官网域名请求,还是不行,自我怀疑好久,是不是3.5接口更新了,请求格式不对?后面才发现是我请求url和text模型的url搞错了

text模型接口URL:https://api.openai.com/v1/completions

3.5模型接口URL:https://api.openai.com/v1/chat/completions 

最终修改请求URL就能正常调用了,分享一下请求体的json数据:

{
    "messages": [
        {
            "content": "你是一个智能的AI助手。",
            "role": "system"
        },
        {
            "content": "请用java写一个helloWorld",
            "role": "user"
        }
    ],
    "model": "gpt-3.5-turbo"
}

请求方式是用okhttp3:

    private static String fetch(String rowStr, String url) {
        RequestBody requestBody = RequestBody.create(rowStr, MediaType.parse("application/json; charset=utf-8"));
        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", "Bearer " + token)
                .post(requestBody)
                .build();

        Call call = client.newCall(request);
        try {
            Response execute = call.execute();
            return execute.body().string();
        } catch (IOException e) {
            return "error";
        }
    }


//client初始化:

static OkHttpClient client = new OkHttpClient();

你可能感兴趣的:(gpt)