Last active
September 25, 2024 01:13
-
-
Save ggorlen/7c944d73e27980544e29aa6de1f2ac54 to your computer and use it in GitHub Desktop.
Streaming OpenAI API responses in Python without a library
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 json | |
import requests | |
api_key = "" | |
url = "https://api.openai.com/v1/chat/completions" | |
headers = { | |
"Authorization": f"Bearer {api_key}", | |
"Content-Type": "application/json" | |
} | |
data = { | |
"temperature": 0.0, | |
"model": "gpt-4o", | |
"messages": [ | |
{"role": "system", "content": "You are a helpful assistant."}, | |
{"role": "user", "content": "Tell me a joke."} | |
], | |
"stream": True | |
} | |
response = requests.post(url, headers=headers, json=data, stream=True) | |
response.raise_for_status() | |
for line in response.iter_lines(): | |
line = line.decode("utf-8") | |
if line.startswith("data: ") and not line.endswith("[DONE]"): | |
data = json.loads(line[len("data: "):]) | |
chunk = data["choices"][0]["delta"].get("content", "") | |
print(chunk, end="", flush=True) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment