You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenAI API迁移:从text-davinci-003转gpt-3.5-turbo后应用崩溃求助

排查gpt-3.5-turbo模型切换导致的应用崩溃问题

尝试将代码从使用text-davinci-003模型切换为gpt-3.5-turbo后应用崩溃,仅修改了API地址、模型参数及消息构造部分,以下是修改前后代码及排查修复方案。

修改前代码(使用text-davinci-003可正常运行)

注:以下代码中加粗标记的行为切换模型时修改的内容

**var url = "https://api.openai.com/v1/completions"**

private fun getResponse(query: String) {
    val queue: RequestQueue = Volley.newRequestQueue(applicationContext)
    val jsonObject: JSONObject? = JSONObject()

    **jsonObject?.put("model", "text-davinci-003")**
    jsonObject?.put("messages", messagesArray)
    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)

    val postRequest: JsonObjectRequest =
        object : JsonObjectRequest(Method.POST, url, jsonObject,
            Response.Listener { response ->
                val responseMsg: String =
                    response.getJSONArray("choices").getJSONObject(0).getString("text")
            },
            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()
                params["Content-Type"] = "application/json"
                params["Authorization"] = "Bearer MYTOKEN"
                return params;
            }
        }
}

修改后代码(使用gpt-3.5-turbo时应用崩溃)

注:以下代码中注释块包裹的部分为修改内容

var url = "https://api.openai.com/v1/chat/completions"

private fun getResponse(query: String) {
    val queue: RequestQueue = Volley.newRequestQueue(applicationContext)
    val jsonObject: JSONObject? = JSONObject()

    // start changes
    jsonObject?.put("model", "gpt-3.5-turbo"); 
    val messagesArray = JSONArray()
    val messageObject1 = JSONObject()
    messageObject1.put("role", "user")
    messageObject1.put("content", query)
    messagesArray.put(messageObject1)
    jsonObject?.put("messages", messagesArray)
    // end changes

    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)

    val postRequest: JsonObjectRequest 

    object : JsonObjectRequest(Method.POST, url, jsonObject,
        Response.Listener { response ->
            val responseMsg: String =
                response.getJSONArray("choices").getJSONObject(0).getString("text")
        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()
            params["Content-Type"] = "application/json"
            params["Authorization"] = "Bearer MYTOKEN"
            return params;
        }
    }
}

问题排查与修复方案

  • 语法错误:变量声明缺失赋值运算符
    修改后的代码中val postRequest: JsonObjectRequest仅声明未赋值,导致编译错误。修复时添加=号:

    val postRequest: JsonObjectRequest =
        object : JsonObjectRequest(Method.POST, url, jsonObject, ...) { ... }
    
  • 语法错误:Listener代码块结构混乱
    Response.ErrorListener未正确闭合Response.Listener的lambda块,导致语法结构错误。修复后调整为:

    val postRequest: JsonObjectRequest =
        object : JsonObjectRequest(Method.POST, url, jsonObject,
            Response.Listener { response ->
                // 响应处理逻辑
            },
            Response.ErrorListener { error ->
                // 错误处理逻辑
            }) {
            // 头部重写代码
        }
    
  • API响应解析字段错误
    gpt-3.5-turbo的Chat Completions接口响应内容在message.content字段,而非text-davinci-003的text字段,原解析代码会抛出JSON异常。修复时修改解析逻辑:

    val responseMsg: String =
        response.getJSONArray("choices")
            .getJSONObject(0)
            .getJSONObject("message")
            .getString("content")
    
  • 潜在空指针风险优化
    jsonObject声明为可空类型,可改为非空声明避免不必要的安全调用:

    val jsonObject = JSONObject() // 去掉?,直接初始化
    

内容的提问来源于stack exchange,提问作者Pepper Robot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 20:27:16