@aeye/replicate 0.3.8 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/aeye-replicate.md +123 -0
  2. package/package.json +5 -4
@@ -0,0 +1,123 @@
1
+ # @aeye/replicate
2
+
3
+ One-line purpose: Replicate provider for `@aeye/ai` — run any open-source model hosted on Replicate (LLMs, SDXL/Flux image models, Whisper, TTS, embeddings) through a per-model **ModelTransformer** adapter system.
4
+
5
+ ## When to use
6
+
7
+ - You want to call community/open-source models on Replicate (`owner/name`) via the unified `@aeye/ai` API.
8
+ - You need image generation/editing/analysis, transcription, speech, or embeddings from models that each have their own bespoke input/output schema.
9
+
10
+ Replicate has no consistent API across models, so **every model you use must have a transformer** registered in config that converts the `@aeye` request to that model's input and parses its output back.
11
+
12
+ ## Install / import
13
+
14
+ ```bash
15
+ npm install @aeye/replicate replicate
16
+ ```
17
+
18
+ `@aeye/ai`, `@aeye/core`, and `replicate` ship as dependencies.
19
+
20
+ ```typescript
21
+ import {
22
+ ReplicateProvider,
23
+ type ReplicateConfig,
24
+ type ReplicateTransformer,
25
+ type ReplicateHooks,
26
+ } from '@aeye/replicate';
27
+ ```
28
+
29
+ The provider's registry name is `replicate`.
30
+
31
+ ## Register with an `@aeye/ai` instance
32
+
33
+ ```typescript
34
+ import { AI } from '@aeye/ai';
35
+ import { ReplicateProvider } from '@aeye/replicate';
36
+
37
+ const replicate = new ReplicateProvider({
38
+ apiKey: process.env.REPLICATE_API_KEY!,
39
+ transformers: {
40
+ // keyed by Replicate model id "owner/name"
41
+ 'meta/meta-llama-3-70b-instruct': llamaTransformer,
42
+ },
43
+ });
44
+
45
+ const ai = AI.with().providers({ replicate }).create({ /* ... */ });
46
+
47
+ await ai.chat.get(
48
+ { messages: [{ role: 'user', content: 'Hi' }] },
49
+ { metadata: { model: 'meta/meta-llama-3-70b-instruct' } }
50
+ );
51
+ ```
52
+
53
+ ## Authentication / credentials
54
+
55
+ - `apiKey` is **required** and is passed to the `replicate` SDK as `auth`. Conventionally `process.env.REPLICATE_API_KEY` (the package does not auto-read any env var — you pass it).
56
+ - `baseUrl` (note lowercase `u`) optionally overrides the Replicate API host.
57
+
58
+ ## Configuration (`ReplicateConfig`)
59
+
60
+ | Field | Type | Notes |
61
+ |-------|------|-------|
62
+ | `apiKey` | `string` (required) | Replicate API token (sent as `auth`) |
63
+ | `baseUrl` | `string` | Optional API host override |
64
+ | `transformers` | `Record<string, ReplicateTransformer>` | Map of `owner/name` → adapter. **Required for any model you call** |
65
+ | `hooks` | `ReplicateHooks` | Per-operation `beforeRequest` / `afterRequest` / `onError` (the `input` arg is the raw Replicate payload) |
66
+
67
+ ## Model transformers (the core concept)
68
+
69
+ A `ReplicateTransformer` is a `ModelTransformer` (from `@aeye/ai`) with one entry per capability. The provider looks up `transformer.<capability>` and uses its converters:
70
+
71
+ | Operation | Transformer key | Converters used |
72
+ |-----------|-----------------|-----------------|
73
+ | `ai.chat.get` | `chat` | `convertRequest`, `parseResponse` |
74
+ | `ai.chat.stream` | `chat` | `convertRequest`, `parseChunk` |
75
+ | `ai.image.generate` | `imageGenerate` | `convertRequest`, `parseResponse` (+ `parseChunk` for stream) |
76
+ | `ai.image.edit` | `imageEdit` | `convertRequest`, `parseResponse` |
77
+ | image analyze | `imageAnalyze` | `convertRequest`, `parseResponse` |
78
+ | `ai.transcribe` | `transcribe` | `convertRequest`, `parseResponse` |
79
+ | `ai.speech` | `speech` | `convertRequest`, `parseResponse` |
80
+ | `ai.embed` | `embed` | `convertRequest`, `parseResponse` |
81
+
82
+ Minimal hand-written transformer for a chat model:
83
+
84
+ ```typescript
85
+ const llamaTransformer: ReplicateTransformer = {
86
+ chat: {
87
+ // map @aeye Request -> Replicate model input
88
+ convertRequest: async (request, ctx) => ({
89
+ prompt: request.messages.map(m => `${m.role}: ${m.content}`).join('\n'),
90
+ max_tokens: request.maxTokens,
91
+ temperature: request.temperature,
92
+ }),
93
+ // map Replicate output -> @aeye Response
94
+ parseResponse: async (output, ctx) => ({
95
+ content: Array.isArray(output) ? output.join('') : String(output),
96
+ }),
97
+ // optional: map a streamed event -> @aeye Chunk
98
+ parseChunk: async (event, ctx) => ({ content: String(event) }),
99
+ },
100
+ };
101
+ ```
102
+
103
+ Pre-built transformer collections may be available from a model-adapter package (e.g. an `@aeye/models` export) — pass them straight into `transformers`.
104
+
105
+ ## Execution model
106
+
107
+ - The provider resolves the model from `request.model` → request metadata → `ctx.metadata.model`. Missing model throws.
108
+ - It calls `client.run('owner/name', { input })` for non-streaming ops and `client.stream(...)` for streaming, where `input` is whatever your transformer's `convertRequest` returns.
109
+ - `response.model` is stamped automatically from the requested model.
110
+
111
+ ## Supported capabilities
112
+
113
+ Chat (+ streaming), image generation (+ streaming), image editing (+ streaming), image analysis (+ streaming), transcription (+ streaming), speech, and embeddings — **each gated on a transformer existing for that model and capability.**
114
+
115
+ `listModels()` pulls models from a few featured Replicate collections (`text-to-image`, `image-to-text`, `text-to-speech`, `speech-to-text`); you can still call any model by full `owner/name` regardless of what `listModels` returns.
116
+
117
+ ## Gotchas
118
+
119
+ - **No transformer = hard error.** Calling a model with no `transformers[model]` entry, or one missing the required `convertRequest`/`parseResponse` (or `parseChunk` for streaming), throws immediately.
120
+ - Model ids must be `owner/name` (the SDK is invoked with a `${string}/${string}` id).
121
+ - No retry config and no custom error classes — Replicate/SDK errors propagate (after `hooks.onError`).
122
+ - Version pinning / reproducibility is the transformer's responsibility (encode a pinned version in the model id or input).
123
+ - `apiKey` is required by the type; nothing reads `REPLICATE_API_KEY` for you.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeye/replicate",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "description": "@aeye Replicate - Replicate integration for @aeye",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -39,11 +39,12 @@
39
39
  "files": [
40
40
  "dist",
41
41
  "README.md",
42
- "LICENSE"
42
+ "LICENSE",
43
+ "aeye-*.md"
43
44
  ],
44
45
  "dependencies": {
45
- "@aeye/ai": "^0.3.8",
46
- "@aeye/core": "^0.3.8",
46
+ "@aeye/ai": "^0.3.9",
47
+ "@aeye/core": "^0.3.9",
47
48
  "replicate": "^1.3.0"
48
49
  },
49
50
  "devDependencies": {