Text embedding

更新时间:
复制 MD 格式

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:

Limits

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_searchplat20240529

Step 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