gpt4 book ai didi

java - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "Unexpected response code 400 for https://api.openai.com/v1/completions" (migrating GPT-3 to GPT-3. 5 API)

转载 作者:行者123 更新时间:2023-12-02 05:47:17 26 4
gpt4 key购买 nike

我有一个 Android 应用程序,目前正在使用 chat gpt 3.0 进行补全,并且工作正常。现在,在他们发布 chat gpt 3.5 Turbo 后,我根据他们的请求示例做了一些更改,但抛出了 400 错误,我感谢任何帮助,谢谢

  • 我的 gpt 3.0 代码(工作正常)
  public static void getResponse(Context context, String URL, String Token, TextView mQuestionText, TextInputEditText queryEdt, TextView mResponseText, String query) throws JSONException {

mQuestionText.setText(query);
queryEdt.setText("");
mResponseText.setText("Please wait..");

RequestQueue requestQueue = Volley.newRequestQueue(context);

JSONObject jsonObject = new JSONObject();
jsonObject.put("model", "text-davinci-003");
jsonObject.put("prompt", query);
jsonObject.put("temperature", 0);
jsonObject.put("max_tokens", 100);
jsonObject.put("top_p", 1);
jsonObject.put("frequency_penalty", 0.0);
jsonObject.put("presence_penalty", 0.0);


JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL ,jsonObject, response -> {
try {
String responseMsg = response.getJSONArray("choices").getJSONObject(0).getString("text");
mResponseText.setText(responseMsg);

// SHUT DOWN TEXT TO SPEECH IN CASE OF QUERY CHANGE
if(textToSpeech != null){
textToSpeech.stop();
textToSpeech.shutdown();
textToSpeech = null;
}

// SPEAK THE RESPONSE FETCHED FROM SERVER
textToSpeech(context,responseMsg);
}catch (Exception e){
// error
Log.d("TAG","Error is " + e.getMessage());
}
}, error -> {
Log.d("TAG","Error " + error.getMessage());
}){
@Override
public Map<String, String> getHeaders() {
Map<String,String> params = new HashMap<>();
params.put("Content-Type","application/json");
params.put("Authorization","Bearer " + Token);
return params;
}
};

requestQueue.add(jsonObjectRequest);
}

  • 现在切换到 3.5 Turbo,我使用 gpt-3.5-turbo 作为模型

public static void getResponse(Context context, String URL, String Token, TextView mQuestionText, TextInputEditText queryEdt, TextView mResponseText, String query) throws JSONException {

mQuestionText.setText(query);
queryEdt.setText("");
mResponseText.setText("Please wait..");

RequestQueue requestQueue = Volley.newRequestQueue(context);

ArrayList<ChatModel> arrayList = new ArrayList<>();
arrayList.add(new ChatModel("user",query));

JSONObject jsonObject = new JSONObject();
jsonObject.put("model", "gpt-3.5-turbo");
jsonObject.put("messages",arrayList);


JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL ,jsonObject, response -> {
try {
String responseMsg = response.getJSONArray("choices").getJSONObject(0).getString("text");
mResponseText.setText(responseMsg);

// SHUT DOWN TEXT TO SPEECH IN CASE OF QUERY CHANGE
if(textToSpeech != null){
textToSpeech.stop();
textToSpeech.shutdown();
textToSpeech = null;
}

// SPEAK THE RESPONSE FETCHED FROM SERVER
textToSpeech(context,responseMsg);
}catch (Exception e){
// error
Log.d("TAG","Error is " + e.getMessage());
}
}, error -> {
Log.d("TAG","Error " + error.getMessage());
}){
@Override
public Map<String, String> getHeaders() {
Map<String,String> params = new HashMap<>();
params.put("Content-Type","application/json");
params.put("Authorization","Bearer " + Token);
return params;
}
};
requestQueue.add(jsonObjectRequest);
}

  • 使用 chat gpt 3.5 Turbo 模型时出错(当我使用 chat gpt 3.0 时它可以工作)

E/Volley: [1922] NetworkUtility.shouldRetryException: Unexpected response code 400 for
https://api.openai.com/v1/completions

  • 基于他们的文档

enter image description here

最佳答案

您使用的是 gpt-3.5-turbo 型号。

Chat Completions API 之间存在三个主要区别(即 GPT-3.5 API)和 Completions API (即 GPT-3 API):

  1. API端点
    • Completions API:https://api.openai.com/v1/completions
    • 聊天完成 API:https://api.openai.com/v1/chat/completions
  2. prompt 参数(Completions API)已替换为 messages 参数(Chat Completions API)
  3. 响应访问
    • Completions API:
      response.getJSONArray("choices").getJSONObject(0).getString("text")
    • 聊天完成 API:response.getJSONArray("choices").getJSONObject(0).getString("message")

问题 1:您使用了错误的 API 端点

改变这个...

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

...对此。

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

问题 2:确保 messages 参数的 JSON 有效

像这样:

JSONObject jsonObject = new JSONObject();
JSONArray messages = new JSONArray();
JSONObject message = new JSONObject();

message.put("role", "user");
message.put("content", "What are your functionalities?");

messages.put(message);

jsonObject.put("messages", messages);

此代码将创建以下 JSON 对象:

{
"messages": [
{
"role": "user",
"content": "What are your functionalities?"
}
]
}

问题 3:您访问响应的方式不正确

改变这个...

response.getJSONArray("choices").getJSONObject(0).getString("text")

...对此。

response.getJSONArray("choices").getJSONObject(0).getString("message")

注意:如果您仍然收到 API 错误 400,请确保您已将 Content-Type header 设置为 application/json。请注意,application/json、UTF-8 不起作用。

关于java - OpenAI ChatGPT (GPT-3.5) API 错误 400 : "Unexpected response code 400 for https://api.openai.com/v1/completions" (migrating GPT-3 to GPT-3. 5 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75616048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com