Applying for and Using the OpenAI Embeddings API

The OpenAI embedding service generates vectors representing input text.

This document mainly introduces the process of using the OpenAI Embeddings API, which allows us to create embedding vectors representing input text.

Application Process

To use the OpenAI Embeddings API, first go to the Ace Data Cloud Console to obtain your API Token for later use.

If you have not yet logged in or registered, you will be automatically redirected to the login page to register and log in. Once you have done so, you will be automatically returned to the current page.

A general application's API Key can be used for services enabled for the account; a dedicated application's Key can be used within that application's service scope. Please check the Key's permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credits are subject to what is displayed in your account; if the balance is insufficient, click the "Recharge" button.

📘 Full documentation: OpenAI Embeddings API →

Basic Usage

Next, you can fill in the corresponding fields in the interface, as shown below:

When using this endpoint for the first time, we need to fill in at least three fields. One is authorization, which can be selected directly from the drop-down list. Another parameter is model, which is the category of OpenAI's official model we choose to use. The currently available models are text-embedding-3-small and text-embedding-3-large. text-embedding-ada-002 is no longer available; if an existing vector index uses this old model, it cannot be used directly with vectors from the new models, and the index must be rebuilt. The last parameter is input, which is the text we need to convert into an embedding vector.

The base rates per million input tokens are 0.027993 Credits for text-embedding-3-small and 0.181957 Credits for text-embedding-3-large. The default vectors have 1536 and 3072 dimensions, respectively. If large batches or token ID input for small are temporarily unavailable, you can submit raw text and split it into batches of no more than 512 items, while continuing to use the same model and dimensions.

You can also see the corresponding generated call code on the right. You can copy the code and run it directly, or click the "Try" button to test it.

Optional parameters:

  • dimensions: Truncate the vector dimensions; the full dimensions are output by default.
  • encoding_format: The return format, either float or base64.

Python sample call code:

import requests

url = "https://api.acedata.cloud/openai/embeddings"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "input": "The food was delicious and the waiter...",
    "model": "text-embedding-3-small",
    "encoding_format": "float"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

After making the call, we see the following result:

{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": [
        0.0022756963,
        -0.009305916,
        0.015742613,
        -0.0077253063,
        -0.0047450014,
        0.014917395,
        -0.009807394,
        -0.038264707,
        -0.0069127847,
        -0.028590616,
        0.025251659,
        ....
        -0.014079482,
        -0.015425222,
        0.0040753055,
        0.002727979,
        -0.03138366,
        0.041159317,
        -0.017608874,
        -0.018637223,
        0.014587308,
        0.010486611,
        -0.015387135,
        -0.019424353,
        -0.002800979
      ]
    }
  ],
  "model": "text-embedding-3-small",
  "usage": {
    "prompt_tokens": 8,
    "total_tokens": 8
  }
}

The returned result contains several fields, described below:

  • model, the model used to convert the text into an embedding vector.
  • usage, the token information for this text-to-vector conversion.
  • data, the embedding vector result after the text is converted.

The data field contains the details of the embedding vector corresponding to the text, and its embedding field contains the generated embedding vector itself.

Error Handling

When calling the API, if an error occurs, the API will return the corresponding error code and message. For example:

  • 400 token_mismatched: Bad request, possibly due to missing or invalid parameters.
  • 400 api_not_implemented: Bad request, possibly due to missing or invalid parameters.
  • 401 invalid_token: Unauthorized, invalid or missing authorization token.
  • 429 too_many_requests: Too many requests, you have exceeded the rate limit.
  • 500 api_error: Internal server error, something went wrong on the server.

Example Error Response

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

Conclusion

Through this document, you have learned how to use the OpenAI Embeddings API to easily access OpenAI's official embedding vector generation functionality. We hope this document helps you better integrate and use the API. If you have any questions, please feel free to contact our technical support team.