Hands-on Quiz: MLOps and LLMOps
Test your understanding of production deployment, streaming protocols, and semantic caching.
Challenge 1: Server-Sent Events Formatter
Write a Python generator function that formats raw string chunks into the standard Server-Sent Events (SSE) protocol specification with data: prefix and double newline terminator.
def format_sse_stream(token_generator):
# Your implementation here
pass
View Solution
import json
def format_sse_stream(token_generator):
for token in token_generator:
payload = json.dumps({"token": token})
yield f"data: {payload}
"
yield "data: [DONE]
"
Challenge 2: Cosine Similarity Matcher for Semantic Cache
Given a target embedding vector $A$ and a candidate cached vector $B$, write the cosine similarity formula using NumPy and return True if similarity is $\ge 0.95$.
View Solution
import numpy as np
def is_semantic_cache_hit(vec_a: list[float], vec_b: list[float], threshold: float = 0.95) -> bool:
a = np.array(vec_a)
b = np.array(vec_b)
cosine_sim = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
return float(cosine_sim) >= threshold