An embedding represents text as a vector: a long list of floating-point numbers, often with hundreds or thousands of elements. Similar texts generally have similar embeddings. That makes vector similarity useful when a system needs to find relevant examples or documents rather than match exact wording. section 5.5
Two practical uses follow the same pattern: embed the available text, embed the new input, and retrieve close matches.
- Dynamic prompt examples: KNNFewShot selects similar training examples as demonstrations for the current input. With multiple input fields, it formats and joins the fields before embedding them. section 5.5
- Document retrieval: a DSPy retriever searches embedded article titles and returns the closest matches to a question. Its k setting controls how many matches come back. section 9.2.1
Finding a relevant title
Given a question about discounts on service plans, a search over five article titles returns two titles about Pricing Plans discount coupons. This illustrates why similarity can surface useful candidates even when the question and titles use different wording. section 9.2.1
Listing 9.3 Embedding text
import dspy
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('BAAI/bge-small-en-v1.5')
embedder = dspy.Embedder(model.encode) #1
question = "How can I add discounts to my service prices when customers pay for a plan?"
corpus = [ #2
"Pricing Plans: Creating Discount Coupons",
"Pricing Plans: Using Discount Coupons",
"Wix Events: About the Event Details and Registration Form Pages",
"Wix Restaurants Request: Changing the Alignment of Your Menus",
"Adding Pinterest Content to Your Site"
]
search = dspy.retrievers.Embeddings(embedder=embedder, corpus=corpus, k=2) #3
prediction = search(question) #4
print(prediction)
The Python listing connects a SentenceTransformer encoder to dspy.Embedder, then passes the embedder, a title corpus, and k=2 to a DSPy retriever. section 9.2.1
The embedding model and storage choice are separate decisions. One title-retrieval setup uses bge-small-en-v1.5, which produces 384-dimensional embeddings. For a corpus below 20,000 documents, the shown DSPy method keeps data in memory; above that threshold, it builds a Faiss database. section 9.2.1














































