pratikbaid3 / flutter_client_sse

Dart package to help consume SSE API. Consumes server sent events by returning parsed model of the event, id, and the data
https://pub.dev/packages/flutter_client_sse
MIT License
40 stars 41 forks source link

Errors using ChatGPT #9

Closed esDotDev closed 1 year ago

esDotDev commented 1 year ago

Trying to listen to a stream from GPT, I can see the data being printed out, but my listeners never fire.

flutter: FormatException: Unexpected character (at character 1)
flutter: data: {"id":"chatcmpl-72CZsAqYS71OoS3bA3uGmCjkkScEO","object":"chat.complet...
flutter: ^
flutter: 
flutter: data: {"id":"chatcmpl-72CZsAqYS71OoS3bA3uGmCjkkScEO","object":"chat.completion.chunk","created":1680759360,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}
flutter: 
flutter: data: {"id":"chatcmpl-72CZsAqYS71OoS3bA3uGmCjkkScEO","object":"chat.completion.

Usage example:

var url = Uri.https('api.openai.com', '/v1/chat/completions');
    final headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer $k'};
    final response = await http.post(
      url,
      headers: headers,
      body: json.encode({
        'stream': true,
        'model': 'gpt-3.5-turbo',
        'messages': messages.map((e) {
          var role = e.chatMessageType == ChatMessageType.user ? 'user' : 'assistant';
          if (e.chatMessageType == ChatMessageType.system) role = 'system';
          return {'role': role, 'content': e.text};
        }).toList(),
        'temperature': temperature ?? .7,
        'max_tokens': 2000,
        'top_p': 1,
        'frequency_penalty': 1.0,
        'presence_penalty': 1.0,
      }),
    );
    final _listener = SSEClient.subscribeToSSE(
      url: 'https://api.openai.com/v1/chat/completions',
      header: headers,
    ).listen((event) {
      print(event);
    });
esDotDev commented 1 year ago

I believe this is because POST is not used, so no body content can be set.

pratikbaid3 commented 1 year ago

Hi @esDotDev. The issue is that the package doesn't support post at them moment. I'll add that in today and create a new release.

Thanks

esDotDev commented 1 year ago

Hi, any luck with this? Is there anything I can do to help?

pratikbaid3 commented 1 year ago

Hi @esDotDev. Sorry for the delay. I am almost done Will create a hotfix in a couple of hours

pratikbaid3 commented 1 year ago

Hi @esDotDev . HTTP POST can now be used in the latest pre-release build of the package. https://pub.dev/packages/flutter_client_sse/versions/2.0.0-beta.1

Thanks and Regards

pratikbaid3 commented 1 year ago

Closing the issue for now. Incase this doesn't solve your issue, feel free to comment and I'll re-open this.

esDotDev commented 1 year ago

Great, ty!

pratikbaid3 commented 1 year ago

Do let me know if this fixes your issue. Thanks