Get AI summaries of any video or article — Sign up free
Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools thumbnail

Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools

Sam Witteveen·
5 min read

Based on Sam Witteveen's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Wikipedia, DuckDuckGo search, and Python REPL can be registered as LangChain tools with names, callable functions, and usage descriptions.

Briefing

LangChain agents can be made to choose among three built-in tools—Wikipedia, DuckDuckGo search, and Python’s Read-Evaluate-Print Loop (Python REPL)—based on the question being asked. The practical payoff is an agent that can look up factual information on the web, pull structured background from Wikipedia, and run real computations in Python, switching tools automatically instead of forcing a single workflow.

The setup starts with installing the Wikipedia API wrapper and then wiring each tool into LangChain as a “chain” or callable function. The Wikipedia tool is configured so a query like “LangChain” returns the matching Wikipedia page content, while the Python REPL tool evaluates Python code and prints results (for example, computing 17 * 2 to produce 34). DuckDuckGo is added as a free search option—unlike paid SERP APIs—so the agent can retrieve current or general web information when Wikipedia isn’t the best fit.

Those tools are then bundled into a list and handed to a zero-shot agent using the ZeroShotAgent with the ZeroShot React description prompt template. The agent runs with verbose logging and a max-iteration limit, meaning it can “think” through multiple steps—deciding whether to call a tool, inspect the observation returned, and then either call another tool or produce a final answer.

Concrete examples show the tool-selection behavior. For “When was Barack Obama born?”, the agent routes the task to DuckDuckGo search, extracts the birth date from the returned results, and then answers directly. For “17 by 6,” it recognizes a computation task and uses Python REPL to calculate the result (102) rather than searching the web.

The prompt itself is central: it instructs the agent to answer questions “as best you can” using the available tools, and it provides tool descriptions that act like decision rules. Wikipedia is described as useful for looking up a “topic, country or person,” DuckDuckGo is described as useful for internet search when another tool can’t find the needed information, and Python REPL is described as the way to run Python code for calculations.

When asked “tell me about langChain,” the agent chooses DuckDuckGo instead of Wikipedia because the two lookup tools overlap. However, the Wikipedia path becomes more likely when the question matches the Wikipedia description more clearly. For “tell me about Singapore,” the agent selects Wikipedia, retrieves the page, and then narrows the response to Singapore as a country (an island and city-state in Southeast Asia).

The agent also demonstrates mixed tool use under uncertainty. For “is 11 a prime number?”, Python REPL initially produces an inconclusive “might be prime” style result, prompting the agent to fall back to DuckDuckGo to confirm. Rephrasing the task by asking it to write a Python function to test primality yields a cleaner multi-step solution: the agent generates the function, runs it, and returns that 11 is prime.

Overall, the workflow shows how tool descriptions and prompt structure drive an agent’s routing decisions, and how better prompting (or adding examples) can improve reliability—especially when tool outputs are ambiguous or when multiple tools can answer the same kind of question.

Cornell Notes

A LangChain zero-shot agent can combine Wikipedia, DuckDuckGo search, and Python REPL so it selects the right tool per question. Wikipedia is used for topic/country/person lookups, DuckDuckGo handles general internet search (without paid API calls), and Python REPL runs computations. The agent follows a React-style loop: it chooses an action (tool), reads the observation returned, and repeats until it can produce a final answer. Examples show successful routing for birth dates (DuckDuckGo), arithmetic (Python REPL), and country facts like Singapore (Wikipedia). When Python REPL results are uncertain (e.g., primality), the agent may fall back to search; rewriting the prompt to force a proper Python function improves outcomes.

How are Wikipedia, DuckDuckGo, and Python REPL turned into tools that a LangChain agent can call?

Each capability is wrapped as a callable tool with (1) a name, (2) a function that runs the tool (e.g., Wikipedia page lookup, Python code evaluation/printing, or DuckDuckGo search), and (3) a description that tells the agent when to use it. Wikipedia uses a Wikipedia API wrapper and returns page content for a query. Python REPL evaluates Python code and prints the result. DuckDuckGo is added via its wrapper so the agent can search the internet and return information without paying for API calls.

What mechanism lets the agent decide which tool to use, and how does it finish the task?

A ZeroShotAgent runs with a React-style prompt template. It iterates: it formats the question, then repeatedly chooses an action from the available tools, supplies an action input, and reads the observation returned. It can call multiple tools across iterations (bounded by max iterations). Once enough information is gathered, it outputs a final answer.

Why did the agent use DuckDuckGo for “tell me about langChain” instead of Wikipedia?

Wikipedia and DuckDuckGo both support “look up information” behavior, so the overlap can confuse routing. The tool descriptions and the model’s internal representation led it to treat DuckDuckGo as the better fit for that query, so it searched the internet and answered from the results rather than pulling the Wikipedia page.

What changed the outcome for “tell me about Singapore,” making Wikipedia the chosen tool?

The Wikipedia tool description explicitly mentions “topic, country or person.” Singapore matches the “country” cue, so the agent selected Wikipedia, retrieved the page, and then focused the response on Singapore as an island and city-state in Southeast Asia, rather than drifting into other page sections.

How did the agent handle “is 11 a prime number?” and why did it sometimes fall back to DuckDuckGo?

Python REPL initially produced an inconclusive result (it checked divisibility in a way that left uncertainty, described as “might be a prime number”). Because the agent still needed a definitive answer, it used DuckDuckGo search to confirm that 11 appears in prime-number listings online. The fallback happened because the observation from Python REPL wasn’t decisive.

How did rewriting the prompt improve the prime-number example?

Instead of asking only whether 11 is prime, the prompt was reframed to require writing a Python function to check primality and then testing it. That forced Python REPL to run a deterministic check: the agent generated the function, executed it on 11, printed the result, and returned “11 is a prime number” without needing a web search.

Review Questions

  1. What specific role do tool descriptions play in a zero-shot agent’s tool-selection decisions?
  2. Give an example of a question where Wikipedia is likely to be chosen over DuckDuckGo, and explain why using the tool descriptions.
  3. Why might Python REPL outputs lead an agent to call DuckDuckGo, and how can prompt rephrasing reduce that risk?

Key Points

  1. 1

    Wikipedia, DuckDuckGo search, and Python REPL can be registered as LangChain tools with names, callable functions, and usage descriptions.

  2. 2

    A ZeroShotAgent with a React-style loop can choose tools automatically, inspect tool observations, and iterate until it can produce a final answer.

  3. 3

    DuckDuckGo is positioned as a no-paid-API alternative to SERP APIs, making it practical for general internet lookups.

  4. 4

    Python REPL is best for deterministic computations (e.g., arithmetic), but ambiguous outputs can trigger additional tool calls.

  5. 5

    Overlapping capabilities between Wikipedia and DuckDuckGo can cause the agent to pick the “wrong” lookup tool unless the prompt cues are strong.

  6. 6

    Rephrasing tasks to force clearer computation logic (like writing a primality-check function) improves reliability and reduces unnecessary web fallbacks.

Highlights

The agent routes “When was Barack Obama born?” to DuckDuckGo search, then converts retrieved observations into a direct date answer.
For “17 by 6,” the agent uses Python REPL to compute 102 instead of searching the web.
Wikipedia is more reliably selected when the question matches its description—e.g., “Singapore” as a country—while overlapping lookup tools can lead to DuckDuckGo being chosen for other topics.
Prime-number checking showed how uncertain Python REPL observations can trigger a fallback to DuckDuckGo, and how prompt rewriting can fix it by enforcing a proper function-based test.

Topics

Mentioned

  • LLM
  • SERP
  • GPT
  • REPL