justinmahar / openai-ext

🤖 Extension to OpenAI's API to support streaming chat completions.
https://justinmahar.github.io/openai-ext/
MIT License
49 stars 3 forks source link

OpenAI API errors get catched silently #6

Closed Anton-Plagemann closed 1 year ago

Anton-Plagemann commented 1 year ago

Hi @justinmahar,

first, thanks for this very nice library! It has saved me some valuable time! 🚀

I have an issue with the exception handling which we could optimize maybe: Sometimes OpenAI responds with a JSON like this:

{
   "message":"That model is currently overloaded with other requests. You can retry your request, or contact us through our help center at help.openai.com if the error persists. (Please include the request ID a937df050d4a28d199c07201da887214 in your message.)",
   "type":"server_error",
   "param":null,
   "code":null
}

In this case the error gets catched silently in this code block:

https://github.com/justinmahar/openai-ext/blob/8364d8b1f5e98d095eabb7223db17989df260f2f/src/OpenAIExt.ts#L194-L197

Unfortunately theres no posibility for overlying code to identify this error and inform the user or retry the request. onContent will be called with

{
   "content":"",
   "isFinal":true
}

in this case.

I would instead recomment to throw such an error.

Is that something you we could change? If it is helpful, I could also work on a fix and submit a PR 😊

justinmahar commented 1 year ago

Hey @Anton-Plagemann, great catch here! (pun intended)

So I've added a throw clause to the catch block and I'm now also throwing an error whenever the parsed blob doesn't contain completion choices (which likely indicates there was an error).

Give it a try with the newest version:

npm i openai-ext@latest

Hope that solves your issue, keep me posted!

Anton-Plagemann commented 1 year ago

Hi @justinmahar,

Thank you so much for your quick response and for implementing the changes! I really appreciate it. I have updated the library in my application and will test it out. I'll let you know as soon as I have any results (could take some time to wait for the next error 😄).

Thanks again for your help!

Anton-Plagemann commented 1 year ago

Hi @justinmahar,

I now can confirm it works as expected and all errors are passed through 🥳 Surprisingly theres also sometimes a Unexpected end of JSON input error, but it seems to be tolerable (maybe 1 or 2 tokens get lost but thats ok 😊). Thanks again for the update!

justinmahar commented 1 year ago

Woohoo!! 🍻