• unwind ai
  • Posts
  • Build a Lite, Self-hosted Perplexity AI Clone using GPT-4o

Build a Lite, Self-hosted Perplexity AI Clone using GPT-4o

AI Search Assistant with GPT-4o in just 15 lines of Python Code (step-by-step instructions)

Ever wondered how Perplexity AI manages to combine web search and LLM intelligence to deliver precise answers? Now you can create your own lite, self-hosted version of Perplexity AI!

In this tutorial, we’ll guide you through building a Generative AI Search Assistant using Streamlit. This app integrates DuckDuckGo's web search with OpenAI's GPT-4o to get accurate and concise responses for any query—completely under your control.

Why Build a Self-hosted AI Search Assistant?
Instead of relying on external platforms like Perplexity, this project gives you:

  • Full control over queries and responses.

  • Tailor the assistant's behavior as per your needs.

  • Choose when and how to run it, with minimal setup.

Don’t forget to share this tutorial on your social channels and tag Unwind AI (X, LinkedIn, Threads, Facebook) to support us!

What We’re Building

This Streamlit app combines the power of search engines and LLMs to provide you with pinpointed answers to your queries. By leveraging OpenAI's GPT-4o and the DuckDuckGo search engine, this AI search assistant delivers accurate and concise responses to your questions.

Features

  • Get pinpointed answers to your queries

  • Utilize DuckDuckGo search engine for web searching

  • Use OpenAI GPT-4o for intelligent answer generation

Prerequisites

Before we begin, make sure you have:

  1. Python installed on your machine (version 3.7 or higher is recommended)

  2. Your OpenAI API Key

  3. Basic familiarity with Python programming

  4. A code editor of your choice (we recommend VS Code or PyCharm for their excellent Python support)

Step-by-Step Instructions

Setting Up the Environment

First, let's get our development environment ready:

  1. Clone the GitHub repository:

git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
  1. Go to the web_search_ai_assistant folder:

cd advanced_tools_frameworks/web_search_ai_assistant
pip install -r requirements.txt
  1. Get your API Keys: Sign up for an OpenAI account to obtain your API key.

Creating the Streamlit App

Let’s create our app. Create a new file gpt4_websearch.py and add the following code:

  1. Import necessary libraries:

    • Streamlit for building the web app

    • Phidata for building AI agents

    • OpenAI for using the LLM

    • Duckduckgo for the search functionality

import streamlit as st
from phi.assistant import Assistant
from phi.tools.duckduckgo import DuckDuckGo
from phi.llm.openai import OpenAIChat
  1. Set up the Streamlit App:

    Streamlit lets you create user interface with just python code, for this app we will:

    • Add a title to the app using 'st.title()'

    • Add a description for the app using 'st.caption()'

st.title("AI Web Search Assistant 🤖")
st.caption("This app allows you to search the web using GPT-4o")
  1. Create and Initialize the AI assistant: 

    • Create a text input to enter their OpenAI API key using 'st.text_input()'

    • If the OpenAI API key is provided, create an instance of Assistant with gpt-4 as LLM and DuckDuckGo as the tool.

# Get OpenAI API key from user
openai_access_token = st.text_input("OpenAI API Key", type="password")

# If OpenAI API key is provided, create an instance of Assistant
if openai_access_token:
    # Create an instance of the Assistant
    assistant = Assistant(
    llm=OpenAIChat(
        model="gpt-4o",
        max_tokens=1024,
        temperature=0.9,
        api_key=openai_access_token) , tools=[DuckDuckGo()], show_tool_calls=True
    )
  1. Search the Web with your Generative AI assistant:

    • Create a text input for the user to enter their query using 'st.text_input()'

    • If a question is asked, run the assistant to get the answer and display it using 'st.write()'

    query= st.text_input("Enter the Search Query", type="default")
    
    if query:
        # Search the web using the AI Assistant
        response = assistant.run(query, stream=False)
        st.write(response)

How the Code Works

  1. Upon running the app, you will be prompted to enter your OpenAI API key. This key is used to authenticate and access the OpenAI language models.

  2. Once you provide a valid API key, an instance of the Assistant class is created. This assistant utilizes the GPT-4o language model from OpenAI and the DuckDuckGo search engine tool.

  3. Enter your search query in the provided text input field.

  4. The assistant will perform the following steps:

    • Conduct a web search using DuckDuckGo based on your query

    • Analyze the search results and extract relevant information

    • Generate a concise and targeted answer using the GPT-4o language model

  5. The pinpointed answer will be displayed in the app, providing you with the information you need.

Running the App

With our code in place, it's time to launch the app.

  • In your terminal, navigate to the project folder, and run the following command

streamlit run gpt4_websearch.py
  • Streamlit will provide a local URL (typically http://localhost:8501). Open this in your web browser, put in your API keys, start asking questions, and watch it magically switch between LLMs!

Working Application Demo

Conclusion

And just like that, you’ve created your own lite, self-hosted version of Perplexity AI! This assistant combines the intelligence of LLMs with web search capabilities to provide accurate answers, tailored to your queries.

With this foundation in place, you can:

  • Customize the Assistant: Modify the code to fit your needs.

  • Explore New Tools: Add more search engines or APIs.

  • Improve Performance: Adjust temperature and token limits for different query types.

Keep experimenting and refining to build even smarter AI solutions!

We share hands-on tutorials like this 2-3 times a week, to help you stay ahead in the world of AI. If you're serious about levelling up your AI skills and staying ahead of the curve, subscribe now and be the first to access our latest tutorials.

Don’t forget to share this tutorial on your social channels and tag Unwind AI (X, LinkedIn, Threads, Facebook) to support us!

Reply

or to participate.