Как я могу перейти с модели text-davinci-003 на модель gpt-3.5-turbo с помощью OpenAI API?

Я попытался изменить свой код, чтобы использовать новую модель OpenAI, но мое приложение перестало работать,

ДО: Жирным шрифтом выделены части кода, которые я изменил и в которых работает с использованием модели 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()
                    // adding headers on below line.
                    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;
                }
            }

Я изменил только те части, которые выделены жирным шрифтом, и теперь это не работает, приложение останавливается.

0 ответов

Другие вопросы по тегам