Skip to content

text-embedding-v4 Embeddings

text-embedding-v4 is Alibaba Tongyi's latest text embedding model. It converts text into high-dimensional vectors and supports 100+ languages, making it ideal for semantic search, RAG (Retrieval-Augmented Generation), clustering, and classification scenarios. This document demonstrates how to call the model through the Agentsflare gateway via the OpenAI-compatible /v1/embeddings endpoint.

Basic Configuration

Before using the API, please make sure you have obtained an API Key. If not, please refer to Create API Key.

Basic Information

  • API Base URL: https://api.agentsflare.com/v1/embeddings
  • Authentication: Bearer Token
  • Content Type: application/json
  • Request Method: POST
  • Pricing: Input $0.07/1M tokens (see Billing)

Request Examples

bash
curl --location --request POST 'https://api.agentsflare.com/v1/embeddings' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--data-raw '{
    "model": "text-embedding-v4",
    "input": "The wind is strong, the sky is high, and the apes cry mournfully.",
    "dimensions": 1024,
    "encoding_format": "float"
}'
python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.agentsflare.com/v1"
)

response = client.embeddings.create(
    model="text-embedding-v4",
    input="The wind is strong, the sky is high, and the apes cry mournfully.",
    dimensions=1024,
    encoding_format="float"
)

embedding = response.data[0].embedding
print(f"dimensions: {len(embedding)}")
print(embedding[:5])
python
import requests

API_KEY = "YOUR_API_KEY"
URL = "https://api.agentsflare.com/v1/embeddings"

payload = {
    "model": "text-embedding-v4",
    "input": "The wind is strong, the sky is high, and the apes cry mournfully.",
    "dimensions": 1024,
    "encoding_format": "float",
}

resp = requests.post(
    URL,
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
    },
    json=payload,
    timeout=60,
)
resp.raise_for_status()

data = resp.json()
embedding = data["data"][0]["embedding"]
print(f"dimensions: {len(embedding)}")
print(embedding[:5])
javascript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.AGENTSFLARE_API_KEY,
  baseURL: "https://api.agentsflare.com/v1"
});

async function main() {
  const response = await client.embeddings.create({
    model: "text-embedding-v4",
    input: "The wind is strong, the sky is high, and the apes cry mournfully.",
    dimensions: 1024,
    encoding_format: "float"
  });

  const embedding = response.data[0].embedding;
  console.log(`dimensions: ${embedding.length}`);
  console.log(embedding.slice(0, 5));
}

main();
go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
)

type EmbeddingRequest struct {
	Model          string `json:"model"`
	Input          string `json:"input"`
	Dimensions     int    `json:"dimensions,omitempty"`
	EncodingFormat string `json:"encoding_format,omitempty"`
}

type EmbeddingResponse struct {
	Data []struct {
		Embedding []float64 `json:"embedding"`
		Index     int       `json:"index"`
	} `json:"data"`
	Usage struct {
		PromptTokens int `json:"prompt_tokens"`
		TotalTokens  int `json:"total_tokens"`
	} `json:"usage"`
}

func main() {
	reqBody := EmbeddingRequest{
		Model:          "text-embedding-v4",
		Input:          "The wind is strong, the sky is high, and the apes cry mournfully.",
		Dimensions:     1024,
		EncodingFormat: "float",
	}
	body, _ := json.Marshal(reqBody)

	req, _ := http.NewRequest("POST", "https://api.agentsflare.com/v1/embeddings", bytes.NewReader(body))
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer YOUR_API_KEY")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	respBytes, _ := io.ReadAll(resp.Body)
	var result EmbeddingResponse
	if err := json.Unmarshal(respBytes, &result); err != nil {
		panic(err)
	}

	fmt.Printf("dimensions: %d\n", len(result.Data[0].Embedding))
	fmt.Println(result.Data[0].Embedding[:5])
	fmt.Printf("total tokens: %d\n", result.Usage.TotalTokens)
}

Batch Input

The input field also accepts an array of strings to vectorize multiple texts in a single request:

bash
curl --location --request POST 'https://api.agentsflare.com/v1/embeddings' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--data-raw '{
    "model": "text-embedding-v4",
    "input": ["First document to embed", "Second document to embed"],
    "dimensions": 1024,
    "encoding_format": "float"
}'

Response Example

json
{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": [0.0234, -0.0156, 0.0421, "... (1024 dimensions in total)"]
    }
  ],
  "model": "text-embedding-v4",
  "usage": {
    "prompt_tokens": 25,
    "total_tokens": 25
  }
}

Request Parameters

ParameterTypeRequiredDescription
modelstringYesModel name, use text-embedding-v4
inputstring / arrayYesText to vectorize; a single string or an array of strings (batch)
dimensionsintegerNoOutput vector dimension, e.g. 1024. If omitted, the model default is used
encoding_formatstringNoVector return format: float (default) or base64

Billing

Billed by input tokens only: $0.07 / 1M tokens. The consumed token count is available in the usage.total_tokens field of the response. See Billing for details.

This documentation is licensed under CC BY-SA 4.0.