Introducing Web Search API Via AI Gateway
If you’ve been hanging around the homelab community for any amount of time, you know that our setups evolve. We start with a simple Raspberry Pi running Pi-hole, move on to a NAS for our media, and eventually, we find ourselves racking servers in our closets to run our own AI models. Welcome to the modern homelab.
As local AI has exploded, thanks to projects like Ollama, text-generation-webui, and vLLM, we’ve run into a very familiar problem: our local Large Language Models (LLMs) are trapped in time. They know everything up to their training cutoff date, but ask them who won the game last night or what the current weather is in your city, and they’ll confidently hallucinate an answer.
To fix this, we need to give our local models eyes on the outside world. Today, we are going to talk about introducing a Web Search API via an AI Gateway. It sounds like a mouthful, but it’s actually a beautifully elegant architecture that you can deploy in your homelab this weekend. Let’s break down what it is, why you need it, the hardware required to run it, and a practical walkthrough to get it working.
What is an AI Gateway?
Before we dive into the web search aspect, let’s talk about the AI Gateway.
If you’ve ever worked with web applications, you’re probably familiar with a reverse proxy like Nginx or Traefik. An AI Gateway is essentially a reverse proxy, but specifically designed for AI models. Instead of routing HTTP traffic to different web servers, it routes AI prompts to different models, APIs, or endpoints.
Tools like LiteLLM, OneAPI, or Portkey act as AI Gateways. You point all your local applications, scripts, and chat interfaces to a single endpoint (the gateway). The gateway then translates your requests into the specific format required by whatever backend model you want to use—whether that’s a local Ollama instance running Llama 3, a cloud API like OpenAI, or a specialized vision model.
Why use an AI Gateway in a Homelab?
- Standardization: Different local models and APIs use slightly different request formats. An AI gateway standardizes everything to the OpenAI API format. If your app supports OpenAI, it now supports your entire homelab.
- Load Balancing: If you have multiple GPUs or multiple servers, you can load balance requests across them.
- Fallbacks: If your local model server crashes, the gateway can automatically fall back to a cloud API so your workflow isn’t interrupted.
- Centralized Management: You can manage API keys, rate limits, and logging all in one place.
The Missing Link: Web Search API
Even with a perfect AI Gateway routing to a powerful local model, the knowledge cutoff problem remains. This is where the Web Search API comes in.
By integrating a web search capability into your AI pipeline, you are essentially building an automated version of Retrieval-Augmented Generation (RAG). Instead of searching your local documents, the system searches the live internet, scrapes the top results, stuffs that text into the prompt context, and asks the LLM to synthesize an answer based on that fresh data.
You have two main routes for the Web Search API:
- Hosted APIs (Tavily, Serper, Brave Search API): These are incredibly fast, optimized for AI, and return clean markdown instead of messy HTML. However, they cost money or have rate limits.
- Self-Hosted Search (SearXNG): The holy grail for homelabbers. SearXNG is a privacy-respecting metasearch engine. You can run it in Docker, expose its JSON API, and use it as your own free, unlimited Web Search API.
For this article, we are going the true self-hosted route. We will use SearXNG to fetch live data, pass that data through an AI Gateway (LiteLLM), and have our local model (via Ollama) generate the final answer.
Hardware Considerations for Your AI Homelab
Since this is a hardware-focused deep dive, let’s talk about what you actually need to run this stack. The beautiful part about this architecture is that it separates the workload. You don’t need a massive GPU on your gateway server; you just need smart networking and enough compute to handle the routing and web scraping.
The AI Compute Node (The Heavy Lifter)
This is the server that actually runs the LLM. Inference is heavily dependent on your GPU.
- GPU: You’ll want at least 12GB of VRAM to comfortably run an 8B parameter model (like Llama 3) with a decent context window. An NVIDIA RTX 3060 12GB is the current budget king of homelab AI. If you have deeper pockets, an RTX 4070 Ti or a used RTX 3090 with 24GB of VRAM will let you run much larger models.
- CPU & RAM: A modern 6-core CPU (like a Ryzen 5 5600X) and 32GB of system RAM are plenty. The RAM is important because the model weights are loaded into system memory before being transferred to the GPU.
The Gateway and Search Node (The Router)
This is where SearXNG and LiteLLM will live. Because these tasks are primarily CPU and network-bound, you don’t need a massive GPU here.
- Hardware: An Intel N100 Mini PC is perfect for this. It sips power (around 6 watts), has plenty of single-core performance for Python-based routing, and can easily handle Docker containers.
- Alternative: An older 8th Gen Intel Core i5 mini PC or a thin client works great too.
- Networking: Since the Gateway will be scraping the internet and sending large text payloads to your GPU server, a Gigabit ethernet connection between your homelab machines is highly recommended over Wi-Fi.
If you want to run everything on a single machine, that’s totally fine too! Just make sure your primary server has the GPU for inference and enough RAM to handle the Docker overhead.
Walkthrough: Setting Up the Stack
Let’s get our hands dirty. We are going to set up three components: SearXNG (for search), Ollama (for the LLM), and LiteLLM (as the AI Gateway).
Step 1: Deploy Ollama
Assuming you are on your AI Compute Node, the easiest way to get Ollama running is via their official install script or Docker. Let’s use Docker Compose to keep things homelab-standard.
Create a docker-compose.yml file:
| |
Spin it up with docker compose up -d. Then, pull a model. We’ll use Llama 3 8B, which is fast and highly capable:
| |
Step 2: Deploy SearXNG (Your Web Search API)
On your Gateway node (or the same machine, if that’s your setup), let’s spin up SearXNG. We need to make sure the JSON format is enabled in its settings.
Create a directory for SearXNG and a settings.yml file inside a searxng folder:
| |
Now, create your docker-compose.yml for SearXNG:
| |
Run docker compose up -d. You can test that your Web Search API is working by hitting the JSON endpoint:
| |
If you get a massive JSON payload back, congratulations! You have a self-hosted Web Search API.
Step 3: Deploy LiteLLM (The AI Gateway)
Now we need the AI Gateway to tie everything together. LiteLLM makes it easy to proxy our Ollama instance.
Create a docker-compose.yml for LiteLLM:
| |
Next to that compose file, create litellm_config.yaml:
| |
Note: Replace <YOUR_OLLAMA_IP> with the IP address of your AI Compute Node.
Bring up the gateway: docker compose up -d. You can test the gateway by sending an OpenAI-formatted request to port 4000:
| |
Step 4: The Python Integration
Now we have our Web Search API (SearXNG) and our AI Gateway (LiteLLM routing to Ollama). We need a script that connects them. This script will:
- Take a user query.
- Send the query to SearXNG.
- Parse the JSON results and extract the top snippets.
- Send those snippets + the original query to LiteLLM.
- Print the LLM’s synthesized answer.
Create a file named ai_search.py:
| |
Testing it Out
Make sure you have the requests library installed (pip install requests), and run your script:
| |
Try asking it something that requires real-time data, like: “What is the latest news regarding the Raspberry Pi 5?”
You will see the script pause, fetch the live data from SearXNG, pass it through LiteLLM, and your local Llama 3 model will spit out a highly accurate, up-to-date response based on the live web search—all running entirely on your own hardware, using your own APIs, without a single penny going to a cloud provider.
Summary and Call to Action
Introducing a Web Search API via an AI Gateway completely changes the game for local, self-hosted AI. By combining the routing power of an AI Gateway like LiteLLM, the real-time fetching capability of a self-hosted SearXNG instance, and the raw inference power of a local GPU running Ollama, you’ve built a private, secure, and highly capable alternative to ChatGPT with web browsing.
Not only does this architecture keep your data entirely within your homelab, but it also standardizes your AI interactions. As you add more models, more GPUs, or even external APIs to your setup, your AI Gateway will seamlessly route traffic where it needs to go, while your Web Search API keeps your models grounded in reality.
What’s next? Try containerizing this Python script and adding a simple web UI (like Streamlit or Gradio) so you can access your new local search engine from any device on your network.
If you found this walkthrough helpful, be sure to check out the rest of the blog for more homelab, self-hosting, and local AI tutorials. Have you set up an AI Gateway in your homelab yet? What hardware are you running it on? Drop a comment below or join the conversation on our community Discord—we’d love to see what you’re building!