大模型网摘deepseek.wang

Tutorials · 2026-10-19

Python Tutorial: Build a Lightweight Web-QA Agent with Search & Fetch

The newly opened Search & Fetch APIs make a web-connected QA agent possible in a few dozen lines of Python. This tutorial walks through the three steps; you only need an API key and the requests library.

Step 1: Search

import requests

HEADERS = {"Authorization": "Bearer <YOUR API KEY>",
           "Content-Type": "application/json"}

r = requests.post("https://api.deepseek.com/v1/tools/search",
                  headers=HEADERS,
                  json={"query": "DeepSeek V4.1-Flash-1017 release",
                        "count": 5, "need_content": False})
for hit in r.json()["results"]:
    print(hit["title"], hit["url"])
    print(hit["summary"])

query accepts 1–128 characters and count defaults to 10 (max 50). When a result looks promising, hand its URL to Fetch.

Step 2: Fetch the page

r = requests.post("https://api.deepseek.com/v1/tools/fetch",
                  headers=HEADERS,
                  json={"url": hit["url"]})
markdown = r.json()["content"]   # single page as Markdown

Slice the first few sections of the Markdown as context instead of feeding the whole page.

Step 3: Feed the chat model

context = "\n".join(f"[{h['title']}] {h['summary']}" for h in hits)
r = requests.post("https://api.deepseek.com/chat/completions",
                  headers=HEADERS,
                  json={"model": "deepseek-flash",
                        "messages": [
                          {"role": "system", "content": "Answer using the sources and cite links.\n" + context},
                          {"role": "user", "content": "What's new in V4.1-Flash-1017?"}
                        ]})
print(r.json()["choices"][0]["message"]["content"])

A more advanced pattern exposes search as a tool call so the model decides when to go online—similar to the approach in the RAG tutorial. Chat API basics are in the Python quickstart.

Cost estimate

A typical QA turn (1 search + 2 fetches + one chat call) costs just over $0.002 on the tools side, plus model tokens. Inside DeepSeek Harness, Claude Code, or Codex, server-side tool calls are free until October 31, 16:00 UTC. Parameter details are in the Search & Fetch API guide.