-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Dev/v0.2 - Enable streaming support for openai v1.0.0b3 #491
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,14 @@ | |
| from flaml.automl.logger import logger_formatter | ||
|
|
||
| from autogen.oai.openai_utils import get_key | ||
| from autogen.token_count_utils import count_token | ||
|
|
||
| try: | ||
| from openai import OpenAI, APIError | ||
| from openai.types.chat import ChatCompletion | ||
| from openai.types.chat.chat_completion import ChatCompletionMessage, Choice | ||
| from openai.types.completion import Completion | ||
| from openai.types.completion_usage import CompletionUsage | ||
| import diskcache | ||
|
|
||
| ERROR = None | ||
|
|
@@ -233,9 +236,8 @@ def yes_or_no_filter(context, response): | |
| response.pass_filter = pass_filter | ||
| # TODO: add response.cost | ||
| return response | ||
| completions = client.chat.completions if "messages" in params else client.completions | ||
| try: | ||
| response = completions.create(**params) | ||
| response = self._completions_create(client, params) | ||
| except APIError: | ||
| logger.debug(f"config {i} failed", exc_info=1) | ||
| if i == last: | ||
|
|
@@ -246,6 +248,67 @@ def yes_or_no_filter(context, response): | |
| cache.set(key, response) | ||
| return response | ||
|
|
||
| def _completions_create(self, client, params): | ||
| completions = client.chat.completions if "messages" in params else client.completions | ||
| # If streaming is enabled, has messages, and does not have functions, then | ||
| # iterate over the chunks of the response | ||
| if params.get("stream", False) and "messages" in params and "functions" not in params: | ||
| response_contents = [""] * params.get("n", 1) | ||
| finish_reasons = [""] * params.get("n", 1) | ||
| completion_tokens = 0 | ||
|
|
||
| # Set the terminal text color to green | ||
| print("\033[32m", end="") | ||
|
|
||
| # Send the chat completion request to OpenAI's API and process the response in chunks | ||
| for chunk in completions.create(**params): | ||
| if chunk.choices: | ||
| for choice in chunk.choices: | ||
| content = choice.delta.content | ||
| finish_reasons[choice.index] = choice.finish_reason | ||
| # If content is present, print it to the terminal and update response variables | ||
| if content is not None: | ||
| print(content, end="", flush=True) | ||
| response_contents[choice.index] += content | ||
| completion_tokens += 1 | ||
| else: | ||
| print() | ||
|
|
||
| # Reset the terminal text color | ||
| print("\033[0m\n") | ||
|
|
||
| # Prepare the final ChatCompletion object based on the accumulated data | ||
| model = chunk.model.replace("gpt-35", "gpt-3.5") # hack for Azure API | ||
| prompt_tokens = count_token(params["messages"], model) | ||
| response = ChatCompletion( | ||
| id=chunk.id, | ||
| model=chunk.model, | ||
| created=chunk.created, | ||
| object="chat.completion", | ||
| choices=[], | ||
| usage=CompletionUsage( | ||
| prompt_tokens=prompt_tokens, | ||
| completion_tokens=completion_tokens, | ||
| total_tokens=prompt_tokens + completion_tokens, | ||
| ), | ||
| ) | ||
| for i in range(len(response_contents)): | ||
| response.choices.append( | ||
| Choice( | ||
| index=i, | ||
| finish_reason=finish_reasons[i], | ||
| message=ChatCompletionMessage( | ||
| role="assistant", content=response_contents[i], function_call=None | ||
| ), | ||
| ) | ||
| ) | ||
| else: | ||
| # If streaming is not enabled, send a regular chat completion request | ||
| # Ensure streaming is disabled | ||
| params["stream"] = False | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? I'd like to avoid modifying the original dict if not necessary. |
||
| response = completions.create(**params) | ||
| return response | ||
|
|
||
| @classmethod | ||
| def extract_text_or_function_call(cls, response: ChatCompletion | Completion) -> List[str]: | ||
| """Extract the text or function calls from a completion or chat response. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.