embedding
Turn text into embedding vectors. An embedding is a list of numbers that stands for the meaning of a piece of text. Two texts with similar meaning get vectors that are close together. You can use embeddings to find related documents, group similar notes, or rank matches for a query.
import { embed, embedMany, cosineSimilarity } from "std::embedding"
node main() {
const query = embed("a red bicycle in the rain")
const docs = embedMany(["apples", "oranges", "a bicycle"])
if (isFailure(query) || isFailure(docs)) { return }
const score = cosineSimilarity(query.value.vector, docs.value.vectors[2])
print(score)
}embed turns one string into one vector. embedMany turns a list of strings into one vector each, in the same order, with one provider call. cosineSimilarity compares two vectors and returns a number from -1 to
- Higher scores indicate more similar vector directions.
Provider usage counts toward the branch's cost and token totals, as with llm(). Cost guards apply.
The default model is OpenAI's text-embedding-3-small. Pass model to pick another one. The provider is derived from the model name, or you can set provider yourself. Ollama works as a local provider. Only compare vectors that came from the same model.
Types
Embedding
One vector and the model that produced it.
/** One vector and the model that produced it. */
export type Embedding = {
vector: number[];
model: string
}(source)
Embeddings
One vector per input, in input order, and the model that produced them.
/** One vector per input, in input order, and the model that produced them. */
export type Embeddings = {
vectors: number[][];
model: string
}(source)
Functions
embed
embed(
text: string,
model: string = "",
provider: string = "",
dimensions: number = 0,
apiKey: string = "",
baseUrl: string = "",
): Result<Embedding>Turn one piece of text into an embedding vector.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| text | string | ||
| model | string | "" | Embedding model (default: text-embedding-3-small). For a local provider (mlx, llama-cpp) this may be a catalog name, alias, or path |
| provider | string | "" | Override the provider (normally derived from the model name) |
| dimensions | number | 0 | Shorten the vector to this length, for models that support it (0 means the model's full length) |
| apiKey | string | "" | Override the API key |
| baseUrl | string | "" | Base URL for the ollama, deepinfra, litellm, openai-compat, or mlx provider |
Returns: Result<Embedding>
Needs: llm
(source)
embedMany
embedMany(
texts: string[],
model: string = "",
provider: string = "",
dimensions: number = 0,
apiKey: string = "",
baseUrl: string = "",
): Result<Embeddings>Turn a list of texts into embedding vectors in one call, one vector per text in the same order.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
| texts | string[] | ||
| model | string | "" | Embedding model (default: text-embedding-3-small). For a local provider (mlx, llama-cpp) this may be a catalog name, alias, or path |
| provider | string | "" | Override the provider (normally derived from the model name) |
| dimensions | number | 0 | Shorten each vector to this length, for models that support it (0 means the model's full length) |
| apiKey | string | "" | Override the API key |
| baseUrl | string | "" | Base URL for the ollama, deepinfra, litellm, openai-compat, or mlx provider |
Returns: Result<Embeddings>
Needs: llm
(source)
cosineSimilarity
cosineSimilarity(a: number[], b: number[]): Result<number>Compare equal-length, nonzero vectors from the same embedding model. Returns cosine similarity from -1 to 1; higher means more similar.
Parameters:
| Name | Type | Default |
|---|---|---|
| a | number[] | |
| b | number[] |
Returns: Result<number>
(source)