-
-
Notifications
You must be signed in to change notification settings - Fork 273
feat: add multimodal embed() activity and provider adapters #926
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
Open
jherr
wants to merge
9
commits into
main
Choose a base branch
from
feat/embed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1e5b1b
chore: update project agent configuration
jherr 45518bf
chore: update project agent configuration
jherr 88f018e
chore: update project agent configuration
jherr 1e18bd5
Merge branch 'main' of github.com:TanStack/ai
jherr 9710fdd
Merge branch 'main' of github.com:TanStack/ai
jherr d2dd0c4
Merge branch 'main' of github.com:TanStack/ai
jherr af708d4
feat: add multimodal embed() activity and provider adapters
jherr 721aac8
chore: remove .agentsroom from branch
jherr 0246c4a
fix: address CodeRabbit review on embed PR
jherr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| '@tanstack/ai': minor | ||
| '@tanstack/ai-event-client': minor | ||
| '@tanstack/ai-openai': minor | ||
| '@tanstack/ai-gemini': minor | ||
| '@tanstack/ai-mistral': minor | ||
| '@tanstack/ai-bedrock': minor | ||
| '@tanstack/ai-ollama': minor | ||
| '@tanstack/ai-cohere': minor | ||
| --- | ||
|
|
||
| Add a multimodal `embed()` activity. A single primitive covers one input or a batch — `input` accepts a string, a text part, an image part, or a fused `{ type: 'content' }` text+image item (one vector per item), with the accepted item types narrowed per model at compile time. Top-level `dimensions` requests Matryoshka output sizes where supported. Results carry `embeddings: [{ vector, index }]` plus `usage` when the provider reports it, and `embed()` participates in generation middleware, debug logging, OTel (`gen_ai.operation.name: embeddings`), and devtools events like every other activity. | ||
|
|
||
| Provider adapters: `openaiEmbedding` (text-embedding-3-small/large), `geminiEmbedding` (gemini-embedding-001), `mistralEmbedding` (mistral-embed, codestral-embed), `ollamaEmbedding` (nomic-embed-text and any local model), `bedrockEmbedding` (Titan Text V2, Titan Multimodal G1 with fused text+image, Cohere Embed v3 on Bedrock), and the new `@tanstack/ai-cohere` package's `cohereEmbedding` (embed-v4.0, multimodal text+image with required `inputType`). |
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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| --- | ||
| title: Cohere | ||
| id: cohere-adapter | ||
| order: 18 | ||
| description: "Use Cohere's embed-v4.0 multimodal embedding model with TanStack AI via @tanstack/ai-cohere — text and image embeddings for semantic search and RAG." | ||
| keywords: | ||
| - tanstack ai | ||
| - cohere | ||
| - embed-v4 | ||
| - embeddings | ||
| - multimodal embeddings | ||
| - semantic search | ||
| - adapter | ||
| --- | ||
|
|
||
| The Cohere adapter provides access to Cohere's embed-v4.0 multimodal embedding model — text, images, and fused text+image inputs, each producing a single vector. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @tanstack/ai-cohere | ||
| ``` | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ```typescript | ||
| import { embed } from "@tanstack/ai"; | ||
| import { cohereEmbedding } from "@tanstack/ai-cohere"; | ||
|
|
||
| const result = await embed({ | ||
| adapter: cohereEmbedding("embed-v4.0"), | ||
| input: ["a red guitar", "a blue drum kit"], | ||
| modelOptions: { inputType: "search_document" }, | ||
| }); | ||
|
|
||
| console.log(result.embeddings[0]?.vector); | ||
| console.log(result.usage?.promptTokens); | ||
| ``` | ||
|
|
||
| `inputType` is required by Cohere's API — use `search_document` at index time and `search_query` at query time (or `classification` / `clustering` for those workloads). TanStack AI enforces this at the type level: `modelOptions` is required for Cohere embedding calls. | ||
|
|
||
| ## Basic Usage - Custom API Key | ||
|
|
||
| ```typescript | ||
| import { embed } from "@tanstack/ai"; | ||
| import { createCohereEmbedding } from "@tanstack/ai-cohere"; | ||
|
|
||
| const adapter = createCohereEmbedding("embed-v4.0", process.env.MY_COHERE_KEY!); | ||
|
|
||
| const result = await embed({ | ||
| adapter, | ||
| input: "a red guitar", | ||
| modelOptions: { inputType: "search_query" }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Multimodal Embeddings | ||
|
|
||
| embed-v4.0 embeds images alongside text. An image part produces an image vector; a `{ type: "content" }` item fuses text and image into one vector — ideal for product catalogs and screenshot search: | ||
|
|
||
| ```typescript | ||
| import { embed } from "@tanstack/ai"; | ||
| import { cohereEmbedding } from "@tanstack/ai-cohere"; | ||
|
|
||
| const productPhoto = "iVBORw0KGgo..."; // base64 image data | ||
|
|
||
| const result = await embed({ | ||
| adapter: cohereEmbedding("embed-v4.0"), | ||
| input: [ | ||
| { | ||
| type: "image", | ||
| source: { | ||
| type: "data", | ||
| value: productPhoto, | ||
| mimeType: "image/png", | ||
| }, | ||
| }, | ||
| { | ||
| type: "content", | ||
| content: [ | ||
| { type: "text", content: "Fender Stratocaster, sunburst finish" }, | ||
| { | ||
| type: "image", | ||
| source: { | ||
| type: "data", | ||
| value: productPhoto, | ||
| mimeType: "image/png", | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| modelOptions: { inputType: "search_document" }, | ||
| }); | ||
|
|
||
| console.log(result.embeddings.length); // 2 | ||
| ``` | ||
|
|
||
| Cohere's API does not fetch remote image URLs. Pass base64 data (or a `data:` URI), or opt into adapter-side downloading: | ||
|
|
||
| ```typescript | ||
| import { embed } from "@tanstack/ai"; | ||
| import { cohereEmbedding } from "@tanstack/ai-cohere"; | ||
|
|
||
| const adapter = cohereEmbedding("embed-v4.0", { allowUrlFetch: true }); | ||
|
|
||
| const result = await embed({ | ||
| adapter, | ||
| input: { | ||
| type: "image", | ||
| source: { type: "url", value: "https://example.com/guitar.png" }, | ||
| }, | ||
| modelOptions: { inputType: "search_document" }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Requesting Dimensions | ||
|
|
||
| embed-v4.0 supports Matryoshka output dimensions via the top-level `dimensions` option: | ||
|
|
||
| ```typescript | ||
| import { embed } from "@tanstack/ai"; | ||
| import { cohereEmbedding } from "@tanstack/ai-cohere"; | ||
|
|
||
| const result = await embed({ | ||
| adapter: cohereEmbedding("embed-v4.0"), | ||
| input: "a red guitar", | ||
| dimensions: 1024, // 256 | 512 | 1024 | 1536 | ||
| modelOptions: { inputType: "search_document" }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| Set your API key in environment variables: | ||
|
|
||
| ```bash | ||
| COHERE_API_KEY=... | ||
| ``` | ||
|
|
||
| Get a key from the [Cohere dashboard](https://dashboard.cohere.com/api-keys). | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### `cohereEmbedding(model, config?)` | ||
|
|
||
| Creates an embedding adapter using `COHERE_API_KEY` from the environment. | ||
|
|
||
| - `model`: `"embed-v4.0"` | ||
| - `config.baseUrl`: override the API base URL (default `https://api.cohere.com`) | ||
| - `config.headers`: extra request headers | ||
| - `config.allowUrlFetch`: download `http(s)` image URLs and inline them as base64 (default `false`) | ||
|
|
||
| ### `createCohereEmbedding(model, apiKey, config?)` | ||
|
|
||
| Same as `cohereEmbedding` with an explicit API key. | ||
|
|
||
| ## Next Steps | ||
|
|
||
| - [Embeddings guide](../embeddings.md) — the full `embed()` API | ||
| - [Generation Hooks](../media/generation-hooks.md) — usage and lifecycle middleware |
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
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
Oops, something went wrong.
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.
why is this an array above and then an object with type: "content" here with content being an array of parts? why do we not use the same convention we use across all our other generations