Skip to content

400 Error thrown due to max token length exceeded returns the request body when streaming #51

@nsadeh

Description

@nsadeh

Steps to reproduce:

  1. Collect a sequence of messages to pass to open AI (of type {:role role :content content}) that exceed the model's token limit. In my case, GPT-3.5 turbo with 4097 tokens
  2. Hit the synchronous chat completions endpoint. I use OpenAI without Azure. You get an exception HTTP response with status 400 and the following body
:body
    "{\n  \"error\": {\n    \"message\": \"This model's maximum context length is 4097 tokens. However, your messages resulted in <more>. Please reduce the length of the messages"
  1. Repeat the same experiment, this time setting the :stream parameter to true. My setup doesn't have a handler in order to receive a channel back
  2. You should see a similar HTTP 400 exception thrown, but in the body you will see your request body rather than the helpful error message.

Attached below is the function I use to invoke the OpenAI endpoint:

(defn ask
 ([questions]
  (ask questions {} {}))
 ([questions payload-params]
  (ask questions payload-params {}))
 ([questions payload-params  opts]
  (let [payload (merge payload-params {:model "gpt-3.5-turbo"
                                       :messages questions
                                       :temperature 0})
        options (assoc opts :api-key openai-token)]
    (api/create-chat-completion payload options))))

In this case it was invoked as (ask messages {:stream true}

Thank you for making this library, it was tremendously helpful in avoiding a lot of really boring work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions