Skip to main content

Q&A with Wikipedia 📚

---
title: "Q&A with Wikipedia 📚"
description: "Using code blocks to query Wikipedia"
input:
- name: question
type: string
description: "The question to answer"
---

<!-- The code block is connected to the web so it can be used to hit external APIs too. This can be very useful in a number of ways. Here we show how it can be used to query for up-to-date information from Wikipedia. -->

Retrieval assisted (RAG) question answering using Wikipedia.
e.g. Try asking "When did OceanGate sink?", "How tall is the tallest penguin species?"

Question: {% $frontmatter.input.question %}

<!-- First we get the language model to generate a search term -->

Let's search Wikipedia to answer this question. What's the best search term to use? Output just the search term.

{% ai #search_term model="openai/gpt-4o-mini" /%}

<!-- This chunk of code will hit Wikipedia's API and extract the top 3 articles -->


`\`\`\js {% #wikipedia_results %}

// Read the term generated by the language model
let input;
try {
input = aimVariables.search_term.result.trim().replace(/^"+|"+$/g, '');
} catch (error) {
console.error("Error reading search term:", error);
return { result: "", error: error.toString() };
}

console.log("Searching for the term", input);

try {
const maxResults = 3;
const searchTerm = encodeURIComponent(input.trim());
const url = `https://en.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch=${searchTerm}&srlimit=${maxResults}&utf8=&origin=*`;
const response = await fetch(url);
const data = await response.json();

const fetchExtract = async (title) => {
console.log("Retrieving", title);
const extractUrl = `https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&titles=${encodeURIComponent(title)}&redirects=1&origin=*`;
const extractResponse = await fetch(extractUrl);
const extractData = await extractResponse.json();
const pageId = Object.keys(extractData.query.pages)[0];
const extract = extractData.query.pages[pageId].extract;
return [title, extract];
}

console.log("data", data);

try {
if (data?.query?.search?.length > 0) {
console.log("Got some results extracting top", data?.query?.search?.length);
const extracts = await Promise.all(data?.query?.search?.map(result => fetchExtract(result.title)));
return { result: extracts, error: null };
} else {
return { result: "No results found.", error: null };
}
} catch (error) {
console.error("Error extracting Wikipedia results:", error);
return { result: "", error: error.toString() };
}
} catch (error) {
console.error("Error processing Wikipedia results:", error);
return { result: "", error: error.toString() };
}

`\`\`\
<!-- The code returns the top 3 Wikipedia articles that match the search term -->

## Answer:

Based on the information above give the most helpful answer to the question.


Results: {% debug($wikipedia_results) %}

Question: {% $frontmatter.input.question %}
{% ai #answer model="openai/gpt-4o-mini" /%}

{% $answer.result %}