SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
960 B · 17 lines typescript
Raw Blame History
1// Embeddings with the Node SDK (client.embeddings.create) + cosine similarity via dot product (vectors are unit-norm).2// STATUS: LIVE_VERIFIED 2026-09-18 (text-embedding-3-small, dimensions=64). Cost negligible.3// Run: node --env-file=.env --experimental-strip-types examples/openai/embeddings/create.ts4import OpenAI from "openai";56const client = new OpenAI();7const res = await client.embeddings.create({8  model: "text-embedding-3-small",9  input: ["OK", "Reply with OK.", "The weather in Montreal"],10  dimensions: 64, // Matryoshka shortening, still L2-normalized by the API11});12const vecs = res.data.map((d) => d.embedding as number[]);13const dot = (a: number[], b: number[]) => a.reduce((s, x, i) => s + x * b[i], 0);14console.log("model:", res.model, "dims:", vecs[0].length, "usage:", res.usage);15console.log("cos(OK, Reply with OK.) =", dot(vecs[0], vecs[1]).toFixed(4));16console.log("cos(OK, weather)        =", dot(vecs[0], vecs[2]).toFixed(4));17