Skip to content

Commit 605e64e

Browse files
committed
docs: rewrite the README for first-time readers
1 parent a74f981 commit 605e64e

1 file changed

Lines changed: 65 additions & 20 deletions

File tree

‎README.md‎

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# @corbits/embedding
22

3-
One API for every embedding provider. Point at any OpenAI-compatible `/v1/embeddings` endpoint, swap models with a config change, and get ordered vectors back.
3+
Batched text embeddings over any OpenAI-compatible `/v1/embeddings` endpoint, built on `@intx/inference` retries and error classification. A retrieval building block for Corbits and Interchange agents that also works standalone.
44

5-
OpenAI, Ollama, TEI, vLLM, and Jina all serve the same wire shape. This package is one code path over that shape, with parameters for what varies between providers:
5+
## Why @corbits/embedding?
66

7-
- `dimensions` — Matryoshka truncation for models that support it, including OpenAI `text-embedding-3` (down to 256), Jina v3 (32), and Jina v4 (128). Sent only when set, so models without MRL stay happy.
8-
- `encodingFormat` — `float` (default) or compact `base64`.
9-
- `batchSize` — inputs per request (default 32). Tune it to provider caps and per-request token ceilings.
7+
1. **One client for every provider.** OpenAI, Ollama, TEI, vLLM and Jina all speak the same wire format. Switching models is a config change.
8+
2. **Order-safe batching.** Inputs are split into batches, and vectors come back in input order, placed by the index each response echoes. Short or duplicate replies throw instead of misaligning results.
9+
3. **Interchange retry semantics.** Requests run through `@intx/inference`, so a 429 backs off and a 401 fails the same way it does for inference calls. Failures throw a typed `EmbeddingRequestError`.
10+
11+
It does not store or search vectors. For that, pair it with [`@corbits/memory`](https://github.com/corbitsdev/corbits-memory).
1012

1113
## Install
1214

@@ -18,36 +20,79 @@ Runs on Bun >= 1.2 or Node >= 24.
1820

1921
## Quickstart
2022

23+
Needs a local Ollama with `nomic-embed-text` pulled.
24+
2125
```ts
2226
import { embedTexts } from "@corbits/embedding";
2327

24-
const config = {
28+
const vectors = await embedTexts(["a", "b"], {
2529
baseURL: "http://localhost:11434/v1",
2630
model: "nomic-embed-text",
27-
};
28-
29-
const vectors = await embedTexts(["a", "b"], config);
31+
});
3032
console.log(vectors.length, vectors[0]?.length); // 2 768
3133
```
3234

33-
`embedTexts(texts, config, options?)` batches sequentially, posts each batch to
34-
`{baseURL}/embeddings`, and returns vectors in input order. `options` is
35-
optional: `deps` (defaults to global `fetch` and `@intx/inference`'s
36-
`createDefaultScheduler()`),
37-
`retryPolicy`, `extractRetryAfterMs`, and `signal`.
35+
## Where it fits
36+
37+
[Interchange](https://github.com/faremeter/interchange) runs AI agents as principals (accounts that hold their own identity, permissions and credentials). Corbits packages add what an agent product needs around it.
38+
39+
- **Runs in:** any process: the Interchange hub (the multi-tenant control plane), an agent sidecar (the runtime next to each agent), or a plain script. No hub is required.
40+
- **Plugs into:** [`@intx/inference`](https://github.com/faremeter/interchange) for transport, retries and error classification, and [`@intx/types`](https://github.com/faremeter/interchange).
41+
- **Pairs with:** [`@corbits/reranking`](https://github.com/corbitsdev/corbits-reranking) to reorder results and [`@corbits/memory`](https://github.com/corbitsdev/corbits-memory) to store and search vectors.
42+
43+
## Reference
44+
45+
| Export | Description |
46+
| -------------------------------------------- | ----------------------------------------------------------- |
47+
| `embedTexts(texts, config, options?)` | Embeds the texts and returns one vector per text, in order. |
48+
| `probeEmbedDims(config, options?)` | Embeds one probe string and returns the vector dimension. |
49+
| `EmbedConfigSchema`, `EmbedConfig` | Schema and type for `config`. |
50+
| `EmbedOptions` | Type for `options`. |
51+
| `EmbeddingRequestError` | Thrown when a request fails. |
52+
| `RequestDependencies`, `RetryAfterExtractor` | Types for `options.deps` and `options.extractRetryAfterMs`. |
3853

39-
## Dimensionality
54+
### Config
4055

41-
`probeEmbedDims(config)` embeds one probe string and reports the length received. Dimensionality follows the provider and the `dimensions` setting, so callers that persist vectors discover it at startup and treat a model swap as a migration. Representative widths: 1536 for OpenAI `text-embedding-3-small`, 768 for `nomic-embed-text`, 384 for `bge-small-en-v1.5`.
56+
| Field | Type | Description |
57+
| ---------------- | ---------------------- | ------------------------------------------------------------------------ |
58+
| `baseURL` | `string` | Server root with its version path, such as `http://host:11434/v1`. |
59+
| `model` | `string` | Model name. |
60+
| `apiKey` | `string?` | Sent as a bearer token. |
61+
| `dimensions` | `number?` | Requested output dimension. Sent only when set; not all models honor it. |
62+
| `encodingFormat` | `"float" \| "base64"?` | Wire format. Defaults to `float`. Results are always `number[]`. |
63+
| `batchSize` | `number?` | Texts per request. Defaults to 32. |
64+
| `timeoutMs` | `number?` | Per-request timeout. Defaults to 30000. |
4265

43-
## Errors
66+
### Options
4467

45-
Every request failure — transport, HTTP status, or a 200 with an unexpected body — throws `EmbeddingRequestError` (`extends Error`), carrying the classified `InferenceError` as `reason` and the request `url`. A config that fails `EmbedConfigSchema` throws arktype's `TraversalError` before any request.
68+
| Field | Description |
69+
| --------------------- | ------------------------------------------------------------------------------- |
70+
| `deps` | `{ fetch, scheduler }`. Defaults to global `fetch` and Interchange's scheduler. |
71+
| `retryPolicy` | Retry policy. Defaults to Interchange's policy. |
72+
| `extractRetryAfterMs` | Reads the server's retry delay. Defaults to parsing `Retry-After`. |
73+
| `signal` | Aborts all pending requests. |
74+
75+
### Errors
76+
77+
A failed request throws `EmbeddingRequestError` with a classified `reason` and the request `url`. An invalid config throws before any request is sent.
78+
79+
### Dimensions
80+
81+
Each model has a fixed output dimension: 768 for `nomic-embed-text`, 1536 for OpenAI `text-embedding-3-small`. If you store vectors, call `probeEmbedDims` at startup. Changing models changes the dimension, so treat it as a schema migration.
4682

4783
## Using with Interchange
4884

49-
Requests go through `deps.fetch`, failures are classified into `@intx/inference`'s `InferenceError`, and `createDefaultRetryPolicy` decides retries: a 429 backs off and retries, a credential failure does not. `@intx/inference` and `@intx/types` are peer dependencies, so the host's Interchange version is the one used.
85+
`@intx/inference` and `@intx/types` are peer dependencies, so your host's Interchange version supplies them.
86+
87+
To share your host's retry scheduler, pass it as `options.deps.scheduler` along with `fetch`.
88+
89+
## Upgrading from 0.1
90+
91+
- Install `@intx/inference` and `@intx/types` (^0.4.0) yourself. They are now peer dependencies.
92+
- `ModelRequestError` is renamed to `EmbeddingRequestError`.
93+
- `runJSONRequest`, `extractRetryAfterMs` and `RunRequestOptions` are no longer exported. To change how retry waits are read, pass `options.extractRetryAfterMs`.
94+
- Config and returned vectors are unchanged.
5095

5196
## License
5297

53-
LGPL-2.1-only — see [`LICENSE`](LICENSE).
98+
[LGPL-2.1-only](https://github.com/corbitsdev/corbits-embedding/blob/main/LICENSE)

0 commit comments

Comments
 (0)