Skip to content

#303: Use a Search Engine With LangChain

All LLMs have a knowledge cut-off point. All things that happened after that point are unknown to the LLM. What can we do if we need a bot that can answer questions on more current events? LangChain has us covered and gives us all the tools we need to ask search engines about current events (or anything else they know about). Let us see what we need to build such a chat bot.

Use DuckDuckGoSearchAPIWrapper not DuckDuckGoSearchResults

There are two similar packages we can use to search DuckDuckGo: DuckDuckGoSearchAPIWrapper and DuckDuckGoSearchResults.

If we believe the documentation, both packages should work and give us the search results. Unfortunately, whenever I tried DuckDuckGoSearchResults I ended up with results that did not match my query. Should this happen to you, try DuckDuckGoSearchAPIWrapper.

Installation

To add the search functionality to our bot we need this additional package:

uv pip install -U langchain langchain_openai langchain_community ddgs

Create the bot

The important part of this bot is the handling of the search results we get back from DuckDuckGo. We must make sure that we can give back something, even if our search did not find anything. The formatting of the results helps us when we try to debug what is going on and it gives the context that we need in our prompt for the LLM.

The glue code for the setup of the LLM and the loop for the question stay the same as with our previous bots. Do not forget to change the chain, here we need to turn our method into a runnable lambda and glue it to our prompt and the LLM:

from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnableLambda

# 1. Setup the search with DuckDuckGo
search = DuckDuckGoSearchAPIWrapper(max_results=5)

def fetch_duckduckgo_results(query: str):
    results = search.results(query, max_results=5)
    if not results:
        return {"snippets": "No relevant results found.", "query": query}

    formatted = "\n\n".join(
        [f"Title: {r['title']}\nSnippet: {r['snippet']}\nURL: {r['link']}" for r in results]
    )
    # print(formatted) # <-- to see what comes back from the search engine
    return {"snippets": formatted, "query": query}

fetch_results = RunnableLambda(fetch_duckduckgo_results)

# 2. Define a prompt that includes the search result
prompt = PromptTemplate.from_template("""
You are an expert assistant that summarizes accurate, web-based information.

Below are 5 DuckDuckGo search results for the query:
"{query}"

{snippets}

Based on these results, write a concise, factual, and well-structured summary answer.
Include useful insights but avoid speculation or repetition.
""")

# 3. Define the LLM
llm = ChatOpenAI(
    model="mistral",
    openai_api_base="http://localhost:1234/v1",
    openai_api_key="not-needed",
    temperature=0.2
)

# 4. Create the chain
chain = fetch_results | prompt | llm

# 5. Loop for questions
while True:
    query = input("You: ")
    if query.lower() in ["quit", "exit", "end"]:
        break
    result = chain.invoke(query)
    print("\n🧾 Synthesized Answer:\n")
    print(result.content) 

Search the web for current events

We can now run our web enabled chat bot and ask questions about more current events:

You: Who did win the F1 2025 United States Grand Prix race?

🧾 Synthesized Answer:

Max Verstappen (Red Bull) won the 2025 United States Grand Prix from pole 
position in both the sprint and the race. He led wire-to-wire, securing 
his fourth victory at this event in five years. Lando Norris (McLaren) 
finished second after a thrilling duel with Charles Leclerc (Ferrari), 
who took third place.

Championship contenders Oscar Piastri (McLaren) retired during the sprint 
but recovered to finish fifth. The race took place on October 19, 2025, 
at the Circuit of the Americas in Austin, Texas.

Additional insights:
- Verstappen's win was commanding, with a nine-second lead over Norris.
- Alpine faced internal struggles, including a strategic error involving 
  Franco Colapinto overtaking Pierre Gasly in the closing laps.


You: What happend in the Louvre in Paris?

🧾 Synthesized Answer:

On October 20, 2025, a daring heist occurred at the Louvre Museum in Paris, 
resulting in the theft of several pieces of Napoleonic jewelry. The robbery 
took place during daylight hours and lasted approximately seven minutes. 
French authorities have estimated the losses at around €88 million.

Key details from the incident include:
- **Security Failures**: The Louvre's director acknowledged that the museum's 
security system was outdated, with the only exterior camera near the thieves' 
entry point facing away from them.
- **Priceless Artifacts**: The stolen items were described as "priceless" by 
French officials, emphasizing their cultural and historical significance.
- **Ongoing Investigation**: Police are searching for four suspects involved 
in the heist. An inquiry has been opened by the Paris prosecutor to assess 
the value of the stolen pieces and investigate the crime further.

The Louvre remains one of the world's most famous museums, known for its vast 
collection of art and historical artifacts. This incident highlights 
vulnerabilities in high-profile cultural institutions despite their global 
prominence.

When you try this bot on your own, you notice that small changes in the question you ask have a large impact on the results you get from DuckDuckGo. If you do not get the answer you expect, try to rephrase your question.

Next

We start to get a feeling on how LangChain works. We see how many parts of a bot we can reuse and where we need to change it to get new functionality. The search with DuckDuckGo is not as stable as I had hoped, but as one of the few search engines we can try that out without first buying API credits.

Next week we try something new and explore our options when it comes to search in our own data. Why not create a bot that answer questions based on a CSV file?