{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Muhammad Fahad Bashir",
  "title_listed": "4. Embeddings Explained - Next in  RAG Series",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://mfahadbashir.hashnode.dev/4-embeddings-explained-next-in-rag-series",
  "publish_date": "2024-09-07",
  "corpus_stage": "",
  "notes": "",
  "id": "afa0645e47fc",
  "collected_at": "2026-10-02T14:10:59",
  "title": "4. Embeddings Explained - Next in  RAG Series",
  "body_markdown": "4. Embeddings Explained - Next in  RAG Series\nUpdated\nSeptember 7, 2024\n•\n3\nmin read\n•\nView as Markdown\nM\nMuhammad Fahad Bashir\nMachine Learning, Artificial Intelligence\nPart of series\nImplementing RAG systems from Scratch in-depth\nIn our previous article, we explored and discussed the concept of splitting documents into chunks. Now, we'll talk next crucial step: embeddings. For this purpose, we use embedding models.\nThey take input and convert it into a numerical representation so we can perform mathematical operations which will be helpful in retrieving our response. Semantic search also becomes possible. In semantic search, the words whose meanings are closer their numerical representation is also closer.\nThese embeddings are then later stored in a vector store.\nWhat are Embeddings?\nEmbeddings are vector representations of text data, converting words or phrases into numerical codes. This process enables computers to understand and process human language, facilitating semantic search and other NLP tasks.\nApplications of Embeddings\nSimilarity Search: Measure similarity between instances, which is particularly useful in NLP tasks.\nClustering and Classification: Utilize embeddings as input features for clustering and classification tasks.\nInformation Retrieval: Leverage embeddings to build search engines and retrieve relevant documents.\nRecommendation Systems: Recommend products, articles, or media to users based on their preferences.\n💡\nEmbeddings are like a bridge that connects how we humans understand things and how computers work their magic. They take all sorts of information, whether it’s text, images, or other stuff, and turn it into numbers, like a secret code. Once we have these number codes, we can do some really cool things with AI.\nEmbedding Class\nThe Embeddings class is used to generate embeddings.This class is designed to provide a standard interface for all of them. Some providers are open source and some require subscription to use. Several providers offer embedding models, including:\nOpenAI\nA121 Labs\nAzure\nGigaChat\nGoogle Generative AI\nTo know more about Embedding Models visit the\nofficial docs\nThese models can accept either documents (multiple text) or queries (single text).\nExamples of Embedding only\nWe can choose any of the providers like Openai, cohere, and others but here we will be giving examples of huggingface. Huggingface is open source so we can use it without api-key and subscription.\nFirst, we have to install the package then we can load the model of any choice.\n!pip install langchain-huggingface\nfrom\nlangchain_huggingface\nimport\nHuggingFaceEmbeddings\nembeddings_model = HuggingFaceEmbeddings(model_name=\n\"sentence-transformers/all-mpnet-base-v2\"\n)\nBy Query\nWe can use\n.embed_query\nto embed a single piece of text\nembeedings=embeddings_model.embed_query(\n\"We are writing this query \"\n)\nprint(embeedings)\nBy Document\nWe can use\n.embed_documents\nto embed a list of strings, recovering a list of embeddings:\nembeddings = embeddings_model.embed_documents(\n    [\n\"Hi there!\"\n,\n\"Oh, hello!\"\n,\n\"What's your name?\"\n,\n\"My friends call me World\"\n,\n\"Hello World!\"\n]\n)\nprint(embeddings)\nRevising the previous steps\nwe will be covering the following steps of the ingestion pipeline.\nimport necessary packages\n!pip install langchain langchain_community \n  !pip install transformers langchain_huggingface\n  !pip install pypdf\n# as using pdf file for example\nload the documents\nfrom\nlangchain.document_loaders\nimport\nPyPDFLoader\n  loaders=PyPDFLoader(\n'/content/FYP Report PhysioFlex(25july).pdf'\n)\n  doc=loaders.load()\n  docs\nsplit them into chunks\nfrom\nlangchain.text_splitter\nimport\nRecursiveCharacterTextSplitter\n  textsplitter=RecursiveCharacterTextSplitter(\n      chunk_size=\n300\n,\n      chunk_overlap=\n50\n)\n  split=textsplitter.split_documents(doc)\n  print(\n\"Total Number of chunks : \"\n, len(split))\n  print(\n\"printing first chunk :'\\n \"\n, split[\n0\n])\nWe used RecursiveCharacterTextSplitter and from that single document it is now splitter into 290 chunks\nmake their embeddings\nfrom\nlangchain_huggingface\nimport\nHuggingFaceEmbeddings\nembed_model = HuggingFaceEmbeddings(model_name=\n'BAAI/bge-small-en-v1.5'\n)\n\nembedded=embed_model.embed_documents([doc.page_content\nfor\ndoc\nin\nsplit])\nprint(embedded[\n1\n])\n# only viewing the emebding of chunk 1\nyou can play around with this\nnotebook.\nConclusion\nIn this article, we explored the concept of embeddings, their applications, and the various embedding models available. By understanding embeddings, we can unlock the power of semantic search and build innovative NLP applications. Stay tuned for our next article in the RAG series, where we'll dive deeper into the world of embeddings and their applications.\n#\nvector-embeddings\n#\nrag\n#\ntutorial\n#\nstep-by-step\n#\nseries\n53\nviews",
  "metadata": {
    "title": "4. Embeddings Explained - Next in  RAG Series",
    "description": "Learn embeddings to enhance NLP tasks by translating text into numerical codes, with models and practical examples explained",
    "keywords": null,
    "author": null,
    "og:title": "4. Embeddings Explained - Next in  RAG Series",
    "og:description": "Learn embeddings to enhance NLP tasks by translating text into numerical codes, with models and practical examples explained",
    "og:url": "https://mfahadbashir.hashnode.dev/4-embeddings-explained-next-in-rag-series",
    "og:site_name": "Muhammad Fahad Bashir",
    "og:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725694785413/5d60f7ab-2e6d-4f7c-8fd6-abbbe97450ae.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "4. Embeddings Explained - Next in  RAG Series",
    "twitter:description": "Learn embeddings to enhance NLP tasks by translating text into numerical codes, with models and practical examples explained",
    "twitter:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725694785413/5d60f7ab-2e6d-4f7c-8fd6-abbbe97450ae.png",
    "article:published_time": "2024-09-07T07:41:41.629Z",
    "article:modified_time": "2024-09-07T14:36:03.950Z",
    "article:author": "https://mfahadbashir.hashnode.dev/@mfahadbashir"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/afa0645e47fc/code/body-000.txt",
      "chars": 186,
      "language": "text"
    },
    {
      "file": "attachments/afa0645e47fc/code/body-001.txt",
      "chars": 88,
      "language": "text"
    },
    {
      "file": "attachments/afa0645e47fc/code/body-002.txt",
      "chars": 210,
      "language": "text"
    },
    {
      "file": "attachments/afa0645e47fc/code/body-003.txt",
      "chars": 150,
      "language": "text"
    },
    {
      "file": "attachments/afa0645e47fc/code/body-004.txt",
      "chars": 149,
      "language": "text"
    },
    {
      "file": "attachments/afa0645e47fc/code/body-005.txt",
      "chars": 305,
      "language": "text"
    },
    {
      "file": "attachments/afa0645e47fc/code/body-006.txt",
      "chars": 263,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725694785413/5d60f7ab-2e6d-4f7c-8fd6-abbbe97450ae.png",
      "bytes": 157648,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/000.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725694785413/5d60f7ab-2e6d-4f7c-8fd6-abbbe97450ae.png",
      "alt": "4. Embeddings Explained - Next in  RAG Series",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725690978811/78c02143-3639-49fc-9a14-801e86815026.png",
      "bytes": 83086,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/002.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725690978811/78c02143-3639-49fc-9a14-801e86815026.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725690353472/c967a491-c03a-44a7-87b8-8940679911db.png",
      "bytes": 45577,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/003.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725690353472/c967a491-c03a-44a7-87b8-8940679911db.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692048311/25eeba54-f1b8-44f6-b401-542d0dcae1a0.png",
      "bytes": 3690,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/004.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692048311/25eeba54-f1b8-44f6-b401-542d0dcae1a0.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692167325/8702e891-94da-4e2e-9644-0ed74e7ff6d3.png",
      "bytes": 3793,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/005.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692167325/8702e891-94da-4e2e-9644-0ed74e7ff6d3.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692658943/407398f6-4f3b-44a5-8b9c-aacf7843f210.png",
      "bytes": 25095,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/006.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692658943/407398f6-4f3b-44a5-8b9c-aacf7843f210.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692861819/539a8a96-81cf-4a1c-bfa2-137a1558da2d.png",
      "bytes": 44227,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/007.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725692861819/539a8a96-81cf-4a1c-bfa2-137a1558da2d.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725693202215/c78d8149-1420-4a3b-9366-f6c73caebb9d.png",
      "bytes": 12349,
      "reused": true,
      "local_path": "attachments/afa0645e47fc/images/008.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725693202215/c78d8149-1420-4a3b-9366-f6c73caebb9d.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725693421520/08ba5ff3-8897-400b-b432-d03ce6ffefb2.png",
      "bytes": 9365,
      "reused": false,
      "local_path": "attachments/afa0645e47fc/images/body-e74ea9daf810a90d2ebf.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1725693421520/08ba5ff3-8897-400b-b432-d03ce6ffefb2.png",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://mfahadbashir.hashnode.dev/4-embeddings-explained-next-in-rag-series.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@mfahadbashir",
      "label": "M",
      "type": "a"
    },
    {
      "href": "https://www.linkedin.com/in/muhammad-fahad-bashir-09568216a/",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/series/rag-series",
      "label": "Part of series Implementing RAG systems from Scratch in-depth",
      "type": "a"
    },
    {
      "href": "https://python.langchain.com/v0.1/docs/integrations/text_embedding/",
      "label": "official docs",
      "type": "a"
    },
    {
      "href": "https://colab.research.google.com/drive/1vCUmJq22WEAJS3X4cQWN5jd-JHJ_OVHE?usp=sharing",
      "label": "notebook.",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/vector-embeddings",
      "label": "# vector-embeddings",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/rag",
      "label": "# rag",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/tutorial",
      "label": "# tutorial",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/step-by-step",
      "label": "# step-by-step",
      "type": "a"
    },
    {
      "href": "https://mfahadbashir.hashnode.dev/tag/series",
      "label": "# series",
      "type": "a"
    }
  ],
  "retry_at": "2026-10-03T08:08:39",
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:46Z",
  "source_url": "https://mfahadbashir.hashnode.dev/4-embeddings-explained-next-in-rag-series",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 4787,
  "expected_body_images": 9,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/afa0645e47fc/pages/source.html",
  "local_html": "attachments/afa0645e47fc/pages/article.html",
  "body_base_path": "attachments/afa0645e47fc/pages/",
  "body_markdown_path": "attachments/afa0645e47fc/pages/article.md"
}