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

feat(dart_frog): allow disabling Response buffer output #1261

Merged
merged 6 commits into from Jan 30, 2024

Conversation

luiscib3r
Copy link
Contributor

@luiscib3r luiscib3r commented Jan 28, 2024

Status

READY

Description

Allows to configure the shelf.io.buffer_output parameter in the context of the shelf.Response to allow returning a streamed response as explained in this issue: dart-lang/shelf#54 (comment)

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@luiscib3r luiscib3r requested a review from a team as a code owner January 28, 2024 02:44
@luiscib3r
Copy link
Contributor Author

luiscib3r commented Jan 28, 2024

Hi. I am using dart frog to build a service that communicates with the OpenAI/Together AI api. So these APIs allow you to obtain the response from the LLM in streaming. A response with 'Content-Type': 'text/event-stream; charset=utf-8' is used to send each token as soon as it is generated. I need to resend that streamed response using dart frog. I created a little example code:

import 'package:dart_frog/dart_frog.dart';

Response onRequest(RequestContext context) {
  return Response.stream(
    body: stream(),
    headers: {
      'Content-Type': 'text/event-stream; charset=utf-8',
      'X-Content-Type-Options': 'nosniff',
    },
    useBufferOutput: false,
  );
}

Stream<List<int>> stream() async* {
  const message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '
      'Sed non risus. Suspendisse lectus tortor, dignissim sit amet, '
      'adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. '
      'Maecenas ligula massa, varius a, semper congue, euismod non, mi. '
      'Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, '
      'non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, '
      'scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. '
      'Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum '
      'augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. '
      'Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum '
      'primis in faucibus orci luctus et ultrices posuere cubilia Curae; '
      'Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. '
      'Maecenas adipiscing ante non diam sodales hendrerit.';

  final words = message.split(' ');

  for (final word in words) {
    yield 'data: $word\n\n'.codeUnits;
    await Future<void>.delayed(const Duration(milliseconds: 300));
  }
}

Without setting the useBufferOutput parameter to false, the response is not returned until the stream has been closed. When the useBufferOutput is false then the response is sent immediately and keeps transmitting new events from the stream until it closes.

@alestiago alestiago changed the title feat(dart_frog): allow streamed response feat(dart_frog): allow disabling Response buffer output Jan 29, 2024
@alestiago alestiago merged commit 159a446 into VeryGoodOpenSource:main Jan 30, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants