Zum Inhalt springen
Embeddings and Semantic Search: Find meaning beyond matching wordsA customer asks why they were charged twice. The application embeds the query and compares it with indexed article vectors to retrieve Duplicate payments. This is an illustrative search pipeline, not measured API output. Learning goals: represent text, compare meaning and retrieve useful context. APPLIED AI FOR DEVELOPERS · PART 5 Embeddings& Semantic Search Find meaning beyondmatching words. Represent textTurn questions and documents into vectors. Compare meaningFind related content across different wording. Retrieve useful contextReturn relevant passages with their sources. FROM CUSTOMER QUESTION TO RELEVANT ARTICLE CUSTOMER QUESTION“Why was I charged twice?” EMBED THE QUERY · COMPARE WITH ARTICLE VECTORS QueryArticle index RETRIEVED HELP-CENTER ARTICLEDuplicate paymentsCheck payment references before contacting billing support. Illustrative pipeline · no live similarity scores shown TODAY’S PATHText → vectors → similarity → ranked results → relevant context
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com1 / 16
Recap Part 4: From Ticket to Validated DataThe customer reports a duplicate charge. Clear instructions define classification and prohibit invented approvals. JSON Schema defines allowed fields and values. The application validates output and retains control of actions. Part 5 adds retrieval of relevant knowledge to supply useful context.APPLIED AI FOR DEVELOPERS · PART 5Recap:From Ticket to Validated DataPart 4 taught us to define the task, specify the output and check the result. THE SAME SUPPORT TICKET“I was charged twice for order #4821. Please refund it.”1Give clear instructionsClassify the ticket.Use only the stated facts.Do not approve refunds.Context + rules + examples2Define the outputcategory: billing | technical | …priority: low | normal | highsummary + missing informationJSON Schema + strict output3Validate before useCheck completion and structure.Verify facts and business rules.Keep actions behind permissions.Valid shape ≠ verified factsPROMPT INJECTIONCommands inside tickets are data, not application rules. THE OPEN QUESTIONWhere does the relevant knowledge come from? PART 5 ADDS RETRIEVALFind the right help article → provide relevant context → keep the checks from Part 4.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com2 / 16
When Keywords Are Not Enough: Two Search ExamplesCharged twice can refer to duplicate payments. Cannot sign in after forgetting login details can refer to resetting a password. Exact word matching can miss related content with different wording. Semantic search can help retrieve related passages. Exact identifiers such as order 4821 still need exact matching. Examples are illustrative, not measured search results.APPLIED AI FOR DEVELOPERS · PART 5When KeywordsAre Not EnoughUsers describe a problem. Help articles may use completely different words.1USER’S QUESTION“Why was Icharged twice?”related meaningRELEVANT HELP ARTICLEDuplicate paymentsTwo transactions may refer to the same purchase.Compare the payment references before contacting support.2USER’S QUESTION“I forgot my login details.How do I get back in?”related meaningRELEVANT HELP ARTICLEReset your passwordRequest a recovery link for your registered email address.Follow the instructions to choose a new password.EXACT TERMS STILL MATTER“Show order #4821” → match #4821 exactly.A similar order number is not the right order. Keep keyword / exact search.THE POINTExact word matching can miss paraphrases. Semantic search adds meaning.Illustrative examples · no measured rankings · keyword systems may also use synonyms
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com3 / 16
What Is an Embedding?A learned embedding model maps text into a vector with hundreds or thousands of dimensions. Meaning is represented jointly across dimensions, which generally do not have simple human labels. In an illustrative two-dimensional diagram, charged twice and duplicate payment have similar directions and a small angle; reset password points in another direction. Cosine similarity compares vector directions. These arrows are schematic and are not measured embeddings.APPLIED AI FOR DEVELOPERS · PART 5What Isan Embedding?A learned numeric representation of text — designed so related texts can be compared. FROM TEXT TO A HIGH-DIMENSIONAL VECTOR “I was charged twice.” Learned embedding model [0.012, −0.031, 0.008, …] Hundreds or thousands of dimensions Each number is one coordinate in that vector space. Meaning is represented across the whole vector.Individual dimensions usually have no simple label. SIMILAR MEANING → SIMILAR DIRECTION xy0 A · charged twicesmall angle to B B · duplicate paymentsimilar direction to A C · reset password Small angle → higher cosine Different direction → lower similarity to A 2D illustration · equal-length arrows · not measured embeddings THE KEY IDEACosine similarity compares directions. Similar does not mean identical or correct.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com4 / 16
From Text to Vector: What the API DoesThe browser sends model, three input texts and float encoding to the OpenAI embeddings endpoint. OpenAI returns one embedding per input, linked by zero-based index. The illustrated response shows only three coordinates per vector, not measured outputs; the selected model returns 1536 coordinates by default. JavaScript then computes cosine similarity locally over the full vectors. The API key belongs in the Authorization header, not the JSON body.APPLIED AI FOR DEVELOPERS · PART 5From Text to Vector:What the API DoesOne API call embeds A, B and C. Our browser then compares the returned vectors. 1 · BROWSER → OPENAI · REQUEST BODY 2 · OPENAI → BROWSER · RESPONSE EXCERPT{ "model": "text-embedding-3-small", "input": [ "I was charged twice.", "There is a duplicate payment on my account.", "How can I reset my password?" ], "encoding_format": "float"}{ "data": [ { "index": 0, "embedding": [0.012, -0.031, 0.008] }, { "index": 1, "embedding": [0.015, -0.029, 0.011] }, { "index": 2, "embedding": [-0.024, 0.017, 0.042] } ]}POST https://api.openai.com/v1/embeddingsAPI key → Authorization: Bearer … header, outside this JSONIllustration: 3 coordinates shown per vector; not measured data.Default for this model: 1,536 coordinates per input text.Match vectors to textsindex 0 → A · index 1 → B · index 2 → CUse index; do not rely on response order.Compute similarity locallycosine(A, B), cosine(A, C), cosine(B, C)JavaScript uses every coordinate.Read usage + measure timeusage.prompt_tokens = input tokensNo generated answer or output tokens.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com5 / 16

APPLIED AI FOR DEVELOPERS · PART 5

Live Demo: Compare Text Similarity

OpenAI generates vectors · JavaScript computes cosine similarity

Similarity results
Cosine calculation ↗
cosine(A, B) = Σ(Aᵢ × Bᵢ) / (‖A‖ × ‖B‖)

Computed locally in JavaScript over every dimension. OpenAI returns the vectors; this calculation makes no additional API call.

Actual pair calculations
Run Compare to see the values from the returned vectors.
JavaScript used by this demo
Sent JSON ↗
No request sent yet.
Response JSON / vectors ↗
No response yet.
Compare the texts to see measured similarity.

Input tokens: — · Time: —

Direct browser → OpenAI; no proxy. Key and vectors stay in runtime memory. Clear or leaving the page removes them. Browser tools and page scripts can access the key. Use a temporary key and revoke it afterwards. Similarity is not a probability of correctness.

EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com6 / 16
Cosine Similarity: See the MathA worked two-dimensional example: A equals 3,0 and B equals 2,2. Their dot product is 6. Their lengths are 3 and square root of 8. Dividing gives 0.7071, the cosine of 45 degrees. Scaling B by a positive factor changes its length but not its direction or cosine similarity. These are toy coordinates, not real embeddings. Actual embeddings use the same sums over all dimensions.APPLIED AI FOR DEVELOPERS · PART 5Cosine Similarity:See the MathCompare directions by dividing the dot product by the two vector lengths. 2D WORKED EXAMPLE · TOY COORDINATES1122330x 45°A = (3, 0) B = (2, 2)1.5 × Bsame direction Longer arrow, same angle → same cosine.The dashed extension changes length, not direction. THE FORMULAcosine(A, B) = (A · B) / (‖A‖ × ‖B‖)1Dot product · multiply, then addA · B = (3 × 2) + (0 × 2) = 62Vector lengths · sum squares, then square root‖A‖ =3² + 0²= 3‖B‖ =2² + 2²=83Divide · remove the effect of length6 / (3 ×8) = 0.7071 = cos(45°)1 = same direction · 0 = perpendicular · −1 = oppositeREAL EMBEDDINGSSame calculation over every dimension. Nonzero vectors required. Not a probability.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com7 / 16
Build the Search IndexBefore searching, prepare the documents. A collection contains several documents. The selected Payment help document is split into three coherent sections. Each section is embedded. Store every vector together with its chunk text, source and access metadata. The highlighted duplicate payment chunk can be traced through the pipeline. Vectors shown are illustrative. Query embedding and comparison happen later, at search time.APPLIED AI FOR DEVELOPERS · PART 5Buildthe Search IndexPrepare the knowledge first. Follow the highlighted section from document to index.1Collect documents2Split into chunks3Embed each chunk4Store for searchACCOUNT HELPDELIVERY HELPPAYMENT HELPPayment methodsDuplicate paymentsInvoice downloadsSelect one document to followCHUNK 1Payment methodsCHUNK 2Duplicate paymentsCHUNK 3Invoice downloadsKeep coherent sectionsVECTOR 1VECTOR 2VECTOR 3Same model + dimensionsSEARCH INDEXVector 1 + text + sourceVector 2 + text + sourceVector 3 + text + sourceLink vectors back to evidenceONE INDEX RECORDVector + chunk text + document ID / section + version + access metadataINDEX TIMEEmbed once; reuse for many queries. Re-index changed content. Search comes next.Schematic vectors, not measured values · each record must keep its text and source reference
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com8 / 16
Chunking: Keep the Meaning TogetherA help document contains payment methods, duplicate payments and invoice downloads. A tiny fragment such as Compare the references loses its subject. One vector for the whole guide mixes topics. A coherent Duplicate payments section keeps its heading, the duplicate-charge situation and the payment-reference instruction together. Tune chunk size and overlap against search tests; retain source and section metadata.APPLIED AI FOR DEVELOPERS · PART 5Chunking:Keep the Meaning TogetherA chunk is the unit we embed and retrieve. Its boundaries affect what search can find. ONE SOURCE DOCUMENTPayment methods | Duplicate payments | Invoice downloads1Too smallA fragment loses its subject.2Too largeMany topics share one vector.3A useful sectionOne coherent issue + its context.“Compare the references.”References to what?The payment context was cut away.Risk: an ambiguous search result.A short chunk is not useful by itself.Payment methods · cards · transfersDuplicate payments · referencesInvoice downloads · billing detailsRisk: relevant details get diluted.The whole guide mixes different needs.PAYMENT HELP / Duplicate paymentsIf the same order was charged twice,compare the payment referencesbefore contacting billing support.Keep the heading and the useful explanation.DESIGN + TESTStart with meaningful sections. Tune size and overlap on real search queries.Attach document ID + section + source. Use overlap where context crosses a boundary.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com9 / 16
What Does a Vector Database Do?A vector database stores vector records linked to chunk text and source or access metadata. Given a query vector, exact search compares all eligible vectors. Approximate nearest-neighbor indexes reduce search work but may miss relevant neighbors. Stored company_id, product and topic metadata supports filtering. The example user belongs to Company A and searches product Shop. Only records authorized for that user and scope may be returned; metadata alone does not enforce access. Our eight-article demo uses a JavaScript array and exact cosine comparisons, without an external database.APPLIED AI FOR DEVELOPERS · PART 5What Doesa Vector Database Do?Keep vectors linked to useful text — and retrieve nearby candidates efficiently. STORED RECORDS · ONE PER CHUNK VECTORCHUNK TEXT / REFERENCEMETADATADuplicatepaymentscompany_id: Aproduct: Shoptopic: billingDuplicatepaymentscompany_id: Bproduct: Shoptopic: billingPaymentmethodscompany_id: Aproduct: ERPtopic: billingHIGHLIGHTED: COMPANY A + PRODUCT SHOPMetadata enables filtering; application code enforces access.1Exact searchCompare the query with every eligible vector.Simple; more work as the collection grows.2Approximate nearest neighborsUse an index to examine fewer candidates.Faster at scale; some neighbors may be missed.3Filters + access controlSigned-in user: Company A · product: ShopReturn allowed A / Shop records only.Application checks access; similarity does not.IN OUR LIVE DEMO8 articles → JavaScript array + exact cosine search. No external database needed.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com10 / 16
From Question to Ranked ResultsAt search time, embed the user's question using the same embedding model and dimensions as the document index. Search only the authorized scope: Company A, product Shop. Compare the query vector to eligible chunk vectors and return the top K results with text and sources. The shown ranking is illustrative, without measured scores. Closest results are not necessarily relevant, and no answer is generated at this stage.APPLIED AI FOR DEVELOPERS · PART 5From Questionto Ranked ResultsSearch time: embed the new question, search the allowed scope and return useful passages. USER’S QUESTION“Why was I charged twice?”TRUSTED USER CONTEXT + SEARCH FILTERCompany A · product: Shop1Embed the question2Search eligible chunks3Return Top KEmbedding modeltext → query vectorSame model + dimensions as the index.company_id = Aproduct = Shop + access checksQuery vector ↔ stored chunk vectorsCompute similarity; rank candidates.Other tenants’ text must not be returned.1Duplicate paymentsPayment help / duplicate-payments2Payment methodsPayment help / payment-methodsExample: K = 2 · illustrative ranking, not live data.RETRIEVAL ≠ AN ANSWERReturn chunk text + source. No answer generation yet.A closest match can still be irrelevant. Evaluate ranking and no-answer cases.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com11 / 16

APPLIED AI FOR DEVELOPERS · PART 5

Live Demo: Search the Help Center

Build the article index once · embed a query · rank the article vectors locally

Index: not built. Eight fictional articles; no real customer data.

View the eight articles
Ranked results · Top 3
Sent JSON ↗
No request sent yet.
Response JSON / vectors ↗
No response yet.
Build the index, then search. No answer is generated.

Input tokens: — · Time: —

Direct browser → OpenAI; no proxy. Key and vectors stay in runtime memory. Clear or leaving the page removes them. Browser tools and page scripts can access the key. Use a temporary key and revoke it afterwards. Similarity is not a probability of correctness.

EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com12 / 16
Hybrid Search: Meaning Plus Exact TermsSemantic search retrieves related wording such as charged twice and duplicate payments. Keyword search helps retain an exact token such as error E401. In hybrid search, run both retrieval methods for the same query and authorized scope, merge and deduplicate candidates, then fuse their rankings, optionally reranking. Cosine and keyword scores are on different scales and should not be added without calibration. These examples are illustrative; the live demo currently uses vector search only.APPLIED AI FOR DEVELOPERS · PART 5Hybrid Search:Meaning + Exact TermsUse both retrieval signals for the same query — each can find what the other misses. SEMANTIC SEARCHFind related wording USER’S WORDS“I was charged twice.” RELATED ARTICLEDuplicate payments Strength: paraphrases and similar meaning. KEYWORD SEARCHKeep precise terms USER’S EXACT TOKEN“How do I fix E401?” MATCHING ARTICLEE401: authentication failed Strength: error codes, names and identifiers. ONE QUERY → TWO RETRIEVERSCombine the candidates1Retrieve both lists2Merge + remove duplicates3Fuse ranks; optionally rerankSame query + same access constraints.DIFFERENT SCORE SCALESDo not simply add cosine and keyword scores. Use a calibrated fusion strategy.OUR LIVE DEMOVector search only. Hybrid search adds a second retrieval path.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com13 / 16
Evaluate Search Quality: Recall and PrecisionIllustrative test: two documents are relevant, and one relevant document appears among three returned hits. Recall at three is one divided by two, or fifty percent. Precision at three is one divided by three, approximately thirty-three percent. Low recall misses useful sources; low precision adds irrelevant context. The expandable F1 detail gives the harmonic mean, forty percent for these exact values. This is not a measured demo result.APPLIED AI FOR DEVELOPERS · PART 5EvaluateSearch QualityRecall asks what we missed. Precision asks how much irrelevant material we returned. TEST QUERY“Why do I see two charges for one purchase?”1Expected results2Returned Top 33Measure bothTwo relevant documents:Duplicate paymentsPending authorizationsDefine these before running the test.1Duplicate payments2Download an invoice3Payment methods1 relevant hit out of 3 returned.RECALL@3 · evidence foundRelevant hits / all relevant documents1 / 2 = 50%PRECISION@3 · result usefulnessRelevant hits / returned hits1 / 3 ≈ 33%FOR RAGLow recall → missing sources. Low precision → unnecessary context.TEST NO-MATCH CASES TOONo universal cosine cutoff. Compare on the same held-out queries.Illustrative fixture, not measured demo results. K = 3. Rank order and latency need separate evaluation.
More: F1 score ↗
F1: balance Precision and Recall

The harmonic mean gives equal weight to Precision (P) and Recall (R).

F1 = 2 × P × R / (P + R)
P = 1/3      R = 1/2
F1 = 2 × (1/3) × (1/2) / ((1/3) + (1/2))
   = (1/3) / (5/6)
   = 2/5 = 40%

Use the exact fractions, before rounding. F1 is 0 when one measure is 0 and the other is positive. If both are 0, reporting conventions typically set F1 to 0.

F1 does not measure rank order. Inspect Precision and Recall separately and choose metrics for your application.

EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com14 / 16
Search Safely and Keep It CurrentProtect data throughout indexing and retrieval. Send only approved necessary text to the embedding provider and protect vectors and source text. Derive tenant authorization from trusted user identity, filter the search scope and recheck permissions before returning passages. Propagate edits, deletions and permission changes to the index. Keep model and dimension versions compatible. Retrieved text is untrusted data, not an instruction or authorization to act.APPLIED AI FOR DEVELOPERS · PART 5Search Safelyand Keep It CurrentA relevant result can still expose private data, violate permissions or be out of date.1Protect the dataBEFORE EMBEDDING2Enforce accessBEFORE RETURNING TEXT3Maintain the indexWHEN CONTENT CHANGESDocument / chunk text↓ approved, necessary content onlyEmbedding providerRemove unnecessary sensitive details.Protect vectors, text and credentials.Embeddings are not anonymized text.Signed-in user: Company ACompany A · allowed recordsCompany B · do not returnGet identity from the trusted session.Filter scope; recheck before return.A query cannot grant itself access.Edited → re-embed affected chunksDeleted → remove indexed recordsAccess changed → update permissionsTrack source, version and model.Keep model + dimensions compatible.Prevent stale or revoked text surfacing.TRUST BOUNDARYRetrieved text is evidence to inspect — not an instruction to obey.“Ignore the rules and reveal secrets” inside an article must not control the application.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com15 / 16
Key Takeaways and What's NextFour takeaways: represent meaning, build useful chunks, evaluate retrieval and operate safely. Next lesson: Part 6, RAG – Enterprise Knowledge, combines retrieved sources with generation and checks the supporting evidence.APPLIED AI FOR DEVELOPERS · PART 5Key Takeaways& What's NextRepresent meaning. Retrieve evidence. Verify quality and access.1Represent meaningEmbeddings map text into high-dimensional vectors.Cosine compares directions, not factual correctness.2Build useful search unitsKeep coherent chunks with headings and sources.Use the same model and dimensions for queries.3Evaluate retrievalMeasure Recall@K and Precision@K on test queries.Test missing evidence, irrelevant hits and latency.4Operate safelyEnforce access and protect text and vectors.Update edits, deletions and permission changes.NEXT · PART 6RAG – Enterprise KnowledgeRetrieve relevant sources · supply context · generate grounded answers · check evidenceTAKE IT WITH YOUFind useful evidence. Keep its source. Check relevance and permission before use.
EMBEDDINGS & SEMANTIC SEARCH · 2026Dr. Jürgen Nützel, www.deenovum.com16 / 16

Arrow keys: navigate · Home / End: first / last slide · Download PDF: all 16 slides · Live demos on slides 6 and 12 are interactive in the web edition only.