Skip to content

About

Run Qwen locally with web search through SerpApi

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

38 Commits

Folders and files

Repository files navigation

Web search for Qwen models using SerpApi

Run Qwen on your device with web search through SerpApi.

  • Chat app (app/): Ask questions in your browser, see every search Qwen makes, and compare models and web search options in Battleground mode.
  • Examples (examples/): Sample Python scripts to connect Qwen with SerpApi.
  • Benchmark (app/benchmark/): Three tests with 100 questions each, our results and what we learned.

How it works

Qwen gets a search tool through function calling. When it decides a question needs a search, it calls the tool with a query. The app runs that search through SerpApi and sends the top results back to Qwen as Markdown. Qwen can search again if the results fall short, then answers with sources.

How a local Qwen model searches the web with SerpApi

Quick start

You need Ollama, Python 3.10 or newer, Node.js 20 or newer (to build the chat app), and a SerpApi API key. The free plan includes 250 searches a month.

1. Install. Then open .env and set SERPAPI_API_KEY to your key.

git clone https://github.com/serpapi/qwen-web-search.git
cd qwen-web-search
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env

2. Get a Qwen model and start Ollama in its own terminal. If the Ollama app is open, quit it first.

ollama pull qwen3.5:9b
OLLAMA_CONTEXT_LENGTH=32768 OLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve

The settings give Qwen room for search results (a 32k-token context) while keeping memory use down. If you pull a different model, set LOCAL_MODEL in .env to its name (see Pick a model).

3. Build the UI and start the app in another terminal:

cd app/web && npm install && npm run build && cd ..
python server.py

Open http://localhost:8000. The first start downloads Qwen's tokenizer from Hugging Face, which the app uses to count search tokens.

Pick a model

Pull the one that fits your computer's memory:

Model Download size Memory needed
qwen3.5:4b 3.4 GB 6 GB
qwen3.5:9b 6.6 GB 8 GB
qwen3.8:27b 18 GB 24 GB

Any Qwen model with the tools tag on Ollama works. Update LOCAL_MODEL with the model name in .env. .env.example has qwen3.5:9b. The app starts with that model and lists every other Qwen model you've pulled, and the examples use it too. If LOCAL_MODEL is empty, the app starts with your biggest Qwen model and the examples use qwen3.8:27b.

Use the app

The app has two modes:

  • Dedicated
  • Battleground

Dedicated: Choose a model and a web search option, then type a question or click an example prompt. Each search appears as a card while Qwen works, with:

  • Search engine chosen by Qwen and its search query
  • What Qwen read: Search results received by Qwen
  • Token usage and time taken for the search

The Answer stats panel adds up the searches, tokens and time for each answer.

Dedicated mode: Qwen searches with SerpApi and streams its answer

Battleground: Compare up to 4 Qwen models with different web search options side by side. Presets fill the columns in one click:

Preset Columns
Search vs no search Your model with SerpApi, and without search
Google vs multiple engines Your model with SerpApi · Google, and with SerpApi · Multiple engines
Model vs model Every Qwen model you've pulled (up to four), each with SerpApi
Small + search vs big alone Your smallest model with SerpApi, and your biggest without search

A table compares the engines used, searches, tokens and total time, and highlights the lowest search tokens and the fastest column. Columns run one after another, so they don't compete for your computer's memory. The time shown doesn't include loading a model.

Battleground mode: three Qwen models answer the same question, one after another (sped up 5x)

Web search options

Option Where the search results come from
No search Nowhere. Qwen answers from its training data.
SerpApi · Google Google, through SerpApi, like qwen_search.py
SerpApi · Multiple engines Qwen chooses Google, Google News, Baidu or Naver for each search, like qwen_search_multi_engines.py

The SerpApi options get the top 5 results as Markdown. To change that, edit app/serpapi_search.py. In the app, Qwen can also limit a Google search to the past day, week, month or year when a question is about recent events. The search card shows a badge such as "Past week" next to the engine name. The examples leave this out to stay short.

Chat in the terminal

From app/, run python chat.py to chat in the terminal, with Qwen choosing the SerpApi engine.

Run the examples

Each example is one file and uses the model in LOCAL_MODEL in .env, or qwen3.8:27b if it's empty. Run them from the repository root:

python examples/qwen_search.py
python examples/qwen_search_multi_engines.py
File What it does
qwen_search.py Qwen searches Google.
qwen_search_multi_engines.py Qwen chooses Google, Google News, Baidu or Naver for each search.

Type a question after You:. A [search] line appears for each search Qwen makes before it answers. Press Ctrl+C to quit.

Benchmark

Three tests with 100 questions each, in English, Chinese and Korean: which model searches best, Google alone vs multiple engines, and a small model with search vs a big model without it. In our runs, a 4B model with search beat a 27B model answering from memory on old facts.

Correct answers, seconds and tokens per answer, without and with SerpApi search, for qwen3.8:27b, qwen3.5:9b and qwen3.5:4b

See the results and how to run the tests, the methodology and the insights.

Project structure

qwen-web-search/
├── examples/
│   ├── qwen_search.py                # Google search in one file
│   └── qwen_search_multi_engines.py  # Qwen chooses the SerpApi engine
├── app/
│   ├── server.py                     # Web server for chat UI
│   ├── chat.py                       # Search loop with local Qwen, and terminal chat
│   ├── models.py                     # Installed Qwen models and the default one
│   ├── tools.py                      # Prompt and search tools Qwen sees
│   ├── serpapi_search.py             # SerpApi search
│   ├── setups.py                     # Web search options shared by UI and benchmark
│   ├── bench.py, report.py           # Benchmark and result tables
│   ├── benchmark/                    # Benchmark questions, results and insights
│   └── web/                          # React chat UI
├── assets/                           # Images
├── .env.example
└── requirements.txt

Development

To work on the UI, run python server.py in app/ and npm run dev in app/web/ at the same time, then open http://localhost:5173. Restart server.py after you change Python files.

Troubleshooting

Problem Fix
address already in use when you start Ollama Ollama is already running. Quit the Ollama app, then start Ollama again.
Can't reach Ollama Ollama isn't running. Start it as in Quick start, then start the app.
Ollama doesn't have ... or model "..." not found Pull the model with ollama pull, or set LOCAL_MODEL in .env to a name from ollama list.
The app says no Qwen model was found Pull one, for example ollama pull qwen3.5:9b, then reload the page. Only Qwen models with tool support are listed.
SERPAPI_API_KEY isn't set Create .env in the repository root, as in Quick start.
A search says Search failed: Invalid API key The key in .env is wrong. Copy it again from your SerpApi dashboard.
A search says Search failed: Your account has run out of searches You've used this month's searches. Check your usage on the SerpApi dashboard.
Your computer slows down while Qwen answers Close other apps or pick a smaller model. Run ollama stop <model> to free the memory.

Learn more

Qwen

SerpApi

Ollama

About

Run Qwen locally with web search through SerpApi

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages