Skip to content

Instantly share code, notes, and snippets.

@dpricha89
Created February 5, 2023 20:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dpricha89/38958cb8d305fe746f0c458b7a560579 to your computer and use it in GitHub Desktop.
Save dpricha89/38958cb8d305fe746f0c458b7a560579 to your computer and use it in GitHub Desktop.
Using SSE with GPT API
# listen.py
#
import requests
import json
import pprint
# pip install sseclient-py
import sseclient
API_KEY = '<INSERT_API_KEY>'
def with_requests(url, headers, body):
"""Get a streaming response for the given event feed using requests."""
import requests
return requests.post(url, stream=True, headers=headers, json=body)
if __name__ == '__main__':
url = 'https://api.openai.com/v1/completions'
headers = {
'Accept': 'text/event-stream',
'Authorization': 'Bearer ' + API_KEY
}
body = {
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0,
"stream": True
}
request = with_requests(url, headers, body)
client = sseclient.SSEClient(request)
for event in client.events():
if event.data != '[DONE]':
print(json.loads(event.data)['choices'][0]['text'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment