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

Consider adding streaming support #97

Open
viictorjimenezzz opened this issue Nov 28, 2024 · 1 comment
Open

Consider adding streaming support #97

viictorjimenezzz opened this issue Nov 28, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@viictorjimenezzz
Copy link

viictorjimenezzz commented Nov 28, 2024

It would be very helpful if text responses could be streamed rather than requiring a full await. This feature would be especially beneficial for chat applications. For instance:

  • In the case of OpenAI, passing stream=True in the configuration is sufficient.
  • For AWS, the converse_stream() function needs to be called instead.

While I'm not a professional developer, I’d like to propose a potential implementation, which builds on the previous issue I raised (#96 ):

#aisuite/providers/openai_provider.py
def response_stream_generator(self, response):
        with response as stream:
            for text in stream:
                yield text.choices[0].delta.content

def chat_completions_create(self, model, messages, **kwargs):
        response = # same

        if kwargs.get("stream", False):
            return self.response_stream_generator(response)
        return response
#aisuite/providers/aws_provider.py
def response_stream_generator(self, response):
        stream = response.get('stream')
        if stream:
            for event in stream:
                chunk = event.get('contentBlockDelta', None)
                if not chunk:
                    continue

                yield chunk.get('delta', {}).get('text', '')

def chat_completions_create(self, model, messages, **kwargs):
        # ... filled inference_config and additional_model_request_fields
        
        additional_model_request_fields.pop("stream", None)

        # Call the Bedrock ConverseStrean API.
        if kwargs.get("stream", False):
            response = self.client.converse_stream(
                modelId=model,  # baseModelId or provisionedModelArn
                messages=formatted_messages,
                system=system_message,
                inferenceConfig=inference_config,
                additionalModelRequestFields=additional_model_request_fields,
            )
            return self.response_stream_generator(response)
        
        response = self.client.converse(
            modelId=model,  # baseModelId or provisionedModelArn
            messages=formatted_messages,
            system=system_message,
            inferenceConfig=inference_config,
            additionalModelRequestFields=additional_model_request_fields,
        )
        return self.normalize_response(response)

See: https://github.com/viictorjimenezzz/aisuite

Thank you for considering this request!

@ksolo ksolo added the enhancement New feature or request label Nov 30, 2024
@HOTSONHONET
Copy link

@viictorjimenezzz I would like to work on this concept.

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

No branches or pull requests

3 participants