anasfik / openai

Dart/Flutter SDK for ChatGPT and all OpenAI APIs (GPT, Dall-e..)
https://pub.dev/packages/dart_openai
MIT License
570 stars 177 forks source link

Http errors are not sent to the stream as errors? #147

Closed Terranic closed 7 months ago

Terranic commented 9 months ago

Hi all,

I´m currently encounting 429 http errors on the openai chatcompletion API,- today it is good test the apps robustness. However, I encounter a problem with the openai dart package, because I´m not getting notified about the 429 error... the stream simply never closes. The app hangs. Is there no support for errors right now? The "Error" print is never reached

Error in browser console: image

Stream<String> callAI(String systemprompt, String prompt,
      {required double temperature, required int maxtokens}) async* {
    var chatStream = OpenAI.instance.chat.createStream(
        model: "gpt-4-1106-preview",
        maxTokens: maxtokens,
        temperature: temperature,
        messages: [
          OpenAIChatCompletionChoiceMessageModel(
              role: OpenAIChatMessageRole.system,
              content: [
                OpenAIChatCompletionChoiceMessageContentItemModel.text(
                    systemprompt)
              ]),
          OpenAIChatCompletionChoiceMessageModel(
              role: OpenAIChatMessageRole.user,
              content: [
                OpenAIChatCompletionChoiceMessageContentItemModel.text(prompt)
              ]),
        ]);

    String res = "";

    chatStream.handleError((onError) => print("ERROR $onError"));

    await for (var token in chatStream) {
      String tok = token.choices.first.delta.content?.first.text ?? "";
      res += tok;
      yield res;
    }
    yield res;
  }
anasfik commented 7 months ago

stream methods now should allow you to catch errors in the onError function callback of your stream subscription.