Use the AI Search Open Platform SDK to convert text into vector embeddings. The resulting vectors can be compared against each other to measure semantic similarity — powering search, recommendations, and retrieval-augmented generation (RAG) pipelines.
Prerequisites
Before you begin, make sure you have:
An activated AI Search Open Platform instance. See Activate AI Search Open Platform
An API key for authentication. See Manage API key
Limits
The request body cannot exceed 8 MB.
For the full list of request parameters, see Text embedding API reference.
Call the text embedding service
The following steps show how to install the SDK, configure the client, submit an embedding request, and read the response.
Step 1: Install the SDK
pip install alibabacloud_searchplat20240529Step 2: Configure the client
from alibabacloud_tea_openapi.models import Config
from alibabacloud_searchplat20240529.client import Client
from alibabacloud_searchplat20240529.models import GetTextEmbeddingRequest
config = Config(
bearer_token="Your API key", # Your API key
endpoint="<your-api-endpoint>", # Your API endpoint, without http://
protocol="http" # Valid values: HTTPS and HTTP
)
client = Client(config=config)Step 3: Submit an embedding request
# Embed a batch of documents to index
request = GetTextEmbeddingRequest(
input=["first document", "second document"],
input_type="document"
)
# Replace "default" with your workspace name and "ops-text-embedding-001" with your service ID.
response = client.get_text_embedding("default", "ops-text-embedding-001", request)
print(response)The response contains one embedding vector per input string. For the full response schema, see Text embedding API reference.
What's next
Text embedding API reference — full parameter list and response schema
Manage API key — rotate or revoke API keys