Based on the title "Build more responsive apps with the Decisions API", students should:
β οΈ Note: The original article content failed to load (500 error). I'll teach this topic based on established OpenAI API knowledge.
Traditional AI API calls work like this:
User Request β Wait... Wait... Wait... β Full Response Arrives
Problems:
Responsive apps need to:
User Request β Partial Response β More Data β Complete
β Show immediately β Update UI
| Unresponsive App | Responsive App |
|---|---|
| Waits for full response | Streams partial results |
| Single blocking call | Incremental updates |
| User waits silently | User sees progress |
from openai import OpenAI
client = OpenAI()
# UNRESPONSIVE approach
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Explain quantum physics"}]
)
print(response.choices[0].message.content) # Waits for everything
# RESPONSIVE approach with streaming
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Explain quantum physics"}],
stream=True # β Key difference
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="", flush=True)
# β Shows text as it arrives
A Decisions API pattern means your app makes smart routing decisions:
def smart_decision_router(user_input):
"""
Decide HOW to handle a request before processing it
"""
# Decision 1: Is this simple or complex?
if len(user_input.split()) < 10:
model = "gpt-3.5-turbo" # Fast, cheap
else:
model = "gpt-4" # Powerful, thorough
# Decision 2: Does it need streaming?
needs_stream = len(user_input) > 50
return model, needs_stream
# Usage
model_choice, use_stream = smart_decision_router(user_input)
from openai import OpenAI
import time
client = OpenAI()
class ResponsiveApp:
def __init__(self):
self.response_buffer = []
def process_with_decisions(self, user_query):
"""
Makes decisions BEFORE calling API for better responsiveness
"""
# DECISION 1: Acknowledge immediately
print("π Processing your request...")
# DECISION 2: Choose strategy
strategy = self.decide_strategy(user_query)
# DECISION 3: Execute with streaming
if strategy["use_stream"]:
return self.stream_response(user_query, strategy["model"])
else:
return self.quick_response(user_query, strategy["model"])
def decide_strategy(self, query):
"""Core decision logic"""
return {
"model": "gpt-4" if "complex" in query else "gpt-3.5-turbo",
"use_stream": True,
"max_tokens": 500
}
def stream_response(self, query, model):
"""Stream for responsiveness"""
print("\nπ Response:\n")
stream = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": query}],
stream=True
)
full_response = ""
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
full_response += content
return full_response
# Run it
app = ResponsiveApp()
app.process_with_decisions("Explain machine learning to me")
import asyncio
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def parallel_decisions(queries: list):
"""
Handle multiple decisions simultaneously
= Much more responsive for complex apps
"""
tasks = [
client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": q}]
)
for q in queries
]
# All decisions made in PARALLEL, not sequence
responses = await asyncio.gather(*tasks)
return [r.choices[0].message.content for r in responses]
# Usage
queries = ["What is AI?", "What is ML?", "What is NLP?"]
results = asyncio.run(parallel_decisions(queries))
RESPONSIVE APP DECISION FRAMEWORK
==================================
1. ACKNOWLEDGE β Tell user something is happening
β
2. DECIDE β Choose best model/approach
β
3. STREAM β Show results as they arrive
β
4. PARALLELIZE β Handle multiple tasks at once
β
5. FALLBACK β Have backup if something fails
| Question | Answer |
|---|---|
| Why use streaming? | Shows results immediately vs waiting |
| What is a decision point? | Logic that routes requests optimally |
| How to parallelize? | Use asyncio.gather() |
| When to use GPT-4 vs 3.5? | Complex vs simple queries |
You should now be able to: