Created
June 14, 2023 15:40
-
-
Save jerrylususu/183665c5dc1bf12a1e89bdbd4816ae81 to your computer and use it in GitHub Desktop.
simple gpt streaming implementation (works as 2023/6/14)
This file contains 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 requests | |
import json | |
import sseclient | |
# API_KEY = 'sk-xxx' | |
def performRequestWithStreaming(): | |
reqUrl = 'https://api.openai.com/v1/chat/completions' | |
reqHeaders = { | |
'Accept': 'text/event-stream', | |
'Authorization': 'Bearer ' + API_KEY | |
} | |
reqBody = { | |
"model": "gpt-3.5-turbo-16k", | |
"messages": [{"role": "system", "content": "you are a helpful assistant"}, {"role":"user","content":"hello"}], | |
"max_tokens": 1000, | |
"temperature": 0, | |
"stream": True, | |
} | |
request = requests.post(reqUrl, stream=True, headers=reqHeaders, json=reqBody) | |
client = sseclient.SSEClient(request) | |
text = "" | |
for event in client.events(): | |
# print(event.data) | |
# print(json.loads(event.data)) | |
if event.data != '[DONE]': | |
delta = json.loads(event.data)['choices'][0]['delta'] | |
if "content" in delta: | |
text += delta["content"] | |
print("\r"+text, end="", flush=True), | |
if __name__ == '__main__': | |
performRequestWithStreaming() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment