LangChain Agent Creation | Online Tutorial
\n\nLangChain is a framework for building LLM applications, which can upgrade model calls into composable, controllable, and scalable application systems.
\n\nLangChain does not solve how to call models, but rather:
\n- \n
- How to organize multi-step reasoning \n
- How to integrate external data \n
- How to safely invoke tools by the model \n
- How to manage context over time \n
\n\n\nOpen source address: https://github.com/langchain-ai/langchain.
\nLangChain tutorial: https://example.com/langchain/langchain-tutorial.html
\n
\n\n \n\n\nImagine you are building an intelligent robot assistant that needs to understand your questions, search for information from various sources, perform logical reasoning, and finally answer you in natural language. Using a language model like GPT alone is like giving the robot a smart brain, but it still lacks hands and eyesβit doesn't know how to retrieve external data or execute specific tasks.
\n
LangChain is exactly such a framework, acting as a connector and coordinator.
\n\nLangChain skillfully connects powerful language models (such as GPT-4, DeepSeek) with external data sources, computing tools, and memory systems to build powerful, practical AI applications.
\n\nIn short, the core value of LangChain lies in: making language models useful, solving several key limitations of large language models (LLMs):
\n- \n
- Real-time knowledge: LLM training data has an expiration date and cannot access the latest information. \n
- Domain specialization: General-purpose LLMs lack private knowledge specific to certain industries or companies. \n
- Operability: LLMs themselves cannot perform actions such as calculations, database queries, or API calls. \n
- Dialogue coherence: In multi-turn conversations, LLMs need to remember previous chat history. \n
LangChain organizes these functions through a series of standardized chains (Chains) and components (Components), allowing developers to quickly build complex AI applications like building blocks.
\n\nLangChain module overview:
\n- \n
- LLMs / ChatModels: Model interface \n
- Prompt Templates: Prompt structuring \n
- Chains: Workflow orchestration \n
- Memory: Context management \n
- Retrievers / VectorStores: Knowledge retrieval \n
- Agents & Tools: Automated decision-making and execution \n
Environment Setup
\n\nInstall using domestic mirror:
\npip install langchain langchain-openai langchain-community python-dotenv -i https://mirrors.aliyun.com/pypi/simple/\n\n\n If you use OpenAI, you can configure environment variables:
\nexport OPENAI_API_KEY=your_key\n\n\n Then use the following code to test:
\n\nExample
\nfrom langchain_openai import ChatOpenAI\n\nllm = ChatOpenAI(model="gpt-4o-mini", temperature=0)\n\nresp = llm.invoke("Explain what LangChain is in one sentence")\n\nprint(resp.content)\n\n\n In China, we can use the DeepSeek large model for testing. If you haven't created one yet, go to https://platform.deepseek.com/api_keys to create an API key.
\n\nDeepSeek API documentation reference: https://api-docs.deepseek.com/zh-cn/.
\n\nIf you need to uniformly manage multiple third-party models, you can choose to install LiteLLM as a model gateway:
\npip install -U litellm\n\n\n However, the examples in this article directly use `ChatOpenAI` with the `openai_api_base` parameter to connect to DeepSeek, without needing to install LiteLLM separately.
\n\nExample
\nimport os\n\nfrom langchain_openai import ChatOpenAI\n\n# It's recommended to place the API Key in environment variables, or assign it directly here (do not hard-code in production environments)\n\n# os.environ = "sk-your_DeepSeek_key"\n\n# Initialize the model\n\n llm = ChatOpenAI(\n\n model="deepseek-v4-pro",# DeepSeek V4 model name\n\n openai_api_key="sk-your_Key",# Enter your DeepSeek API Key\n\n openai_api_base="https://api.deepseek.com",# DeepSeek API address\n\n temperature=0.7,\n\n max_tokens=1024\n\n)\n\n# Test it\n\n response = llm.invoke("Hello, DeepSeek! Please give a brief self-introduction.")\n\nprint(response.content)\n\n\n Running the above code will output:
\nHello! Nice to meet you! I am DeepSeek, an AI assistant created by DeepSeek company. My features include: ...
\n\n\n\n
Building LCEL Chains (LangChain Expression Language)
\n\nSimply calling the model is not powerful enough; we need to build a standard processing flow:
\nPrompt -> LLM -> OutputParser
\n\nComplete code as follows:
\n\nExample
\nfrom langchain_core.prompts import ChatPromptTemplate\n\nfrom langchain_core.output_parsers import StrOutputParser\n\nfrom langchain_openai import ChatOpenAI\n\n# 1. Define the model (Model)\n\n llm = ChatOpenAI(\n\n model="deepseek-v4-pro",\n\n openai_api_key="your_DEEPSEEK_API_KEY",\n\n openai_api_base="https://api.deepseek.com"\n\n)\n\n# 2. Define the prompt template (Prompt)\n\n# system: Set the AI's role\n\n# user: User's specific input\n\n prompt = ChatPromptTemplate.from_messages([\n\n("system","You are a senior tech expert, skilled at using easy-to-understand'Plain language / Simple words'Explain complex tech concepts. Your explanation should include a vivid analogy."),\n\n("user","{concept}")\n\n])\n\n# 3. Define the output parser (Output Parser)\n\n# Convert the model's Message object directly to plain text\n\nparser= StrOutputParser()\n\n# 4. Build the chain (Chain) - Use pipe operator | to connect\n\n# Flow: Input dictionary -> Fill Prompt -> Send to LLM -> Parse output\n\n chain = prompt | llm | parser\n\n# 5. Invoke the chain\n\n concept_to_explain ="Quantum entanglement"\n\nprint(f"Currently explaining the concept:{concept_to_explain}...\\n")\n\nresult = chain.invoke({"concept": concept_to_explain})\n\nprint("--- DeepSeek Answer ---")\n\nprint(result)\n\n\n Streaming Output
\n\nIn real-world applications (such as chatbots), we need to output text character by character, just like ChatGPT, instead of waiting until generation is complete before displaying all at once.
\n\nLangChain supports this very simply by using .stream() instead of .invoke():
\n\nExample
\nfrom langchain_core.prompts import ChatPromptTemplate\n\nfrom langchain_core.output_parsers import StrOutputParser\n\nfrom langchain_openai import ChatOpenAI\n\n# 1. Define the model (Model)\n\n llm = ChatOpenAI(\n\n model="deepseek-v4-pro",\n\n openai_api_key="your_DEEPSEEK_API_KEY",\n\n openai_api_base="https://api.deepseek.com"\n\n)\n\n# 2. Define the prompt template (Prompt)\n\n# system: Set the AI's role\n\n# user: User's specific input\n\n prompt = ChatPromptTemplate.from_messages([\n\n("system","You are a senior tech expert, skilled at using easy-to-understand'Plain language / Simple words'Explain complex tech concepts. Your explanation should include a vivid analogy."),\n\n("user","{concept}")\n\n])\n\n# 3. Define the output parser (Output Parser)\n\n# Convert the model's Message object directly to plain text\n\nparser= StrOutputParser()\n\n# 4. Build the chain (Chain) - Use pipe operator | to connect\n\n# Flow: Input dictionary -> Fill Prompt -> Send to LLM -> Parse output\n\n chain = prompt | llm | parser\n\n# 5. Invoke the chain\n\n concept_to_explain ="Quantum entanglement"\n\nprint(f"Currently explaining the concept:{concept_to_explain} (Streaming output)...\\n")\n\n# Here, chunk is each generated segment\n\nfor chunk in chain.stream({"concept": "Recurrent Neural Network"}):\n\nprint(chunk, end="", flush=True)\n\n\n Code Structure
\n\nPrompt
\n- \n
ChatPromptTemplateclearly distinguishes betweensystem / user\n - Prompt is a structured input function, not a string \n
LLM
\n- \n
ChatOpenAIis just a unified interface \n - LangChain does not enhance model capabilities, only enhances controllability \n
OutputParser
\n- \n
- Model output is always
Message\n StrOutputParseris explicit type conversion \n - Without a Parser, engineering is uncontrollable \n
\n\n
Detailed Explanation of Core Components
\n\n1. Models
\n\nLangChain provides a unified abstract interface. Currently, it is recommended to use Chat Models, as most modern models (like DeepSeek, GPT-4) are optimized for conversation.
\n- \n
- Chat Models: Input and output are structured messages. \n
SystemMessage: Sets the AI's role. \n HumanMessage: Messages sent by the user. \n AIMessage: Messages returned by the AI. \n
2. Prompts
\n\nIn the new version, it is recommended to use ChatPromptTemplate to build conversational prompts, which better aligns with current mainstream LLM calling habits.
from langchain_core.prompts import ChatPromptTemplate\n# Use from_messages to build structured templates\nprompt_template = ChatPromptTemplate.from_messages([\n ("system", "You are a professional{role}γ"),\n ("user", "{content}")\n])\n# Dynamically fill variables\nprompt = prompt_template.invoke({"role": "Food critic", "content": "Evaluate this bowl of Zhajiangmian (noodles with soybean paste)"})\n# Output: A list containing SystemMessage and HumanMessage\n\n\n \n\n
3. Chains & LCEL
\n\nImportant update: LLMChain has been deprecated. Now use LCEL (pipe operator `|`) syntax. This approach is more flexible and supports asynchronous and streaming outputs.
# Modern way: Prompt | Model | OutputParser\nfrom langchain_core.output_parsers import StrOutputParser\n# Here, llm is an instance of ChatOpenAI(model="deepseek-v4-pro")\nchain = prompt_template | llm | StrOutputParser()\n# Invoke the chain\nresult = chain.invoke({"role": "Tour guide", "content": "Introduce the Forbidden City"})\nprint(result) # Directly outputs string\n\n\n 4. Indexing and Retrieval
\n\nThis is the core of RAG (Retrieval-Augmented Generation). The new version emphasizes converting documents into "retrievers".
\n- \n
- Process:
DocumentLoaders(load) βTextSplitter(split) βEmbeddings(vectorize) βVectorStore(store). \n - Retriever: No longer just database search, it is a component that can be integrated into chains. \n
# Convert vector store to retriever\nretriever = vectorstore.as_retriever()\n# Reference in new-style chain\n# chain = {"context": retriever, "question": RunnablePassthrough()} | prompt | llm\n\n\n Complete RAG Practical Example
\n\nNow let's implement a complete RAG application. First, install additional dependencies:
\npip install langchain-community faiss-cpu sentence-transformers -i https://mirrors.aliyun.com/pypi/simple/\n\n\n Example
\nimport os\n\nfrom dotenv import load_dotenv\n\nfrom langchain_openai import ChatOpenAI\n\nfrom langchain_community.embeddings import HuggingFaceEmbeddings\n\nfrom langchain_core.prompts import ChatPromptTemplate\n\nfrom langchain_core.output_parsers import StrOutputParser\n\nfrom langchain_core.runnables import RunnablePassthrough\n\nfrom langchain_community.vectorstores import FAISS\n\nfrom langchain.text_splitter import RecursiveCharacterTextSplitter\n\n# Load environment variables\n\n load_dotenv()\n\n# Initialize model and Embedding\n\n llm = ChatOpenAI(\n\n model=os.getenv('DEEPSEEK_MODEL','deepseek-v4-pro'),\n\n openai_api_key=os.getenv('DEEPSEEK_API_KEY'),\n\n openai_api_base=os.getenv('DEEPSEEK_BASE_URL','https://api.deepseek.com'),\n\n)\n\n# Use free local embedding model (automatically downloads on first run)\n\n embeddings = HuggingFaceEmbeddings(\n\n model_name="sentence-transformers/all-MiniLM-L6-v2"\n\n)\n\n# --- 1. Prepare knowledge documents ---\n\n# In real projects, these documents can be loaded from files, web pages, databases, etc.\n\n documents =[\n\n"LangChain isAn open-source framework for building large language model applications, created by Harrison Chase in 2022.",\n\n"LangChain Core components include: model interfaces, prompt templates, Chaining, memory, retrieval, and agents.",\n\n"LCELοΌLangChain Expression LanguageοΌis LangChain A new generation of Chaining construction syntax, using the pipe operator | Connects various components.",\n\n"RAGοΌRetrieval-Augmented Generation) By retrieving relevant documents before generation, letting LLMs answer questions beyond their training data.",\n\n"LangGraph is LangChain A new framework launched by the team, specifically used for building complex multi-step AI agent workflows.",\n\n"LangSmith is LangChain An observability platform, used for debugging, testing, and monitoring LLM applications.",\n\n]\n\n# --- 2. Text splitting ---\n\n text_splitter = RecursiveCharacterTextSplitter(\n\n chunk_size=100,\n\n chunk_overlap=20\n\n)\n\n# These documents are already short; in real scenarios, long documents would be split into multiple segments\n\n texts = text_splitter.create_documents(documents)\n\n# --- 3. Vectorization and storage in FAISS ---\n\n vectorstore = FAISS.from_documents(t\n\n
\n
YouTip