How to Build AI-Powered Semantic Search with OpenAI Embeddings and Milvus
This guide explains vector embeddings, OpenAI's text‑embedding models, and how to use PyMilvus with Zilliz Cloud to create a Retrieval‑Augmented Generation (RAG) system for fast, accurate semantic search.
Vector embeddings are essential in AI because they convert complex unstructured data into numeric vectors that capture semantic meaning, enabling more effective analysis and content generation.
OpenAI offers several high‑quality text‑embedding models—such as text‑embedding‑ada‑002 , text‑embedding‑3‑small , and text‑embedding‑3‑large —which can be used for semantic search, clustering, recommendation, anomaly detection, and classification.
What Are Vector Embeddings and Embedding Models?
Vector embeddings represent data (text, images, audio, etc.) as numeric vectors, preserving semantic relationships. Embedding models are algorithms that learn patterns from unstructured data and map them into high‑dimensional space, where similar items have nearby vectors.
In natural language processing, embeddings place related words like “king” and “queen” close together, while unrelated words like “banana” are farther apart.
Embedding Models in Retrieval‑Augmented Generation (RAG)
RAG systems enhance large language models (LLMs) by providing additional context before generation. Data is transformed into embeddings and stored in vector databases such as Milvus (available as a managed service on Zilliz Cloud). This enables fast, fact‑based responses for a wide range of applications.
OpenAI Text Embedding Models
text‑embedding‑3‑large : 3,072 dimensions, $0.13 per 1M tokens, best for high accuracy and rich embeddings.
text‑embedding‑3‑small : 8,191 dimensions, $0.10 per 1M tokens, balances speed and cost.
text‑embedding‑ada‑002 : 1,536 dimensions, $0.02 per 1M tokens, versatile and cost‑effective.
Choosing the Right Model
text‑embedding‑3‑large : Ideal when accuracy and embedding richness are critical, despite higher resource usage.
text‑embedding‑3‑small : Suits real‑time or resource‑constrained scenarios where speed matters.
text‑embedding‑ada‑002 : Provides a solid middle ground with good performance and efficiency.
Generating Vector Embeddings with OpenAI
First, set up a Zilliz Cloud account, create a serverless cluster, and obtain the public endpoint and API key.
Then, use PyMilvus (or the OpenAI Python SDK) to generate embeddings and store them in a Milvus collection.
Example: text‑embedding‑ada‑002
from pymilvus.model.dense import OpenAIEmbeddingFunction</code><code>from pymilvus import MilvusClient</code><code>OPENAI_API_KEY = "your-openai-api-key"</code><code>ef = OpenAIEmbeddingFunction("text-embedding-ada-002", api_key=OPENAI_API_KEY)</code><code>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."]</code><code>docs_embeddings = ef(docs)</code><code>queries = ["When was artificial intelligence founded", "Where was Alan Turing born?"]</code><code>query_embeddings = ef(queries)</code><code>client = MilvusClient(uri=ZILLIZ_PUBLIC_ENDPOINT, token=ZILLIZ_API_KEY)</code><code>COLLECTION = "documents"</code><code>if client.has_collection(collection_name=COLLECTION):</code><code> client.drop_collection(collection_name=COLLECTION)</code><code>client.create_collection(collection_name=COLLECTION, dimension=ef.dim, auto_id=True)</code><code>for doc, embedding in zip(docs, docs_embeddings):</code><code> client.insert(COLLECTION, {"text": doc, "vector": embedding})</code><code>results = client.search(collection_name=COLLECTION, data=query_embeddings, consistency_level="Strong", output_fields=["text"])Example: text‑embedding‑3‑small
from pymilvus import model, MilvusClient</code><code>OPENAI_API_KEY = "your-openai-api-key"</code><code>ef = model.dense.OpenAIEmbeddingFunction(model_name="text-embedding-3-small", api_key=OPENAI_API_KEY)</code><code>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."]</code><code>docs_embeddings = ef.encode_documents(docs)</code><code>queries = ["When was artificial intelligence founded", "Where was Alan Turing born?"]</code><code>query_embeddings = ef.encode_queries(queries)</code><code>client = MilvusClient(uri=ZILLIZ_PUBLIC_ENDPOINT, token=ZILLIZ_API_KEY)</code><code>COLLECTION = "documents"</code><code>if client.has_collection(collection_name=COLLECTION):</code><code> client.drop_collection(collection_name=COLLECTION)</code><code>client.create_collection(collection_name=COLLECTION, dimension=ef.dim, auto_id=True)</code><code>for doc, embedding in zip(docs, docs_embeddings):</code><code> client.insert(COLLECTION, {"text": doc, "vector": embedding})</code><code>results = client.search(collection_name=COLLECTION, data=query_embeddings, consistency_level="Strong", output_fields=["text"])Example: text‑embedding‑3‑large
from pymilvus.model.dense import OpenAIEmbeddingFunction</code><code>from pymilvus import MilvusClient</code><code>OPENAI_API_KEY = "your-openai-api-key"</code><code>ef = OpenAIEmbeddingFunction("text-embedding-3-large", api_key=OPENAI_API_KEY)</code><code>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."]</code><code>docs_embeddings = ef(docs)</code><code>queries = ["When was artificial intelligence founded", "Where was Alan Turing born?"]</code><code>query_embeddings = ef(queries)</code><code>client = MilvusClient(uri=ZILLIZ_PUBLIC_ENDPOINT, token=ZILLIZ_API_KEY)</code><code>COLLECTION = "documents"</code><code>if client.has_collection(collection_name=COLLECTION):</code><code> client.drop_collection(collection_name=COLLECTION)</code><code>client.create_collection(collection_name=COLLECTION, dimension=ef.dim, auto_id=True)</code><code>for doc, embedding in zip(docs, docs_embeddings):</code><code> client.insert(COLLECTION, {"text": doc, "vector": embedding})</code><code>results = client.search(collection_name=COLLECTION, data=query_embeddings, consistency_level="Strong", output_fields=["text"])By following these steps, you can generate embeddings with any of the three OpenAI models, store them in a Milvus collection on Zilliz Cloud, and perform semantic search, laying the groundwork for more advanced AI applications.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
