2

I tried to change my code to be able to use the new OpenAI model but my application stops working,

BEFORE: In Bold are parts of the code that I changed and where working using text-davinci-003 model

**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;
                }
            }

AFTER: In Bold are parts of the code that I changed and arent working with gpt-3.5-turbo model

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;
                }
            }

I only changed the parts that are in bold and now it does nto work, the application stops.

John Detlefs
  • 952
  • 8
  • 16
  • 2
    I just switched my model from -003 to 3.5 turbo. I had to refactor the entire call. I only use Javascript but I wanted to point out that you are using some unnecessary parameters. You don't need to specify Temp or penalties as they are set as 0 by default. Try eliminating those. You'll also need to put the API call into Postman or whatever you use. I discovered the 'model' identifier needed to be refactored in how it's included in the call so take a closer look at your error messages on the API call for clues. – Unnamed Mistress Mar 04 '23 at 20:07
  • Could you provide some details for how you needed to refactor the call in JavaScript? – zero_cool Apr 01 '23 at 22:28

0 Answers0