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
.
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:
"""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
__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 |
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
options: show_root_headings: true show_object_full_path: true show_root_heading: true heading_level: 3