{
  "source_file": "0930",
  "source_platform": "Medium",
  "author": "Cristian Leo",
  "title_listed": "How to Query a Knowledge Graph with LLMs Using gRAG",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://towardsdatascience.com/how-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322/",
  "publish_date": "2024-11-07",
  "corpus_stage": "",
  "notes": "",
  "id": "a6e5a94070f2",
  "collected_at": "2026-10-02T06:00:45",
  "title": "How to Query a Knowledge Graph with LLMs Using gRAG | Towards Data Science",
  "body_markdown": "![Revisit consent button](../images/body-df29beacd7496220cc94.svg)\nWe value your privacy\nWe use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking \"Accept All\", you consent to our use of cookies.\nCustomise Reject All Accept All\nCustomise Consent Preferences ![](../images/body-2159bb67f3de8ad870eb.svg)\nWe use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.\nThe cookies that are categorised as \"Necessary\" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more\nNecessaryAlways Active\nNecessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.\n  * Cookie\nBCTempID\n  * Duration\n10 minutes\n  * Description\nNo description available.\n\n\n  * Cookie\n__cf_bm\n  * Duration\n1 hour\n  * Description\nThis cookie, set by Cloudflare, is used to support Cloudflare Bot Management. \n\n\n  * Cookie\nAWSALBCORS\n  * Duration\n7 days\n  * Description\nAmazon Web Services set this cookie for load balancing.\n\n\n  * Cookie\n_cfuvid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to track users across sessions to optimize user experience by maintaining session consistency and providing personalized services\n\n\n  * Cookie\nli_gc\n  * Duration\n6 months\n  * Description\nLinkedin set this cookie for storing visitor's consent regarding using cookies for non-essential purposes.\n\n\n  * Cookie\n__hssrc\n  * Duration\nsession\n  * Description\nThis cookie is set by Hubspot whenever it changes the session cookie. The __hssrc cookie set to 1 indicates that the user has restarted the browser, and if the cookie does not exist, it is assumed to be a new session.\n\n\n  * Cookie\n__hssc\n  * Duration\n1 hour\n  * Description\nHubSpot sets this cookie to keep track of sessions and to determine if HubSpot should increment the session number and timestamps in the __hstc cookie.\n\n\n  * Cookie\nwpEmojiSettingsSupports\n  * Duration\nsession\n  * Description\nWordPress sets this cookie when a user interacts with emojis on a WordPress site. It helps determine if the user's browser can display emojis properly.\n\n\n  * Cookie\nBCSessionID\n  * Duration\n1 year 1 month 4 days\n  * Description\nBlueconic sets this cookie as a unique identifier for the BlueConic profile.\n\n\n  * Cookie\n_octo\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nlogged_in\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\n__Secure-YEC\n  * Duration\npast\n  * Description\nYouTube sets this cookie to stores the user's video player preferences using embedded YouTube video\n\n\n  * Cookie\n__eoi\n  * Duration\n6 months\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTGCORS\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\nlogin-status-p\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTG\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\ncsrf_token\n  * Duration\nsession\n  * Description\nNo description available.\n\n\n  * Cookie\ntoken_v2\n  * Duration\n1 day\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nD\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nPHPSESSID\n  * Duration\nsession\n  * Description\nThis cookie is native to PHP applications. The cookie stores and identifies a user's unique session ID to manage user sessions on the website. The cookie is a session cookie and will be deleted when all the browser windows are closed.\n\n\n  * Cookie\nVISITOR_PRIVACY_METADATA\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to store the user's cookie consent state for the current domain. \n\n\n  * Cookie\ncookietest\n  * Duration\nsession\n  * Description\nThe cookietest cookie is typically used to determine whether the user's browser accepts cookies, essential for website functionality and user experience.\n\n\n  * Cookie\n__Host-airtable-session\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\n__Host-airtable-session.sig\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\nm\n  * Duration\n1 year 1 month 4 days\n  * Description\nStripe sets this cookie for fraud prevention purposes. It identifies the device used to access the website, allowing the website to be formatted accordingly.\n\n\n  * Cookie\nBIGipServer*\n  * Duration\nsession\n  * Description\nMarketo sets this cookie to collect information about the user's online activity and build a profile about their interests to provide advertisements relevant to the user. \n\n\n  * Cookie\n__cfruid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to identify trusted web traffic.\n\n\n  * Cookie\n_GRECAPTCHA\n  * Duration\n6 months\n  * Description\nGoogle Recaptcha service sets this cookie to identify bots to protect the website against malicious spam attacks.\n\n\n  * Cookie\n__Secure-YNID\n  * Duration\n6 months\n  * Description\nGoogle cookie used to protect user security and prevent fraud, especially during the login process.\n\n\n  * Cookie\ncookieyes-consent\n  * Duration\n1 year\n  * Description\nCookieYes sets this cookie to remember users' consent preferences so that their preferences are respected on subsequent visits to this site. It does not collect or store any personal information about the site visitors.\n\n\nFunctional\nFunctional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.\n  * Cookie\nlidc\n  * Duration\n1 day\n  * Description\nLinkedIn sets the lidc cookie to facilitate data center selection.\n\n\n  * Cookie\nbrw\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nbrwConsent\n  * Duration\n5 minutes\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nWMF-Uniq\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nloom_anon_comment\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nloom_referral_video\n  * Duration\nsession\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nVISITOR_INFO1_LIVE\n  * Duration\n6 months\n  * Description\nA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.\n\n\n  * Cookie\nyt-remote-connected-devices\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nytidb::LAST_RESULT_ENTRY_KEY\n  * Duration\nNever Expires\n  * Description\nThe cookie ytidb::LAST_RESULT_ENTRY_KEY is used by YouTube to store the last search result entry that was clicked by the user. This information is used to improve the user experience by providing more relevant search results in the future.\n\n\n  * Cookie\nyt-remote-device-id\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-name\n  * Duration\nsession\n  * Description\nThe yt-remote-session-name cookie is used by YouTube to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\nyt-remote-fast-check-period\n  * Duration\nsession\n  * Description\nThe yt-remote-fast-check-period cookie is used by YouTube to store the user's video player preferences for embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-app\n  * Duration\nsession\n  * Description\nThe yt-remote-session-app cookie is used by YouTube to store user preferences and information about the interface of the embedded YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-available\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-available cookie is used to store the user's preferences regarding whether casting is available on their YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-installed\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-installed cookie is used to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\ncp_session\n  * Duration\n3 months\n  * Description\nCodepen sets this cookie for Help systems found in the website.\n\n\n  * Cookie\nloid\n  * Duration\n1 year 1 month 4 days\n  * Description\nThis cookie is set by the Reddit. The cookie enables the sharing of content from the website onto the social media platform.\n\n\nAnalytics\nAnalytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.\n  * Cookie\n__hstc\n  * Duration\n6 months\n  * Description\nHubspot set this main cookie for tracking visitors. It contains the domain, initial timestamp (first visit), last timestamp (last visit), current timestamp (this visit), and session number (increments for each subsequent session).\n\n\n  * Cookie\nhubspotutk\n  * Duration\n6 months\n  * Description\nHubSpot sets this cookie to keep track of the visitors to the website. This cookie is passed to HubSpot on form submission and used when deduplicating contacts.\n\n\n  * Cookie\n_ga\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors.\n\n\n  * Cookie\n_ga_*\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to store and count page views.\n\n\n  * Cookie\n__Host-psifi.analyticsTrace\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n__Host-psifi.analyticsTraceV2\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n_gh_sess\n  * Duration\nsession\n  * Description\nGitHub sets this cookie for temporary application and framework state between pages like what step the user is on in a multiple step form.\n\n\n  * Cookie\nYSC\n  * Duration\nsession\n  * Description\nYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.\n\n\n  * Cookie\najs_anonymous_id\n  * Duration\n1 year\n  * Description\nThis cookie is set by Segment to count the number of people who visit a certain site by tracking if they have visited before.\n\n\n  * Cookie\nvuid\n  * Duration\n1 year 1 month 4 days\n  * Description\nVimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. \n\n\nPerformance\nPerformance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.\n  * Cookie\nAWSALB\n  * Duration\n7 days\n  * Description\nAWSALB is an application load balancer cookie set by Amazon Web Services to map the session to the target.\n\n\n  * Cookie\nacq\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nacq.sig\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nptc\n  * Duration\n2 years\n  * Description\nNo description available.\n\n\nAdvertisement\nAdvertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.\n  * Cookie\nmuc_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to collect user behaviour and interaction data to optimize the website.\n\n\n  * Cookie\nguest_id_marketing\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\nguest_id_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\npersonalization_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to integrate and share features for social media and also store information about how the user uses the website, for tracking and targeting.\n\n\n  * Cookie\nguest_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor. It registers if a user is signed in to the Twitter platform and collects information about ad preferences.\n\n\n  * Cookie\nbcookie\n  * Duration\n1 year\n  * Description\nLinkedIn sets this cookie from LinkedIn share buttons and ad tags to recognize browser IDs.\n\n\n  * Cookie\n__Secure-ROLLOUT_TOKEN\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to manage feature rollout and experimentation. It helps Google control which new features or interface changes are shown to users as part of testing and staged rollouts, ensuring consistent experience for a given user during an experiment.\n\n\n  * Cookie\nyt.innertube::nextId\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nyt.innertube::requests\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nsession_tracker\n  * Duration\nsession\n  * Description\nThis cookie is set by the Reddit. This cookie is used to identify trusted web traffic. It also helps in adverstising on the website.\n\n\n  * Cookie\nedgebucket\n  * Duration\nsession\n  * Description\nReddit sets this cookie to save the information about a log-on Reddit user, for the purpose of advertisement recommendations and updating the content.\n\n\n  * Cookie\ndid\n  * Duration\n1 year\n  * Description\nArbor sets this cookie to show targeted ads to site visitors.This cookie expires after 2 months or 1 year.\n\n\nUncategorised\nOther uncategorised cookies are those that are being analysed and have not been classified into a category as yet.\nNo cookies to display.\nReject All Save My Preferences Accept All\nToggle Search[](https://towardsdatascience.com/)Toggle Mobile Navigation\n[](https://towardsdatascience.com/)\nPublish AI, ML & data-science insights to a global community of data professionals.\nSign in[Submit an Article](https://write.insightmediagroup.io)\n[Latest](https://towardsdatascience.com/latest/)[Editor's Picks](https://towardsdatascience.com/tag/editors-pick/)[Newsletter](https://towardsdatascience.com/tag/the-variable/)[ShipAI](https://towardsdatascience.com/shipai/)\n* * *\n[Write For TDS](https://towardsdatascience.com/submissions/)\n[LinkedIn](https://www.linkedin.com/company/towards-data-science/)[X](https://x.com/TDataScience)Toggle Search\n[Data Science](https://towardsdatascience.com/category/data-science/)\n# How to Query a Knowledge Graph with LLMs Using gRAG\nGoogle, Microsoft, LinkedIn, and many more tech companies are using Graph RAG. Why? Let's understand it by building one from scratch.\n[Cristian Leo](https://towardsdatascience.com/author/cristianleo120/)\nNovember 7, 202425 min read\n![Image illustrating a knowledge graph with interconnected nodes and edges against a tech-inspired gradient background - Image generated by the author using DALL-E](../images/000.jpg)\nImage illustrating a knowledge graph with interconnected nodes and edges against a tech-inspired gradient background - Image generated by the author using DALL-E\nYou may not realize it, but you've been interacting with Knowledge Graphs (KGs) more frequently than you might think. They're the technology behind many modern search engines, Retrieval-Augmented Generation (RAG) systems for Large Language Models (LLMs), and various query tools. But what exactly are Knowledge Graphs, and why are they so integral to these technologies? Let's delve into it.\nLearn this step by step with the interactive [AI and Data Scientist](https://roadmap.sh/ai-data-scientist) roadmap.\n## Introduction to Knowledge Graphs\nA Knowledge Graph (KG) is a structured representation of information that captures real-world entities and the relationships between them. Imagine a network where each point represents an entity - such as a product, person, or concept - and the lines connecting them represent the relationships they share. This interconnected web allows for a rich semantic understanding of data, where the focus isn't just on individual pieces of information but on how these pieces relate to one another.\n### Nodes\nAt the heart of a knowledge graph are nodes (entities). To illustrate this, let's consider building a knowledge graph using a publicly available Amazon dataset of toy products, this will be the dataset we will use later on in our practical application. What might we find in such a dataset?\nNaturally, we would have products. In our knowledge graph, each product in the dataset becomes a node. This product node includes all information about the item, such as its description, price, stock quantity, and ratings.\n![Product Node in Neo4j - Image by Author](../images/001.png)\nProduct Node in Neo4j - Image by Author\nBut products aren't the only entities we can represent. Knowledge graphs are flexible, allowing us to create as many types of nodes as needed. For example, since every product has a manufacturer, we can also create nodes for each manufacturer. A manufacturer node might include properties like the company's name, location, and contact information.\n### Edges\nHowever, nodes most of the time can be connected by each other. For example, a product node could be connected to a manufacturer node, since the manufacturer produces the product, and the product is produced by the manufacturer. This relationship is known as the edges in a knowledge graph.\nEdges are the links that define how two entities are related, and in a knowledge graph, these relationships are explicitly modeled and stored. This is a significant shift from traditional relational databases, where such relationships are often inferred at query time using JOIN operations.\nConsider the relationship between a product and its manufacturer. In a relational database, we would join tables to associate a product with its manufacturer during a query. In a knowledge graph, however, we directly specify this relationship by creating an edge between the product node and the manufacturer node.\n![2 Product Nodes connected to 1 Manufacturer nodes via edges - Image by Author](../images/002.png)\n2 Product Nodes connected to 1 Manufacturer nodes via edges - Image by Author\nTaking our toy dataset as an example, we know that every product is associated with a manufacturer. Therefore, we can create a \"manufactured_by\" edge that connects a product node to its corresponding manufacturer node, indicating who produces the product. For instance, the product \"DJI Phantom 2 with H3–3D Gimbal\" would be connected to the manufacturer \"DJI\" through this edge.\nEdges themselves can carry properties, providing additional context about the relationships they represent. Focusing on the \"manufactured_by\" relationship from product to manufacturer, we might include a property like \"since,\" which indicates the date when the manufacturing relationship was established.\n## The Math Behind Knowledge Graphs\nAll right, you should now have the basics of KGs, but let's go a step further and see how the math plays out. Moreover, in our example, we will query a knowledge graph using natural language, in order to achieve that, we need to introduce two more components: **embeddings** and **cosine similarity**.\nA knowledge graph **G** is a directed, labeled graph that can be formally represented as:\n![Knowledge Graph Formula - Image by Author](../images/003.png)\nKnowledge Graph Formula - Image by Author\nWhere:\n  * **V** is the set of **vertices** or **nodes**.\n  * **E⊆V×V** is the set of **edges** , representing relationships between nodes.\n  * **ϕV:V→A_V** ​ is a function mapping nodes to their attributes.\n  * **ϕE:E→A_E** ​ is a function mapping edges to their attributes.\n\n\nEach node **v ∈ V** represents an entity or concept and can be characterized by a set of attributes. Mathematically, a node can be defined as:\n![Node Formula - Image by Author](../images/004.png)\nNode Formula - Image by Author\nWhere:\n  * **id_v** is a unique identifier for the node.\n  * **A_v = { (k,a_k) ∣ k ∈ K_v }** is a set of attribute-value pairs, with **K_v** being the set of attribute keys for node **v**.\n\n\nFor example, a product might look like:\n![Product Node - Image by Author](../images/005.png)\nProduct Node - Image by Author\nNext, an edge **e ∈ E** represents a relationship between two nodes and is defined as:\n![Edge Formula - Image by Author](../images/006.png)\nEdge Formula - Image by Author\nWhere:\n  * **v_i, v_j ∈ V** are the source and target nodes.\n  * **r_ij** is the type of relationship (e.g., **MANUFACTURED_BY** , **BELONGS_TO**).\n  * **A_e = { (k, a_k) ∣ k ∈ K_e }** is a set of attribute-value pairs associated with the edge.\n\n\nFor example, an edge represents that a product is manufactured by a manufacturer:\n![Edge example - Image by Author](../images/007.png)\nEdge example - Image by Author\nWhere **A_e** ​ might include:\n![Edge properties - Image by Author](../images/008.png)\nEdge properties - Image by Author\n### Embeddings and Vector Representations\nAs of now, our Knowledge Graph has both structured (numerical) and unstructured (text) data. However, math just doesn't work very well with text. To enable semantic search and natural language querying, we need to convert textual data into numerical vector representations. Here's where **embeddings** do their magic**.** We convert the text data in our KG into embedding, and the same for our natural language query.\n> An **embedding** is a mapping of discrete objects, such as words, phrases, or documents, into a continuous vector space. In this space, semantically similar items are located close to each other. This is achieved by training machine learning models on large corpora of text data to learn patterns and contexts in which words or phrases appear.\nAn embedding function **ϕ** maps textual descriptions to points in a high-dimensional vector space **R^n** :\n![Embedding Function - Image by Author](../images/009.png)\nEmbedding Function - Image by Author\nWhere:\n  * **T** is the set of all possible textual data (e.g., product descriptions, queries).\n  * **n** is the dimensionality of the embedding space, which can range from hundreds to thousands of dimensions depending on the model\n\n\nEmbeddings are essential elements in our architecture because they allow us to perform mathematical operations on textual data. By representing text as vectors, we can compute distances and similarities between different pieces of text. This capability is essential for tasks like semantic search, where we want to find products that are semantically similar to a user's query, even if the exact words used are different. Moreover, you will find that different embedding models can bring very different results. In your AI application consider exploring several models and see which one delivers the best results. Some of the big names in the space are OpenAI, Gemini, and Voyage.\n### Cosine Similarity\nNow that we have both our query and our nodes embedded, we need to find a way to compute the similarity between the query and the nodes. One of the most popular approaches is **cosine similarity**.\nCosine similarity measures the cosine of the angle between two non-zero vectors in an inner product space. It provides a normalized measure of similarity that is independent of the magnitude of the vectors.\nGiven two vectors **A,B ∈ R^n** the cosine similarity **cos⁡θ** is defined as:\n![Cosine Similarity - Image by Author](../images/013.png)\nCosine Similarity - Image by Author\nThe value of cosine similarity ranges from -1 to 1, where 1 indicates that the vectors are identical in direction (maximum similarity), 0 indicates orthogonality (no similarity), and -1 indicates diametrically opposed vectors (opposite meanings). In the context of embeddings generated from textual data, cosine similarity values typically range from 0 to 1 because the components of the embeddings are usually non-negative.\nTo compute the cosine similarity between the query embedding and product embeddings, we follow these steps:\n**Compute the Dot Product** :\n![Compute Dot Product - Image by Author](../images/011.png)\nCompute Dot Product - Image by Author\n**Compute the Norms** :\n![Compute Norms - Image by Author](../images/012.png)\nCompute Norms - Image by Author\n**Compute Cosine Similarity** :\n![Compute Cosine Similarity - Image by Author](../images/013.png)\nCompute Cosine Similarity - Image by Author\nSuppose we have two embeddings:\n![Embedding Vectors A, and B - Image by Author](../images/014.png)\nEmbedding Vectors A, and B - Image by Author\nCompute the dot product:\n![Compute dot product of A and B - Image by Author](../images/015.png)\nCompute dot product of A and B - Image by Author\nCompute the norms:\n![Compute norms of A and B - Image by Author](../images/016.png)\nCompute norms of A and B - Image by Author\nCompute cosine similarity:\n![Compute cosine similarity - Image by Author](../images/017.png)\nCompute cosine similarity - Image by Author\n### Mathematical Modeling of Edge Traversal\nIn advanced applications of knowledge graphs, we often want to consider not just the semantic similarity between entities (captured by embeddings) but also the structure of the graph itself. This involves modeling how we traverse the edges - go around the node with the highest similarity - of the graph to understand the relationships and importance of different nodes within the network.\n**Adjacency Matrix Representation** To mathematically represent the structure of a knowledge graph, we use an **adjacency matrix**. Suppose our graph **G** has **n** nodes. The adjacency matrix **A** is an **n×n** matrix where each element **A_ij** ​ indicates whether there is a direct connection (edge) from node **v_i** ​ to node **v_j** :\n![Adjacency Matrix - Image by Author](../images/018.png)\nAdjacency Matrix - Image by Author\nConsider a simple graph with three products:\n  * **Product A** (v1​)\n  * **Product B** (v2​)\n  * **Product C** (v3​)\n\n\nSuppose:\n  * Product A is related to Product B.\n  * Product B is related to Product C.\n\n\nThe adjacency matrix **A** would be:\n![Adjacency matrix A - Image by author](../images/019.png)\nAdjacency matrix A - Image by author\nThis matrix helps us compute paths and determine how nodes are connected within the graph.\n**Random Walks and Transition Probabilities** Next, to analyze how likely we are to move from one node to another, we use the concept of **random walks**. In a random walk, starting from a node, we randomly select an outgoing edge to follow to the next node.\nWe define the **transition probability matrix** **P** to represent the probabilities of moving from one node to another:\n![Transition Probability Matrix Formula - Image by Author](../images/020.png)\nTransition Probability Matrix Formula - Image by Author\nWhere:\n  * **A_ij** ​ is the adjacency between nodes **v_i** ​ and **v_j** ​.\n  * **∑A_ik** ​ is the total number of outgoing edges from node **v_i** ​.\n\n\nThis formula normalizes the adjacency matrix so that each row sums to 1, turning it into a probability distribution.\nUsing the previous adjacency matrix **A** :\n![Adjacency Matrix A - Image by Author](../images/019.png)\nAdjacency Matrix A - Image by Author\nCalculate **P** :\n  * For node **v1** ​: Sum of outgoing edges =**A12 + A13 = 1 + 0 =1**\n\n\n![Node v1 Calculation - Image by Author](../images/body-bdab9d9e357bc091b697.png)\nNode v1 Calculation - Image by Author\n  * For node **v2** ​: Sum of outgoing edges = **A23 =1**\n\n\n![Node v2 Calculation - Image by Author](../images/body-251fd35c222bdb045d38.png)\nNode v2 Calculation - Image by Author\n  * For node **v3** ​: No outgoing edges, so the probabilities are zero.\n\n\nTransition probability matrix P:\n![Transition Probability Matrix - Image by Author](../images/body-226e9a58017113fba4ff.png)\nTransition Probability Matrix - Image by Author\n**Personalized PageRank Algorithm** Finally, the **Personalized PageRank** algorithm allows us to compute a relevance score for each node in the graph, considering both the structure of the graph and a personalization (preference) vector.\nThe PageRank vector π is calculated using the iterative formula:\n![Personalized PageRank Formula - Image by Author](../images/body-2b0157e8c1bf20bb89c5.png)\nPersonalized PageRank Formula - Image by Author\nWhere:\n  * **α** is the **teleportation probability** , usually set to 0.15. It represents the probability of jumping back to a personalized starting point rather than following an outgoing edge.\n  * **π0** ​ is the **personalization vector** , indicating our starting preference or importance of nodes.\n  * **P^T** is the **transpose** of the transition probability matrix **P**.\n  * **π** is the PageRank vector containing the relevance scores of the nodes.\n\n\n**Teleportation** ensures that the random walker has a chance to jump to any node based on our preferences, preventing them from getting stuck in sink nodes (nodes with no outgoing edges).\n**Integrating Embeddings with Graph Structure** To effectively recommend products or retrieve information, we need to combine semantic similarity (from embeddings) - measures how closely the product's description matches the user's query in terms of meaning- with graph relevance (from PageRank) - reflects the product's importance within the graph's structure, considering relationships and connectivity. We define a **composite score** **S(p)** for each product **p** :\n![Composite Score Formula - Image by Author](../images/body-f97574cba5e7af4239b2.png)\nComposite Score Formula - Image by Author\nWhere:\n  * **λ** is a weighting parameter between 0 and 1 that balances the importance of semantic similarity and graph relevance. If λ=1, the ranking relies entirely on semantic similarity. If λ=0, it depends solely on graph relevance. A value like λ=0.5 gives equal weight to both.\n\n\nSuppose, we have a user query where the embedding is already computed. The product embeddings and their cosine similarities with the query are:\n  * **Product A** : cos⁡θ =0.8\n  * **Product B** : cos⁡θ=0.6\n  * PageRank score πA=0.3\n  * PageRank score πB=0.7\n\n\nWith λ=0.5:\n![product A and B composite scores - Image by Author](../images/body-8af280df3cbbcb32fd63.png)\nproduct A and B composite scores - Image by Author\nEven though Product A has a higher semantic similarity, Product B has a higher overall score due to its greater graph relevance. Therefore, Product B would be ranked higher in the recommendations.\n## Benefits of Knowledge Graphs\n**Semantic Understanding** One of the most significant benefits of knowledge graphs is their ability to capture complex relationships and context between different entities. This capability makes LLMs able to reason about data in a way that's more aligned with human thinking. Instead of treating data points as isolated pieces of information, knowledge graphs interconnect entities through explicit relationships, providing a rich semantic understanding of the data.\nFor example, imagine an air conditioner knowledge graph. A single **Product** node isn't just a standalone item; it's connected to its **Manufacturer** , **Features** , **Category** , and **SubCategory**. This interconnectedness allows the system to comprehend that a particular air conditioner is linked to other entities like its brand, features it offers - such as \"Energy Efficient\" or \"Remote Control\" - and the category it belongs to, like \"Portable Air Conditioners.\" This depth of semantic richness enables more accurate and context-aware responses to user queries, significantly enhancing user experience.\n**Flexibility** Another advantage of knowledge graphs is their inherent flexibility. They allow for the easy addition of new nodes or relationships without necessitating significant alterations to the existing schema. This feature is particularly beneficial in dynamic environments where data is continually evolving.\nFor instance, suppose we decide to incorporate **Customer Reviews** into our knowledge graph. We can simply add new **Review** nodes and establish relationships like **REVIEWED_BY** connecting **Product** nodes to **Customer** nodes. There's no need to redesign the entire data model or migrate existing data. This adaptability makes knowledge graphs highly suitable for applications where requirements and data structures are constantly changing.\n**Efficient Querying** Knowledge graphs are optimized for querying relationships between entities, making data retrieval more efficient - especially for complex queries involving multiple interconnected entities. This efficiency becomes evident when dealing with intricate queries that would be cumbersome in traditional databases.\nConsider a scenario where we want to find all air conditioners manufactured by \"CoolTech Industries\" that have the feature \"Energy Efficient.\" In a traditional relational database, executing this query would require complex JOIN operations across multiple tables, which can be time-consuming and resource-intensive.\nIn contrast, a knowledge graph simplifies this process significantly:\n  1. **Start at the Manufacturer Node:** Begin by locating the **Manufacturer** node where `name = \"CoolTech Industries\"`.\n  2. **Traverse Relationships:** Move along the **MANUFACTURED_BY** edges to find all connected **Product** nodes.\n  3. **Filter by Features:** For these products, traverse the **HAS_FEATURE** edges to identify those connected to a **Feature** node where `name = \"Energy Efficient\"`.\n\n\nThis direct traversal of relationships eliminates the need for costly JOIN operations, resulting in faster and more efficient querying. The ability to navigate through interconnected data seamlessly not only improves performance but also enhances the capability to derive insights from complex data relationships.\n## Practical Application: Building and Querying a Knowledge Graph with Embeddings\nIn this section, we will create a knowledge graph using a public [dataset of Amazon toy products](https://www.kaggle.com/datasets/PromptCloudHQ/toy-products-on-amazon) ([License](https://creativecommons.org/licenses/by-sa/4.0/)). We will add embeddings to enable semantic search and query the database using natural language. By the end of this section, you will understand how to build a knowledge graph, add embeddings, and perform semantic searches to find products that match natural language queries.\n### Setting Up the Environment\nBefore we begin, ensure you have the necessary tools installed and configured:\n**Clone the Repository** The dataset and code are available in the GitHub repository [rag-knowledge-graph](https://github.com/cristianleoo/rag-knowledge-graph). Clone this repository to your local machine:\nbash\n\n```\ngit clone https://github.com/cristianleoo/rag-knowledge-graph.git\n```\n\nIf you don't like using the terminal, or you don't have git installed, follow this link and download the repo:\n> [**GitHub - cristianleoo/rag-knowledge-graph**](https://github.com/cristianleoo/rag-knowledge-graph)\n**Install Neo4j**[Download](https://neo4j.com/download/?utm_source=GSearch&utm_medium=PaidSearch&utm_campaign=Evergreen&utm_content=AMS-Search-SEMCE-DSA-None-SEM-SEM-NonABM&utm_term=&utm_adgroup=DSA&gad_source=1&gclid=Cj0KCQiAoae5BhCNARIsADVLzZfw0vpWmdrWBAPirQbj7S9bdao_udYYZMGo7L9Ej1ZvTib8s4PXxlkaAmimEALw_wcB) and install Neo4j from the official website. Follow the installation instructions specific to your operating system.\n**Start the Neo4j Server** Once installed, start the Neo4j server. You can do this via the Neo4j Desktop application or by running the server from the command line:\nbash\n\n```\nneo4j start\n```\n\nWrite down the password you will use for the database as we will need it in a later step\n**Install Required Python Libraries** Navigate to the cloned repository directory and set up a virtual environment. Install the required libraries using `pip`:\nbash\n\n```\ncd rag-knowledge-graph\npython -m venv venv\nsource venv/bin/activate  # On Windows, use venvScriptsactivate\npip install -r requirements.txt\n```\n\n### Step 1: Load and Preprocess the Dataset\nWe begin by importing the necessary libraries and loading the dataset.\npython\n\n```\nimport pandas as pd\npd.set_option('display.max_columns', None)\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport networkx as nx\nfrom py2neo import Graph, Node, Relationship\nimport google.generativeai as genai\nimport time\nfrom tqdm import tqdm\nfrom ratelimit import limits, sleep_and_retry\nimport os\n```\n\n**Loading the Dataset**\npython\n\n```\ndf = pd.read_csv('dataset/products.csv')\ndf.head()\n```\n\nHere, we use `pandas` to read the CSV file containing Amazon toy products, and display the first few rows:\ntext\n\n```\nuniq_id                                   product_name  ... sellers\n0  eac7efa5dbd3d667f26eb3d3ab504464                      Hornby 2014 Catalogue  ... {\"seller\"=>[{\"Seller_name_1\"=>\"Amazon.co.uk\", ...\n1  b17540ef7e86e461d37f3ae58b7b72ac  FunkyBuys® Large Christmas Holiday Express...  ... {\"seller\"=>{\"Seller_name_1\"=>\"UHD WHOLESALE\", ...\n2  348f344247b0c1a935b1223072ef9d8a   CLASSIC TOY TRAIN SET TRACK CARRIAGES LIGHT...  ... {\"seller\"=>[{\"Seller_name_1\"=>\"DEAL-BOX\", \"Sel...\n3  e12b92dbb8eaee78b22965d2a9bbbd9f        HORNBY Coach R4410A BR Hawksworth Corridor  ... NaN\n4  e33a9adeed5f36840ccc227db4682a36  Hornby 00 Gauge 0-4-0 Gildenlow Salt Co. Steam...  ... NaN\n\n[5 rows x 18 columns]\n```\n\n**Data Overview** We examine the dataset to understand the structure and identify missing values.\npython\n\n```\nfor col in df.columns:\n    print(f\"{col:<50} | {df[col].isna().sum() / len(df):>6.2%} missing | {df[col].nunique():>6} unique values | {df[col].dtype}\")\n```\n\nThis will print out:\nbash\n\n```\nuniq_id                                           |  0.00% missing | 10000 unique values | object\nproduct_name                                      |  0.00% missing | 9964 unique values | object\nmanufacturer                                      |  0.07% missing | 2651 unique values | object\nprice                                             | 14.35% missing | 2625 unique values | object\nnumber_available_in_stock                         | 25.00% missing |   89 unique values | object\nnumber_of_reviews                                 |  0.18% missing |  194 unique values | object\nnumber_of_answered_questions                      |  7.65% missing |   19 unique values | float64\naverage_review_rating                             |  0.18% missing |   19 unique values | object\namazon_category_and_sub_category                  |  6.90% missing |  255 unique values | object\ncustomers_who_bought_this_item_also_bought        | 10.62% missing | 8755 unique values | object\ndescription                                       |  6.51% missing | 8514 unique values | object\nproduct_information                               |  0.58% missing | 9939 unique values | object\nproduct_description                               |  6.51% missing | 8514 unique values | object\nitems_customers_buy_after_viewing_this_item       | 30.65% missing | 6749 unique values | object\ncustomer_questions_and_answers                    | 90.86% missing |  910 unique values | object\ncustomer_reviews                                  |  0.21% missing | 9901 unique values | object\nsellers                                           | 30.82% missing | 6581 unique values | object\n```\n\nFrom here, we can see the percentage of missing values, the number of unique values, and the data type for each column. This helps us identify which columns are essential and how to handle missing data.\n**Data Cleaning and Preprocessing** We clean the data by extracting useful information and handling missing values.\npython\n\n```\n# Extract currency symbol and price into separate columns\ndf['currency'] = df['price'].str.extract(r'([^0-9]+)')\ndf['price_value'] = df['price'].str.extract(r'(d+.?d*)').astype(float)\ndf['stock_type'] = df['number_available_in_stock'].str.extract(r'([^0-9]+)')\ndf['stock_availability'] = df['number_available_in_stock'].str.extract(r'(d+.?d*)')\n\n# Clean up average review rating\ndf['average_review_rating'] = df['average_review_rating'].str.replace(' out of 5 stars', '').astype(float)\n# Clean up number of reviews\ndf['number_of_reviews'] = df['number_of_reviews'].str.replace(',', '').fillna(0).astype(int)\n```\n\nIn particular, we extract numerical values from strings in the `price` and `number_available_in_stock` columns, so that we can treat those columns as int and float.\nThen, we clean the `average_review_rating` and `number_of_reviews` columns to ensure they are numeric.\nWe drop unnecessary columns and handle missing data.\nsql\n\n```\n# Drop irrelevant columns\ndf = df.drop(['price', 'number_available_in_stock', 'customers_who_bought_this_item_also_bought',\n              'items_customers_buy_after_viewing_this_item', 'customer_questions_and_answers', 'sellers'], axis=1)\n\n# Drop rows with essential missing data\ndf.dropna(subset=['product_information', 'price_value', 'description', 'amazon_category_and_sub_category'], how='any', inplace=True)\n```\n\nIn this example, we drop some features to keep it simple. However, in production, you may want to keep as many relevant features as possible, as they could provide useful insights for analysis and for the model.\nWe check the data again to ensure it's clean.\nbash\n\n```\n# Fill missing values with defaults\ndf['amazon_category_and_sub_category'] = df['amazon_category_and_sub_category'].fillna('')\ndf['manufacturer'] = df['manufacturer'].fillna('Unknown')\ndf['number_of_answered_questions'] = df['number_of_answered_questions'].fillna(0.0)\ndf['average_review_rating'] = df['average_review_rating'].fillna(0.0)\ndf['description'] = df['description'].fillna('')\ndf['product_description'] = df['product_description'].fillna('')\ndf['product_information'] = df['product_information'].fillna('')\ndf['customer_reviews'] = df['customer_reviews'].fillna('')\ndf['stock_availability'] = df['stock_availability'].astype(float).fillna(0.0)\ndf['stock_type'] = df['stock_type'].fillna('Out of stock')\n```\n\nNext, we fill missing values in categorical columns with default strings, and numerical columns with values zeros so we don't bring null values into our KG.\npython\n\n```\n# Function to combine product title and description\ndef complete_product_description(row):\n    return f\"Product Title: {row['product_name']}nProduct Description: {row['product_description']}\"\n\n# Apply the function to create a new column\ndf['description_complete'] = df.apply(complete_product_description, axis=1)\n\n# Display the first few rows\ndf.head()\n```\n\nFinally, we define a function called `complete_product_description` that takes a row from the dataframe and combines the `product_name` and `product_description` into a single string. We then apply this function to each row in the dataframe `df` to create a new column called `description_complete`. This new column contains the complete description of each product, which we will use later for generating embeddings.\nLet's call `df.head()`, and display the first few rows of the dataframe to verify that the new column has been added correctly.\nbash\n\n```\nuniq_id                                   product_name  ...                                 description_complete\n0  eac7efa5dbd3d667f26eb3d3ab504464                      Hornby 2014 Catalogue  ...  Product Title: Hornby 2014 CataloguenProduct Description: Hornby 2014 Catalogue Box Contains 1 x Hornby 2014 Catalogue\n1  b17540ef7e86e461d37f3ae58b7b72ac  FunkyBuys® Large Christmas Holiday Express...  ...  Product Title: FunkyBuys® Large Christmas Holiday Express Festival Deluxe Railway Train SetnProduct Description: Size Name:Large FunkyBuys® Large Christmas Holiday Express Festival Deluxe Railway Train Set Light Up with Realistic Sounds Xmas Tree Decoration For Kids Gift\n2  348f344247b0c1a935b1223072ef9d8a   CLASSIC TOY TRAIN SET TRACK CARRIAGES LIGHT...  ...  Product Title: CLASSIC TOY TRAIN SET TRACK CARRIAGES LIGHT ENGINE SOUNDS KIDS XMAS GIFTnProduct Description: BIG CLASSIC TOY TRAIN SET TRACK CARRIAGE LIGHT ENGINE SOUNDS KIDS XMAS GIFT This is a classic train set with a steam engine that features working headlights and realistic engine sounds. The tracks can be assembled in various configurations. Great gift for kids.\n3  e12b92dbb8eaee78b22965d2a9bbbd9f        HORNBY Coach R4410A BR Hawksworth Corridor  ...  Product Title: HORNBY Coach R4410A BR Hawksworth Corridor 3rdnProduct Description: Hornby 00 Gauge BR Hawksworth 3rd Class W 2107 Corridor Coach R4410A\n4  e33a9adeed5f36840ccc227db4682a36  Hornby 00 Gauge 0-4-0 Gildenlow Salt Co. Steam...  ...  Product Title: Hornby 00 Gauge 0-4-0 Gildenlow Salt Co. Steam Locomotive R9671nProduct Description: Hornby RailRoad 0-4-0 Gildenlow Salt Co. Steam Locomotive R9671\n\n[5 rows x 13 columns]\n```\n\n### Step 2: Connect to Neo4j and Prepare the Database\nWe connect to our Neo4j graph database where we'll store the knowledge graph.\npython\n\n```\n# Connect to Neo4j (adjust credentials as needed)\ngraph = Graph(\"bolt://localhost:7687\", auth=(\"neo4j\", \"YOUR_PASSWORD\")) # replace this with your password\n\n# Clear existing data (optional)\ngraph.run(\"MATCH (n) DETACH DELETE n\")\n```\n\nUsing the `py2neo` library, we establish a connection to the Neo4j database. Replace `\"YOUR_PASSWORD\"` with your actual Neo4j password. The command `graph.run(\"MATCH (n) DETACH DELETE n\")` deletes all existing nodes and relationships in the database, providing a clean slate for our new knowledge graph. This step is optional but recommended to avoid conflicts with existing data.\n### Step 3: Create the Knowledge Graph\nWe create nodes for products, manufacturers, and categories, and establish relationships between them.\npython\n\n```\ndef create_knowledge_graph(df):\n    # Create unique constraints\n    try:\n        # For Neo4j 5.x and later\n        graph.run(\"CREATE CONSTRAINT product_id IF NOT EXISTS FOR (p:Product) REQUIRE p.uniq_id IS UNIQUE\")\n        graph.run(\"CREATE CONSTRAINT manufacturer_name IF NOT EXISTS FOR (m:Manufacturer) REQUIRE m.name IS UNIQUE\")\n        graph.run(\"CREATE CONSTRAINT category_name IF NOT EXISTS FOR (c:Category) REQUIRE c.name IS UNIQUE\")\n    except Exception as e:\n        # For Neo4j 4.x\n        try:\n            graph.run(\"CREATE CONSTRAINT ON (p:Product) ASSERT p.uniq_id IS UNIQUE\")\n            graph.run(\"CREATE CONSTRAINT ON (m:Manufacturer) ASSERT m.name IS UNIQUE\")\n            graph.run(\"CREATE CONSTRAINT ON (c:Category) ASSERT c.name IS UNIQUE\")\n        except Exception as e:\n            print(f\"Warning: Could not create constraints: {e}\")\n\n    for _, row in df.iterrows():\n        # Create Product node\n        product = Node(\n            \"Product\",\n            uniq_id=row['uniq_id'],\n            name=row['product_name'],\n            description=row['product_description'],\n            price=float(row['price_value']),\n            currency=row['currency'],\n            review_rating=float(row['average_review_rating']),\n            review_count=int(row['number_of_reviews']),\n            stock_type=row['stock_type'] if pd.notna(row['stock_type']) else None,\n            description_complete=row['description_complete']\n        )\n\n        # Create Manufacturer node\n        manufacturer = Node(\"Manufacturer\", name=row['manufacturer'])\n\n        # Create Category nodes from hierarchy\n        categories = row['amazon_category_and_sub_category'].split(' > ')\n        previous_category = None\n        for cat in categories:\n            category = Node(\"Category\", name=cat.strip())\n            graph.merge(category, \"Category\", \"name\")\n            if previous_category:\n                # Create hierarchical relationship between categories\n                rel = Relationship(previous_category, \"HAS_SUBCATEGORY\", category)\n                graph.merge(rel)\n            previous_category = category\n        # Merge nodes and create relationships\n        graph.merge(product, \"Product\", \"uniq_id\")\n        graph.merge(manufacturer, \"Manufacturer\", \"name\")\n        # Connect product to manufacturer\n        graph.merge(Relationship(product, \"MANUFACTURED_BY\", manufacturer))\n        # Connect product to lowest-level category\n        graph.merge(Relationship(product, \"BELONGS_TO\", previous_category))\n# Create the knowledge graph\ncreate_knowledge_graph(df)\n```\n\nThis function`create_knowledge_graph` iterates over each row in the dataframe `df`. For each product:\n  * We create a `Product` node with properties such as `uniq_id`, `name`, `description`, `price`, `currency`, `review_rating`, `review_count`, `stock_type`, and `description_complete`.\n  * We create a `Manufacturer` node based on the `manufacturer` field.\n  * We process the `amazon_category_and_sub_category` field to create a hierarchy of `Category` nodes. We split the categories by the `>` delimiter and created nodes for each category level.\n  * We establish a `HAS_SUBCATEGORY` relationship between categories to represent the hierarchy.\n  * We create relationships between the product and its manufacturer (`MANUFACTURED_BY`) and between the product and the most specific category (`BELONGS_TO`).\n  * We use `graph.merge` to ensure that nodes and relationships are created only if they do not already exist, preventing duplicates in the graph.\n\n\n### Step 4: Query the Knowledge Graph and Visualize Results\nNow, let's run a sample query to retrieve data from the graph and visualize it.\npython\n\n```\ndef run_query_with_viz(query, title, viz_query=None):\n    print(f\"n=== {title} ===\")\n    # Run and display query results as a DataFrame\n    results = graph.run(query).data()\n    df = pd.DataFrame(results)\n    display(df)\n\n    # Create visualization\n    plt.figure(figsize=(12, 8))\n    G = nx.Graph()\n    # Add nodes and edges\n    for record in results:\n        product_name = record['Product']\n        manufacturer_name = record['Manufacturer']\n        G.add_node(product_name, label=product_name[:30], type='Product')\n        G.add_node(manufacturer_name, label=manufacturer_name, type='Manufacturer')\n        G.add_edge(product_name, manufacturer_name)\n    # Draw graph\n    pos = nx.spring_layout(G)\n    nx.draw_networkx_nodes(G, pos, nodelist=[n for n, attr in G.nodes(data=True) if attr['type'] == 'Product'],\n                           node_color='lightblue', node_size=500, label='Products')\n    nx.draw_networkx_nodes(G, pos, nodelist=[n for n, attr in G.nodes(data=True) if attr['type'] == 'Manufacturer'],\n                           node_color='lightgreen', node_size=700, label='Manufacturers')\n    nx.draw_networkx_edges(G, pos)\n    nx.draw_networkx_labels(G, pos)\n    plt.title(title)\n    plt.legend()\n    plt.axis('off')\n    plt.show()\n\n# Find most expensive products\nquery1 = \"\"\"\nMATCH (p:Product)-[:MANUFACTURED_BY]->(m:Manufacturer)\nRETURN m.name as Manufacturer, p.name as Product, p.price as Price\nORDER BY p.price DESC\nLIMIT 5\n\"\"\"\nrun_query_with_viz(query1, \"Most Expensive Products\")\n```\n\nYou may notice that we are creating another function `run_query_with_viz` . We aren't doing this just for the sake of creating functions, but because this will let us both run a query against the database and create a helper function to plot the results. However, you can also run the query in Neo4j and get an even better visualization there.\nIn the function we run a Cypher query and displays the results in a pandas DataFrame. We handle the visualization side using NetworkX and Matplotlib, showing the relationships between products and manufacturers.\nThen, we call the function passing a query to retrieve the top 5 most expensive products along with their manufacturers and prices. This will return:\ntext\n\n```\n=== Most Expensive Products ===\nManufacturer Product Price\n0 DJI DJI Phantom 2 with H3-3D Gimbal 995.11\n1 Sideshow Indiana Jones - 12 Inch Action Figures: Indian... 719.95\n2 AUTOart Autoart 70206 - Aston Martin V12 Vantage - 201... 648.95\n3 Bushiroad Weiss Schwarz Extra Booster Clannad Vol.3 629.95\n4 Dragon Panzer II - Kpfw - Ausf.C - DX'10 - 1:6th Scale 592.95\nNumber of visualization records: 10\n```\n\n![Most expensive product visualization - Image by Author](../images/body-5209abd1c8c1bd546b0b.png)\nMost expensive product visualization - Image by Author\nIn this case, the output DataFrame displays the manufacturers, products, and prices of the top 5 most expensive products. The visualization is a graph where product nodes are connected to manufacturer nodes, with different colors representing different types of nodes.\n### Step 5: Generate and Store Embeddings\nWe generate embeddings for the product descriptions to enable semantic search. For this part you will need to get an API Key for Google AI Studio. Don't worry it's completely free, and it will take you just a couple of minutes to get one:\n> [**Get a Gemini API key | Google AI for Developers**](https://ai.google.dev/gemini-api/docs/api-key)\npython\n\n```\n# Configure the embedding API (replace with your actual API key)\nos.environ[\"GOOGLE_API_KEY\"] = \"your_api_key_here\"\ngenai.configure(api_key=os.getenv(\"GOOGLE_API_KEY\"))\n\n# Test the embedding API\nresult = genai.embed_content(\n    model=\"models/text-embedding-004\",\n    content=\"What is the meaning of life?\",\n    task_type=\"retrieval_document\",\n    title=\"Embedding of single string\"\n)\n# Print a portion of the embedding vector\nprint(str(result['embedding'])[:50], '... TRIMMED]')\n```\n\nWe set up the embedding API by configuring the `genai` library with your API key (replace `\"your_api_key_here\"` with your actual key). We test the embedding API by generating an embedding for a sample text and printing a portion of the embedding vector to verify that it works.\nbash\n\n```\n[-0.02854543, 0.044588115, -0.034197364, -0.004266 ... TRIMMED]\n```\n\nThis output shows the beginning of the embedding vector for the sample text. The actual length of the vector is 768, as this is the dimensionality of _text-embedding-004._ However, note that different embedding algorithms may have different numbers of dimensions. As a result, they may provide different results.\nNext, we define functions to generate embeddings for product descriptions and store them in the knowledge graph.\npython\n\n```\n# Rate limiter decorator\n@sleep_and_retry\n@limits(calls=1500, period=60)\ndef get_embedding(text):\n    try:\n        result = genai.embed_content(\n            model=\"models/text-embedding-004\",\n            content=text,\n            task_type=\"retrieval_document\",\n        )\n        return result['embedding']\n    except Exception as e:\n        print(f\"Error getting embedding: {e}\")\n        return None\n\ndef add_embeddings_to_products(batch_size=50):\n    # Get the total number of products to process\n    total_query = \"\"\"\n    MATCH (p:Product)\n    WHERE p.description_embedding IS NULL\n      AND p.description IS NOT NULL\n    RETURN count(p) AS total\n    \"\"\"\n    total_result = graph.run(total_query).data()\n    total_to_process = total_result[0]['total'] if total_result else 0\n    print(f\"Total products to process: {total_to_process}n\")\n    total_processed = 0\n    # Initialize tqdm progress bar\n    with tqdm(total=total_to_process, desc='Processing products', unit='product') as pbar:\n        while True:\n            # Get batch of products\n            query = \"\"\"\n            MATCH (p:Product)\n            WHERE p.description_embedding IS NULL\n              AND p.description IS NOT NULL\n            RETURN p.uniq_id AS id, p.description AS description\n            LIMIT $batch_size\n            \"\"\"\n            products = graph.run(query, parameters={'batch_size': batch_size}).data()\n            if not products:\n                break\n            # Process each product in the batch\n            for product in products:\n                try:\n                    if product['description']:\n                        embedding = get_embedding(product['description'])\n                        if embedding:\n                            # Update product with embedding\n                            graph.run(\"\"\"\n                            MATCH (p:Product {uniq_id: $id})\n                            SET p.description_embedding = $embedding\n                            \"\"\", parameters={\n                                'id': product['id'],\n                                'embedding': embedding\n                            })\n                    total_processed += 1\n                    pbar.update(1)  # Update the progress bar\n                except Exception as e:\n                    print(f\"Error processing product {product['id']}: {e}\")\n            # Add a small delay between batches\n            time.sleep(1)\n    print(f\"nTotal products processed: {total_processed}\")\n    return total_processed\n# Add embeddings to products\nprint(\"Adding embeddings to products...n\")\ntotal_processed = add_embeddings_to_products()\nprint(f\"nProcess completed. Total products processed: {total_processed}\")\n```\n\n`get_embedding` retrieves the embedding for a given text while respecting API rate limits using the `ratelimit` library.\nThe `add_embeddings_to_products` function:\n  * Retrieves the total number of products that need embeddings.\n  * Processes products in batches, generating embeddings for their descriptions.\n  * Updates the `Product` nodes in the graph with the new embeddings.\n  * Uses a progress bar to display the processing status.\n  * Adds a small delay between batches to comply with API rate limits.\n\n\nbash\n\n```\nAdding embeddings to products...\n\nTotal products to process: 7434\nProcessing products:   0%|          | 0/7434 [00:00<?, ?product/s]\nProcessing products: 100%|██████████| 7434/7434 [27:20<00:00, 4.53product/s]\nTotal products processed: 7434\nProcess completed. Total products processed: 7434\n```\n\nThe output shows that all products have been processed and embeddings have been added.\n### Step 6: Verify Embeddings\nWe check how many products now have embeddings.\npython\n\n```\n# Verify embeddings\nprint(\"nVerifying embeddings:\")\nresult = graph.run(\"\"\"\nMATCH (p:Product)\nWHERE p.description_embedding IS NOT NULL\nRETURN count(p) as count\n\"\"\").data()\nprint(f\"Products with embeddings: {result[0]['count']}\")\n```\n\nThis Cypher query counts the number of `Product` nodes where `description_embedding` is not null. We print the count to verify that embeddings have been successfully added to the products.\nbash\n\n```\nVerifying embeddings:\nProducts with embeddings: 7434\n```\n\nThis confirms that all products now have embeddings.\n### Step 7: Perform Semantic Search\nWe use the embeddings to perform a semantic search based on a user's natural language query.\npython\n\n```\ndef semantic_search(query_text, n=5):\n    # Get query embedding\n    query_embedding = get_embedding(query_text)\n    if not query_embedding:\n        print(\"Failed to get query embedding\")\n        return []\n\n    # Search for similar products using dot product and magnitude for cosine similarity\n    results = graph.run(\"\"\"\n    MATCH (p:Product)\n    WHERE p.description_embedding IS NOT NULL\n    WITH p,\n    reduce(dot = 0.0, i in range(0, size(p.description_embedding)-1) |\n    dot + p.description_embedding[i] * $embedding[i]) /\n    (sqrt(reduce(a = 0.0, i in range(0, size(p.description_embedding)-1) |\n    a + p.description_embedding[i] * p.description_embedding[i])) *\n    sqrt(reduce(b = 0.0, i in range(0, size($embedding)-1) |\n    b + $embedding[i] * $embedding[i])))\n    AS similarity\n    WHERE similarity > 0\n    RETURN\n    p.name as name,\n    p.description as description,\n    p.price as price,\n    similarity as score\n    ORDER BY similarity DESC\n    LIMIT $n\n    \"\"\", parameters={'embedding': query_embedding, 'n': n}).data()\n    return results\n\n# Test the search with debug info\nprint(\"nTesting semantic search:\")\nresults = semantic_search(\"Give me a set of cards\", n=2)\nprint(f\"nNumber of results: {len(results)}\")\nfor r in results:\n    print(f\"nProduct: {r.get('name', 'No name')}\")\n    print(f\"Price: ${r.get('price', 'N/A')}\")\n    print(f\"Score: {r.get('score', 'N/A'):.3f}\")\n    desc = r.get('description', 'No description')\n    print(f\"Description: {desc}\")\n```\n\nThe function `semantic_search` generates an embedding for the user's query. It uses a Cypher query to compute the cosine similarity between the query embedding and each product's description embedding. Since Neo4j's standard library may not have a built-in cosine similarity function, we calculate it manually using the dot product and magnitudes. Then, it filters products with a positive similarity score and returns the top `n` results sorted by similarity.\nWe test the function with the query \"Give me a set of cards\" and print out the results, including the product name, price, similarity score, and description.\nbash\n\n```\nTesting semantic search:\n\nNumber of results: 2\nProduct: Yu-Gi-Oh Metal Raiders Booster\nPrice: $9.76\nScore: 0.852\nDescription: 9 Cards Per Pack.\nProduct: AKB48 Trading Card Game &amp; Collection vol.1 Booster (15packs)\nPrice: $12.25\nScore: 0.827\nDescription: 15 packs, 6 cards per pack.\n```\n\nThis output shows that the semantic search successfully retrieved products related to card sets, even if the exact wording differs from the query.\n## Conclusion\nIn this exercise, we have loaded and preprocessed a dataset of Amazon toy products. Next, we created a knowledge graph by adding nodes for products, manufacturers, and categories, and establishing relationships between them. For each node, we generated embeddings for product descriptions and stored them in the knowledge graph. Finally, we performed semantic searches using embeddings to find products matching natural language queries.\nIn this application, I showed a basic implementation of Graph RAG using cosine similarity. With its simplicity comes its limitations. This may not be something you want to use for your business. Instead, considering using a more advanced approach leveraging the PageRank algorithm or other retrieval functions. Or consider using an easy to use framework like Llama Index or Langchain which provide seamless integrations with the Embedding Model and Neo4j. More articles to come on this!\n## Register Now to Continue Reading\n### Unlock Towards Data Science with a Free Account\nAll articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more.\nRegister for freeSign in to your account\n* * *\n![](../images/body-8f73ac5a40c91a7b503f.jpeg)\nWritten By\n## [Cristian Leo](https://towardsdatascience.com/author/cristianleo120/)\n[See all from Cristian Leo](https://towardsdatascience.com/author/cristianleo120/)\n[Data Science](https://towardsdatascience.com/tag/data-science/)[Hands On Tutorials](https://towardsdatascience.com/tag/hands-on-tutorials/)[Knowledge Graph](https://towardsdatascience.com/tag/knowledge-graph/)[Llm](https://towardsdatascience.com/tag/llm/)[Retrieval Augmented Gen](https://towardsdatascience.com/tag/retrieval-augmented-gen/)\nShare This Article\n  * [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F&title=How%20to%20Query%20a%20Knowledge%20Graph%20with%20LLMs%20Using%20gRAG)\n  * [](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F&title=How%20to%20Query%20a%20Knowledge%20Graph%20with%20LLMs%20Using%20gRAG)\n  * [](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F&text=How%20to%20Query%20a%20Knowledge%20Graph%20with%20LLMs%20Using%20gRAG)\n\n\nTowards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.\n[Write for TDS](https://towardsdatascience.com/submissions/)\n## Related Articles\n  * [![](../images/021.png)](https://towardsdatascience.com/stepping-on-everyones-toes-for-everyone-s-benefit-626a0c2664b5/)\n## [Stepping on Everyone's Toes for Everyone's Benefit](https://towardsdatascience.com/stepping-on-everyones-toes-for-everyone-s-benefit-626a0c2664b5/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nThe Role of Data Science Departments in 21st Century Organizations\n[Jason Nett](https://towardsdatascience.com/author/jasonnett/)\nJanuary 8, 202117 min read\n  * [![](../images/022.jpg)](https://towardsdatascience.com/machine-learning-streaming-with-kafka-debezium-and-bentoml-c5f3996afe8f/)\n## [Machine Learning Streaming with Kafka, Debezium, and BentoML](https://towardsdatascience.com/machine-learning-streaming-with-kafka-debezium-and-bentoml-c5f3996afe8f/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nCreating a real-time price recommender system using modern data-related tools\n[João Pedro](https://towardsdatascience.com/author/joaopedro214/)\nAugust 22, 202211 min read\n  * [![](../images/023.png)](https://towardsdatascience.com/fluorescent-neuronal-cells-dataset-part-ii-e1ac27e26d7/)\n## [Fluorescent Neuronal Cells dataset - part II](https://towardsdatascience.com/fluorescent-neuronal-cells-dataset-part-ii-e1ac27e26d7/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nPeculiar traits and challenges\n[Luca Clissa](https://towardsdatascience.com/author/luca.clissa/)\nNovember 30, 20225 min read\n  * [![](../images/024.jpeg)](https://towardsdatascience.com/chain-of-thought-prompting-facilitate-llms-reasoning-abilities-313cd7714938/)\n## [Chain of Thought Prompting Facilitate LLMs Reasoning Abilities](https://towardsdatascience.com/chain-of-thought-prompting-facilitate-llms-reasoning-abilities-313cd7714938/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nDemonstrated with examples\n[Soner Yıldırım](https://towardsdatascience.com/author/sonery/)\nJune 12, 20237 min read\n  * [![](../images/025.jpeg)](https://towardsdatascience.com/a-day-in-the-life-of-a-data-engineer-d65293272121/)\n## [A day in the life of a data engineer](https://towardsdatascience.com/a-day-in-the-life-of-a-data-engineer-d65293272121/)\n[Coding](https://towardsdatascience.com/category/programming/coding/)\nBreaking down the main activities of a data engineer in 2021\n[mehdio](https://towardsdatascience.com/author/mehdio/)\nApril 20, 20215 min read\n  * [![](../images/026.jpg)](https://towardsdatascience.com/imputation-of-missing-data-in-tables-with-datawig-2d7ab327ece2/)\n## [Imputation of Missing Data in Tables with DataWig](https://towardsdatascience.com/imputation-of-missing-data-in-tables-with-datawig-2d7ab327ece2/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nImplementing Amazon's DataWig in Python to impute missing values in tabular data\n[Kenneth Leung](https://towardsdatascience.com/author/kennethleungty/)\nAugust 24, 20222 min read\n  * [![](../images/027.jpeg)](https://towardsdatascience.com/from-business-intelligence-to-data-science-94a1fd866231/)\n## [From Business Intelligence to Data Science](https://towardsdatascience.com/from-business-intelligence-to-data-science-94a1fd866231/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nI'm a Manager of Data Science & Machine Learning, specialising in design & building of solutions powered by DS & ML for enterprises for...\n[Raj](https://towardsdatascience.com/author/rajkumar.kaliyaperumal/)\nMarch 17, 20196 min read\n  * [![](../images/028.jpg)](https://towardsdatascience.com/your-churn-threshold-is-a-pricing-decision/)\n## [Your Churn Threshold Is a Pricing Decision](https://towardsdatascience.com/your-churn-threshold-is-a-pricing-decision/)\n[Data Science](https://towardsdatascience.com/category/data-science/)\nHow unit economics should set your classification cutoff, and why they rarely do.\n[Fabio Oliveira](https://towardsdatascience.com/author/fabio.oliveira/)\nJune 17, 202614 min read\n  * [![](../images/029.jpeg)](https://towardsdatascience.com/8-methods-for-handling-missing-values-with-python-pandas-842544cdf891/)\n## [8 Methods For Handling Missing Values With Python Pandas](https://towardsdatascience.com/8-methods-for-handling-missing-values-with-python-pandas-842544cdf891/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\n#7: Using the previous or next value\n[Soner Yıldırım](https://towardsdatascience.com/author/sonery/)\nNovember 11, 20216 min read\n\n\n  * [YouTube](https://www.youtube.com/c/TowardsDataScience)\n  * [X](https://x.com/TDataScience)\n  * [LinkedIn](https://www.linkedin.com/company/towards-data-science/)\n  * [Threads](https://www.threads.net/@towardsdatascience)\n  * [Bluesky](https://bsky.app/profile/towardsdatascience.com)\n\n\n[](https://towardsdatascience.com/)\nYour home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.\nSubscribe to Our Newsletter[Write For TDS](https://towardsdatascience.com/submissions/)•[About](https://towardsdatascience.com/about-towards-data-science-d691af11cc2f/)•[Advertise](https://contact.towardsdatascience.com/advertise-with-towards-data-science)•[Privacy Policy](https://towardsdatascience.com/privacy-policy/)•[Terms of Use](https://towardsdatascience.com/website-terms-of-use/)\n© Insight Media Group, LLC 2026•[The New Stack](https://thenewstack.io)•[roadmap.sh](https://roadmap.sh)•[Creator Network](https://insightmediagroup.io/creator-network)\n####  Recommended Articles \nClose\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500) How Relevance Models Foreshadowed Transformers for NLP ](https://towardsdatascience.com/how-relevance-models-foreshadowed-transformers-for-nlp/ \"How Relevance Models Foreshadowed Transformers for NLP\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500) Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1 ](https://towardsdatascience.com/javascript-fatigue-you-dont-need-js-to-build-chatgpt/ \"Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500) The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs ](https://towardsdatascience.com/subset-sum-problem-solved-in-linear-time-for-dense-enough-inputs/ \"The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500) Does Your Company Have a Data Strategy? ](https://towardsdatascience.com/does-your-company-have-a-data-strategy-e8944dbec4ae/ \"Does Your Company Have a Data Strategy?\")\n\n\n",
  "metadata": {
    "title": "How to Query a Knowledge Graph with LLMs Using gRAG | Towards Data Science",
    "description": "Google, Microsoft, LinkedIn, and many more tech companies are using Graph RAG. Why? Let's understand it by building one from scratch.",
    "keywords": null,
    "author": "Cristian Leo",
    "og:locale": "en_US",
    "og:type": "article",
    "og:title": "How to Query a Knowledge Graph with LLMs Using gRAG",
    "og:description": "Google, Microsoft, LinkedIn, and many more tech companies are using Graph RAG. Why? Let's understand it by building one from scratch.",
    "og:url": "https://towardsdatascience.com/how-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322/",
    "og:site_name": "Towards Data Science",
    "og:image": "https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/0UeZPcm_AcNpatRN4.jpg",
    "og:image:width": "1792",
    "og:image:height": "1024",
    "og:image:type": "image/jpeg",
    "twitter:card": "summary_large_image",
    "twitter:site": "@TDataScience",
    "twitter:creator": "@TDataScience",
    "twitter:label1": "Written by",
    "twitter:data1": "Cristian Leo",
    "twitter:label2": "Est. reading time",
    "twitter:data2": "25 min read",
    "article:published_time": "2024-11-07T20:03:44.000Z",
    "article:modified_time": "2024-11-07T20:03:44.000Z",
    "article:author": "https://towardsdatascience.com/author/cristianleo120/"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_000.txt",
      "chars": 66
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_001.txt",
      "chars": 12
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_002.txt",
      "chars": 139
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_003.txt",
      "chars": 324
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_004.txt",
      "chars": 51
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_005.txt",
      "chars": 694
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_006.txt",
      "chars": 153
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_007.txt",
      "chars": 1651
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_008.txt",
      "chars": 608
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_009.txt",
      "chars": 413
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_010.txt",
      "chars": 714
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_011.txt",
      "chars": 353
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_012.txt",
      "chars": 1664
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_013.txt",
      "chars": 229
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_014.txt",
      "chars": 2571
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_015.txt",
      "chars": 1504
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_016.txt",
      "chars": 400
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_017.txt",
      "chars": 480
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_018.txt",
      "chars": 64
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_019.txt",
      "chars": 2694
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_020.txt",
      "chars": 291
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_021.txt",
      "chars": 229
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_022.txt",
      "chars": 53
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_023.txt",
      "chars": 1476
    },
    {
      "language": "txt",
      "file": "attachments/a6e5a94070f2/code/block_024.txt",
      "chars": 286
    }
  ],
  "images": [
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:52:53Z"
    },
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:52:54Z"
    },
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:52:54Z"
    },
    {
      "status": "http_404",
      "url": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500",
      "src": "https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500",
      "alt": "",
      "downloaded": false,
      "discovery": "saved_markdown",
      "retried_at": "2026-10-20T17:52:54Z"
    }
  ],
  "audio": [],
  "videos": [
    {
      "url": "https://www.youtube.com/c/TowardsDataScience",
      "kind": "channel_reference",
      "status": "reference_only"
    }
  ],
  "links": [
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/latest/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/editors-pick/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/the-variable/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/shipai/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/submissions/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/category/data-science/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/cristianleo120/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/data-science/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/hands-on-tutorials/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/knowledge-graph/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/llm/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/tag/retrieval-augmented-gen/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/stepping-on-everyones-toes-for-everyone-s-benefit-626a0c2664b5/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/category/artificial-intelligence/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/jasonnett/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/machine-learning-streaming-with-kafka-debezium-and-bentoml-c5f3996afe8f/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/category/artificial-intelligence/machine-learning/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/author/joaopedro214/"
    },
    {
      "type": "internal",
      "href": "https://towardsdatascience.com/fluorescent-neuronal-cells-dataset-part-ii-e1ac27e26d7/"
    },
    {
      "type": "external",
      "href": "https://write.insightmediagroup.io"
    },
    {
      "type": "external",
      "href": "https://www.linkedin.com/company/towards-data-science/"
    },
    {
      "type": "external",
      "href": "https://x.com/TDataScience"
    },
    {
      "type": "external",
      "href": "https://roadmap.sh/ai-data-scientist"
    },
    {
      "type": "external",
      "href": "https://www.kaggle.com/datasets/PromptCloudHQ/toy-products-on-amazon"
    },
    {
      "type": "external",
      "href": "https://creativecommons.org/licenses/by-sa/4.0/"
    },
    {
      "type": "external",
      "href": "https://github.com/cristianleoo/rag-knowledge-graph"
    },
    {
      "type": "external",
      "href": "https://neo4j.com/download/?gad_source=1&utm_adgroup=DSA"
    },
    {
      "type": "external",
      "href": "https://ai.google.dev/gemini-api/docs/api-key"
    },
    {
      "type": "external",
      "href": "https://www.facebook.com/sharer/sharer.php?title=How+to+Query+a+Knowledge+Graph+with+LLMs+Using+gRAG&u=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F"
    },
    {
      "type": "external",
      "href": "https://www.linkedin.com/shareArticle?mini=true&title=How+to+Query+a+Knowledge+Graph+with+LLMs+Using+gRAG&url=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F"
    },
    {
      "type": "external",
      "href": "https://x.com/share?text=How+to+Query+a+Knowledge+Graph+with+LLMs+Using+gRAG&url=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F"
    },
    {
      "type": "external",
      "href": "https://www.youtube.com/c/TowardsDataScience"
    },
    {
      "type": "external",
      "href": "https://www.threads.net/@towardsdatascience"
    },
    {
      "type": "external",
      "href": "https://bsky.app/profile/towardsdatascience.com"
    },
    {
      "type": "external",
      "href": "https://thenewstack.io"
    },
    {
      "type": "external",
      "href": "https://roadmap.sh"
    },
    {
      "type": "external",
      "href": "https://insightmediagroup.io/creator-network"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-20T17:27:49Z",
  "source_url": "https://towardsdatascience.com/how-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322/",
  "delivery_status": "unverified",
  "discovery_status": "http_403",
  "missing_reason": "源页面未重新核验:http_403；已按保留正文补采图片",
  "repair_error": "",
  "actual_video_files": [],
  "expected_saved_body_images": 4,
  "image_discovery": "saved_markdown",
  "source_body_markdown": "![Revisit consent button](https://cdn-cookieyes.com/assets/images/revisit.svg)\nWe value your privacy\nWe use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking \"Accept All\", you consent to our use of cookies.\nCustomise Reject All Accept All\nCustomise Consent Preferences ![](https://cdn-cookieyes.com/assets/images/close.svg)\nWe use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.\nThe cookies that are categorised as \"Necessary\" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more\nNecessaryAlways Active\nNecessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.\n  * Cookie\nBCTempID\n  * Duration\n10 minutes\n  * Description\nNo description available.\n\n\n  * Cookie\n__cf_bm\n  * Duration\n1 hour\n  * Description\nThis cookie, set by Cloudflare, is used to support Cloudflare Bot Management. \n\n\n  * Cookie\nAWSALBCORS\n  * Duration\n7 days\n  * Description\nAmazon Web Services set this cookie for load balancing.\n\n\n  * Cookie\n_cfuvid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to track users across sessions to optimize user experience by maintaining session consistency and providing personalized services\n\n\n  * Cookie\nli_gc\n  * Duration\n6 months\n  * Description\nLinkedin set this cookie for storing visitor's consent regarding using cookies for non-essential purposes.\n\n\n  * Cookie\n__hssrc\n  * Duration\nsession\n  * Description\nThis cookie is set by Hubspot whenever it changes the session cookie. The __hssrc cookie set to 1 indicates that the user has restarted the browser, and if the cookie does not exist, it is assumed to be a new session.\n\n\n  * Cookie\n__hssc\n  * Duration\n1 hour\n  * Description\nHubSpot sets this cookie to keep track of sessions and to determine if HubSpot should increment the session number and timestamps in the __hstc cookie.\n\n\n  * Cookie\nwpEmojiSettingsSupports\n  * Duration\nsession\n  * Description\nWordPress sets this cookie when a user interacts with emojis on a WordPress site. It helps determine if the user's browser can display emojis properly.\n\n\n  * Cookie\nBCSessionID\n  * Duration\n1 year 1 month 4 days\n  * Description\nBlueconic sets this cookie as a unique identifier for the BlueConic profile.\n\n\n  * Cookie\n_octo\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nlogged_in\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\n__Secure-YEC\n  * Duration\npast\n  * Description\nYouTube sets this cookie to stores the user's video player preferences using embedded YouTube video\n\n\n  * Cookie\n__eoi\n  * Duration\n6 months\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTGCORS\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\nlogin-status-p\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nAWSALBTG\n  * Duration\n7 days\n  * Description\nNo description available.\n\n\n  * Cookie\ncsrf_token\n  * Duration\nsession\n  * Description\nNo description available.\n\n\n  * Cookie\ntoken_v2\n  * Duration\n1 day\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nD\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nPHPSESSID\n  * Duration\nsession\n  * Description\nThis cookie is native to PHP applications. The cookie stores and identifies a user's unique session ID to manage user sessions on the website. The cookie is a session cookie and will be deleted when all the browser windows are closed.\n\n\n  * Cookie\nVISITOR_PRIVACY_METADATA\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to store the user's cookie consent state for the current domain. \n\n\n  * Cookie\ncookietest\n  * Duration\nsession\n  * Description\nThe cookietest cookie is typically used to determine whether the user's browser accepts cookies, essential for website functionality and user experience.\n\n\n  * Cookie\n__Host-airtable-session\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\n__Host-airtable-session.sig\n  * Duration\n1 year\n  * Description\nThis cookie is used to enable us to integrate the services of Airtable.\n\n\n  * Cookie\nm\n  * Duration\n1 year 1 month 4 days\n  * Description\nStripe sets this cookie for fraud prevention purposes. It identifies the device used to access the website, allowing the website to be formatted accordingly.\n\n\n  * Cookie\nBIGipServer*\n  * Duration\nsession\n  * Description\nMarketo sets this cookie to collect information about the user's online activity and build a profile about their interests to provide advertisements relevant to the user. \n\n\n  * Cookie\n__cfruid\n  * Duration\nsession\n  * Description\nCloudflare sets this cookie to identify trusted web traffic.\n\n\n  * Cookie\n_GRECAPTCHA\n  * Duration\n6 months\n  * Description\nGoogle Recaptcha service sets this cookie to identify bots to protect the website against malicious spam attacks.\n\n\n  * Cookie\n__Secure-YNID\n  * Duration\n6 months\n  * Description\nGoogle cookie used to protect user security and prevent fraud, especially during the login process.\n\n\n  * Cookie\ncookieyes-consent\n  * Duration\n1 year\n  * Description\nCookieYes sets this cookie to remember users' consent preferences so that their preferences are respected on subsequent visits to this site. It does not collect or store any personal information about the site visitors.\n\n\nFunctional\nFunctional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.\n  * Cookie\nlidc\n  * Duration\n1 day\n  * Description\nLinkedIn sets the lidc cookie to facilitate data center selection.\n\n\n  * Cookie\nbrw\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nbrwConsent\n  * Duration\n5 minutes\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nWMF-Uniq\n  * Duration\n1 year\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nloom_anon_comment\n  * Duration\n1 year\n  * Description\nNo description available.\n\n\n  * Cookie\nloom_referral_video\n  * Duration\nsession\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nVISITOR_INFO1_LIVE\n  * Duration\n6 months\n  * Description\nA cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface.\n\n\n  * Cookie\nyt-remote-connected-devices\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nytidb::LAST_RESULT_ENTRY_KEY\n  * Duration\nNever Expires\n  * Description\nThe cookie ytidb::LAST_RESULT_ENTRY_KEY is used by YouTube to store the last search result entry that was clicked by the user. This information is used to improve the user experience by providing more relevant search results in the future.\n\n\n  * Cookie\nyt-remote-device-id\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to store the user's video preferences using embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-name\n  * Duration\nsession\n  * Description\nThe yt-remote-session-name cookie is used by YouTube to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\nyt-remote-fast-check-period\n  * Duration\nsession\n  * Description\nThe yt-remote-fast-check-period cookie is used by YouTube to store the user's video player preferences for embedded YouTube videos.\n\n\n  * Cookie\nyt-remote-session-app\n  * Duration\nsession\n  * Description\nThe yt-remote-session-app cookie is used by YouTube to store user preferences and information about the interface of the embedded YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-available\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-available cookie is used to store the user's preferences regarding whether casting is available on their YouTube video player.\n\n\n  * Cookie\nyt-remote-cast-installed\n  * Duration\nsession\n  * Description\nThe yt-remote-cast-installed cookie is used to store the user's video player preferences using embedded YouTube video.\n\n\n  * Cookie\ncp_session\n  * Duration\n3 months\n  * Description\nCodepen sets this cookie for Help systems found in the website.\n\n\n  * Cookie\nloid\n  * Duration\n1 year 1 month 4 days\n  * Description\nThis cookie is set by the Reddit. The cookie enables the sharing of content from the website onto the social media platform.\n\n\nAnalytics\nAnalytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.\n  * Cookie\n__hstc\n  * Duration\n6 months\n  * Description\nHubspot set this main cookie for tracking visitors. It contains the domain, initial timestamp (first visit), last timestamp (last visit), current timestamp (this visit), and session number (increments for each subsequent session).\n\n\n  * Cookie\nhubspotutk\n  * Duration\n6 months\n  * Description\nHubSpot sets this cookie to keep track of the visitors to the website. This cookie is passed to HubSpot on form submission and used when deduplicating contacts.\n\n\n  * Cookie\n_ga\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to calculate visitor, session and campaign data and track site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognise unique visitors.\n\n\n  * Cookie\n_ga_*\n  * Duration\n1 year 1 month 4 days\n  * Description\nGoogle Analytics sets this cookie to store and count page views.\n\n\n  * Cookie\n__Host-psifi.analyticsTrace\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n__Host-psifi.analyticsTraceV2\n  * Duration\n6 hours\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\n_gh_sess\n  * Duration\nsession\n  * Description\nGitHub sets this cookie for temporary application and framework state between pages like what step the user is on in a multiple step form.\n\n\n  * Cookie\nYSC\n  * Duration\nsession\n  * Description\nYSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages.\n\n\n  * Cookie\najs_anonymous_id\n  * Duration\n1 year\n  * Description\nThis cookie is set by Segment to count the number of people who visit a certain site by tracking if they have visited before.\n\n\n  * Cookie\nvuid\n  * Duration\n1 year 1 month 4 days\n  * Description\nVimeo installs this cookie to collect tracking information by setting a unique ID to embed videos on the website. \n\n\nPerformance\nPerformance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.\n  * Cookie\nAWSALB\n  * Duration\n7 days\n  * Description\nAWSALB is an application load balancer cookie set by Amazon Web Services to map the session to the target.\n\n\n  * Cookie\nacq\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nacq.sig\n  * Duration\npast\n  * Description\nDescription is currently not available.\n\n\n  * Cookie\nptc\n  * Duration\n2 years\n  * Description\nNo description available.\n\n\nAdvertisement\nAdvertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.\n  * Cookie\nmuc_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to collect user behaviour and interaction data to optimize the website.\n\n\n  * Cookie\nguest_id_marketing\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\nguest_id_ads\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor.\n\n\n  * Cookie\npersonalization_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to integrate and share features for social media and also store information about how the user uses the website, for tracking and targeting.\n\n\n  * Cookie\nguest_id\n  * Duration\n1 year 1 month 4 days\n  * Description\nTwitter sets this cookie to identify and track the website visitor. It registers if a user is signed in to the Twitter platform and collects information about ad preferences.\n\n\n  * Cookie\nbcookie\n  * Duration\n1 year\n  * Description\nLinkedIn sets this cookie from LinkedIn share buttons and ad tags to recognize browser IDs.\n\n\n  * Cookie\n__Secure-ROLLOUT_TOKEN\n  * Duration\n6 months\n  * Description\nYouTube sets this cookie to manage feature rollout and experimentation. It helps Google control which new features or interface changes are shown to users as part of testing and staged rollouts, ensuring consistent experience for a given user during an experiment.\n\n\n  * Cookie\nyt.innertube::nextId\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nyt.innertube::requests\n  * Duration\nNever Expires\n  * Description\nYouTube sets this cookie to register a unique ID to store data on what videos from YouTube the user has seen.\n\n\n  * Cookie\nsession_tracker\n  * Duration\nsession\n  * Description\nThis cookie is set by the Reddit. This cookie is used to identify trusted web traffic. It also helps in adverstising on the website.\n\n\n  * Cookie\nedgebucket\n  * Duration\nsession\n  * Description\nReddit sets this cookie to save the information about a log-on Reddit user, for the purpose of advertisement recommendations and updating the content.\n\n\n  * Cookie\ndid\n  * Duration\n1 year\n  * Description\nArbor sets this cookie to show targeted ads to site visitors.This cookie expires after 2 months or 1 year.\n\n\nUncategorised\nOther uncategorised cookies are those that are being analysed and have not been classified into a category as yet.\nNo cookies to display.\nReject All Save My Preferences Accept All\nToggle Search[](https://towardsdatascience.com/)Toggle Mobile Navigation\n[](https://towardsdatascience.com/)\nPublish AI, ML & data-science insights to a global community of data professionals.\nSign in[Submit an Article](https://write.insightmediagroup.io)\n[Latest](https://towardsdatascience.com/latest/)[Editor's Picks](https://towardsdatascience.com/tag/editors-pick/)[Newsletter](https://towardsdatascience.com/tag/the-variable/)[ShipAI](https://towardsdatascience.com/shipai/)\n* * *\n[Write For TDS](https://towardsdatascience.com/submissions/)\n[LinkedIn](https://www.linkedin.com/company/towards-data-science/)[X](https://x.com/TDataScience)Toggle Search\n[Data Science](https://towardsdatascience.com/category/data-science/)\n# How to Query a Knowledge Graph with LLMs Using gRAG\nGoogle, Microsoft, LinkedIn, and many more tech companies are using Graph RAG. Why? Let's understand it by building one from scratch.\n[Cristian Leo](https://towardsdatascience.com/author/cristianleo120/)\nNovember 7, 202425 min read\n![Image illustrating a knowledge graph with interconnected nodes and edges against a tech-inspired gradient background - Image generated by the author using DALL-E](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/0UeZPcm_AcNpatRN4.jpg)\nImage illustrating a knowledge graph with interconnected nodes and edges against a tech-inspired gradient background - Image generated by the author using DALL-E\nYou may not realize it, but you've been interacting with Knowledge Graphs (KGs) more frequently than you might think. They're the technology behind many modern search engines, Retrieval-Augmented Generation (RAG) systems for Large Language Models (LLMs), and various query tools. But what exactly are Knowledge Graphs, and why are they so integral to these technologies? Let's delve into it.\nLearn this step by step with the interactive [AI and Data Scientist](https://roadmap.sh/ai-data-scientist) roadmap.\n## Introduction to Knowledge Graphs\nA Knowledge Graph (KG) is a structured representation of information that captures real-world entities and the relationships between them. Imagine a network where each point represents an entity - such as a product, person, or concept - and the lines connecting them represent the relationships they share. This interconnected web allows for a rich semantic understanding of data, where the focus isn't just on individual pieces of information but on how these pieces relate to one another.\n### Nodes\nAt the heart of a knowledge graph are nodes (entities). To illustrate this, let's consider building a knowledge graph using a publicly available Amazon dataset of toy products, this will be the dataset we will use later on in our practical application. What might we find in such a dataset?\nNaturally, we would have products. In our knowledge graph, each product in the dataset becomes a node. This product node includes all information about the item, such as its description, price, stock quantity, and ratings.\n![Product Node in Neo4j - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/17OwRWKgLmgKmCuoRvanQOA.png)\nProduct Node in Neo4j - Image by Author\nBut products aren't the only entities we can represent. Knowledge graphs are flexible, allowing us to create as many types of nodes as needed. For example, since every product has a manufacturer, we can also create nodes for each manufacturer. A manufacturer node might include properties like the company's name, location, and contact information.\n### Edges\nHowever, nodes most of the time can be connected by each other. For example, a product node could be connected to a manufacturer node, since the manufacturer produces the product, and the product is produced by the manufacturer. This relationship is known as the edges in a knowledge graph.\nEdges are the links that define how two entities are related, and in a knowledge graph, these relationships are explicitly modeled and stored. This is a significant shift from traditional relational databases, where such relationships are often inferred at query time using JOIN operations.\nConsider the relationship between a product and its manufacturer. In a relational database, we would join tables to associate a product with its manufacturer during a query. In a knowledge graph, however, we directly specify this relationship by creating an edge between the product node and the manufacturer node.\n![2 Product Nodes connected to 1 Manufacturer nodes via edges - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/14YmjUb5Ps1rmV43nGJ1k1w.png)\n2 Product Nodes connected to 1 Manufacturer nodes via edges - Image by Author\nTaking our toy dataset as an example, we know that every product is associated with a manufacturer. Therefore, we can create a \"manufactured_by\" edge that connects a product node to its corresponding manufacturer node, indicating who produces the product. For instance, the product \"DJI Phantom 2 with H3–3D Gimbal\" would be connected to the manufacturer \"DJI\" through this edge.\nEdges themselves can carry properties, providing additional context about the relationships they represent. Focusing on the \"manufactured_by\" relationship from product to manufacturer, we might include a property like \"since,\" which indicates the date when the manufacturing relationship was established.\n## The Math Behind Knowledge Graphs\nAll right, you should now have the basics of KGs, but let's go a step further and see how the math plays out. Moreover, in our example, we will query a knowledge graph using natural language, in order to achieve that, we need to introduce two more components: **embeddings** and **cosine similarity**.\nA knowledge graph **G** is a directed, labeled graph that can be formally represented as:\n![Knowledge Graph Formula - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1jC1a8TmWgfOAbueYt1t_-w.png)\nKnowledge Graph Formula - Image by Author\nWhere:\n  * **V** is the set of **vertices** or **nodes**.\n  * **E⊆V×V** is the set of **edges** , representing relationships between nodes.\n  * **ϕV:V→A_V** ​ is a function mapping nodes to their attributes.\n  * **ϕE:E→A_E** ​ is a function mapping edges to their attributes.\n\n\nEach node **v ∈ V** represents an entity or concept and can be characterized by a set of attributes. Mathematically, a node can be defined as:\n![Node Formula - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/18ybKO6L3o-jDOL_5_imMng.png)\nNode Formula - Image by Author\nWhere:\n  * **id_v** is a unique identifier for the node.\n  * **A_v = { (k,a_k) ∣ k ∈ K_v }** is a set of attribute-value pairs, with **K_v** being the set of attribute keys for node **v**.\n\n\nFor example, a product might look like:\n![Product Node - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1IlG406in7XOSlpXl3_lK6A.png)\nProduct Node - Image by Author\nNext, an edge **e ∈ E** represents a relationship between two nodes and is defined as:\n![Edge Formula - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/181s2NXF2e_z9Nl-u6GPsDw.png)\nEdge Formula - Image by Author\nWhere:\n  * **v_i, v_j ∈ V** are the source and target nodes.\n  * **r_ij** is the type of relationship (e.g., **MANUFACTURED_BY** , **BELONGS_TO**).\n  * **A_e = { (k, a_k) ∣ k ∈ K_e }** is a set of attribute-value pairs associated with the edge.\n\n\nFor example, an edge represents that a product is manufactured by a manufacturer:\n![Edge example - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1tDF2UZaQDI3UOJsNK5m_IA.png)\nEdge example - Image by Author\nWhere **A_e** ​ might include:\n![Edge properties - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1QXZIwYMCM40EerM8BNKsUQ.png)\nEdge properties - Image by Author\n### Embeddings and Vector Representations\nAs of now, our Knowledge Graph has both structured (numerical) and unstructured (text) data. However, math just doesn't work very well with text. To enable semantic search and natural language querying, we need to convert textual data into numerical vector representations. Here's where **embeddings** do their magic**.** We convert the text data in our KG into embedding, and the same for our natural language query.\n> An **embedding** is a mapping of discrete objects, such as words, phrases, or documents, into a continuous vector space. In this space, semantically similar items are located close to each other. This is achieved by training machine learning models on large corpora of text data to learn patterns and contexts in which words or phrases appear.\nAn embedding function **ϕ** maps textual descriptions to points in a high-dimensional vector space **R^n** :\n![Embedding Function - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1ACXwF9TRinppGcooWK8rGw.png)\nEmbedding Function - Image by Author\nWhere:\n  * **T** is the set of all possible textual data (e.g., product descriptions, queries).\n  * **n** is the dimensionality of the embedding space, which can range from hundreds to thousands of dimensions depending on the model\n\n\nEmbeddings are essential elements in our architecture because they allow us to perform mathematical operations on textual data. By representing text as vectors, we can compute distances and similarities between different pieces of text. This capability is essential for tasks like semantic search, where we want to find products that are semantically similar to a user's query, even if the exact words used are different. Moreover, you will find that different embedding models can bring very different results. In your AI application consider exploring several models and see which one delivers the best results. Some of the big names in the space are OpenAI, Gemini, and Voyage.\n### Cosine Similarity\nNow that we have both our query and our nodes embedded, we need to find a way to compute the similarity between the query and the nodes. One of the most popular approaches is **cosine similarity**.\nCosine similarity measures the cosine of the angle between two non-zero vectors in an inner product space. It provides a normalized measure of similarity that is independent of the magnitude of the vectors.\nGiven two vectors **A,B ∈ R^n** the cosine similarity **cos⁡θ** is defined as:\n![Cosine Similarity - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1dPw-sV5KXkQxBuUjNxGIEw.png)\nCosine Similarity - Image by Author\nThe value of cosine similarity ranges from -1 to 1, where 1 indicates that the vectors are identical in direction (maximum similarity), 0 indicates orthogonality (no similarity), and -1 indicates diametrically opposed vectors (opposite meanings). In the context of embeddings generated from textual data, cosine similarity values typically range from 0 to 1 because the components of the embeddings are usually non-negative.\nTo compute the cosine similarity between the query embedding and product embeddings, we follow these steps:\n**Compute the Dot Product** :\n![Compute Dot Product - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1dkFHufSLYqpFN_H16T-47g.png)\nCompute Dot Product - Image by Author\n**Compute the Norms** :\n![Compute Norms - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1NmUcDYZqt3uAambQt9m-mw.png)\nCompute Norms - Image by Author\n**Compute Cosine Similarity** :\n![Compute Cosine Similarity - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1dPw-sV5KXkQxBuUjNxGIEw.png)\nCompute Cosine Similarity - Image by Author\nSuppose we have two embeddings:\n![Embedding Vectors A, and B - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1xAD5iEP6NZ9OSB7xgexagQ.png)\nEmbedding Vectors A, and B - Image by Author\nCompute the dot product:\n![Compute dot product of A and B - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1xf1eoIX2BRrDXi7NRcWzAQ.png)\nCompute dot product of A and B - Image by Author\nCompute the norms:\n![Compute norms of A and B - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1cqtYu-OKJ7rQLw1DqfBWZw.png)\nCompute norms of A and B - Image by Author\nCompute cosine similarity:\n![Compute cosine similarity - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1Z8OQU4MJ4uM0MHmJKpJ-Rg.png)\nCompute cosine similarity - Image by Author\n### Mathematical Modeling of Edge Traversal\nIn advanced applications of knowledge graphs, we often want to consider not just the semantic similarity between entities (captured by embeddings) but also the structure of the graph itself. This involves modeling how we traverse the edges - go around the node with the highest similarity - of the graph to understand the relationships and importance of different nodes within the network.\n**Adjacency Matrix Representation** To mathematically represent the structure of a knowledge graph, we use an **adjacency matrix**. Suppose our graph **G** has **n** nodes. The adjacency matrix **A** is an **n×n** matrix where each element **A_ij** ​ indicates whether there is a direct connection (edge) from node **v_i** ​ to node **v_j** :\n![Adjacency Matrix - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1T02kr3J1IHWirPSChyU4gA.png)\nAdjacency Matrix - Image by Author\nConsider a simple graph with three products:\n  * **Product A** (v1​)\n  * **Product B** (v2​)\n  * **Product C** (v3​)\n\n\nSuppose:\n  * Product A is related to Product B.\n  * Product B is related to Product C.\n\n\nThe adjacency matrix **A** would be:\n![Adjacency matrix A - Image by author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1WOQ8QooNlva4MArzwUnuEQ.png)\nAdjacency matrix A - Image by author\nThis matrix helps us compute paths and determine how nodes are connected within the graph.\n**Random Walks and Transition Probabilities** Next, to analyze how likely we are to move from one node to another, we use the concept of **random walks**. In a random walk, starting from a node, we randomly select an outgoing edge to follow to the next node.\nWe define the **transition probability matrix** **P** to represent the probabilities of moving from one node to another:\n![Transition Probability Matrix Formula - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/19Vf3cHnC-7749z8qZjRGg.png)\nTransition Probability Matrix Formula - Image by Author\nWhere:\n  * **A_ij** ​ is the adjacency between nodes **v_i** ​ and **v_j** ​.\n  * **∑A_ik** ​ is the total number of outgoing edges from node **v_i** ​.\n\n\nThis formula normalizes the adjacency matrix so that each row sums to 1, turning it into a probability distribution.\nUsing the previous adjacency matrix **A** :\n![Adjacency Matrix A - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1WOQ8QooNlva4MArzwUnuEQ.png)\nAdjacency Matrix A - Image by Author\nCalculate **P** :\n  * For node **v1** ​: Sum of outgoing edges =**A12 + A13 = 1 + 0 =1**\n\n\n![Node v1 Calculation - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1-J7YnbHDchLOsbpiZZKQNw.png)\nNode v1 Calculation - Image by Author\n  * For node **v2** ​: Sum of outgoing edges = **A23 =1**\n\n\n![Node v2 Calculation - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/13yu-YEj7aYGrioNHltsk_w.png)\nNode v2 Calculation - Image by Author\n  * For node **v3** ​: No outgoing edges, so the probabilities are zero.\n\n\nTransition probability matrix P:\n![Transition Probability Matrix - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1W6DPfr1min1Ip-yZiphgbw.png)\nTransition Probability Matrix - Image by Author\n**Personalized PageRank Algorithm** Finally, the **Personalized PageRank** algorithm allows us to compute a relevance score for each node in the graph, considering both the structure of the graph and a personalization (preference) vector.\nThe PageRank vector π is calculated using the iterative formula:\n![Personalized PageRank Formula - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1VtEcQdu1d9EnF-2HZ3tANA.png)\nPersonalized PageRank Formula - Image by Author\nWhere:\n  * **α** is the **teleportation probability** , usually set to 0.15. It represents the probability of jumping back to a personalized starting point rather than following an outgoing edge.\n  * **π0** ​ is the **personalization vector** , indicating our starting preference or importance of nodes.\n  * **P^T** is the **transpose** of the transition probability matrix **P**.\n  * **π** is the PageRank vector containing the relevance scores of the nodes.\n\n\n**Teleportation** ensures that the random walker has a chance to jump to any node based on our preferences, preventing them from getting stuck in sink nodes (nodes with no outgoing edges).\n**Integrating Embeddings with Graph Structure** To effectively recommend products or retrieve information, we need to combine semantic similarity (from embeddings) - measures how closely the product's description matches the user's query in terms of meaning- with graph relevance (from PageRank) - reflects the product's importance within the graph's structure, considering relationships and connectivity. We define a **composite score** **S(p)** for each product **p** :\n![Composite Score Formula - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1qS8JqjafqdegVX1kk0POhw.png)\nComposite Score Formula - Image by Author\nWhere:\n  * **λ** is a weighting parameter between 0 and 1 that balances the importance of semantic similarity and graph relevance. If λ=1, the ranking relies entirely on semantic similarity. If λ=0, it depends solely on graph relevance. A value like λ=0.5 gives equal weight to both.\n\n\nSuppose, we have a user query where the embedding is already computed. The product embeddings and their cosine similarities with the query are:\n  * **Product A** : cos⁡θ =0.8\n  * **Product B** : cos⁡θ=0.6\n  * PageRank score πA=0.3\n  * PageRank score πB=0.7\n\n\nWith λ=0.5:\n![product A and B composite scores - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/1pIl5if17I65cbqANWuQPKg.png)\nproduct A and B composite scores - Image by Author\nEven though Product A has a higher semantic similarity, Product B has a higher overall score due to its greater graph relevance. Therefore, Product B would be ranked higher in the recommendations.\n## Benefits of Knowledge Graphs\n**Semantic Understanding** One of the most significant benefits of knowledge graphs is their ability to capture complex relationships and context between different entities. This capability makes LLMs able to reason about data in a way that's more aligned with human thinking. Instead of treating data points as isolated pieces of information, knowledge graphs interconnect entities through explicit relationships, providing a rich semantic understanding of the data.\nFor example, imagine an air conditioner knowledge graph. A single **Product** node isn't just a standalone item; it's connected to its **Manufacturer** , **Features** , **Category** , and **SubCategory**. This interconnectedness allows the system to comprehend that a particular air conditioner is linked to other entities like its brand, features it offers - such as \"Energy Efficient\" or \"Remote Control\" - and the category it belongs to, like \"Portable Air Conditioners.\" This depth of semantic richness enables more accurate and context-aware responses to user queries, significantly enhancing user experience.\n**Flexibility** Another advantage of knowledge graphs is their inherent flexibility. They allow for the easy addition of new nodes or relationships without necessitating significant alterations to the existing schema. This feature is particularly beneficial in dynamic environments where data is continually evolving.\nFor instance, suppose we decide to incorporate **Customer Reviews** into our knowledge graph. We can simply add new **Review** nodes and establish relationships like **REVIEWED_BY** connecting **Product** nodes to **Customer** nodes. There's no need to redesign the entire data model or migrate existing data. This adaptability makes knowledge graphs highly suitable for applications where requirements and data structures are constantly changing.\n**Efficient Querying** Knowledge graphs are optimized for querying relationships between entities, making data retrieval more efficient - especially for complex queries involving multiple interconnected entities. This efficiency becomes evident when dealing with intricate queries that would be cumbersome in traditional databases.\nConsider a scenario where we want to find all air conditioners manufactured by \"CoolTech Industries\" that have the feature \"Energy Efficient.\" In a traditional relational database, executing this query would require complex JOIN operations across multiple tables, which can be time-consuming and resource-intensive.\nIn contrast, a knowledge graph simplifies this process significantly:\n  1. **Start at the Manufacturer Node:** Begin by locating the **Manufacturer** node where `name = \"CoolTech Industries\"`.\n  2. **Traverse Relationships:** Move along the **MANUFACTURED_BY** edges to find all connected **Product** nodes.\n  3. **Filter by Features:** For these products, traverse the **HAS_FEATURE** edges to identify those connected to a **Feature** node where `name = \"Energy Efficient\"`.\n\n\nThis direct traversal of relationships eliminates the need for costly JOIN operations, resulting in faster and more efficient querying. The ability to navigate through interconnected data seamlessly not only improves performance but also enhances the capability to derive insights from complex data relationships.\n## Practical Application: Building and Querying a Knowledge Graph with Embeddings\nIn this section, we will create a knowledge graph using a public [dataset of Amazon toy products](https://www.kaggle.com/datasets/PromptCloudHQ/toy-products-on-amazon) ([License](https://creativecommons.org/licenses/by-sa/4.0/)). We will add embeddings to enable semantic search and query the database using natural language. By the end of this section, you will understand how to build a knowledge graph, add embeddings, and perform semantic searches to find products that match natural language queries.\n### Setting Up the Environment\nBefore we begin, ensure you have the necessary tools installed and configured:\n**Clone the Repository** The dataset and code are available in the GitHub repository [rag-knowledge-graph](https://github.com/cristianleoo/rag-knowledge-graph). Clone this repository to your local machine:\nbash\n\n```\ngit clone https://github.com/cristianleoo/rag-knowledge-graph.git\n```\n\nIf you don't like using the terminal, or you don't have git installed, follow this link and download the repo:\n> [**GitHub - cristianleoo/rag-knowledge-graph**](https://github.com/cristianleoo/rag-knowledge-graph)\n**Install Neo4j**[Download](https://neo4j.com/download/?utm_source=GSearch&utm_medium=PaidSearch&utm_campaign=Evergreen&utm_content=AMS-Search-SEMCE-DSA-None-SEM-SEM-NonABM&utm_term=&utm_adgroup=DSA&gad_source=1&gclid=Cj0KCQiAoae5BhCNARIsADVLzZfw0vpWmdrWBAPirQbj7S9bdao_udYYZMGo7L9Ej1ZvTib8s4PXxlkaAmimEALw_wcB) and install Neo4j from the official website. Follow the installation instructions specific to your operating system.\n**Start the Neo4j Server** Once installed, start the Neo4j server. You can do this via the Neo4j Desktop application or by running the server from the command line:\nbash\n\n```\nneo4j start\n```\n\nWrite down the password you will use for the database as we will need it in a later step\n**Install Required Python Libraries** Navigate to the cloned repository directory and set up a virtual environment. Install the required libraries using `pip`:\nbash\n\n```\ncd rag-knowledge-graph\npython -m venv venv\nsource venv/bin/activate  # On Windows, use venvScriptsactivate\npip install -r requirements.txt\n```\n\n### Step 1: Load and Preprocess the Dataset\nWe begin by importing the necessary libraries and loading the dataset.\npython\n\n```\nimport pandas as pd\npd.set_option('display.max_columns', None)\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport networkx as nx\nfrom py2neo import Graph, Node, Relationship\nimport google.generativeai as genai\nimport time\nfrom tqdm import tqdm\nfrom ratelimit import limits, sleep_and_retry\nimport os\n```\n\n**Loading the Dataset**\npython\n\n```\ndf = pd.read_csv('dataset/products.csv')\ndf.head()\n```\n\nHere, we use `pandas` to read the CSV file containing Amazon toy products, and display the first few rows:\ntext\n\n```\nuniq_id                                   product_name  ... sellers\n0  eac7efa5dbd3d667f26eb3d3ab504464                      Hornby 2014 Catalogue  ... {\"seller\"=>[{\"Seller_name_1\"=>\"Amazon.co.uk\", ...\n1  b17540ef7e86e461d37f3ae58b7b72ac  FunkyBuys® Large Christmas Holiday Express...  ... {\"seller\"=>{\"Seller_name_1\"=>\"UHD WHOLESALE\", ...\n2  348f344247b0c1a935b1223072ef9d8a   CLASSIC TOY TRAIN SET TRACK CARRIAGES LIGHT...  ... {\"seller\"=>[{\"Seller_name_1\"=>\"DEAL-BOX\", \"Sel...\n3  e12b92dbb8eaee78b22965d2a9bbbd9f        HORNBY Coach R4410A BR Hawksworth Corridor  ... NaN\n4  e33a9adeed5f36840ccc227db4682a36  Hornby 00 Gauge 0-4-0 Gildenlow Salt Co. Steam...  ... NaN\n\n[5 rows x 18 columns]\n```\n\n**Data Overview** We examine the dataset to understand the structure and identify missing values.\npython\n\n```\nfor col in df.columns:\n    print(f\"{col:<50} | {df[col].isna().sum() / len(df):>6.2%} missing | {df[col].nunique():>6} unique values | {df[col].dtype}\")\n```\n\nThis will print out:\nbash\n\n```\nuniq_id                                           |  0.00% missing | 10000 unique values | object\nproduct_name                                      |  0.00% missing | 9964 unique values | object\nmanufacturer                                      |  0.07% missing | 2651 unique values | object\nprice                                             | 14.35% missing | 2625 unique values | object\nnumber_available_in_stock                         | 25.00% missing |   89 unique values | object\nnumber_of_reviews                                 |  0.18% missing |  194 unique values | object\nnumber_of_answered_questions                      |  7.65% missing |   19 unique values | float64\naverage_review_rating                             |  0.18% missing |   19 unique values | object\namazon_category_and_sub_category                  |  6.90% missing |  255 unique values | object\ncustomers_who_bought_this_item_also_bought        | 10.62% missing | 8755 unique values | object\ndescription                                       |  6.51% missing | 8514 unique values | object\nproduct_information                               |  0.58% missing | 9939 unique values | object\nproduct_description                               |  6.51% missing | 8514 unique values | object\nitems_customers_buy_after_viewing_this_item       | 30.65% missing | 6749 unique values | object\ncustomer_questions_and_answers                    | 90.86% missing |  910 unique values | object\ncustomer_reviews                                  |  0.21% missing | 9901 unique values | object\nsellers                                           | 30.82% missing | 6581 unique values | object\n```\n\nFrom here, we can see the percentage of missing values, the number of unique values, and the data type for each column. This helps us identify which columns are essential and how to handle missing data.\n**Data Cleaning and Preprocessing** We clean the data by extracting useful information and handling missing values.\npython\n\n```\n# Extract currency symbol and price into separate columns\ndf['currency'] = df['price'].str.extract(r'([^0-9]+)')\ndf['price_value'] = df['price'].str.extract(r'(d+.?d*)').astype(float)\ndf['stock_type'] = df['number_available_in_stock'].str.extract(r'([^0-9]+)')\ndf['stock_availability'] = df['number_available_in_stock'].str.extract(r'(d+.?d*)')\n\n# Clean up average review rating\ndf['average_review_rating'] = df['average_review_rating'].str.replace(' out of 5 stars', '').astype(float)\n# Clean up number of reviews\ndf['number_of_reviews'] = df['number_of_reviews'].str.replace(',', '').fillna(0).astype(int)\n```\n\nIn particular, we extract numerical values from strings in the `price` and `number_available_in_stock` columns, so that we can treat those columns as int and float.\nThen, we clean the `average_review_rating` and `number_of_reviews` columns to ensure they are numeric.\nWe drop unnecessary columns and handle missing data.\nsql\n\n```\n# Drop irrelevant columns\ndf = df.drop(['price', 'number_available_in_stock', 'customers_who_bought_this_item_also_bought',\n              'items_customers_buy_after_viewing_this_item', 'customer_questions_and_answers', 'sellers'], axis=1)\n\n# Drop rows with essential missing data\ndf.dropna(subset=['product_information', 'price_value', 'description', 'amazon_category_and_sub_category'], how='any', inplace=True)\n```\n\nIn this example, we drop some features to keep it simple. However, in production, you may want to keep as many relevant features as possible, as they could provide useful insights for analysis and for the model.\nWe check the data again to ensure it's clean.\nbash\n\n```\n# Fill missing values with defaults\ndf['amazon_category_and_sub_category'] = df['amazon_category_and_sub_category'].fillna('')\ndf['manufacturer'] = df['manufacturer'].fillna('Unknown')\ndf['number_of_answered_questions'] = df['number_of_answered_questions'].fillna(0.0)\ndf['average_review_rating'] = df['average_review_rating'].fillna(0.0)\ndf['description'] = df['description'].fillna('')\ndf['product_description'] = df['product_description'].fillna('')\ndf['product_information'] = df['product_information'].fillna('')\ndf['customer_reviews'] = df['customer_reviews'].fillna('')\ndf['stock_availability'] = df['stock_availability'].astype(float).fillna(0.0)\ndf['stock_type'] = df['stock_type'].fillna('Out of stock')\n```\n\nNext, we fill missing values in categorical columns with default strings, and numerical columns with values zeros so we don't bring null values into our KG.\npython\n\n```\n# Function to combine product title and description\ndef complete_product_description(row):\n    return f\"Product Title: {row['product_name']}nProduct Description: {row['product_description']}\"\n\n# Apply the function to create a new column\ndf['description_complete'] = df.apply(complete_product_description, axis=1)\n\n# Display the first few rows\ndf.head()\n```\n\nFinally, we define a function called `complete_product_description` that takes a row from the dataframe and combines the `product_name` and `product_description` into a single string. We then apply this function to each row in the dataframe `df` to create a new column called `description_complete`. This new column contains the complete description of each product, which we will use later for generating embeddings.\nLet's call `df.head()`, and display the first few rows of the dataframe to verify that the new column has been added correctly.\nbash\n\n```\nuniq_id                                   product_name  ...                                 description_complete\n0  eac7efa5dbd3d667f26eb3d3ab504464                      Hornby 2014 Catalogue  ...  Product Title: Hornby 2014 CataloguenProduct Description: Hornby 2014 Catalogue Box Contains 1 x Hornby 2014 Catalogue\n1  b17540ef7e86e461d37f3ae58b7b72ac  FunkyBuys® Large Christmas Holiday Express...  ...  Product Title: FunkyBuys® Large Christmas Holiday Express Festival Deluxe Railway Train SetnProduct Description: Size Name:Large FunkyBuys® Large Christmas Holiday Express Festival Deluxe Railway Train Set Light Up with Realistic Sounds Xmas Tree Decoration For Kids Gift\n2  348f344247b0c1a935b1223072ef9d8a   CLASSIC TOY TRAIN SET TRACK CARRIAGES LIGHT...  ...  Product Title: CLASSIC TOY TRAIN SET TRACK CARRIAGES LIGHT ENGINE SOUNDS KIDS XMAS GIFTnProduct Description: BIG CLASSIC TOY TRAIN SET TRACK CARRIAGE LIGHT ENGINE SOUNDS KIDS XMAS GIFT This is a classic train set with a steam engine that features working headlights and realistic engine sounds. The tracks can be assembled in various configurations. Great gift for kids.\n3  e12b92dbb8eaee78b22965d2a9bbbd9f        HORNBY Coach R4410A BR Hawksworth Corridor  ...  Product Title: HORNBY Coach R4410A BR Hawksworth Corridor 3rdnProduct Description: Hornby 00 Gauge BR Hawksworth 3rd Class W 2107 Corridor Coach R4410A\n4  e33a9adeed5f36840ccc227db4682a36  Hornby 00 Gauge 0-4-0 Gildenlow Salt Co. Steam...  ...  Product Title: Hornby 00 Gauge 0-4-0 Gildenlow Salt Co. Steam Locomotive R9671nProduct Description: Hornby RailRoad 0-4-0 Gildenlow Salt Co. Steam Locomotive R9671\n\n[5 rows x 13 columns]\n```\n\n### Step 2: Connect to Neo4j and Prepare the Database\nWe connect to our Neo4j graph database where we'll store the knowledge graph.\npython\n\n```\n# Connect to Neo4j (adjust credentials as needed)\ngraph = Graph(\"bolt://localhost:7687\", auth=(\"neo4j\", \"YOUR_PASSWORD\")) # replace this with your password\n\n# Clear existing data (optional)\ngraph.run(\"MATCH (n) DETACH DELETE n\")\n```\n\nUsing the `py2neo` library, we establish a connection to the Neo4j database. Replace `\"YOUR_PASSWORD\"` with your actual Neo4j password. The command `graph.run(\"MATCH (n) DETACH DELETE n\")` deletes all existing nodes and relationships in the database, providing a clean slate for our new knowledge graph. This step is optional but recommended to avoid conflicts with existing data.\n### Step 3: Create the Knowledge Graph\nWe create nodes for products, manufacturers, and categories, and establish relationships between them.\npython\n\n```\ndef create_knowledge_graph(df):\n    # Create unique constraints\n    try:\n        # For Neo4j 5.x and later\n        graph.run(\"CREATE CONSTRAINT product_id IF NOT EXISTS FOR (p:Product) REQUIRE p.uniq_id IS UNIQUE\")\n        graph.run(\"CREATE CONSTRAINT manufacturer_name IF NOT EXISTS FOR (m:Manufacturer) REQUIRE m.name IS UNIQUE\")\n        graph.run(\"CREATE CONSTRAINT category_name IF NOT EXISTS FOR (c:Category) REQUIRE c.name IS UNIQUE\")\n    except Exception as e:\n        # For Neo4j 4.x\n        try:\n            graph.run(\"CREATE CONSTRAINT ON (p:Product) ASSERT p.uniq_id IS UNIQUE\")\n            graph.run(\"CREATE CONSTRAINT ON (m:Manufacturer) ASSERT m.name IS UNIQUE\")\n            graph.run(\"CREATE CONSTRAINT ON (c:Category) ASSERT c.name IS UNIQUE\")\n        except Exception as e:\n            print(f\"Warning: Could not create constraints: {e}\")\n\n    for _, row in df.iterrows():\n        # Create Product node\n        product = Node(\n            \"Product\",\n            uniq_id=row['uniq_id'],\n            name=row['product_name'],\n            description=row['product_description'],\n            price=float(row['price_value']),\n            currency=row['currency'],\n            review_rating=float(row['average_review_rating']),\n            review_count=int(row['number_of_reviews']),\n            stock_type=row['stock_type'] if pd.notna(row['stock_type']) else None,\n            description_complete=row['description_complete']\n        )\n\n        # Create Manufacturer node\n        manufacturer = Node(\"Manufacturer\", name=row['manufacturer'])\n\n        # Create Category nodes from hierarchy\n        categories = row['amazon_category_and_sub_category'].split(' > ')\n        previous_category = None\n        for cat in categories:\n            category = Node(\"Category\", name=cat.strip())\n            graph.merge(category, \"Category\", \"name\")\n            if previous_category:\n                # Create hierarchical relationship between categories\n                rel = Relationship(previous_category, \"HAS_SUBCATEGORY\", category)\n                graph.merge(rel)\n            previous_category = category\n        # Merge nodes and create relationships\n        graph.merge(product, \"Product\", \"uniq_id\")\n        graph.merge(manufacturer, \"Manufacturer\", \"name\")\n        # Connect product to manufacturer\n        graph.merge(Relationship(product, \"MANUFACTURED_BY\", manufacturer))\n        # Connect product to lowest-level category\n        graph.merge(Relationship(product, \"BELONGS_TO\", previous_category))\n# Create the knowledge graph\ncreate_knowledge_graph(df)\n```\n\nThis function`create_knowledge_graph` iterates over each row in the dataframe `df`. For each product:\n  * We create a `Product` node with properties such as `uniq_id`, `name`, `description`, `price`, `currency`, `review_rating`, `review_count`, `stock_type`, and `description_complete`.\n  * We create a `Manufacturer` node based on the `manufacturer` field.\n  * We process the `amazon_category_and_sub_category` field to create a hierarchy of `Category` nodes. We split the categories by the `>` delimiter and created nodes for each category level.\n  * We establish a `HAS_SUBCATEGORY` relationship between categories to represent the hierarchy.\n  * We create relationships between the product and its manufacturer (`MANUFACTURED_BY`) and between the product and the most specific category (`BELONGS_TO`).\n  * We use `graph.merge` to ensure that nodes and relationships are created only if they do not already exist, preventing duplicates in the graph.\n\n\n### Step 4: Query the Knowledge Graph and Visualize Results\nNow, let's run a sample query to retrieve data from the graph and visualize it.\npython\n\n```\ndef run_query_with_viz(query, title, viz_query=None):\n    print(f\"n=== {title} ===\")\n    # Run and display query results as a DataFrame\n    results = graph.run(query).data()\n    df = pd.DataFrame(results)\n    display(df)\n\n    # Create visualization\n    plt.figure(figsize=(12, 8))\n    G = nx.Graph()\n    # Add nodes and edges\n    for record in results:\n        product_name = record['Product']\n        manufacturer_name = record['Manufacturer']\n        G.add_node(product_name, label=product_name[:30], type='Product')\n        G.add_node(manufacturer_name, label=manufacturer_name, type='Manufacturer')\n        G.add_edge(product_name, manufacturer_name)\n    # Draw graph\n    pos = nx.spring_layout(G)\n    nx.draw_networkx_nodes(G, pos, nodelist=[n for n, attr in G.nodes(data=True) if attr['type'] == 'Product'],\n                           node_color='lightblue', node_size=500, label='Products')\n    nx.draw_networkx_nodes(G, pos, nodelist=[n for n, attr in G.nodes(data=True) if attr['type'] == 'Manufacturer'],\n                           node_color='lightgreen', node_size=700, label='Manufacturers')\n    nx.draw_networkx_edges(G, pos)\n    nx.draw_networkx_labels(G, pos)\n    plt.title(title)\n    plt.legend()\n    plt.axis('off')\n    plt.show()\n\n# Find most expensive products\nquery1 = \"\"\"\nMATCH (p:Product)-[:MANUFACTURED_BY]->(m:Manufacturer)\nRETURN m.name as Manufacturer, p.name as Product, p.price as Price\nORDER BY p.price DESC\nLIMIT 5\n\"\"\"\nrun_query_with_viz(query1, \"Most Expensive Products\")\n```\n\nYou may notice that we are creating another function `run_query_with_viz` . We aren't doing this just for the sake of creating functions, but because this will let us both run a query against the database and create a helper function to plot the results. However, you can also run the query in Neo4j and get an even better visualization there.\nIn the function we run a Cypher query and displays the results in a pandas DataFrame. We handle the visualization side using NetworkX and Matplotlib, showing the relationships between products and manufacturers.\nThen, we call the function passing a query to retrieve the top 5 most expensive products along with their manufacturers and prices. This will return:\ntext\n\n```\n=== Most Expensive Products ===\nManufacturer Product Price\n0 DJI DJI Phantom 2 with H3-3D Gimbal 995.11\n1 Sideshow Indiana Jones - 12 Inch Action Figures: Indian... 719.95\n2 AUTOart Autoart 70206 - Aston Martin V12 Vantage - 201... 648.95\n3 Bushiroad Weiss Schwarz Extra Booster Clannad Vol.3 629.95\n4 Dragon Panzer II - Kpfw - Ausf.C - DX'10 - 1:6th Scale 592.95\nNumber of visualization records: 10\n```\n\n![Most expensive product visualization - Image by Author](https://assets.insightmediagroup.io/media/wp-content/uploads/2024/11/10qmLnUjudxnzkmsRk3YkGg.png)\nMost expensive product visualization - Image by Author\nIn this case, the output DataFrame displays the manufacturers, products, and prices of the top 5 most expensive products. The visualization is a graph where product nodes are connected to manufacturer nodes, with different colors representing different types of nodes.\n### Step 5: Generate and Store Embeddings\nWe generate embeddings for the product descriptions to enable semantic search. For this part you will need to get an API Key for Google AI Studio. Don't worry it's completely free, and it will take you just a couple of minutes to get one:\n> [**Get a Gemini API key | Google AI for Developers**](https://ai.google.dev/gemini-api/docs/api-key)\npython\n\n```\n# Configure the embedding API (replace with your actual API key)\nos.environ[\"GOOGLE_API_KEY\"] = \"your_api_key_here\"\ngenai.configure(api_key=os.getenv(\"GOOGLE_API_KEY\"))\n\n# Test the embedding API\nresult = genai.embed_content(\n    model=\"models/text-embedding-004\",\n    content=\"What is the meaning of life?\",\n    task_type=\"retrieval_document\",\n    title=\"Embedding of single string\"\n)\n# Print a portion of the embedding vector\nprint(str(result['embedding'])[:50], '... TRIMMED]')\n```\n\nWe set up the embedding API by configuring the `genai` library with your API key (replace `\"your_api_key_here\"` with your actual key). We test the embedding API by generating an embedding for a sample text and printing a portion of the embedding vector to verify that it works.\nbash\n\n```\n[-0.02854543, 0.044588115, -0.034197364, -0.004266 ... TRIMMED]\n```\n\nThis output shows the beginning of the embedding vector for the sample text. The actual length of the vector is 768, as this is the dimensionality of _text-embedding-004._ However, note that different embedding algorithms may have different numbers of dimensions. As a result, they may provide different results.\nNext, we define functions to generate embeddings for product descriptions and store them in the knowledge graph.\npython\n\n```\n# Rate limiter decorator\n@sleep_and_retry\n@limits(calls=1500, period=60)\ndef get_embedding(text):\n    try:\n        result = genai.embed_content(\n            model=\"models/text-embedding-004\",\n            content=text,\n            task_type=\"retrieval_document\",\n        )\n        return result['embedding']\n    except Exception as e:\n        print(f\"Error getting embedding: {e}\")\n        return None\n\ndef add_embeddings_to_products(batch_size=50):\n    # Get the total number of products to process\n    total_query = \"\"\"\n    MATCH (p:Product)\n    WHERE p.description_embedding IS NULL\n      AND p.description IS NOT NULL\n    RETURN count(p) AS total\n    \"\"\"\n    total_result = graph.run(total_query).data()\n    total_to_process = total_result[0]['total'] if total_result else 0\n    print(f\"Total products to process: {total_to_process}n\")\n    total_processed = 0\n    # Initialize tqdm progress bar\n    with tqdm(total=total_to_process, desc='Processing products', unit='product') as pbar:\n        while True:\n            # Get batch of products\n            query = \"\"\"\n            MATCH (p:Product)\n            WHERE p.description_embedding IS NULL\n              AND p.description IS NOT NULL\n            RETURN p.uniq_id AS id, p.description AS description\n            LIMIT $batch_size\n            \"\"\"\n            products = graph.run(query, parameters={'batch_size': batch_size}).data()\n            if not products:\n                break\n            # Process each product in the batch\n            for product in products:\n                try:\n                    if product['description']:\n                        embedding = get_embedding(product['description'])\n                        if embedding:\n                            # Update product with embedding\n                            graph.run(\"\"\"\n                            MATCH (p:Product {uniq_id: $id})\n                            SET p.description_embedding = $embedding\n                            \"\"\", parameters={\n                                'id': product['id'],\n                                'embedding': embedding\n                            })\n                    total_processed += 1\n                    pbar.update(1)  # Update the progress bar\n                except Exception as e:\n                    print(f\"Error processing product {product['id']}: {e}\")\n            # Add a small delay between batches\n            time.sleep(1)\n    print(f\"nTotal products processed: {total_processed}\")\n    return total_processed\n# Add embeddings to products\nprint(\"Adding embeddings to products...n\")\ntotal_processed = add_embeddings_to_products()\nprint(f\"nProcess completed. Total products processed: {total_processed}\")\n```\n\n`get_embedding` retrieves the embedding for a given text while respecting API rate limits using the `ratelimit` library.\nThe `add_embeddings_to_products` function:\n  * Retrieves the total number of products that need embeddings.\n  * Processes products in batches, generating embeddings for their descriptions.\n  * Updates the `Product` nodes in the graph with the new embeddings.\n  * Uses a progress bar to display the processing status.\n  * Adds a small delay between batches to comply with API rate limits.\n\n\nbash\n\n```\nAdding embeddings to products...\n\nTotal products to process: 7434\nProcessing products:   0%|          | 0/7434 [00:00<?, ?product/s]\nProcessing products: 100%|██████████| 7434/7434 [27:20<00:00, 4.53product/s]\nTotal products processed: 7434\nProcess completed. Total products processed: 7434\n```\n\nThe output shows that all products have been processed and embeddings have been added.\n### Step 6: Verify Embeddings\nWe check how many products now have embeddings.\npython\n\n```\n# Verify embeddings\nprint(\"nVerifying embeddings:\")\nresult = graph.run(\"\"\"\nMATCH (p:Product)\nWHERE p.description_embedding IS NOT NULL\nRETURN count(p) as count\n\"\"\").data()\nprint(f\"Products with embeddings: {result[0]['count']}\")\n```\n\nThis Cypher query counts the number of `Product` nodes where `description_embedding` is not null. We print the count to verify that embeddings have been successfully added to the products.\nbash\n\n```\nVerifying embeddings:\nProducts with embeddings: 7434\n```\n\nThis confirms that all products now have embeddings.\n### Step 7: Perform Semantic Search\nWe use the embeddings to perform a semantic search based on a user's natural language query.\npython\n\n```\ndef semantic_search(query_text, n=5):\n    # Get query embedding\n    query_embedding = get_embedding(query_text)\n    if not query_embedding:\n        print(\"Failed to get query embedding\")\n        return []\n\n    # Search for similar products using dot product and magnitude for cosine similarity\n    results = graph.run(\"\"\"\n    MATCH (p:Product)\n    WHERE p.description_embedding IS NOT NULL\n    WITH p,\n    reduce(dot = 0.0, i in range(0, size(p.description_embedding)-1) |\n    dot + p.description_embedding[i] * $embedding[i]) /\n    (sqrt(reduce(a = 0.0, i in range(0, size(p.description_embedding)-1) |\n    a + p.description_embedding[i] * p.description_embedding[i])) *\n    sqrt(reduce(b = 0.0, i in range(0, size($embedding)-1) |\n    b + $embedding[i] * $embedding[i])))\n    AS similarity\n    WHERE similarity > 0\n    RETURN\n    p.name as name,\n    p.description as description,\n    p.price as price,\n    similarity as score\n    ORDER BY similarity DESC\n    LIMIT $n\n    \"\"\", parameters={'embedding': query_embedding, 'n': n}).data()\n    return results\n\n# Test the search with debug info\nprint(\"nTesting semantic search:\")\nresults = semantic_search(\"Give me a set of cards\", n=2)\nprint(f\"nNumber of results: {len(results)}\")\nfor r in results:\n    print(f\"nProduct: {r.get('name', 'No name')}\")\n    print(f\"Price: ${r.get('price', 'N/A')}\")\n    print(f\"Score: {r.get('score', 'N/A'):.3f}\")\n    desc = r.get('description', 'No description')\n    print(f\"Description: {desc}\")\n```\n\nThe function `semantic_search` generates an embedding for the user's query. It uses a Cypher query to compute the cosine similarity between the query embedding and each product's description embedding. Since Neo4j's standard library may not have a built-in cosine similarity function, we calculate it manually using the dot product and magnitudes. Then, it filters products with a positive similarity score and returns the top `n` results sorted by similarity.\nWe test the function with the query \"Give me a set of cards\" and print out the results, including the product name, price, similarity score, and description.\nbash\n\n```\nTesting semantic search:\n\nNumber of results: 2\nProduct: Yu-Gi-Oh Metal Raiders Booster\nPrice: $9.76\nScore: 0.852\nDescription: 9 Cards Per Pack.\nProduct: AKB48 Trading Card Game &amp; Collection vol.1 Booster (15packs)\nPrice: $12.25\nScore: 0.827\nDescription: 15 packs, 6 cards per pack.\n```\n\nThis output shows that the semantic search successfully retrieved products related to card sets, even if the exact wording differs from the query.\n## Conclusion\nIn this exercise, we have loaded and preprocessed a dataset of Amazon toy products. Next, we created a knowledge graph by adding nodes for products, manufacturers, and categories, and establishing relationships between them. For each node, we generated embeddings for product descriptions and stored them in the knowledge graph. Finally, we performed semantic searches using embeddings to find products matching natural language queries.\nIn this application, I showed a basic implementation of Graph RAG using cosine similarity. With its simplicity comes its limitations. This may not be something you want to use for your business. Instead, considering using a more advanced approach leveraging the PageRank algorithm or other retrieval functions. Or consider using an easy to use framework like Llama Index or Langchain which provide seamless integrations with the Embedding Model and Neo4j. More articles to come on this!\n## Register Now to Continue Reading\n### Unlock Towards Data Science with a Free Account\nAll articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more.\nRegister for freeSign in to your account\n* * *\n![](https://assets.insightmediagroup.io/media/1787657505387_wlyc0o.jpeg)\nWritten By\n## [Cristian Leo](https://towardsdatascience.com/author/cristianleo120/)\n[See all from Cristian Leo](https://towardsdatascience.com/author/cristianleo120/)\n[Data Science](https://towardsdatascience.com/tag/data-science/)[Hands On Tutorials](https://towardsdatascience.com/tag/hands-on-tutorials/)[Knowledge Graph](https://towardsdatascience.com/tag/knowledge-graph/)[Llm](https://towardsdatascience.com/tag/llm/)[Retrieval Augmented Gen](https://towardsdatascience.com/tag/retrieval-augmented-gen/)\nShare This Article\n  * [](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F&title=How%20to%20Query%20a%20Knowledge%20Graph%20with%20LLMs%20Using%20gRAG)\n  * [](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F&title=How%20to%20Query%20a%20Knowledge%20Graph%20with%20LLMs%20Using%20gRAG)\n  * [](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Fhow-to-query-a-knowledge-graph-with-llms-using-grag-38bfac47a322%2F&text=How%20to%20Query%20a%20Knowledge%20Graph%20with%20LLMs%20Using%20gRAG)\n\n\nTowards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program.\n[Write for TDS](https://towardsdatascience.com/submissions/)\n## Related Articles\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/01/1vPPmTRQTLkBZx-XLn_RYkQ.png)](https://towardsdatascience.com/stepping-on-everyones-toes-for-everyone-s-benefit-626a0c2664b5/)\n## [Stepping on Everyone's Toes for Everyone's Benefit](https://towardsdatascience.com/stepping-on-everyones-toes-for-everyone-s-benefit-626a0c2664b5/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nThe Role of Data Science Departments in 21st Century Organizations\n[Jason Nett](https://towardsdatascience.com/author/jasonnett/)\nJanuary 8, 202117 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/08/0cOEnBC1EBjJOCTNj-scaled.jpg)](https://towardsdatascience.com/machine-learning-streaming-with-kafka-debezium-and-bentoml-c5f3996afe8f/)\n## [Machine Learning Streaming with Kafka, Debezium, and BentoML](https://towardsdatascience.com/machine-learning-streaming-with-kafka-debezium-and-bentoml-c5f3996afe8f/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nCreating a real-time price recommender system using modern data-related tools\n[João Pedro](https://towardsdatascience.com/author/joaopedro214/)\nAugust 22, 202211 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/11/1IyxoMmtImWikZkzpNuPkJg.png)](https://towardsdatascience.com/fluorescent-neuronal-cells-dataset-part-ii-e1ac27e26d7/)\n## [Fluorescent Neuronal Cells dataset - part II](https://towardsdatascience.com/fluorescent-neuronal-cells-dataset-part-ii-e1ac27e26d7/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nPeculiar traits and challenges\n[Luca Clissa](https://towardsdatascience.com/author/luca.clissa/)\nNovember 30, 20225 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2023/06/1UDSd0kq8-JhpjYwMVfuqkg-scaled.jpeg)](https://towardsdatascience.com/chain-of-thought-prompting-facilitate-llms-reasoning-abilities-313cd7714938/)\n## [Chain of Thought Prompting Facilitate LLMs Reasoning Abilities](https://towardsdatascience.com/chain-of-thought-prompting-facilitate-llms-reasoning-abilities-313cd7714938/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\nDemonstrated with examples\n[Soner Yıldırım](https://towardsdatascience.com/author/sonery/)\nJune 12, 20237 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/04/1_XxoPiORp13EASAvrL7VQQ.jpeg)](https://towardsdatascience.com/a-day-in-the-life-of-a-data-engineer-d65293272121/)\n## [A day in the life of a data engineer](https://towardsdatascience.com/a-day-in-the-life-of-a-data-engineer-d65293272121/)\n[Coding](https://towardsdatascience.com/category/programming/coding/)\nBreaking down the main activities of a data engineer in 2021\n[mehdio](https://towardsdatascience.com/author/mehdio/)\nApril 20, 20215 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2022/08/0UDAtYOTEL_y4LlAW-scaled.jpg)](https://towardsdatascience.com/imputation-of-missing-data-in-tables-with-datawig-2d7ab327ece2/)\n## [Imputation of Missing Data in Tables with DataWig](https://towardsdatascience.com/imputation-of-missing-data-in-tables-with-datawig-2d7ab327ece2/)\n[Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/)\nImplementing Amazon's DataWig in Python to impute missing values in tabular data\n[Kenneth Leung](https://towardsdatascience.com/author/kennethleungty/)\nAugust 24, 20222 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2019/03/1Xtsxt66P34yDlhKeFHBSXA.jpeg)](https://towardsdatascience.com/from-business-intelligence-to-data-science-94a1fd866231/)\n## [From Business Intelligence to Data Science](https://towardsdatascience.com/from-business-intelligence-to-data-science-94a1fd866231/)\n[Deep Learning](https://towardsdatascience.com/category/artificial-intelligence/deep-learning/)\nI'm a Manager of Data Science & Machine Learning, specialising in design & building of solutions powered by DS & ML for enterprises for...\n[Raj](https://towardsdatascience.com/author/rajkumar.kaliyaperumal/)\nMarch 17, 20196 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2026/06/00_cover_burning_dollar-scaled-1.jpg)](https://towardsdatascience.com/your-churn-threshold-is-a-pricing-decision/)\n## [Your Churn Threshold Is a Pricing Decision](https://towardsdatascience.com/your-churn-threshold-is-a-pricing-decision/)\n[Data Science](https://towardsdatascience.com/category/data-science/)\nHow unit economics should set your classification cutoff, and why they rarely do.\n[Fabio Oliveira](https://towardsdatascience.com/author/fabio.oliveira/)\nJune 17, 202614 min read\n  * [![](https://assets.insightmediagroup.io/media/wp-content/uploads/2021/11/1mwV8cgwAYS0HttDy-Iph1w.jpeg)](https://towardsdatascience.com/8-methods-for-handling-missing-values-with-python-pandas-842544cdf891/)\n## [8 Methods For Handling Missing Values With Python Pandas](https://towardsdatascience.com/8-methods-for-handling-missing-values-with-python-pandas-842544cdf891/)\n[Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/)\n#7: Using the previous or next value\n[Soner Yıldırım](https://towardsdatascience.com/author/sonery/)\nNovember 11, 20216 min read\n\n\n  * [YouTube](https://www.youtube.com/c/TowardsDataScience)\n  * [X](https://x.com/TDataScience)\n  * [LinkedIn](https://www.linkedin.com/company/towards-data-science/)\n  * [Threads](https://www.threads.net/@towardsdatascience)\n  * [Bluesky](https://bsky.app/profile/towardsdatascience.com)\n\n\n[](https://towardsdatascience.com/)\nYour home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.\nSubscribe to Our Newsletter[Write For TDS](https://towardsdatascience.com/submissions/)•[About](https://towardsdatascience.com/about-towards-data-science-d691af11cc2f/)•[Advertise](https://contact.towardsdatascience.com/advertise-with-towards-data-science)•[Privacy Policy](https://towardsdatascience.com/privacy-policy/)•[Terms of Use](https://towardsdatascience.com/website-terms-of-use/)\n© Insight Media Group, LLC 2026•[The New Stack](https://thenewstack.io)•[roadmap.sh](https://roadmap.sh)•[Creator Network](https://insightmediagroup.io/creator-network)\n####  Recommended Articles \nClose\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607686/image?etag=1782161464448&width=500&height=500) How Relevance Models Foreshadowed Transformers for NLP ](https://towardsdatascience.com/how-relevance-models-foreshadowed-transformers-for-nlp/ \"How Relevance Models Foreshadowed Transformers for NLP\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607639/image?etag=1781250125848&width=500&height=500) Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1 ](https://towardsdatascience.com/javascript-fatigue-you-dont-need-js-to-build-chatgpt/ \"Javascript Fatigue: HTMX Is All You Need to Build ChatGPT — Part 1\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/607930/image?etag=1782124566054&width=500&height=500) The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs ](https://towardsdatascience.com/subset-sum-problem-solved-in-linear-time-for-dense-enough-inputs/ \"The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs\")\n  * [ ![](https://h030.towardsdatascience.com/rest/v2/contentStores/d7d3a4ab-7caf-4383-bbd2-fbf7fb4f1787/items/3660/image?etag=1780176246070&width=500&height=500) Does Your Company Have a Data Strategy? ](https://towardsdatascience.com/does-your-company-have-a-data-strategy-e8944dbec4ae/ \"Does Your Company Have a Data Strategy?\")\n\n\n",
  "body_base_path": "attachments/a6e5a94070f2/pages/",
  "body_markdown_path": "attachments/a6e5a94070f2/pages/archived-body.md",
  "local_html": "attachments/a6e5a94070f2/pages/archived-body.html",
  "body_source": "prior_capture"
}