Prerequisites

  • Python 3.12+
  • OpenAI API Key
  • Tavily API Key (for search)

Step 1: Setup

pip install langchain-openai langchain tavily-python

Step 2: The Code

Copy this code into agent.py:

from langchain.agents import initialize_agent, AgentType from langchain_openai import ChatOpenAI from langchain_community.tools.tavily_search import TavilySearchResults # 1. Initialize LLM llm = ChatOpenAI(model="gpt-4o", temperature=0) # 2. Define Tools tools = [TavilySearchResults(max_results=1)] # 3. Create Agent agent = initialize_agent( tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True ) # 4. Run! agent.run("Search for the latest AI news and summarize it.")

How It Works

  1. Perception: The agent receives your command.
  2. Reasoning: The LLM "thinks" and decides it needs to search.
  3. Action: It calls the Tavily Search tool.
  4. Observation: It reads the search results.
  5. Response: It synthesizes the answer back to you.

Next Steps

To make this a true "Agent", give it more tools:

  • File Write Access: To save reports.
  • Email Access: To send summaries.
  • Calendar Access: To schedule meetings.

Final Verdict

Build, Don't Just Chat

The future belongs to those who build autonomous systems. This simple script is the foundation of billion-dollar startups.