Skip to content

Commit ee7b566

Browse files
committed
Rewrite the README for first-time readers
1 parent 57f1631 commit ee7b566

1 file changed

Lines changed: 77 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 77 additions & 16 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,6 +20,8 @@ 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

@@ -30,24 +34,81 @@ const vectors = await embedTexts(["a", "b"], config);
3034
console.log(vectors.length, vectors[0]?.length); // 2 768
3135
```
3236

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`.
37+
## Where it fits
38+
39+
[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.
40+
41+
- **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.
42+
- **Plugs into:** [`@intx/inference`](https://github.com/faremeter/interchange) for transport, retries and error classification, and [`@intx/types`](https://github.com/faremeter/interchange).
43+
- **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.
44+
45+
## Reference
46+
47+
| Export | Description |
48+
| -------------------------------------------- | ----------------------------------------------------------- |
49+
| `embedTexts(texts, config, options?)` | Embeds the texts and returns one vector per text, in order. |
50+
| `probeEmbedDims(config, options?)` | Embeds one probe string and returns the vector dimension. |
51+
| `EmbedConfigSchema`, `EmbedConfig` | Schema and type for `config`. |
52+
| `EmbedOptions` | Type for `options`. |
53+
| `EmbeddingRequestError` | Thrown when a request fails. |
54+
| `RequestDependencies`, `RetryAfterExtractor` | Types for `options.deps` and `options.extractRetryAfterMs`. |
3855

39-
## Dimensionality
56+
### Config
4057

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`.
58+
| Field | Type | Description |
59+
| ---------------- | ---------------------- | ------------------------------------------------------------------------ |
60+
| `baseURL` | `string` | Server root with its version path, such as `http://host:11434/v1`. |
61+
| `model` | `string` | Model name. |
62+
| `apiKey` | `string?` | Sent as a bearer token. |
63+
| `dimensions` | `number?` | Requested output dimension. Sent only when set; not all models honor it. |
64+
| `encodingFormat` | `"float" \| "base64"?` | Wire format. Defaults to `float`. Results are always `number[]`. |
65+
| `batchSize` | `number?` | Texts per request. Defaults to 32. |
66+
| `timeoutMs` | `number?` | Per-request timeout. Defaults to 30000. |
4267

43-
## Errors
68+
### Options
4469

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.
70+
| Field | Description |
71+
| --------------------- | ------------------------------------------------------------------------------- |
72+
| `deps` | `{ fetch, scheduler }`. Defaults to global `fetch` and Interchange's scheduler. |
73+
| `retryPolicy` | Retry policy. Defaults to Interchange's policy. |
74+
| `extractRetryAfterMs` | Reads the server's retry delay. Defaults to parsing `Retry-After`. |
75+
| `signal` | Aborts all pending requests. |
76+
77+
### Errors
78+
79+
A failed request throws `EmbeddingRequestError` with a classified `reason` and the request `url`. An invalid config throws before any request is sent.
80+
81+
### Dimensions
82+
83+
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.
4684

4785
## Using with Interchange
4886

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.
87+
`@intx/inference` and `@intx/types` are peer dependencies, so your host's Interchange version supplies them.
88+
89+
To share the retry scheduler with the rest of your host, pass it in:
90+
91+
```ts
92+
import { createDefaultScheduler } from "@intx/inference";
93+
import { embedTexts } from "@corbits/embedding";
94+
95+
const scheduler = createDefaultScheduler();
96+
const config = {
97+
baseURL: "http://localhost:11434/v1",
98+
model: "nomic-embed-text",
99+
};
100+
const vectors = await embedTexts(["hello"], config, {
101+
deps: { fetch, scheduler },
102+
});
103+
```
104+
105+
## Upgrading from 0.1
106+
107+
- Install `@intx/inference` and `@intx/types` (^0.4.0) yourself. They are now peer dependencies.
108+
- `ModelRequestError` is renamed to `EmbeddingRequestError`.
109+
- `runJSONRequest`, `extractRetryAfterMs` and `RunRequestOptions` are no longer exported. To change how retry waits are read, pass `options.extractRetryAfterMs`.
110+
- Config and returned vectors are unchanged.
50111

51112
## License
52113

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

0 commit comments

Comments
 (0)