---
title: "Q&A with Wikipedia 📚"
description: "Using code blocks to query Wikipedia"
input:
- name: question
type: string
description: "The question to answer"
---
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 %}
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" /%}
`\`\`\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() };
}
`\`\`\
## 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 %}