Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream functionality breaks on Web after 1.9.7 #57

Closed
iqfareez opened this issue Jun 2, 2023 · 3 comments
Closed

Stream functionality breaks on Web after 1.9.7 #57

iqfareez opened this issue Jun 2, 2023 · 3 comments

Comments

@iqfareez
Copy link

iqfareez commented Jun 2, 2023

Issue

The chat completion stream on Flutter Web platforms returns all chat at the end. Not streaming.

Using version dart_openai: ^2.0.1

See video:

flutter_chatgpt_notok.webm

Note: Works ok on other platforms (Tested on Windows)

Expected

After downgrading & pinned the package version: dart_openai: 1.9.7. The stream completion on web works normally again.

See video:

flutter_chatgpt_ok.webm

Code snippet

Stream<OpenAIStreamChatCompletionModel> chatStream =
    OpenAI.instance.chat.createStream(
  model: "gpt-3.5-turbo",
  messages: _aiMessages,
);

chatStream.listen((chatStreamEvent) {
  debugPrint(chatStreamEvent.toString());
  // existing id: just update to the same text bubble
  if (chatResponseId == chatStreamEvent.id) {
    chatResponseContent +=
        chatStreamEvent.choices.first.delta.content ?? '';

    _addMessageStream(chatResponseContent);

    if (chatStreamEvent.choices.first.finishReason == "stop") {
      isAiTyping = false;
      _aiMessages.add(OpenAIChatCompletionChoiceMessageModel(
        content: chatResponseContent,
        role: OpenAIChatMessageRole.assistant,
      ));
      chatResponseId = '';
      chatResponseContent = '';
    }
  } else {
    // new id: create new text bubble
    chatResponseId = chatStreamEvent.id;
    chatResponseContent = chatStreamEvent.choices.first.delta.content ?? '';
    onMessageReceived(id: chatResponseId, message: chatResponseContent);
    isAiTyping = true;
  }
});

Full code: https://github.com/iqfareez/flutter_chatgpt/blob/main/lib/screens/chat_page.dart

@elias-avatar
Copy link

Thank you for figuring this out @iqfareez, downgrade to 1.9.7 fixed streaming on web for me

@anasfik anasfik closed this as completed Jul 10, 2023
danieljohnmeen added a commit to danieljohnmeen/Flutter-ChatGPT that referenced this issue Aug 12, 2023
AG-Akash09 added a commit to AG-Akash09/Flutter-GPT that referenced this issue Sep 16, 2023
@khanliKU
Copy link

Did this get fixed? I am using 3.0.0 and Streams don't work on the web.

@elias-avatar
Copy link

I don't think it was fixed. I've stayed on 1.9.7 for anything web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants