Heroku AI embeddings provider for the Vercel AI SDK.
npm install heroku-ai-provider aiProvision access to the cohere-embedding-multilingual model on your app $APP_NAME:
heroku ai:models:create -a $APP_NAME cohere-embed-multilingual --as EMBEDDINGSet Heroku environment variables:
export HEROKU_EMBEDDING_MODEL_ID=$(heroku config:get -a $APP_NAME EMBEDDING_MODEL_ID)
export HEROKU_EMBEDDING_KEY=$(heroku config:get -a $APP_NAME EMBEDDING_KEY)
export HEROKU_EMBEDDING_URL=$(heroku config:get -a $APP_NAME EMBEDDING_URL)import { heroku } from "heroku-ai-provider";
const model = heroku.embedding(process.env.HEROKU_EMBEDDING_MODEL_ID);
const { embeddings } = await model.doEmbed({
values: ["Hello world", "How are you?"],
});
console.log(embeddings);
// Output: [[0.1, 0.2, ...], [0.3, 0.4, ...]]import { createHeroku } from "heroku-ai-provider";
const herokuProvider = createHeroku({
apiKey: "your-api-key",
baseURL: "https://custom-heroku-api.com/v1",
headers: {
"X-Custom-Header": "custom-value",
},
});
const model = herokuProvider.embedding(process.env.HEROKU_EMBEDDING_MODEL_ID);const { embeddings } = await model.doEmbed({
values: ["Hello world"],
providerOptions: {
heroku: {
inputType: "search_document",
encodingFormat: "base64",
embeddingType: "binary",
allowIgnoredParams: true,
},
},
});pnpm testWe have created some examples to demonstrate common use cases. To run an example, use the command:
pnpm example <file> <function><file>: example files use the naming convention:<file>.example.ts. They can be found in theexamples/directory.<function>: the name of the function, within the example file, to run.
pnpm example embeddings batchCreates a new Heroku provider instance.
apiKey?: string- Your Heroku API key (defaults toHEROKU_API_KEYenv var)baseURL?: string- Custom API base URL (defaults tohttps://api.heroku.com/v1)headers?: Record<string, string>- Additional headers to include in requestsfetch?: FetchFunction- Custom fetch implementation
Creates an embedding model instance.
modelId: string- The model ID to use. Currently,cohere-embedding-multilingualis the only supported model ID.
An EmbeddingModelV2<string> instance.
Generates embeddings for the provided text values.
values: string[]- Array of text strings to embedabortSignal?: AbortSignal- Optional abort signalheaders?: Record<string, string>- Additional request headersproviderOptions?: { heroku?: HerokuEmbeddingOptions }- Provider-specific options
{
embeddings: number[][];
usage?: { tokens: number };
response: { headers: Record<string, string>; body: unknown };
}inputType?: 'search_document' | 'search_query' | 'classification' | 'clustering'- Type of input (default: 'search_document')encodingFormat?: 'raw' | 'base64'- encoding format (default: 'raw')embeddingType?: 'float' | 'int8' | 'uint8' | 'binary' | 'ubinary'- Custom embedding type (default: 'float')allowIgnoredParams?: boolean- Specifies whether to ignore unsupported parameters in request instead of throwing an error. (default: false)
The package includes comprehensive error handling for common API errors:
- Rate limiting
- Authentication errors
- Invalid model IDs
- Input validation errors
- Network errors