How to use Wikipedia tool in LangChain
Quick answer
Use the
Wikipedia loader from langchain.document_loaders to fetch Wikipedia articles, then integrate it with LangChain chains or vectorstores for querying. This enables retrieval of Wikipedia content for downstream tasks using OpenAI or other LLMs.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install langchain openai wikipedia
Setup
Install the required packages and set your OpenAI API key as an environment variable.
pip install langchain openai wikipedia
# Set environment variable in your shell
# export OPENAI_API_KEY=os.environ["OPENAI_API_KEY"] (Linux/macOS)
# setx OPENAI_API_KEY os.environ["OPENAI_API_KEY"] (Windows) Step by step
This example shows how to load a Wikipedia page using LangChain's Wikipedia loader and then query it with an OpenAI chat model.
import os
from langchain_community.document_loaders import Wikipedia
from langchain_openai import ChatOpenAI
from langchain.chains.question_answering import load_qa_chain
# Load Wikipedia page content
wiki_loader = Wikipedia()
docs = wiki_loader.load("Python (programming language)")
# Initialize OpenAI chat model
chat = ChatOpenAI(model_name="gpt-4o", temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])
# Load a question answering chain
qa_chain = load_qa_chain(chat, chain_type="stuff")
# Ask a question about the loaded Wikipedia content
question = "What is Python used for?"
answer = qa_chain.run(input_documents=docs, question=question)
print(answer) output
Python is a versatile programming language used for web development, data analysis, artificial intelligence, scientific computing, automation, and more.
Common variations
- Use different Wikipedia page titles by changing the argument to
wiki_loader.load(). - Use
ChatOpenAIwith different models likegpt-4o-minifor faster, cheaper responses. - Combine Wikipedia loader with vectorstores like FAISS for semantic search over multiple articles.
- Use async versions of LangChain components for concurrency.
Troubleshooting
- If you get
ModuleNotFoundError, ensurelangchainandwikipediapackages are installed. - If Wikipedia page not found, verify the page title spelling or try a different title.
- For API key errors, confirm
OPENAI_API_KEYis set correctly in your environment.
Key Takeaways
- Use LangChain's
Wikipedialoader to fetch article content easily. - Combine Wikipedia content with OpenAI chat models for question answering.
- Adjust model and chain types for cost, speed, and accuracy trade-offs.