OpenAI

OpenAI / text-embedding-ada-002

OpenAI's legacy text embedding model; with an average level of price and performance when compared with text-embedding-3-large and text-embedding-3-small.

Introduction to text-embedding-ada-002

text-embedding-ada-002 is OpenAI's legacy text embedding model.

Comparing text-embedding-ada-002 with the other two newly released embedding models:

ModelDimensionsMax TokensModel MIRACL avgMETB avgPrice
text-embedding-3-large3072819154.964.6$0.13 / 1M tokens
text-embedding-ada-0021536819131.461.0$0.10 / 1M tokens
text-embedding-3-small1536819144.062.3$0.02 / 1M tokens

How to generate vector embeddings with text-embedding-ada-002

There are two primary ways to create vector embeddings:

  1. PyMilvus: the Python SDK for Milvus that seamlessly integrates with the text-embedding-ada-002 model.
  2. OpenAI Library: the Python SDK offered by OpenAI.

Once the vector embeddings are generated, they can be stored in Zilliz Cloud (a fully managed vector database service powered by Milvus) and used for semantic similarity search. Here are four key steps:

  1. Sign up for a Zilliz Cloud account for free.
  2. Set up a serverless cluster and obtain the Public Endpoint and API Key.
  3. Create a vector collection and insert your vector embeddings.
  4. Run a semantic search on the stored embeddings.
from pymilvus.model.dense import OpenAIEmbeddingFunction
from pymilvus import MilvusClient

OPENAI_API_KEY = "your-openai-api-key"
ef = OpenAIEmbeddingFunction("text-embedding-ada-002", api_key=OPENAI_API_KEY)

docs = [
   "Artificial intelligence was founded as an academic discipline in 1956.",
   "Alan Turing was the first person to conduct substantial research in AI.",
   "Born in Maida Vale, London, Turing was raised in southern England."
]

# Generate embeddings for documents
docs_embeddings = ef(docs)

queries = ["When was artificial intelligence founded",
          "Where was Alan Turing born?"]

# Generate embeddings for queries
query_embeddings = ef(queries)

# Connect to Zilliz Cloud with Public Endpoint and API Key
client = MilvusClient(
    uri=ZILLIZ_PUBLIC_ENDPOINT,
    token=ZILLIZ_API_KEY)

COLLECTION = "documents"
if client.has_collection(collection_name=COLLECTION):
    client.drop_collection(collection_name=COLLECTION)
client.create_collection(
    collection_name=COLLECTION,
    dimension=ef.dim,
    auto_id=True)

for doc, embedding in zip(docs, docs_embeddings):
    client.insert(COLLECTION, {"text": doc, "vector": embedding})
    
results = client.search(
    collection_name=COLLECTION,
    data=query_embeddings,
    consistency_level="Strong",
    output_fields=["text"])

For more information, refer to our PyMilvus Embedding Model documentation.

from openai import OpenAI
from pymilvus import MilvusClient

OPENAI_API_KEY = "your-openai-api-key"
client = OpenAI(api_key=OPENAI_API_KEY)
docs = [
   "Artificial intelligence was founded as an academic discipline in 1956.",
   "Alan Turing was the first person to conduct substantial research in AI.",
   "Born in Maida Vale, London, Turing was raised in southern England."
]

# Generate embeddings for documents
results = client.embeddings.create(input=docs, model="text-embedding-ada-002")
docs_embeddings = [data.embedding for data in results.data]

queries = ["When was artificial intelligence founded",
          "Where was Alan Turing born?"]

# Generate embeddings for queries
response = client.embeddings.create(input=queries, model="text-embedding-ada-002")
query_embeddings = [data.embedding for data in response.data]

# Connect to Zilliz Cloud with Public Endpoint and API Key
client = MilvusClient(
    uri=ZILLIZ_PUBLIC_ENDPOINT,
    token=ZILLIZ_API_KEY)

COLLECTION = "documents"
if client.has_collection(collection_name=COLLECTION):
    client.drop_collection(collection_name=COLLECTION)
client.create_collection(
    collection_name=COLLECTION,
    dimension=1536,
    auto_id=True)

for doc, embedding in zip(docs, docs_embeddings):
    client.insert(COLLECTION, {"text": doc, "vector": embedding})
    
results = client.search(
    collection_name=COLLECTION,
    data=query_embeddings,
    consistency_level="Strong",
    output_fields=["text"])

For more information, refer to OpenAI documentation.