这是indexloc提供的服务,不要输入任何密码
Skip to content

“createStream” method may lead to duplicate invocations of listeners. #173

@NingNing0111

Description

@NingNing0111

Test Code

  test('chat stream', () async {
    OpenAI.baseUrl = "https://api.openai.com";
    OpenAI.apiKey = "sk-**********";
    var createStream =
        OpenAI.instance.chat.createStream(model: "gpt-3.5-turbo", messages: [
      OpenAIChatCompletionChoiceMessageModel(
          role: OpenAIChatMessageRole.user,
          content: [
            OpenAIChatCompletionChoiceMessageContentItemModel.text("Hi!")
          ])
    ]);

    final completer = Completer<bool>();
    // Count the number of calls
    var count = 0;
    // Merge results
    var assistant = "";
    createStream.listen((event) {
      count++;
      final content = event.choices.first.delta.content;
      if (content != null) {
        assistant = assistant + content.first!.text!;
      }
    }, onDone: () {
      completer.complete(true);
      print(assistant);
      print(count);
    });
    await completer.future;
  });

Logs

Hello! How can I assist you today?Hello! How can I assist you today?Hello! How can I assist you today?Hello! How can I assist you today?Hello! How can I assist you today?Hello! How can I help you today?Hello! How can I assist you today?Hello! How can I assist you today?Hello! How can I assist you today?Hello! How can I assist you today?Hello! How can I help you today?
121

 If my processing method is incorrect, how can I correctly obtain unique values?

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