juliuscanute / qr_code_scanner

QR Code Scanner for Flutter
BSD 2-Clause "Simplified" License
1.01k stars 799 forks source link

[BUG] When using assistant V1 Threads, Message List returns empty messages #727

Closed OscillatingMonkey closed 8 months ago

OscillatingMonkey commented 8 months ago

Describe the bug

When using openAI.threads.messages.listMessage the right number of messages are returned but contain no content. Here is the implementation

  String narrative = '';
  String status = '';
  List<CreateMessageResponse> messageData = [];

  late OpenAI openAI;
  late ThreadResponse thread;
  late CreateRunResponse run;

  @override
  void initState() {
    super.initState();

    openAI = OpenAI
        .instance
        .build(token: token, baseOption: HttpSetup(receiveTimeout: const Duration(seconds: 20)),enableLog: true);

    loadNarrative();
  }

  void loadNarrative() async {
    ThreadResponse thread = await openAI.threads.createThread();
    MessageData threadMessages = await openAI
        .threads
        .messages
        .createMessage(
          threadId: thread.id,
          request: CreateMessage(
              role: "user",
              content: "Tell me about ${widget.query}"
          ));

    final request = CreateRun(assistantId: assistantId);

    CreateRunResponse run = await openAI.threads.runs.createRun(threadId: thread.id, request: request);

    setState(() {
      status = run.status;
    });

    Timer.periodic(const Duration(seconds: 15), (timer) async {
      openAI.threads.messages.listMessage(threadId: thread.id).then((res) {
        setState(() {
          messageData = res;
        });
      });
    });

    Timer.periodic(const Duration(seconds: 1), (timer) async {
      var data = await openAI.threads.runs.retrieveRun(
          threadId: thread.id,
          runId: run.id
      );

      setState(() {
        status = data.status;
      });

      if (data.status == 'completed') {
        timer.cancel();
      }
    });
  }

The message object is always empty. Please don't mind the timer on this call, to make sure there was no issue with retrieving messages too quickly I am doing an impractically long poll for them as part of debugging.

Screenshot 2024-03-03 at 1 00 25 AM

While debugging I found that when converting the response from openAI to an object, the JSON contained all of the necessary data but the mapping seemed incorrect.

Screenshot 2024-03-03 at 12 53 16 AM

I did not dig beyond this but it seems the response data is not being properly converted to a usable object and is causing messages to exist without content.

Flutter information Always provide the output of flutter doctor -v as it is needed in order to know on which Flutter versions the bug exists in.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/mbashir/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Users/mbashir/Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    ✗ Xcode end user license agreement not signed; open Xcode or run the command 'sudo
      xcodebuild -license'.
    ✗ Xcode requires additional components to be installed in order to run.
      Launch Xcode and install additional required components when prompted or run:
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is
        different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods.
      To re-install see
      https://guides.cocoapods.org/using/getting-started.html#installation for
      instructions.

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • Android Studio at /Users/mbashir/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.4)
    • IntelliJ at /Users/mbashir/Applications/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 78.1.1
    • Dart plugin version 233.13763.5

[✓] Connected device (3 available)
    • Pixel 6a (mobile) • 192.168.86.226:5555 • android-arm64  • Android 14 (API 34)
    • macOS (desktop)   • macos               • darwin-arm64   • macOS 14.3.1 23D60
      darwin-arm64
    • Chrome (web)      • chrome              • web-javascript • Google Chrome
      122.0.6261.94

[✓] Network resources
    • All expected network resources are available.

Device (please complete the following information):

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.