Embeddings

Usage guide of embeddings endpoint

Introduction to Embeddings

OpenAI’s text embeddings measure the relatedness of text strings. Embeddings are commonly used for:

  • Search (where results are ranked by relevance to a query string)

  • Clustering (where text strings are grouped by similarity)

  • Recommendations (where items with related text strings are recommended)

  • Anomaly detection (where outliers with little relatedness are identified)

  • Diversity measurement (where similarity distributions are analyzed)

  • Classification (where text strings are classified by their most similar label)

An embedding is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness.

Currently available Embedding models are:

Model
Output Dimension

text-embedding-3-large Most capable embedding model for both english and non-english tasks

3,072

text-embedding-3-small Increased performance over 2nd generation ada embedding model

1,536

text-embedding-ada-002 Most capable 2nd generation embedding model, replacing 16 first generation models

1,536

Using Embeddings

To use embeddings, you simply need to send a post request to the embeddings endpoint (https://api.webraft.in/v2/embeddings) with an input field containing text and a model field containing the model name.

Python

You can use the requests library to make HTTP requests in Python. First, you need to install requests if you haven't already:

pip install requests

Then, you can use the following code:

import requests
import json

url = 'https://api.webraft.in/v2/embeddings'
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY',
}
data = {
    "input": "Your text string goes here",
    "model": "text-embedding-3-large"
}

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.content)

Curl

Here is the complete curl command:

curl https://api.webraft.in/v2/embeddings \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "input": "Your text string goes here",
    "model": "text-embedding-3-large"
  }'

Node.js

You can use the axios library to make HTTP requests in Node.js. First, you need to install axios if you haven't already:

npm install axios

Then, you can use the following code:

const axios = require('axios');

const url = 'https://api.webraft.in/v2/embeddings';
const headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_API_KEY',
};
const data = {
    input: "Your text string goes here",
    model: "text-embedding-3-large"
};

axios.post(url, data, { headers: headers })
    .then(response => {
        console.log(response.data);
    })
    .catch(error => {
        console.error('Error making the request:', error);
    });

The response will contain the embedding vector along with some additional metadata. Example Response:

{
  "object": "list",
  "data": [
    {
      "object": "embedding",
      "index": 0,
      "embedding": [
        -0.006929283495992422,
        -0.005336422007530928,
        ... (omitted for spacing)
        -4.547132266452536e-05,
        -0.024047505110502243
      ],
    }
  ],
  "model": "text-embedding-3-large",
  "usage": {
    "prompt_tokens": 5,
    "total_tokens": 5
  }
}

In the next section, we'll talk about integrating webraftai api with commonly known AI softwares.

Last updated