Skip to content

Commit 42314c6

Browse files
committed
Rewrite the README for first-time readers
1 parent 1a7678b commit 42314c6

1 file changed

Lines changed: 71 additions & 16 deletions

File tree

‎README.md‎

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
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+
Embedding vectors for Interchange agents: a retrieval primitive (a small search building block) that plugs into `@intx/inference`.
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+
An embedding is a list of numbers that captures what a piece of text means. Similar texts get similar numbers, so you can search by meaning. This package sends your text to an embedding model and gives you the vectors back in order. It works with any server that speaks the OpenAI embeddings format, including OpenAI, Ollama, TEI, vLLM and Jina. To switch models, change the config. It does not store or search vectors. For that, pair it with [`@corbits/memory`](https://github.com/corbitsdev/corbits-memory).
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+
## Where it fits
8+
9+
[Interchange](https://github.com/faremeter/interchange) runs AI agents with their own identity and permissions. Corbits packages add the parts an agent product needs around it.
10+
11+
- **Runs in:** any process. That can be the Interchange hub (the server that manages tenants and agents), an agent sidecar (the runtime next to each agent), or a plain script. No hub is needed.
12+
- **Plugs into:** [`@intx/inference`](https://github.com/faremeter/interchange) for retries and error types, and [`@intx/types`](https://github.com/faremeter/interchange).
13+
- **Pairs with:** [`@corbits/reranking`](https://github.com/corbitsdev/corbits-reranking) to sort search results, and [`@corbits/memory`](https://github.com/corbitsdev/corbits-memory) to store and search vectors.
1014

1115
## Install
1216

@@ -18,6 +22,8 @@ Runs on Bun >= 1.2 or Node >= 24.
1822

1923
## Quickstart
2024

25+
This example needs a local Ollama with the model pulled: `ollama pull nomic-embed-text`.
26+
2127
```ts
2228
import { embedTexts } from "@corbits/embedding";
2329

@@ -30,24 +36,73 @@ const vectors = await embedTexts(["a", "b"], config);
3036
console.log(vectors.length, vectors[0]?.length); // 2 768
3137
```
3238

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`.
39+
## Reference
40+
41+
| Export | Description |
42+
| -------------------------------------------- | ----------------------------------------------------------- |
43+
| `embedTexts(texts, config, options?)` | Embeds the texts and returns one vector per text, in order. |
44+
| `probeEmbedDims(config, options?)` | Embeds one test string and returns the vector length. |
45+
| `EmbedConfigSchema`, `EmbedConfig` | Schema and type for `config`. |
46+
| `EmbedOptions` | Type for `options`. |
47+
| `EmbeddingRequestError` | Thrown when a request fails. |
48+
| `RequestDependencies`, `RetryAfterExtractor` | Types for `options.deps` and `options.extractRetryAfterMs`. |
49+
50+
### Config
3851

39-
## Dimensionality
52+
| Field | Type | Description |
53+
| ---------------- | ---------------------- | -------------------------------------------------------------------------- |
54+
| `baseURL` | `string` | Server root with its version path, such as `http://host:11434/v1`. |
55+
| `model` | `string` | Model name. |
56+
| `apiKey` | `string?` | Sent as a bearer token. |
57+
| `dimensions` | `number?` | Asks for shorter vectors. Only some models support it. Sent only when set. |
58+
| `encodingFormat` | `"float" \| "base64"?` | Wire format. Defaults to `float`. You always get numbers back. |
59+
| `batchSize` | `number?` | Texts per request. Defaults to 32. |
60+
| `timeoutMs` | `number?` | Time limit per request. Defaults to 30000. |
4061

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`.
62+
### Options
4263

43-
## Errors
64+
| Field | Description |
65+
| --------------------- | ------------------------------------------------------------------------------- |
66+
| `deps` | `{ fetch, scheduler }`. Defaults to global `fetch` and Interchange's scheduler. |
67+
| `retryPolicy` | Decides when to retry. Defaults to Interchange's policy. |
68+
| `extractRetryAfterMs` | Reads how long the server asks you to wait. Defaults to `Retry-After`. |
69+
| `signal` | Cancels the requests. |
4470

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.
71+
### Errors
72+
73+
A failed request throws `EmbeddingRequestError`. Its `reason` says what went wrong, and `url` says where. A bad config throws before any request is sent.
74+
75+
### Vector length
76+
77+
Each model returns vectors of a fixed length. `nomic-embed-text` returns 768 numbers, and OpenAI `text-embedding-3-small` returns 1536. If you store vectors, call `probeEmbedDims` at startup to learn the length. Changing models changes the length, so treat it like a database migration.
4678

4779
## Using with Interchange
4880

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.
81+
`@intx/inference` and `@intx/types` are peer dependencies. Your host's Interchange version supplies them. Rate limits (HTTP 429) are retried after a pause. Bad credentials are not retried.
82+
83+
The scheduler is the Interchange object that times retries. To share one with the rest of your host, pass it in:
84+
85+
```ts
86+
import { createDefaultScheduler } from "@intx/inference";
87+
import { embedTexts } from "@corbits/embedding";
88+
89+
const scheduler = createDefaultScheduler();
90+
const config = {
91+
baseURL: "http://localhost:11434/v1",
92+
model: "nomic-embed-text",
93+
};
94+
const vectors = await embedTexts(["hello"], config, {
95+
deps: { fetch, scheduler },
96+
});
97+
```
98+
99+
## Upgrading from 0.1
100+
101+
- Install `@intx/inference` and `@intx/types` (^0.4.0) yourself. They are now peer dependencies.
102+
- `ModelRequestError` is renamed to `EmbeddingRequestError`.
103+
- `runJSONRequest`, `extractRetryAfterMs` and `RunRequestOptions` are no longer exported. To change how retry waits are read, pass `options.extractRetryAfterMs`.
104+
- Config and returned vectors are unchanged.
50105

51106
## License
52107

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

0 commit comments

Comments
 (0)