gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:49:46 31 4
gpt4 key购买 nike

我收到以下错误:

[3067] NetworkUtility.shouldRetryException: Unexpected response code400 for https://api.openai.com/v1/chat/completions

代码:

private fun getResponseTurbo(query: String) {
// setting text on for question on below line.
questionTV.text = query
queryEdt.setText("")
// creating a queue for request queue.
val queue: RequestQueue = Volley.newRequestQueue(applicationContext)
// creating a json object on below line.
val jsonObject: JSONObject ? = JSONObject()
// adding params to json object.

jsonObject ? .put("model", "gpt-3.5-turbo")
jsonObject ? .put("messages", "[{'role': 'user', 'content': 'What are your functionalities?'}]")

jsonObject ? .put("temperature", 0)
jsonObject ? .put("max_tokens", 48)
jsonObject ? .put("top_p", 1)
jsonObject ? .put("frequency_penalty", 0)
jsonObject ? .put("presence_penalty", 0)

// on below line making json object request.
val postRequest: JsonObjectRequest =
// on below line making json object request.
object: JsonObjectRequest(Method.POST, url_turbo, jsonObject,
Response.Listener {
response - >
// on below line getting response message and setting it to text view.
val responseMsg: String =
response.getJSONArray("choices").getJSONObject(0).getString("message")
chattypingLT.visibility = View.GONE
ll_copy_share.visibility = View.VISIBLE
responseTV.text = responseMsg
},
// adding on error listener
Response.ErrorListener {
error - >
Log.e("TAGAPI", "Error is : " + error.message + "\n" + error)
}) {
override fun getHeaders(): kotlin.collections.MutableMap < kotlin.String, kotlin.String > {
val params: MutableMap < String,
String > = HashMap()
// adding headers on below line.
params["Content-Type"] = "application/json"
params["Authorization"] =
"Bearer APIKEY"
return params;
}
}

// on below line adding retry policy for our request.
postRequest.setRetryPolicy(object: RetryPolicy {
override fun getCurrentTimeout(): Int {
return 50000
}

override fun getCurrentRetryCount(): Int {
return 50000
}

@Throws(VolleyError::class)
override fun retry(error: VolleyError) {}
})
// on below line adding our request to queue.
queue.add(postRequest)
}

最佳答案

您使用的是 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")

问题

您答对了第一点和第三点。这(即第二个要点)是代码中有问题的部分:

jsonObject ? .put("messages", "[{'role': 'user', 'content': 'What are your functionalities?'}]")

为什么?

您提供的代码中 messages 参数的 JSON 无效,这是因为它对字符串值使用单引号。在 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?"
}
]
}

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

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

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