2. Defining Tools and Capabilities
Building the functions. Writing the specific Python scripts for web scraping and API calls that the agent will use as its hands and eyes.
An agent is only as smart as the tools you give it. For our Research Bot, we need to write three Python functions. These functions are standard Python code; there is no AI inside them. They are just the physical “tools” the AI will trigger.
The Importance of Docstrings
When you write a tool for an agent, the Docstring (the comment at the top of the function) is the most critical part. The LLM does not read your Python code. It only reads the Docstring. If your Docstring is vague, the LLM will hallucinate the arguments.
def search_web(query: str) -> str:
"""
Performs a Google Search and returns the top 5 URLs and snippets.
Use this tool when you need to find up-to-date information on a topic.
Args:
query (str): The specific search string to query Google with.
"""
# Implementation using DuckDuckGo API or Serper API goes here...
return results_json
Tool 2: read_url
Once the agent has the URLs from the search tool, it needs to read them. We will write a function called read_url(url: str). This function uses Python’s requests library to fetch the HTML, and BeautifulSoup to strip out all the messy <div> tags and JavaScript, returning only clean text.
Why? Because raw HTML is massive. If you feed raw HTML to your local 8B model, it will immediately exceed the context window and crash with an OOM error.
Tool 3: save_file
Finally, the agent needs a way to output its work. We write a function save_file(filename: str, content: str).
In our Docstring for this tool, we must be explicit: “Saves a markdown report to the disk. The filename must end in .md.” If you don’t include that instruction, the agent might try to save it as a .txt or a .docx.
The Tool Registry
Once the three Python functions are written, we place them in a dictionary (a Tool Registry). When the LLM outputs a JSON string like {"tool": "search_web", "args": {"query": "solid state batteries"}}, our main Python loop will parse that JSON, look up “search_web” in the registry, and execute the function with the provided argument.
