[Wikidata](https://www.wikidata.org/wiki/Wikidata:Main_Page) is a free and open knowledge base and central storage for structured data from Wikimedia sister projects including Wikipedia. Each entity is referenced by its `qid` and each property by its `pid`. Wikidata can be queried with [[SPARQL]] or with a [search API](https://www.wikidata.org/w/api.php) that better handles fuzzy matching. See [[how knowledge graphs integrate with LLMs]] for a full walkthrough. To query with SPARQL and [[Python]] ```python # Set up SPARQL endpoints sparql = SPARQLWrapper("https://query.wikidata.org/sparql") sparql.setReturnFormat(JSON) sparql.addCustomHttpHeader("User-Agent", "WorldBankKGBot/1.0 (<your_email>@domain.com)") def query_via_sparql(query: str) -> str: """Runs any SPARQL query.""" sparql.setQuery(query) results = sparql.query().convert() bindings = results['results']['bindings'] if bindings: first_binding = bindings[0] # Get the first variable name from the binding var_name = next(iter(first_binding)) return first_binding[var_name]['value'] return None ``` To use the search API with [[Python]] ```python import requests def query_via_fuzzy_search(label: str) -> str: """Tries to find the QID via the Wikidata search API (fuzzy match).""" search_url = "https://www.wikidata.org/w/api.php" params = { "action": "wbsearchentities", "search": label, "language": "en", "format": "json", "type": "item" } response = requests.get(search_url, params=params) response.raise_for_status() search_results = response.json() if search_results['search']: qid = search_results['search'][0]['id'] return f"http://www.wikidata.org/entity/{qid}" return None ```