Skip to content

Embedder SDK

The Embedder SDK gives you access to raw embedding models allowing you to call them directly from your Python code.

Getting started

To get started with the Embedder SDK, create a new client and access the embedder property.

import xai_sdk

client = xai_sdk.Client()
sampler = client.embedder

Embedding from a model

The main function of the Embedder SDK is the embed function, which given a list of raw texts and a model name, embed the texts with the model. The function returns an async iterator that streams out the embedded results.

For spam embedding:

simple_completion.py
"""An example demonstrating spam embedding."""

import asyncio

import xai_sdk


HOST = "api.x.ai"
API_KEY = "1111"

async def main():
    prompts = [
        "This is a crazy promotion for a new product.",
        "Thi is not a crazy promotion",
    ]

    client = xai_sdk.Client(api_key=API_KEY, api_host=HOST)
    async for embedding in client.embedder.embed(texts=prompts, model_name="spam"):
        print(embedding)

asyncio.run(main())

API Reference

Embedder API to embed text data.

AsyncEmbedder

AsyncEmbedder is a client to the raw embedding service.

Source code in xai_sdk/embedder.py
class AsyncEmbedder:
    """AsyncEmbedder is a client to the raw embedding service."""

    def __init__(self, stub: embedder_public_pb2_grpc.EmbedderStub):
        """Initializes a new instance of the `AsyncEmbedder` class.

        Args:
            stub: The gRPC stub to use for interacting with the API.
        """
        self._stub = stub

    async def embed(self, texts: list[str], model_name: str):# -> list[Tuple[list[float], int]]:
        """Embeds the given texts.

        Args:
            texts: The list of raw texts to embed.
            model_name: The name of the model to use for embedding.

        Returns:
            A list of tuples, where each tuple contains the embedding and the shape.
        """

        request = embedder_public_pb2.EmbedRequest(texts=texts, model_name=model_name)
        response = await self._stub.Embed(request)
        for embedding in response.embeddings:
            yield embedding.values, embedding.shape

__init__(stub)

Initializes a new instance of the AsyncEmbedder class.

Parameters:

Name Type Description Default
stub EmbedderStub

The gRPC stub to use for interacting with the API.

required
Source code in xai_sdk/embedder.py
def __init__(self, stub: embedder_public_pb2_grpc.EmbedderStub):
    """Initializes a new instance of the `AsyncEmbedder` class.

    Args:
        stub: The gRPC stub to use for interacting with the API.
    """
    self._stub = stub

embed(texts, model_name) async

Embeds the given texts.

Parameters:

Name Type Description Default
texts list[str]

The list of raw texts to embed.

required
model_name str

The name of the model to use for embedding.

required

Returns:

Type Description

A list of tuples, where each tuple contains the embedding and the shape.

Source code in xai_sdk/embedder.py
async def embed(self, texts: list[str], model_name: str):# -> list[Tuple[list[float], int]]:
    """Embeds the given texts.

    Args:
        texts: The list of raw texts to embed.
        model_name: The name of the model to use for embedding.

    Returns:
        A list of tuples, where each tuple contains the embedding and the shape.
    """

    request = embedder_public_pb2.EmbedRequest(texts=texts, model_name=model_name)
    response = await self._stub.Embed(request)
    for embedding in response.embeddings:
        yield embedding.values, embedding.shape

options: show_root_headings: true show_object_full_path: true show_root_heading: true heading_level: 3