Text embeddings · Embed a single string2 / 5

Embed a single string

Example on GitHub(packages/sdk/examples/embed-p2p.ts)

Now that we have a model in memory, let's hand it a string and see what comes back.

embed({ modelId, text: "..." }) resolves to { embedding: number[] }. For GTE_LARGE_FP16 the array has 1024 numbers, one per feature the model learned during training. Each number is a small float, mostly between -1 and 1.

We don't need to understand what each number means yet. We're going to compare vectors in the next lessons. For now, the part to remember is that every input produces a 1024-number array on GTE_LARGE_FP16.

The embed() call for one text takes a modelId and a single text string. For GTE_LARGE_FP16 the return is a 1024-number array. You would call it like so:

const { embedding } = await embed({
  modelId,
  text: "Hello, world!",
});
console.log("Input:", "Hello, world!");
console.log("Embedding dimensions:", embedding.length);
console.log("First 10 values:", embedding.slice(0, 10));

embedding is a number[]. Use .length for the dimension and .slice(0, 10) to peek at the first few values without spamming the console.

Note: the SDK always returns the same number[] length regardless of how long the input text is. Short sentences and paragraphs both produce a 1024-number vector for GTE_LARGE_FP16. The numbers are different, but the length is identical.

Questions

Question 1 of 2

What does embed resolve to for a single string?

Question 2 of 2

Given a short sentence and a full paragraph embedded with the same model, what stays the same between the two results?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.