-
Notifications
You must be signed in to change notification settings - Fork 9
docs(models): config-selectable backends (backend: <module>) #556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+95
−1
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
321d95c
docs(models): document registerBackend / defineBackend custom backends
heskew 2144c0f
docs(models): import models in custom-backend example; note generateS…
heskew c355cff
docs(models): set custom-backend VersionBadge to v5.1.15; note stream…
heskew 5d323d3
docs(models): document config-selectable backends (backend: <module>)
heskew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -134,3 +134,87 @@ models: | |||||
| | `model` | — | Bedrock model identifier; the vendor prefix selects the request format | | ||||||
|
|
||||||
| The model identifier's vendor prefix (`anthropic.`, `meta.`, `amazon.titan-`, `cohere.`, `mistral.`) determines the request/response format Harper uses; an unrecognized prefix is rejected with an error. Tool support depends on the underlying model family. Bedrock embedding APIs accept one text per request, so batch `embed()` calls are issued sequentially. | ||||||
|
|
||||||
| ## Custom backends | ||||||
|
|
||||||
| <VersionBadge version="v5.1.15" /> | ||||||
|
|
||||||
| Beyond the four built-ins, a component or application can register its own backend — including an in-process one that runs inference locally instead of calling an HTTP service. A registered backend is selected by its logical name through the same `model` option as a configured backend. | ||||||
|
|
||||||
| Custom backends can be added two ways: **registered programmatically** (below), or **selected in config** by pointing the `backend` field at a module — see [Config-selectable backends](#config-selectable-backends). | ||||||
|
|
||||||
| ### defineBackend() | ||||||
|
|
||||||
| ```typescript | ||||||
| defineBackend(spec: DefineBackendSpec): ModelBackend | ||||||
| ``` | ||||||
|
|
||||||
| Builds a `ModelBackend` from the methods it implements. `capabilities()` is derived from which of `embed` / `generate` / `generateStream` are supplied; `tools` and `adapters` cannot be inferred from method presence, so declare them explicitly. | ||||||
|
|
||||||
| | Field | Type | Default | Description | | ||||||
| | ---------------- | ---------- | ------- | -------------------------------------------------------------------- | | ||||||
| | `name` | `string` | — | Backend name, used in analytics and error messages (required) | | ||||||
| | `embed` | `function` | — | `embed(input, opts)` implementation, if the backend embeds | | ||||||
| | `generate` | `function` | — | `generate(input, opts)` implementation, if the backend generates | | ||||||
| | `generateStream` | `function` | — | `generateStream(input, opts)` implementation, if the backend streams | | ||||||
| | `tools` | `boolean` | `false` | Whether `generate` supports tool calls | | ||||||
| | `adapters` | `boolean` | `false` | Whether the backend supports per-call adapter selection | | ||||||
|
|
||||||
| `embed` and `generate` return the shape the built-in backends return: `{ status: 'completed', output, usage? }`, where `output` is `Float32Array[]` for `embed` and `{ content, finishReason }` for `generate`. `generateStream` is an async generator yielding incremental `{ deltaContent?, deltaToolCalls?, finishReason? }` chunks — the same [`generateStream()`](./api#generatestream) shape, not a wrapped result. At least one method must be supplied. A backend that supplies only `generateStream` still satisfies `generate()`: Harper drains the stream into a single result. | ||||||
|
|
||||||
| ### registerBackend() | ||||||
|
|
||||||
| ```typescript | ||||||
| registerBackend(kind: 'embedding' | 'generative', id: string, backend: ModelBackend): void | ||||||
| ``` | ||||||
|
|
||||||
| Registers `backend` under the logical name `id` for the given `kind`. Also available as `models.registerBackend(...)` / `scope.models.registerBackend(...)`. Register during component initialization (for example, in `handleApplication`) so the backend is in place before requests arrive; the registry is process-wide, so each worker thread that loads the component registers its own instance. | ||||||
|
|
||||||
| Use a provider-namespaced `id` (e.g. `local:bge-small`) to avoid collisions when more than one component registers backends. | ||||||
|
|
||||||
| ```javascript | ||||||
| import { models, registerBackend, defineBackend } from 'harper'; | ||||||
| import { init, embed } from 'some-local-embedding-library'; | ||||||
|
|
||||||
| await init(); | ||||||
|
|
||||||
| registerBackend( | ||||||
| 'embedding', | ||||||
| 'local:bge-small', | ||||||
| defineBackend({ | ||||||
| name: 'local:bge-small', | ||||||
| async embed(input) { | ||||||
| const texts = Array.isArray(input) ? input : [input]; | ||||||
| const vectors = await embed(texts); | ||||||
| return { status: 'completed', output: vectors.map((v) => Float32Array.from(v)) }; | ||||||
| }, | ||||||
| }) | ||||||
| ); | ||||||
|
|
||||||
| // Selected like any other model: | ||||||
| const [vector] = await models.embed('What is Harper?', { model: 'local:bge-small' }); | ||||||
| ``` | ||||||
|
|
||||||
| A registered backend takes precedence over a configuration entry with the same logical name, because registration runs after the configuration is loaded. A backend whose `capabilities()` disagrees with the methods it actually implements is registered as-is and fails at call time — `defineBackend()` keeps the two consistent. | ||||||
|
|
||||||
| ### Config-selectable backends | ||||||
|
|
||||||
| <VersionBadge version="v5.1.16" /> | ||||||
|
|
||||||
| A `backend` value in the [`models` configuration](./overview#configuration) that isn't a built-in name is resolved as a **module specifier** and imported at startup; the module's default export — or a `register` export — is a factory that registers the backend. This lets an operator select a custom backend entirely from config, the same way the built-ins are selected. | ||||||
|
|
||||||
| ```yaml | ||||||
| models: | ||||||
| embedding: | ||||||
| default: | ||||||
| backend: '@acme/embedder' # an installed package | ||||||
| model: bge-small | ||||||
| ``` | ||||||
|
|
||||||
| The `backend` specifier is resolved as: | ||||||
|
|
||||||
| - a **bare package** (`@acme/embedder`) — resolved from the Harper instance's `node_modules`; install the backend as a dependency. Preferred, since it carries no filesystem path and travels with the deployment. | ||||||
| - an **instance-root-relative path** (`./backends/local.js`) — resolved against the Harper instance root. | ||||||
| - an **absolute path**. | ||||||
|
|
||||||
| The factory has the signature `({ logicalName, kind, config }) => void | Promise<void>` and registers via [`registerBackend`](#registerbackend); it receives the config entry with `${VAR}` placeholders already resolved. A `backend` that is neither a built-in nor an importable module is logged and skipped at startup, leaving other entries unaffected. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make the integration clearer for developers implementing custom backends, explicitly state that the factory should register the backend by calling
Suggested change
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better grammatical flow and professionalism, consider changing 'added two ways' to 'added in two ways'.