Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Webinar: Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain

Webinar: Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain

Slides for my Webinar

Sebastian Gingter

November 15, 2023
Tweet

More Decks by Sebastian Gingter

Other Decks in Programming

Transcript

  1. 2 ▪ Consultant & Erklärbär 🐻💬 bei der Thinktecture AG

    ▪ Fokus auf ▪ Generative AI im Businessumfeld ▪ Flexible und skalierbare Backend-Architekturen ▪ Frontends mit Blazor ▪ Entwicklerproduktivität ▪ Softwarequalität ▪ Alles rund um .NET Core ▪ [email protected] ▪ Twitter: @phoenixhawk Sebastian Gingter mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  2. 3 ▪ Ein bisschen Hintergrund-Info & Theorie ▪ Überblick über

    das Themengebiet Semantische Suche ▪ Probleme und mögliche Strategien ▪ Kein C#, sondern Python 😱 ▪ Kein Deep-Dive in ▪ LLMs ▪ Vektor-Datenbanken ▪ LangChain Was Euch erwartet (und was nicht): mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  3. 4 ▪ Introduction (and a bit of theory) ▪ Embeddings

    ▪ Vector-Databases ▪ Indexing ▪ Retrieval ▪ Not good enough? – Indexing II ▪ HyDE & alternative indexing methods ▪ Responses: RAG–Pattern (Retrieval Augmented Generation) ▪ Conclusion Agenda mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  4. 5 • https://www.thinktecture.com/technologien/ai/ Generative AI Topic Hub and upcoming Webinars

    Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain
  5. 6 Introduction mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche

    Introduction Embeddings Vector-DBs Indexing Retrieval Indexing II RAG
  6. 7 ▪ Classic search: lexical ▪ Compares words, parts of

    words and variants ▪ Classic SQL: WHERE ‘content’ LIKE ‘%searchterm%’ ▪ We can search only for things where we know that its somewhere in the text ▪ New: Semantic search ▪ Compares for the same contextual meaning ▪ “Das Rudel rollt das runde Gerät auf dem Rasen herum” ▪ “The pack enjoys rolling a round thing on the green grass” ▪ “Die Hunde spielen auf der Wiese mit dem Ball” ▪ “The dogs play with the ball on the meadow” mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche Semantic Search
  7. 8 ▪ How to grasp “semantics”? ▪ Computers only calculate

    on numbers ▪ Computing is “applied mathematics” ▪ AI also only calculates on numbers ▪ We need a numeric representation of meaning ➔ “Embeddings” mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche Semantic Search
  8. 9 Embeddings mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche

    Introduction Embeddings Vector-DBs Indexing Retrieval Indexing II RAG
  9. 10 Embedding (math.) Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und

    LangChain ▪ Natural language is very complex ▪ Task: Map high complexity to lower complexity ▪ Topologic: Value of a high dimensional space is “embedded” into a lower dimensional space ▪ injective function German: Injektive Abbildung, linkseindeutige Funktion ▪ Similar to hash, or a lossy compression
  10. 11 ▪ Embedding model (specialized ML model) converting text into

    a numeric representation of its meaning ▪ Representation is a vector in an n-dimensional space ▪ n floating point values ▪ OpenAI “text-embedding-ada-002” uses 1532 dimensions ▪ Other models may use 400-750 and up to around 1000 dimensions ▪ see also https://huggingface.co/spaces/mteb/leaderboard mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche Embeddings
  11. 12 ▪ Embedding models are unique vectors from different models

    are incompatible with each other ▪ Each dimension has a different meaning, individual to the model ▪ Some embedding models are multi-language, but not all ▪ In an LLM, also the first step is to embed the input into a lower dimensional space mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche Embeddings
  12. 13 ▪ Mathematical quantity with a direction and length ▪

    Ԧ 𝑎 = 𝑎𝑥 𝑎𝑦 Quelle: https://www.grund-wissen.de/mathematik/lineare-algebra-und-analytische-geometrie/vektoren.html mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche What is a vector?
  13. 15 Vectors in 3D Moderne Semantische Suche mit LLMs, Vektor-Datenbanken

    und LangChain Ԧ 𝑎 = 𝑎𝑥 𝑎𝑦 𝑎𝑧
  14. 16 Vectors in multidimensional space Moderne Semantische Suche mit LLMs,

    Vektor-Datenbanken und LangChain Ԧ 𝑎 = 𝑎𝑢 𝑎𝑣 𝑎𝑤 𝑎𝑥 𝑎𝑦 𝑎𝑧
  15. 18 𝐵𝑟𝑜𝑡ℎ𝑒𝑟 − 𝑀𝑎𝑛 + 𝑊𝑜𝑚𝑎𝑛 ≈ 𝑆𝑖𝑠𝑡𝑒𝑟 Word2Vec Mikolov

    et al., Google, 2013 Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain Man Woman Brother Sister Paper: https://arxiv.org/abs/1301.3781
  16. 19 Embedding-Model Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain

    ▪ Task: Create a vector from an input ▪ Extract meaning / semantics ▪ Embedding models usually are very shallow which makes them very fast Word2Vec is only two layers ▪ Similar to the first step of an LLM ▪ Convert text to values for input layer ▪ This comparison is flawed, but one could say: ▪ The embedding model ‘maps’ the meaning into the model’s ‘brain’
  17. 21 Embedding-Model Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain

    [ 0.50451 , 0.68607 , -0.59517 , -0.022801, 0.60046 , -0.13498 , -0.08813 , 0.47377 , -0.61798 , -0.31012 , -0.076666, 1.493 , -0.034189, -0.98173 , 0.68229 , 0.81722 , -0.51874 , -0.31503 , -0.55809 , 0.66421 , 0.1961 , -0.13495 , -0.11476 , -0.30344 , 0.41177 , -2.223 , -1.0756 , -1.0783 , -0.34354 , 0.33505 , 1.9927 , -0.04234 , -0.64319 , 0.71125 , 0.49159 , 0.16754 , 0.34344 , -0.25663 , -0.8523 , 0.1661 , 0.40102 , 1.1685 , -1.0137 , -0.21585 , -0.15155 , 0.78321 , -0.91241 , -1.6106 , -0.64426 , -0.51042 ]
  18. 23 Embedding-Model Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain

    Source: http://jalammar.github.io/illustrated-word2vec/
  19. 24 ▪ Embedding model: “Analog to digital converter for text”

    ▪ Embeds the high-dimensional natural language meaning into a lower dimensional-space (the model’s ‘brain’) ▪ No magic, just applied mathematics ▪ Math. representation: Vector of n dimensions ▪ Technical representation: array of floating point numbers mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche Recap Embeddings
  20. 26 Vector-Databases mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche

    Introduction Embeddings Vector-DBs Indexing Retrieval Indexing II RAG
  21. 27 ▪ Mostly document-based ▪ Index: Embedding (vector) ▪ Document

    (content) ▪ Metadata ▪ Query functionalities Vector-Databases mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  22. ▪ Pinecone ▪ Milvus ▪ Chroma ▪ Weaviate ▪ Deep

    Lakee ▪ Qdrant ▪ Elasticsearch ▪ Vespa ▪ Vald ▪ ScaNN ▪ Pgvector (PostgreSQL Extension) ▪ Faiss ▪ … Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain Vector-Databases ▪ … (probably) coming to a relational database near you soon(ish) SQL Server Example: https://learn.microsoft.com/en-us/samples/azure-samples/azure-sql-db-openai/azure-sql-db-openai/
  23. 29 ▪ (Search-)Algorithms ▪ Cosine Similarity 𝑆𝐶(a,b) = a ∙𝑏

    𝑎 × 𝑏 ▪ Manhatten Distance (L1 norm, taxicab) ▪ Euclidean Distance (L2 norm) ▪ Minkowski Distance (~ generalization of L1 and L2 norms) ▪ L∞ ( L-Infinity), Chebyshev Distance ▪ Jaccard index / similarity coefficient (Tanimoto index) ▪ Nearest Neighbour ▪ Bregman divergence ▪ … Vector-Databases mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  24. 31 Indexing mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche

    Introduction Embeddings Vector-DBs Indexing Retrieval Indexing II RAG
  25. 32 ▪ Loading ▪ Clean-up ▪ Splitting ▪ Embedding ▪

    Storing Indexing mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  26. 34 ▪ HTML Tags ▪ Formatting information ▪ Normalization ▪

    lowercasing ▪ stemming, lemmatization ▪ remove punctuation & stop words ▪ Enrichment ▪ tagging ▪ keywords, categories ▪ metadata Clean-up mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  27. 35 ▪ Document is too large / too much content

    / not concise enough Splitting (Text Segmentation) mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche ▪ by size (text length) ▪ by character (\n\n) ▪ by paragraph, sentence, words (until small enough) ▪ by size (tokens) ▪ overlapping chunks (token-wise)
  28. 36 ▪ Indexing Vector-Databases mit LLMs, Vektor-Datenbanken und LangChain Moderne

    Semantische Suche Splitted (smaller) parts Embedding- Model Embedding 𝑎 𝑏 𝑐 … Vector- Database Document Metadata: Reference to original document
  29. 37 Retrieval (Search) mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische

    Suche Introduction Embeddings Vector-DBs Indexing Retrieval Indexing II RAG
  30. 38 Retrieval mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche

    Embedding- Model Embedding 𝑎 𝑏 𝑐 … Vector- Database “What is the name of the teacher?” Query Doc. 1: 0.86 Doc. 2: 0.84 Doc. 3: 0.79 Weighted result … (Answer generation)
  31. 40 Indexing II Not good enough? mit LLMs, Vektor-Datenbanken und

    LangChain Moderne Semantische Suche Introduction Embeddings Vector-DBs Indexing Retrieval Indexing II RAG
  32. 42 ▪ Search for a hypothetical Document HyDE (Hypothetical Document

    Embedddings) mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche LLM, e.g. GPT-3.5-turbo Embedding 𝑎 𝑏 𝑐 … Vector- Database Doc. 3: 0.86 Doc. 2: 0.81 Doc. 1: 0.81 Weighted result Hypothetical Document Embedding- Model Write a company policy that contains all information which will answer the given question: {QUERY} “What should I do, if I missed the last train?” Query Source: https://arxiv.org/abs/2212.10496
  33. 44 ▪ Downside of HyDE: ▪ Each request needs to

    be transformed through an LLM (slow & expensive) ▪ A lot of requests will probably be very similar to each other ▪ Each time a different hyp. document is generated, even for an extremely similar request ▪ Leads to very different results each time ▪ Idea: Alternative indexing ▪ Transform the document, not the query Other transformations? mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  34. 45 Alternative Indexing HyQE: Hypothetical Question Embedding – Thinktecture-Style 😉

    mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche LLM, e.g. GPT-3.5-turbo Transformed document Write 3 questions, which are answered by the following document. Chunk of Document Embedding- Model Embedding 𝑎 𝑏 𝑐 … Vector- Database Metadata: content of original chunk
  35. 46 ▪ Retrieval Alternative Indexing mit LLMs, Vektor-Datenbanken und LangChain

    Moderne Semantische Suche Embedding- Model Embedding 𝑎 𝑏 𝑐 … Vector- Database Doc. 3: 0.89 Doc. 1: 0.86 Doc. 2: 0.76 Weighted result Original document from metadata “What should I do, if I missed the last train?” Query
  36. 47 ▪ Tune text cleanup, segmentation, splitting ▪ HyDE or

    HyQE or alternative indexing ▪ How many questions? ▪ With or without summary ▪ Other approaches ▪ Only generate summary ▪ Extract “Intent” from user input and search by that ▪ Transform document and query to a common search embedding ▪ HyKSS: Hybrid Keyword and Semantic Search https://www.deg.byu.edu/papers/HyKSS.pdf ▪ Always evaluate approaches with your own data & queries ▪ The actual / final approach is more involved as it seems on the first glance Recap: Not good enough? mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  37. 49 Responses: RAG mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische

    Suche Introduction Embeddings Vector-DBs Indexing Retrieval Indexing II RAG
  38. 50 RAG (Retrieval Augmented Generation) mit LLMs, Vektor-Datenbanken und LangChain

    Moderne Semantische Suche Embedding- Model Embedding 𝑎 𝑏 𝑐 … Vector- Database Search Result LLM “You can get a hotel room or take a cab. € 200 to € 300 might still be okay to get you to your destination. Please make sure to ask the cab driver for a fixed fee upfront.” Answer the user’s question. Relevant document: {SearchResult} Question: {Query} System Prompt “What should I do, if I missed the last train?” Query
  39. 53 ▪ Semantic search is a first and fast Generative

    AI business use-case ▪ Quality of results depend heavily on data quality and preparation pipeline ▪ RAG pattern can produce breathtaking good results without the need for user training Conclusion mit LLMs, Vektor-Datenbanken und LangChain Moderne Semantische Suche
  40. Moderne Semantische Suche mit LLMs, Vektor-Datenbanken und LangChain Sebastian Gingter

    [email protected] Consultant & Erklärbär 🐻💬 Slides & Code https://www.thinktecture.com/de/sebastian-gingter