I replaced Google Search with DuckDuckGo and Perplexity - my results were noticeably better
Back to Tutorials
techTutorialintermediate

I replaced Google Search with DuckDuckGo and Perplexity - my results were noticeably better

June 23, 202614 views5 min read

Learn to build a hybrid search system using DuckDuckGo and Perplexity APIs to find more reliable, fact-based information than traditional Google search.

Introduction

In today's information landscape, traditional search engines like Google have become cluttered with AI-generated content that often lacks depth and reliability. This tutorial will guide you through setting up and using DuckDuckGo and Perplexity as a powerful search duo to find more accurate, well-sourced information. We'll focus on practical implementation using their APIs and browser extensions to create a more effective research workflow.

Prerequisites

  • Basic understanding of web APIs and HTTP requests
  • Python 3.7+ installed on your system
  • Access to a code editor (VS Code, PyCharm, or similar)
  • API keys for Perplexity (free tier available)
  • Browser with extension support (Chrome, Firefox, or Edge)

Step-by-Step Instructions

1. Setting Up Your Development Environment

First, we need to create a Python environment with the necessary libraries. This will allow us to programmatically interact with both search services.

mkdir search_automation
 cd search_automation
 python -m venv search_env
 source search_env/bin/activate  # On Windows: search_env\Scripts\activate
 pip install requests python-dotenv

Why: Creating a virtual environment isolates our project dependencies and prevents conflicts with system-wide packages. The requests library will handle HTTP communication with the APIs, while python-dotenv helps manage API keys securely.

2. Creating Environment Variables for API Keys

Create a .env file in your project directory to store your Perplexity API key securely:

PERPLEXITY_API_KEY=your_actual_api_key_here
DUCKDUCKGO_API_KEY=your_duckduckgo_api_key

Why: Storing API keys in environment variables keeps them out of your source code, preventing accidental exposure in version control systems or code repositories.

3. Implementing DuckDuckGo Search Integration

Create a Python script called duckduckgo_search.py to interface with DuckDuckGo's API:

import requests
import os
from dotenv import load_dotenv

load_dotenv()

class DuckDuckGoSearch:
    def __init__(self):
        self.base_url = "https://api.duckduckgo.com/"
        self.api_key = os.getenv('DUCKDUCKGO_API_KEY')

    def search(self, query, max_results=10):
        params = {
            'q': query,
            'format': 'json',
            'no_html': '1',
            'skip_disambig': '1',
            'limit': max_results
        }
        
        response = requests.get(self.base_url, params=params)
        
        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(f"DuckDuckGo API error: {response.status_code}")

# Example usage
if __name__ == "__main__":
    ddg = DuckDuckGoSearch()
    results = ddg.search("machine learning applications")
    for result in results['RelatedTopics'][:5]:
        print(f"Title: {result['Text']}")
        print(f"URL: {result['FirstURL']}")
        print("---")

Why: DuckDuckGo's API provides clean, structured results without the AI-generated noise that plagues Google search. The API returns results in JSON format, making it easy to parse and use in automated workflows.

4. Setting Up Perplexity API Integration

Create a PerplexitySearch class in a separate file called perplexity_search.py:

import requests
import os
from dotenv import load_dotenv

load_dotenv()

class PerplexitySearch:
    def __init__(self):
        self.base_url = "https://api.perplexity.ai/"
        self.api_key = os.getenv('PERPLEXITY_API_KEY')
        self.headers = {
            'Authorization': f'Bearer {self.api_key}',
            'Content-Type': 'application/json'
        }

    def search(self, query, max_results=5):
        payload = {
            'query': query,
            'sources': ['web', 'arxiv', 'wikipedia'],
            'max_results': max_results
        }
        
        response = requests.post(
            f'{self.base_url}search',
            headers=self.headers,
            json=payload
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(f"Perplexity API error: {response.status_code}")

    def chat(self, query, context=None):
        payload = {
            'query': query,
            'context': context
        }
        
        response = requests.post(
            f'{self.base_url}chat',
            headers=self.headers,
            json=payload
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            raise Exception(f"Perplexity chat API error: {response.status_code}")

Why: Perplexity's API provides AI-powered search with better context understanding and citation features. Unlike Google's AI-overloaded results, Perplexity focuses on providing factual, source-backed answers.

5. Creating a Unified Search Interface

Now create a main search orchestrator that combines both services:

from duckduckgo_search import DuckDuckGoSearch
from perplexity_search import PerplexitySearch
import json


class UnifiedSearch:
    def __init__(self):
        self.ddg = DuckDuckGoSearch()
        self.pp = PerplexitySearch()

    def search_both(self, query):
        print(f"Searching for: {query}\n")
        
        # Get DuckDuckGo results
        print("=== DuckDuckGo Results ===")
        try:
            ddg_results = self.ddg.search(query)
            for i, topic in enumerate(ddg_results['RelatedTopics'][:3]):
                print(f"{i+1}. {topic['Text']}")
                print(f"   URL: {topic['FirstURL']}")
                print()
        except Exception as e:
            print(f"DuckDuckGo search failed: {e}")
        
        # Get Perplexity results
        print("=== Perplexity Results ===")
        try:
            pp_results = self.pp.search(query)
            for i, result in enumerate(pp_results['results'][:3]):
                print(f"{i+1}. {result['title']}")
                print(f"   Content: {result['content'][:200]}...")
                print(f"   Source: {result['source']}")
                print()
        except Exception as e:
            print(f"Perplexity search failed: {e}")

# Example usage
if __name__ == "__main__":
    unified = UnifiedSearch()
    unified.search_both("impact of climate change on agriculture")

Why: This unified approach gives you the best of both worlds - DuckDuckGo's comprehensive web results and Perplexity's fact-based AI answers. The combination provides a more balanced and reliable research experience.

6. Installing Browser Extensions for Quick Access

Install the DuckDuckGo extension for your browser to enable instant search enhancements:

  1. Visit your browser's extension marketplace (Chrome Web Store, Firefox Add-ons)
  2. Search for "DuckDuckGo Privacy Essentials"
  3. Install and enable the extension
  4. Configure it to use DuckDuckGo as your default search engine

Why: Browser extensions provide immediate access to enhanced search functionality without leaving your current workflow. They also improve privacy by blocking trackers and maintaining your search history.

Summary

This tutorial demonstrated how to build a hybrid search system using DuckDuckGo and Perplexity APIs. By implementing both services in a Python environment and using browser extensions, you've created a more reliable research workflow that avoids the AI-generated content pitfalls of traditional search engines. The combination provides better source verification, cleaner results, and more factual information for your research needs.

The key advantages of this approach include:

  • Reduced AI-generated noise in search results
  • Better source verification and citation tracking
  • Enhanced privacy through DuckDuckGo's privacy-focused approach
  • Automated research workflow that can be extended and customized

Remember to regularly update your API keys and monitor usage limits to maintain uninterrupted access to these powerful search tools.

Source: ZDNet AI

Related Articles