Reference · API

OpenAI Embeddings API

Turn text into vectors for semantic search and RAG.

Turn text into vectors for semantic search and RAG.

Endpoint

POST https://api.openai.com/v1/embeddings

Request

| Field | Purpose |

|-------|---------|

| `model` | e.g. `text-embedding-3-small` |

| `input` | String or array of strings |

Python example

from openai import OpenAI
client = OpenAI()
emb = client.embeddings.create(
    model="text-embedding-3-small",
    input="Annual refund policy for pro plans",
)
vector = emb.data[0].embedding
print(len(vector), "dimensions")

Usage in RAG

1. Embed all document chunks once → store vectors + text

2. Embed user question at query time

3. Cosine similarity → top-k chunks → LLM prompt

Tips

  • Batch inputs to save requests
  • Keep the **same model** for index and queries
  • Store raw text alongside vectors for prompt packing

Related