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.