惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
宝玉的分享
宝玉的分享
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
P
Proofpoint News Feed
L
LangChain Blog
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
Martin Fowler
Martin Fowler

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work Top 15 Reinforcement Learning Questions That Will Appear in Exams The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026
Building Your First AI Agent with LangChain: From Chatbot...
Muhammad Kum · 2026-04-18 · via DEV Community

Introduction
Imagine having a personal assistant that can:

  • Answer your questions from your own documents
  • Search the internet for real-time information
  • Execute code and automate tasks
  • Remember your previous conversations

That's exactly what AI Agents do — and with LangChain, you can build one in Python in under 30 minutes.

In this post, I'll walk you through:

  1. What AI Agents are and why they matter
  2. How LangChain makes building agents easy
  3. Building a fully functional AI Chatbot Agent step-by-step
  4. Adding memory, tools, and real-world capabilities

What is an AI Agent?
An AI Agent is an autonomous system that can:

  1. Think — Understand your request using an LLM (Large Language Model)
  2. Decide — Choose the right tool or action to take
  3. Act — Execute the action (search, calculate, code, respond)
  4. Learn — Remember context from previous interactions

What is LangChain?
LangChain is a Python framework that makes it easy to build LLM-powered applications.

  1. Your LLM (OpenAI, Google Gemini, Llama, etc.)
  2. Your data (documents, databases, APIs)
  3. Your tools (search engines, calculators, code executors)

Why LangChain?

  1. Chains — Connect multiple LLM calls together
  2. Memory — Store and recall conversation history
  3. Tools — Give your agent superpowers (Google search, Wikipedia, Python REPL)
  4. RAG — Retrieve answers from your own documents
  5. Agents — Autonomous decision-making bots

Let's Build: AI Agent Chatbot with LangChain

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain.agents import initialize_agent, AgentType, Tool
from langchain.tools import DuckDuckGoSearchRun, WikipediaQueryRun
from langchain.utilities import WikipediaAPIWrapper
from langchain.memory import ConversationBufferMemory
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
from dotenv import load_dotenv

load_dotenv()

llm = ChatOpenAI(model="gpt-4", temperature=0)

---- Tool 1: Internet Search ----

search = DuckDuckGoSearchRun()

---- Tool 2: Wikipedia ----

wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())

---- Tool 3: RAG (Your Documents) ----

loader = TextLoader("my_document.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
docs = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(docs, embeddings)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore.as_retriever()
)

---- Define All Tools ----

tools = [
Tool(
name="Internet Search",
func=search.run,
description="Use this to search the internet for current information"
),
Tool(
name="Wikipedia",
func=wikipedia.run,
description="Use this to look up detailed information from Wikipedia"
),
Tool(
name="Document QA",
func=qa_chain.run,
description="Use this to answer questions from uploaded documents"
)
]

---- Memory ----

memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)

---- Create Agent ----

agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
memory=memory,
verbose=True,
handle_parsing_errors=True
)

---- Chat Loop ----

print("AI Agent Ready! Type 'quit' to exit.\n")

while True:
user_input = input("You: ")
if user_input.lower() == "quit":
print("Goodbye!")
break
response = agent.run(user_input)
print(f"Agent: {response}\n")