openai-kotlin: [error : You didn't provide an API key]

Description

I made everything correctly for chat completion but I still have problem

Steps to Reproduce

My Code is

val chatCompletionRequest = ChatCompletionRequest(
    model = ModelId("gpt-3.5-turbo"),
    messages = listOf(
        ChatMessage(
            role = ChatRole.User,
            content = "Hello!"
        )
    )
)

val openAi = OpenAI(api_key)

GlobalScope.launch {
    val completion: ChatCompletion = openAi.chatCompletion(chatCompletionRequest)
    answer = completion.choices[0].message.toString()
}

I/System.out: HttpClient: REQUEST: https://api.openai.com/v1/chat/completions I/System.out: METHOD: HttpMethod(value=POST) I/System.out: COMMON HEADERS I/System.out: -> Accept: application/json I/System.out: -> Accept-Charset: UTF-8 I/System.out: -> Authorization: Bearer MY_API_KEY I/System.out: CONTENT HEADERS I/System.out: -> Content-Length: 73 I/System.out: -> Content-Type: application/json D/NetworkSecurityConfig: No Network Security Config specified, using platform default I/System.out: HttpClient: RESPONSE: 200 I/System.out: METHOD: HttpMethod(value=POST) I/System.out: FROM: https://api.openai.com/v1/chat/completions I/System.out: COMMON HEADERS I/System.out: -> access-control-allow-origin: * I/System.out: -> alt-svc: h3=“:443”; ma=86400, h3-29=“:443”; ma=86400 I/System.out: -> cache-control: no-cache, must-revalidate I/System.out: -> cf-cache-status: DYNAMIC I/System.out: -> cf-ray: 7b1bd2aeae3db9e1-OTP I/System.out: -> content-type: application/json I/System.out: -> date: Sun, 02 Apr 2023 20:29:16 GMT I/System.out: -> openai-model: gpt-3.5-turbo-0301 I/System.out: -> openai-organization: user-dermqywvibsj8hgo3u9pi910 I/System.out: -> openai-processing-ms: 645 I/System.out: -> openai-version: 2020-10-01 I/System.out: -> server: cloudflare I/System.out: -> strict-transport-security: max-age=15724800; includeSubDomains I/System.out: -> x-ratelimit-limit-requests: 20 I/System.out: -> x-ratelimit-remaining-requests: 19 I/System.out: -> x-ratelimit-reset-requests: 3s I/System.out: -> x-request-id: 0f69a1910c7a3bad98d5ec558bcd0cb4

**My Error Message is : { “error”: { “message”: “You didn’t provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you’re accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.”, “type”: “invalid_request_error”, “param”: null, “code”: null } }

Environment

  • openai-kotlin version: [3.2.0]
  • Kotlin version: [‘1.8.20’]
  • OS: [Android 11]

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 18 (4 by maintainers)

Most upvoted comments

@alihsan-tsdln like this:

OpenAI(
    config = OpenAIConfig(
        token = "abc",
        headers = mapOf("api-key" to "abc"),
        host = OpenAIHost.azure(...),
        proxy = null,
        retry = RetryStrategy()
    )

api_key probably don’t hold a correct API key value. Double-check by debugging or printing it before creating the OpenAI instance.