python --version
pip install openai fastapi uvicorn
export OPENAI_API_KEY=sk-...
Enable streaming to receive tokens as they are generated, print them in real time, and measure time-to-first-token.
1 import time 2 from openai import OpenAI 3 4 client = OpenAI() 5 6 def stream_chat(prompt: str) -> str: 7 start = time.monotonic() 8 first_token_time = None 9 full_response = "" 10 11 stream = client.chat.completions.create( 12 model="gpt-4o-mini", 13 messages=[{"role": "user", "content": prompt}], 14 stream=True, 15 ) 16 17 for chunk in stream: 18 delta = chunk.choices[0].delta.content 19 if delta: 20 if first_token_time is None: 21 first_token_time = time.monotonic() 22 ttft = first_token_time - start 23 print(f"\n[TTFT: {ttft:.3f}s]\n") 24 print(delta, end="", flush=True) 25 full_response += delta 26 27 total = time.monotonic() - start 28 print(f"\n\n[Total: {total:.3f}s | {len(full_response)} chars]") 29 return full_response 30 31 if __name__ == "__main__": 32 stream_chat("Explain quantum entanglement in 3 paragraphs.") 33
Sign in to share your feedback and join the discussion.