liveBook · Manning

Embedding

As explained in Building LLM Applications with DSPy

A numerical vector representation of an item, such as text, that encodes useful features.

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

How Building LLM Applications with DSPy covers it

How deep it goes
Defines embeddings as floating-point vectors and works through retrieval setup, without detailing how an embedding model produces its values.
Examples
Uses airline utterances for demonstration selection and discount-coupon article titles for question-to-document matching.
What it assumes
Assumes basic Python and an available training set or text corpus, while defining embeddings before using them.
What you can do after
Apply it via SentenceTransformer and DSPy to select prompt examples or retrieve the k closest titles from a corpus.
How it connects
Connects embeddings to vector similarity, k-nearest neighbors, few-shot prompting, vector stores, and retrieval.

Also explained in

Compare how 47 books explain embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.