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

Public helper for recombining ChatCompletionChunks into ChatCompletion #1833

Open
1 task done
jackmpcollins opened this issue Oct 30, 2024 · 1 comment
Open
1 task done
Labels
enhancement New feature or request

Comments

@jackmpcollins
Copy link

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • This is a feature request for the Python library

Describe the feature or improvement you're requesting

After streaming a chat completion response it is often necessary to recombine the streamed chunks into a message. Two examples are

Currently the internal class ChatCompletionStreamState makes this easy, but it is private which indicates it should not be relied on. Would it be possible to make this or similar functionality a supported part of the public API?

The current feature set of ChatCompletionStreamState is ideal:

  • get a ChatCompletion at any point during the stream (current_completion_snapshot). This allows logging a partial stream response in case of error, including if max_tokens was reached.
  • parse the chunks into correct pydantic BaseModels for the tools/response_format (get_final_completion())

Example usage of the existing class

import openai
from openai.lib.streaming.chat._completions import ChatCompletionStreamState

state = ChatCompletionStreamState(
    input_tools=openai.NOT_GIVEN,
    response_format=openai.NOT_GIVEN,
)

response = client.chat.completions.create(...)
for chunk in response:
    state.handle_chunk(chunk)

print(state.current_completion_snapshot)
print(state.get_final_completion())

Additional context

No response

@RobertCraigie
Copy link
Collaborator

Thanks for the suggestion, I think this is a good idea!

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

2 participants