Skip to content

Embeddings API

The Embeddings API generates vector representations of input text using the AIRefinery or the AsyncAIRefinery client, which can be used in machine learning models and algorithms for tasks like semantic search and clustering.

Asynchronous Embedding Creation

AsyncAIRefinery.embeddings.create()

This method generates vector representations of input text in an asynchronous manner, which are useful for machine learning tasks.

Parameters:
  • input (string or array, Required): The text to embed, provided as a single string or an array of strings/tokens.

  • model (string, Required): The ID of the model to use for embedding. Check available models using the List models API or see the Model overview for descriptions.

  • dimensions (integer, Optional): Specifies the number of dimensions for the output embeddings. Only available in text-embedding-3 and later models.

  • encoding_format (string, Optional, Defaults to "float"): The format for the returned embeddings, either "float" or "base64".

  • user (string, Optional): A unique identifier for your end-user, aiding OpenAI in monitoring and detecting abuse.

Returns:
  • A list of embedding objects, each containing the following attributes:
    • embedding (array): The embedding vector, which is a list of floats. The length of the vector depends on the model used, as listed in the embedding guide.
    • index (integer): The index of the embedding within the list of embeddings.
    • object (string): The object type, which is always "embedding".
Example Usage:
import os  
import asyncio  

from air import AsyncAIRefinery  
from air import login  

# Authenticate using environment variables for account and API key  
auth = login(  
    account=str(os.getenv("ACCOUNT")),  
    api_key=str(os.getenv("API_KEY"))  
)  

async def create_embedding():  
    # Initialize the AI client with authentication details
    client = AsyncAIRefinery(**auth.openai())  # Supports a non-async AIRefinery client too

    # Create an embedding for the input text  
    response = await client.embeddings.create(
        input=["hi"],
        model="intfloat/e5-mistral-7b-instruct",
    )
    print(response)

# Example call to create_embedding function  
if __name__ == "__main__":  
    asyncio.run(create_embedding())  

Synchronous Embedding Creation

AIRefinery.embeddings.create()

This method generates vector representations of input text in a synchronous manner, which are useful for machine learning tasks. This method supports the same parameters and return structure as the asynchronous method (AsyncAIRefinery.embeddings.create()) described above.

Example Usage:
import os  
import asyncio  

from air import AIRefinery  
from air import login  

# Authenticate using environment variables for account and API key  
auth = login(  
    account=str(os.getenv("ACCOUNT")),  
    api_key=str(os.getenv("API_KEY"))  
)  

def create_embedding():  
    # Initialize the AI client with authentication details
    client = AIRefinery(**auth.openai())  # Supports a async AIRefinery client too

    # Create an embedding for the input text  
    response = client.embeddings.create(
        input=["hi"],
        model="intfloat/e5-mistral-7b-instruct",
    )
    print(response)

# Example call to create_embedding function  
if __name__ == "__main__":  
    create_embedding()