lhh737/LangChain-ReAct-Agent
基于 LangChain/LangGraph 的 ReAct Agent,结合 RAG 检索、工具调用与 Streamlit 界面,面向智能客服与报告生成场景。
LangChain ReAct Agent – An AI‑powered chatbot with reasoning, tool use and RAG
What it is
- A Python application that builds a ReAct (Reasoning + Acting) agent on top of LangChain and LangGraph.
- The agent can answer questions from a knowledge base, call external tools (weather, user data, report generation) and switch prompts dynamically, all while showing its internal thought‑action‑observation loop in a Streamlit UI.
Key capabilities
| Feature | What it does |
|---|---|
| ReAct reasoning loop | The agent repeatedly generates a Thought, decides on an Action (e.g., call a tool or run a RAG query), receives an Observation, and loops until it can answer. |
| RAG‑enhanced QA | Documents (txt/pdf) about robot vacuums are indexed in a Chroma vector store using DashScope embeddings; the agent retrieves relevant chunks and lets the LLM summarise them. |
| Multi‑tool calling | Built‑in tools for weather lookup, user‑specific data retrieval, and report generation. The middleware automatically selects the right tool based on the user’s intent. |
| Dynamic prompt switching | Two system‑prompt templates – one for ordinary Q&A, another for report generation – are swapped at runtime by the middleware. |
| Streamlit front‑end | A web UI shows the conversation, streams the LLM output token‑by‑token, and visualises the agent’s internal reasoning chain. |
| Modular, YAML‑driven design | Agent logic, RAG settings, vector‑store parameters and prompt paths live in separate YAML files, making the system easy to re‑configure without code changes. |
How it works (high‑level flow)
- User types a query in the Streamlit chat box.
- The middleware decides which prompt template to use and starts the ReAct loop.
- The agent produces a Thought → decides on an Action (e.g., RAG search or call weather API).
- The selected tool runs, returns an Observation.
- Steps 3‑4 repeat until the agent can emit a final answer, which is streamed back to the UI.
Technology stack
- LLM: Alibaba DashScope ChatTongyi (via the DashScope API).
- Agent framework: LangChain 0.3 + LangGraph 0.2.
- Vector store: Chroma (persistent on‑disk index).
- Embedding model: DashScope embedding service.
- Document processing: PyPDF, RecursiveCharacterTextSplitter.
- Front‑end: Streamlit 1.40.
- Configuration: YAML files for agent, RAG, prompts, Chroma.
Getting started (as described in the README)
# 1. Clone
git clone https://github.com/lhh737/LangChain-ReAct-Agent.git && cd LangChain-ReAct-Agent
# 2. Install
pip install -r requirements.txt
# 3. Set your DashScope API key
export DASHSCOPE_API_KEY="your-key" # or `set DASHSCOPE_API_KEY=your-key` on Windows
# 4. Build the knowledge base (first run only)
python -c "from rag.vector_store import VectorStoreService; VectorStoreService().load_document()"
# 5. Launch the UI
streamlit run app.py
Open http://localhost:8501 in a browser, then try sample questions such as:
- “扫地机器人有哪些主要功能?” → triggers RAG QA.
- “如果机器人无法正常回充,该如何处理?” → triggers fault‑diagnosis reasoning.
- “请根据用户数据生成一份个性化使用报告” → triggers the report‑generation tool chain.
Project layout
agent/– core ReAct logic and tool definitions.rag/– vector‑store handling and RAG service.model/– factory that creates the LLM and embedding client.config/– YAML files controlling behaviour.prompts/– raw prompt templates.utils/– helper modules (config loader, file handling, logging, etc.).app.py– Streamlit entry point.
Who might use this
- Developers wanting a reference implementation of a ReAct‑style agent with tool use.
- Teams building AI‑augmented customer‑service bots that need to combine knowledge‑base lookup with external data.
- Anyone exploring LangChain/LangGraph patterns for dynamic prompt management and RAG integration.
License: MIT (see LICENSE).
Related
- Project
- Project
- Project
- Project
- Project