DeepSeek + LangChain Development Tutorial
LangChain is the most popular framework for building LLM applications. This tutorial starts from scratch and teaches you how to build complete AI applications using DeepSeek models + LangChain.
Start LearningWhy Choose DeepSeek + LangChain?
DeepSeek offers extremely high performance and low-cost API services, while LangChain provides a mature LLM application development framework. Combining the two makes AI application development simple and efficient.
Environment Preparation
Before coding, you need to prepare the Python development environment and necessary dependencies.
1.1 Install Python
Ensure your system has Python 3.9 or higher installed. Python 3.11+ is recommended for better performance.
1.2 Create a Virtual Environment (Recommended)
Using a virtual environment isolates project dependencies and avoids conflicts with other projects.
1.3 Install LangChain and Dependencies
1.4 Get DeepSeek API Key
Visit platform.deepseek.com, register and log in, then create a key on the "API Keys" page. It is recommended to set the API Key as an environment variable:
Security Note
Never hardcode API keys in your code. Use environment variables or a .env file to manage sensitive information. Add the .env file to .gitignore.
Basic Call — Using LangChain to Call DeepSeek
LangChain uses the ChatOpenAI class to be compatible with OpenAI-format APIs. Simply modify the base_url and model parameters to connect to DeepSeek.
2.1 Simplest Call
2.2 Streaming Output
Streaming output allows the AI to return content word by word like typing, improving user experience.
2.3 Using DeepSeek R1 Reasoning Model
DeepSeek R1 is a reasoning-enhanced model that performs better on complex tasks such as mathematics, logic, and programming. The calling method is exactly the same as V3, just change the model name.
Model Selection Recommendations
- deepseek-chat (V3): Daily conversation, content writing, translation, general code generation, best cost-performance
- deepseek-reasoner (R1): Mathematical reasoning, complex logical analysis, algorithm design, scientific research
Chain Chaining
Chain is the core abstraction of LangChain. It connects multiple components together to form reusable processing flows. From simple LLMChain to complex SequentialChain, build your AI workflow step by step.
3.1 LLMChain — The Simplest Chain
LLMChain combines PromptTemplate and LLM, and is the most basic chaining pattern.
3.2 SequentialChain — Execute Multiple Chains Sequentially
SequentialChain connects multiple steps, where the output of the previous step serves as the input to the next.
3.3 Using LCEL (LangChain Expression Language)
LCEL is the recommended way to build chains in LangChain, using the pipe operator | to combine components, making the code more concise and readable.
ConversationChain Dialogue Memory
Memory is the core of a dialogue system. LangChain provides various memory components that allow DeepSeek to remember context and enable natural multi-turn conversations.
4.1 ConversationBufferMemory — Complete Memory
Retains all conversation history. Suitable for short conversations; token consumption increases as the conversation grows long.
4.2 ConversationBufferWindowMemory — Sliding Window Memory
Retains only the most recent K turns to avoid excessive token consumption. Suitable for long conversations.
4.3 ConversationSummaryMemory — Summary Memory
Uses LLM to automatically summarize historical conversations, retaining key information while controlling token consumption.
4.4 Using RunnableWithMessageHistory (Recommended)
The new version of LangChain recommends using RunnableWithMessageHistory, which is more flexible and controllable.
Memory Component Selection Recommendations
- Short conversations (<10 turns): ConversationBufferMemory, retain full context
- Long conversations (>10 turns): ConversationBufferWindowMemory or ConversationSummaryMemory
- Production environment: Recommend RunnableWithMessageHistory, combined with external storage like Redis
Agent Tool Calling
Agent is one of LangChain's most powerful features. It allows the LLM to make autonomous decisions, select and call external tools to accomplish complex tasks.
5.1 Custom Tool — Calculator
Use the @tool decorator to define custom tools, allowing the Agent to call them when needed.
5.2 Web Search Tool
Integrate the Tavily Search API to enable the Agent to search the internet in real time for the latest information.
Agent Development Notes
- Set temperature to 0: Agents require deterministic output to avoid random failures in tool calls.
- Write clear tool descriptions: The docstring directly affects whether the Agent correctly invokes the tool.
- Security first: eval() is for demonstration only; use a safe expression parser in production.
- Use langgraph: LangGraph is the recommended Agent framework by LangChain, more stable than the legacy AgentExecutor.
RAG Retrieval-Augmented Generation
RAG (Retrieval-Augmented Generation) enables DeepSeek to answer questions based on your private documents. This is the core technology for building enterprise knowledge base Q&A systems.
6.1 Complete RAG Flow
From document loading to vector storage to retrieval-based Q&A, the complete RAG implementation process.
6.2 Using a Local Embedding Model
If you don't want to rely on the OpenAI Embedding API, you can use a local model.
RAG Optimization Tips
- chunk_size tuning: Too small loses context, too large reduces retrieval accuracy. For Chinese documents, 300-800 characters is recommended.
- Hybrid retrieval: Combine keyword retrieval (BM25) and vector retrieval to improve recall.
- Re-ranking: Use a Cross-Encoder to re-rank results after retrieval.
- Citation tracing: Require the model to cite specific document fragments in the prompt to enhance credibility.
Integration with Ollama Local Model
If you run DeepSeek models locally with Ollama, LangChain provides seamless integration. Data is processed entirely locally, ensuring privacy and security.
7.1 Using ChatOllama to Connect to Local Model
7.2 Local Ollama + RAG
Combine local Ollama models with RAG workflows to build a fully localized knowledge base Q&A system.
Local Deployment Notes
- Ollama service running: Ensure Ollama is running in the background, listening on localhost:11434 by default
- Model downloaded: Use
ollama listto confirm the model exists - Embedding model: Local RAG requires a separate embedding model, such as nomic-embed-text or bge-m3
- Performance depends on hardware: 8B models run well on consumer GPUs, CPU inference is slower
Complete Project Example — Intelligent Customer Service Bot
Integrate all the knowledge learned earlier to build a complete intelligent customer service bot. It has conversational memory, knowledge base retrieval, tool invocation, and streaming output capabilities.
8.1 Project Structure
8.2 config.py — Configuration File
8.3 tools.py — Custom Tools
8.4 knowledge.py — 知识库管理
8.5 chatbot.py — 核心聊天逻辑
8.6 main.py — Main Entry
8.7 Running the Project
More DeepSeek Tutorials
Continue exploring DeepSeek usage, deployment, and model knowledge.
How to Use DeepSeek Models
Zero-based introduction, covering all four usage methods.
DeepSeek Model Deployment Tutorial
Deployment solutions with Ollama, Docker, vLLM, K8s.
DeepSeek Model Download
Download guides for Ollama, Hugging Face, GitHub.
DeepSeek Model Complete Guide
Technical architecture, Benchmark performance comparison, model selection.