Created
March 2, 2023 18:12
-
-
Save transmissions11/96828f42a0f05d0eddf342addde8e169 to your computer and use it in GitHub Desktop.
Reproduces a bug in the OpenAI Chat completions API when streaming w/ n>1
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
import openai | |
openai.api_key = "[REDACTED]" | |
responses = {} | |
for resp in openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[{"role": "user", "content": "Write a hash function in java."}], | |
n=5, | |
stream=True, | |
): | |
choice = resp.choices[0] | |
delta = choice.delta | |
# Populate the dict with an empty string if the key doesn't exist. | |
responses[choice.index] = responses.get(choice.index, "") | |
if hasattr(delta, "content"): | |
responses[choice.index] += delta.content | |
if choice.finish_reason is not None: | |
print("\n" + str(choice.index) + " finished due to: " + choice.finish_reason) | |
for i in range(0, len(responses)): | |
print(responses[i]) | |
print("---------------") | |
print("\n\nObserve that some of the responses are cut off!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When running this you'll see one of two outputs (run many times and you should see both, generally #1 happens more often though):
1. Only one or two of the responses have reached a natural stopping point, while the rest are cut off prematurely.
2. One response completes fully and the rest have not received any tokens before stopping.