Embeddings

Supported Providers
  • AI21

  • Anyscale

  • Azure OpenAI

  • AWS Bedrock

  • Cohere

  • Fireworks AI

  • Google Gemini

  • Jina

  • Mistral AI

  • Nomic AI

  • Ollama

  • OpenAI

  • Together AI

  • Cloudflare Workers AI

  • Zhipu AI

Create Embeddings

POST /embeddings

Generate embeddings using the selected Large Language Model (LLM).

Embeddings

post
Authorizations
Body
inputone ofRequired

Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. The input must not exceed the max input tokens for the model (8192 tokens for text-embedding-ada-002), cannot be an empty string, and any array must be 2048 dimensions or less. Example Python code for counting tokens.

Example: The quick brown fox jumped over the lazy dog
stringOptional

The string that will be turned into an embedding.

Default: ""Example: This is a test.
or
string[] · min: 1 · max: 2048Optional

The array of strings that will be turned into an embedding.

Example: ['This is a test.']
or
integer[] · min: 1 · max: 2048Optional

The array of integers that will be turned into an embedding.

Example: [1212, 318, 257, 1332, 13]
or
modelany ofRequired

ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.

Example: text-embedding-3-small
stringOptional
or
string · enumOptionalPossible values:
encoding_formatstring · enumOptional

The format to return the embeddings in. Can be either float or base64.

Default: floatExample: floatPossible values:
dimensionsinteger · min: 1Optional

The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.

userstringOptional

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Learn more.

Example: user-1234
Responses
200
OK
application/json
post
curl https://api.portkey.ai/v1/embeddings \
  -H "x-portkey-api-key: $PORTKEY_API_KEY" \
  -H "x-portkey-virtual-key: $PORTKEY_PROVIDER_VIRTUAL_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-ada-002",
    "encoding_format": "float"
  }'
200

OK

{
  "data": [
    {
      "index": 1,
      "embedding": [
        1
      ],
      "object": "embedding"
    }
  ],
  "model": "text",
  "object": "list",
  "usage": {
    "prompt_tokens": 1,
    "total_tokens": 1
  }
}

This endpoint allows you to generate embeddings for text inputs using a specific model. The response will be an Embedding Object consistent with OpenAI's Embedding Object format.

SDK Usage

The embeddings.create method in the Portkey SDK facilitates the generation of embeddings using various LLMs. This method provides a straightforward interface similar to the OpenAI API for generating embeddings.

Method Signature

portkey.embeddings.create(requestParams[, configParams]);

Parameters

  1. requestParams (Object): Parameters for the embedding request. All OpenAI params are supported. These parameters include the input text and model, and are automatically transformed by Portkey for LLMs other than OpenAI. Parameters not supported by other LLMs will be omitted.

  2. configParams (Object): Additional configuration options for the request. This is an optional parameter that can include custom config options for this specific request. These will override the configs set in the Portkey Client.

Example Usage

import Portkey from 'portkey-ai';

// Initialize the Portkey client
const portkey = new Portkey({
    apiKey: "PORTKEY_API_KEY",  // Replace with your Portkey API key
    virtualKey: "VIRTUAL_KEY"   // Optional: For virtual key management
});

// Generate embeddings
async function getEmbeddings() {
    const embeddings = await portkey.embeddings.create({
        input: "embed this",
        model: "text-embedding-3-large",
    });

    console.log(embeddings);
}
await getEmbeddings();

// Generate embeddings with config params
async function getEmbeddingsWithConfig() {
    const embeddings = await portkey.embeddings.create({
        input: "embed this",
        model: "text-embedding-3-large",
    }, {config: "custom-config-123"});

    console.log(embeddings);
}
await getEmbeddingsWithConfig();

Response Format

The response will conform to the Embedding Object schema from the Portkey API, typically including a list of embedding vectors consistent with the format provided by OpenAI for embedding objects.

Last updated

Was this helpful?