1

I've been searching a long time, but I didn't find anything that could help me.

I'm trying to use a OpenAI API, but every time I'm getting errors. What I am doing wrong?

The error is:

Error from the API

The code is:

RetrofitInstance

class RetrofitInstance {

    companion object{
        private lateinit var RETROFIT: Retrofit
        private var client = OkHttpClient.Builder().apply {
            addInterceptor(ChatGPTInterceptor())
        }.build()

        fun getRetrofitInstance(pathUrl: String): Retrofit{
            if(!::RETROFIT.isInitialized){
                RETROFIT = Retrofit.Builder()
                    .baseUrl(pathUrl)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
            }
            return RETROFIT
        }
    }

}

ChatGPTInterceptor

class ChatGPTInterceptor: Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val proceed = chain.request()
            .newBuilder()
            .addHeader("content-type", "application/json")
            .addHeader("X-RapidAPI-Key", "Key-Censored")
            .addHeader("X-RapidAPI-Host", "openai80.p.rapidapi.com")
            .build()
        return chain.proceed(proceed)
    }
}

MainActivity

val sendMessageModel = SendMessageModel("user", binding.inputText.text.toString())

            val realSendMessageModel = RealSendMessageModel("text-davinci-003", arrayListOf(sendMessageModel))

            val retrofitInstance = RetrofitInstance.getRetrofitInstance("https://openai80.p.rapidapi.com/")
            val endpoint = retrofitInstance.create(ChatGPTEndpoint::class.java)
            val callback = endpoint.getRetrofitAnswer(realSendMessageModel)
            callback.enqueue(object: Callback<AnswerModel>{
                override fun onResponse(call: Call<AnswerModel>, response: Response<AnswerModel>) {
                    val s = response
                }

                override fun onFailure(call: Call<AnswerModel>, t: Throwable) {
                    val s = t
                }
            })

ChatGPTEndpoint

interface ChatGPTEndpoint {

    @POST("chat/completions")
    fun getRetrofitAnswer(@Body message: RealSendMessageModel): Call<AnswerModel>

}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/75981657/edit) Thanks in advance. – Peter Mortensen Apr 16 '23 at 14:32

0 Answers0