Skip to main content
← CoursesAI Engineering with PythonModule 2 · Tool Use & Function CallingEmbeddings: text → vectorswrite33 / 101
💬 Discuss🧪 Playground+100 XP
Task
📝 **Question:** **Write the function** \`cos_sim(a, b)\` — cosine similarity between two equal-length vectors of floats, no numpy. Round the result to 3 decimals. Formula: \`\`\` dot(a, b) / (norm(a) * norm(b)) \`\`\` where \`dot\` is the sum of paired products and \`norm\` is sqrt of sum of squares. Then compare a real query against three documents: \`\`\` query = [0.21, -0.04, 0.87, 0.14] # "How do I cancel my subscription?" refund = [0.19, -0.05, 0.85, 0.16] # "refund my plan please" — should be CLOSE weather = [0.92, 0.41, 0.02, 0.78] # "what is the capital of France?" — far unrelated = [0.00, 1.00, 0.00, 0.00] # orthogonal — exactly 0 with query? no, very small \`\`\` Print three lines: \`\`\` refund: 0.999 weather: 0.263 unrelated: -0.044 \`\`\` 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.

Keep going

✏️ Write your code here
🐍
Loading Python...
First visit only — ~5-10s. Stays cached afterward.
📊 Result
Press Run to see result...
📣 Help someone learn PythonShare this lesson with a friend — the first 15 are free, no signup.Tweet

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…