Skip to content

Instantly share code, notes, and snippets.

@ggorlen
Last active June 26, 2024 23:56
Show Gist options
  • Save ggorlen/7c944d73e27980544e29aa6de1f2ac54 to your computer and use it in GitHub Desktop.
Save ggorlen/7c944d73e27980544e29aa6de1f2ac54 to your computer and use it in GitHub Desktop.
Streaming OpenAI API responses in Python without a library
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