The problem of API-KEY in "gpt-3.5-turbo-0301"

Hi all,

I’m having trouble about API-KEY.
I was programming in kotlin using “gpt-3.5-turbo-0301” to communicate with chatGPT.
Code are next lines.

val params: MutableMap<String, String> = HashMap()
params[“Content-Type”] = “application/json”
params[“Authorization”] =
“Bearer sk-********”
return params;

Retun code is 400.
< “error”: {
“message”: "You didn’t provide an API key.>
But this API-KEY is not a prblem in Node.js and “text-davinci-003”.

How can I fix this problem?

Hi @saeda.saburo,

did you change your URL when changing the model?

Did you also change the way you submit your request? If you come from Davinci you have to keep in mind that you also have to change your request body?
(OpenAI API compared to OpenAI API)

1 Like

Hello fellow Kotlin developer!
I imagine you also come from an Android dev background? Can I just say, I absolutely love Jetpack Compose. Reactive UI is the bees knees. I prefer it over ReactJS, anyday.

Your request seems strange. What are you using to make the actual API request? Retrofit?

In my opinion, you should be setting up an intercepting object to implement your headers. Otherwise you will be manually setting them on each call - which defeats DRY principles.

Here’s an example:

object OpenAiInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
            .newBuilder()
            .header("Authorization", "Bearer sk-REDACTED")
            .header("Content-Type", "application/json")
            .build()
        return chain.proceed(request)
    }
}

Which is then added to the Retrofit helper

object RetrofitHelper {
    fun getInstance(baseUrl: String, interceptor: Interceptor): Retrofit {
        val mHttpLoggingInterceptor = HttpLoggingInterceptor()
            .setLevel(HttpLoggingInterceptor.Level.BODY)

        val okHttpClient = OkHttpClient()
            .newBuilder()
            .addInterceptor(interceptor)
            .build()

        return Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

}

Thanks for the quick advice. Problem solved.
I used the same URL, as in “text-davinci-003”, for “gpt-3.5-turbo”.

I tried this in nodejs to check the API-KEY, but the code was from the open site, with no URL, and there was no problem!

I needed to calm down and check all the details!

Happy to hear that!

The URL-Thing was btw. my first contact point with the forum :slight_smile:

That’s allways a good idea :wink: