Build an AI Assistant with Wolfram Alpha and Wikipedia in Python

Alessio Frateily

Hatched by Alessio Frateily

May 12, 2024

4 min read


Build an AI Assistant with Wolfram Alpha and Wikipedia in Python

In today's fast-paced world, technology plays a crucial role in enhancing our daily lives. One such technology that has gained immense popularity is artificial intelligence (AI). AI assistants have become an integral part of our lives, helping us with various tasks and providing us with valuable information. In this article, we will explore how to build an AI assistant using Wolfram Alpha and Wikipedia in Python.

Wolfram Alpha is a computational search engine that specializes in answering factual queries and performing calculations. It uses a vast amount of curated data to provide accurate and relevant information to users. On the other hand, Wikipedia is a free online encyclopedia that contains a massive amount of information on a wide range of topics.

To build our AI assistant, we will leverage the capabilities of both Wolfram Alpha and Wikipedia. The user's query will be passed to Wolfram Alpha for processing. If Wolfram Alpha returns a result, it will be presented to the user. If no result is obtained, the user's query will be used as keywords to search Wikipedia for relevant articles.

By combining the power of Wolfram Alpha and Wikipedia, we can create an AI assistant that can provide users with accurate and comprehensive information on a wide range of topics. Whether it's solving complex mathematical equations or looking up historical facts, our AI assistant will be able to assist users in their quest for knowledge.

Now, let's dive into the technical details of building our AI assistant. We will be using the Python programming language for this project. Python provides a wide range of libraries and tools that make it easy to interact with APIs and process textual data.

First, we need to install the necessary libraries. We will be using the WolframAlpha and Wikipedia-API libraries. You can install them using pip:


pip install wolframalpha wikipedia-api


Next, we need to import the libraries and set up our API keys. To use the Wolfram Alpha API, you need to sign up for an API key on the Wolfram Alpha Developer Portal. Once you have the API key, you can set it up in your code:


import wolframalpha

import wikipediaapi

# Set up Wolfram Alpha API

app_id = 'YOUR_APP_ID'

client = wolframalpha.Client(app_id)

# Set up Wikipedia API

wiki = wikipediaapi.Wikipedia('en')


Now that we have set up our APIs, we can define a function that takes a user's query as input and returns the result from Wolfram Alpha or Wikipedia:


def get_answer(query):

# Query Wolfram Alpha

res = client.query(query)

# Check if Wolfram Alpha returned a result

if res['@success'] == 'true':

# Extract the result from the response

answer = next(res.results).text

return answer

# If no result from Wolfram Alpha, search Wikipedia

page =

if page.exists():

return page.summary

# If no result from Wikipedia, return None

return None


With this function in place, we can now interact with our AI assistant. We can prompt the user to enter a query and pass it to the `get_answer` function:


while True:

query = input("Enter your query: ")

answer = get_answer(query)

if answer:



print("Sorry, I couldn't find an answer to your query.")


By running this code, the AI assistant will continuously prompt the user for queries and provide answers based on the information retrieved from Wolfram Alpha and Wikipedia.

In conclusion, building an AI assistant with Wolfram Alpha and Wikipedia in Python can greatly enhance the user experience and provide valuable information on a wide range of topics. By leveraging the power of these two platforms, we can create an AI assistant that is capable of answering complex queries and providing accurate and comprehensive information. So why not give it a try and build your own AI assistant today?

Actionable Advice:

  • 1. Experiment with different API calls and explore the capabilities of Wolfram Alpha and Wikipedia to maximize the accuracy and relevance of the information provided by your AI assistant.
  • 2. Implement a natural language processing (NLP) component to enhance the user experience by allowing more conversational queries and responses.
  • 3. Continuously update and refine your AI assistant's knowledge base by incorporating new data and information from reliable sources to ensure the most up-to-date and accurate responses.

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)