“OpenAI ChatGPT (GPT-3.5) API error: ‘Invalid URL (POST /chat/v1/completions)'”

huangapple go评论241阅读模式
英文:

OpenAI ChatGPT (GPT-3.5) API error: "Invalid URL (POST /chat/v1/completions)"

问题

我按照教程制作了一个ChatGPT应用程序,遇到了这个错误:

无法加载响应,原因是:
{'error': {
'message': '无效的URL(POST /chat/v1/completions)',
'type':'invalid_request_error',
'param':null,
'code':null
}
}

这是我的代码:

JSONObject jsonBody = new JSONObject();
        try {
            jsonBody.put("model", "gpt-3.5-turbo");
            jsonBody.put("messages", question);
            jsonBody.put("max_tokens", 4000);
            jsonBody.put("temperature", 0);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        RequestBody body = RequestBody.create(jsonBody.toString(),JSON);
        Request request = new Request.Builder()
                .url("https://api.openai.com/chat/v1/completions")
                .addHeader("Authorization", "Bearer HIDDEN_KEY")
                .addHeader("Content-Type", "application/json")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                addResponse("由于" + e.getMessage() + "无法加载响应");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if(response.isSuccessful()){
                    JSONObject jsonObject  = null;
                    try {
                        jsonObject = new JSONObject(response.body().string());
                        JSONArray jsonArray = jsonObject.getJSONArray("choices");
                        String result = jsonArray.getJSONObject(0).getString("message");
                        addResponse(result.trim());
                    } catch (JSONException e) {
                        throw a RuntimeException(e);
                    }

                }else{
                    addResponse("由于" + response.body().string() + "无法加载响应");
                }
            }

我尝试更改模型、在URL中删除"/chat/"并将提示直接发送到URL中。我是一个初学者制作应用程序和Java编程(但我在编程方面不是初学者),因此我明白也许这段代码不是很好,因为我几乎只是从教程中复制和粘贴代码。

谢谢你的帮助!

英文:

I followed a tutorial to make an chatgpt app and get this error :

Failed to load response due to {
'error' : {
'message' : 'Invalid URL (POST /chat/v1/completions)',
'type':'invalid_request_error',
'param':null,
'code':null
}
}

This is my code :

JSONObject jsonBody = new JSONObject();
        try {
            jsonBody.put("model", "gpt-3.5-turbo");
            jsonBody.put("messages", question);
            jsonBody.put("max_tokens", 4000);
            jsonBody.put("temperature", 0);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        RequestBody body = RequestBody.create(jsonBody.toString(),JSON);
        Request request = new Request.Builder()
                .url("https://api.openai.com/chat/v1/completions")
                .addHeader("Authorization", "Bearer HIDDEN_KEY")
                .addHeader("Content-Type", "application/json")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                addResponse("Failed to load response due to pd "+e.getMessage());
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if(response.isSuccessful()){
                    JSONObject jsonObject  = null;
                    try {
                        jsonObject = new JSONObject(response.body().string());
                        JSONArray jsonArray = jsonObject.getJSONArray("choices");
                        String result = jsonArray.getJSONObject(0).getString("message");
                        addResponse(result.trim());
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }

                }else{
                    addResponse("Failed to load response due to "+response.body().string());
                }
            }

I tried changing the model, removing the \chat\ in the URL and send the prompt directly in URL too

I'm new to app making and java coding (but I'm no beginner in coding) so I understand that maybe this code isn't great as I almost only copy and paste the code from the tutorial.

Thanks for your help !

答案1

得分: 0

更改此处的拼写错误:

https://api.openai.com/chat/v1/completions

...为:

https://api.openai.com/v1/chat/completions

请参阅文档

英文:

You have a typo.

Change this...

https://api.openai.com/chat/v1/completions

...to this.

https://api.openai.com/v1/chat/completions

See the documentation.

huangapple
  • 本文由 发表于 2023年3月31日 21:37:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定