LangChain Skills 模式实战:构建按需加载知识的 SQL 助手

2/13/2026
4 min read

在先前的文章中,我们探讨了如何通过 Deep Agents CLI 模拟 Deep Agent 使用 Skills 的模式。如今,LangChain 已原生支持这一特性,极大地简化了开发流程。本文将带领大家深入体验这一功能,构建一个更智能的 SQL 助手。

构建复杂的 AI Agent 时,开发者往往陷入两难境地:是将所有上下文(数据库表结构、API 文档、业务规则)一次性注入 System Prompt,导致上下文窗口(Context Window)溢出且分散模型注意力?还是选择成本高昂的频繁微调(Fine-tuning)?

**Skills 模式(Skills Pattern)**提供了一条优雅的中间路线。它通过动态加载所需知识,实现了上下文的高效利用。LangChain 对此模式的原生支持,意味着我们可以更轻松地构建具备“按需学习”能力的 Agent。

本文将结合官方文档 Build a SQL assistant with on-demand skills,引导读者从零开始,构建一个支持“按需加载知识”的 SQL Assistant。

1. 核心概念:为何选择 Skills 模式?

传统 SQL Agent 的局限性

在传统的 SQL Agent 架构中,我们通常需要在 System Prompt 中提供完整的 Database Schema。随着业务发展,当表数量扩展到数百张时,这种方式会带来显著问题:

  • Token 消耗巨大:每次对话都携带大量无关的表结构,造成资源浪费。

  • 幻觉风险增加:过多的无关干扰信息会降低模型的推理准确性。

  • 维护困难:所有业务线的知识紧密耦合,难以独立迭代。

Skills 模式:基于渐进式披露的解决方案

Skills 模式基于**渐进式披露(Progressive Disclosure)**原则,将知识获取过程分层处理:

  • Agent 初始状态:仅掌握有哪些“技能”(Skills)及其简要描述(Description),保持轻量级。

  • 运行时加载:当面对具体问题(如“查询库存”)时,Agent 主动调用工具(load_skill)加载该技能详细的上下文(Schema + Prompt)。

  • 执行任务:基于加载的精确上下文,执行具体的任务(如编写并执行 SQL)。

这种模式有效支持了无限扩展团队解耦,使 Agent 能够适应日益复杂的业务场景。

2. 系统架构设计

本实战项目将构建一个包含两个核心 Skills 的 SQL Assistant,以演示该模式的实际应用:

  • Sales Analytics(销售分析):负责sales_data表,处理收入统计、订单趋势分析等。

  • Inventory Management(库存管理):负责inventory_items表,处理库存水平监控、位置查询等。

3. 开发环境搭建

本项目采用 Pythonuv进行高效的依赖管理。

核心依赖安装

uv add langchain langchain-openai langgraph psycopg2-binary python-dotenv langchain-community

PostgreSQL 环境配置

本地启动一个 Postgres 实例,并创建agent_platform数据库。我们提供了setup_db.py脚本来自动初始化表结构和测试数据(详见文末源码)。

4. 核心实现步骤详解### ขั้นตอนที่ 1: กำหนดทักษะเฉพาะด้าน (The Knowledge)

เราจะกำหนดทักษะเป็นโครงสร้างพจนานุกรม โดยจำลองกระบวนการโหลดจากระบบไฟล์หรือฐานข้อมูล โปรดสังเกตความแตกต่างระหว่าง description (ใช้สำหรับ Agent ในการตัดสินใจเลือก) และ content (บริบทรายละเอียดที่โหลดจริง)

SKILLS = {"sales_analytics": {"description":"Useful for analyzing sales revenue, trends...","content":"""... Table Schema: sales_data ..."" },"inventory_management": {"description":"Useful for checking stock levels...","content":"""... Table Schema: inventory_items ..."" }}

ขั้นตอนที่ 2: สร้างเครื่องมือหลัก (The Capabilities)

Agent ขึ้นอยู่กับเครื่องมือสำคัญสองอย่างในการทำงานให้สำเร็จ:

  • load_skill(skill_name)**: โหลดรายละเอียดของทักษะที่ระบุแบบไดนามิกในขณะรันไทม์ **

  • run_sql_query(query)**: ดำเนินการคำสั่ง SQL เฉพาะ **

ขั้นตอนที่ 3: จัดระเบียบตรรกะของ Agent (The Brain)

ใช้ LangGraph เพื่อสร้าง ReAct Agent System Prompt มีบทบาทสำคัญในที่นี้ โดยจะแนะนำ Agent ให้ปฏิบัติตามขั้นตอนการปฏิบัติงานมาตรฐาน (SOP) Identify -> Load -> Query อย่างเคร่งครัด

system_prompt ="""1. Identify the relevant skill.2. Use 'load_skill' to get schema.3. Write and execute SQL using 'run_sql_query'....Do not guess table names. Always load the skill first.""

5. การตรวจสอบผลการทำงาน

โดยการรัน test_agent.py เราได้ทำการทดสอบการสืบค้นในสองโดเมนที่แตกต่างกันคือ Sales และ Inventory ด้านล่างนี้คือบันทึกเอาต์พุตจริงจากคอนโซล ซึ่งแสดงให้เห็นว่า Agent โหลดทักษะแบบไดนามิกตามคำถามอย่างไร:

Testing Sales Query...Agent calling tools: [{'name': 'load_skill', 'args': {'skill_name': 'sales_analytics'}, 'id': 'call_f270d76b7ce4404cb5f61bf2', 'type': 'tool_call'}]Tool output:You are a Sales Analytics Expert.You have access to the 'sales_data' table.Table Schema:- id: integer...Agent calling tools: [{'name': 'run_sql_query', 'args': {'query': 'SELECT SUM(amount) as total_revenue FROM sales_data;'}, 'id': 'call_b4f3e686cc7f4f22b3bb9ea7', 'type': 'tool_call'}]Tool output: [(Decimal('730.50'),)]...Agent response: The total revenue is $730.50.Testing Inventory Query...Agent calling tools: [{'name': 'load_skill', 'args': {'skill_name': 'inventory_management'}, 'id': 'call_18c823b2d5064e95a0cfe2e3', 'type': 'tool_call'}]Tool output:You are an Inventory Management Expert.You have access to the 'inventory_items' table.Table Schema...Agent calling tools: [{'name': 'run_sql_query', 'args': {'query': "SELECT warehouse_location FROM inventory_items WHERE product_name = 'Laptop';"}, 'id': 'call_647ee3a444804bd98a045f00', 'type': 'tool_call'}]Tool output: [('Warehouse A',)]...Agent response: The Laptop is located in **Warehouse A**.## 6. แหล่งอ้างอิงซอร์สโค้ดฉบับเต็ม\n\nต่อไปนี้คือซอร์สโค้ดฉบับเต็มของโปรเจ็กต์ ซึ่งรวมถึงสคริปต์เริ่มต้นฐานข้อมูลและโปรแกรมหลักของ Agent\n\n### 1. การเริ่มต้นฐานข้อมูล (setup_db.py)\n\n`importpsycopg2frompsycopg2.extensionsimportISOLATION_LEVEL_AUTOCOMMITimportosfromdotenvimportload_dotenvload_dotenv()# โปรดตรวจสอบให้แน่ใจว่าได้กำหนดค่าข้อมูลการเชื่อมต่อฐานข้อมูลใน .env แล้วDB_HOST = os.getenv(### 2. Agent 主程序 (main.py)

importosfromtypingimportAnnotated, Literal, TypedDict, Union, Dictfromdotenvimportload_dotenvfromlangchain_openaiimportChatOpenAIfromlangchain_core.toolsimporttoolfromlangchain_core.messagesimportSystemMessage, HumanMessage, AIMessage, ToolMessagefromlangchain_community.utilitiesimportSQLDatabasefromlangchain_community.agent_toolkitsimportSQLDatabaseToolkitfromlanggraph.graphimportStateGraph, START, END, MessagesStatefromlanggraph.prebuiltimportToolNode, tools_conditionload_dotenv()# --- Configuration ---BASE_URL = os.getenv("BASIC_MODEL_BASE_URL")API_KEY = os.getenv("BASIC_MODEL_API_KEY")MODEL_NAME = os.getenv("BASIC_MODEL_MODEL")DB_URI =f"postgresql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}:{os.getenv('DB_PORT')}/{os.getenv('DB_NAME')}"# --- Database Setup ---db = SQLDatabase.from_uri(DB_URI)# --- Skills Definition ---SKILLS: Dict[str, Dict[str, str]] = {"sales_analytics": {"description":"Useful for analyzing sales revenue, trends, and regional performance.","content": """You are a Sales Analytics Expert.You have access to the 'sales_data' table.Table Schema:- id: integer (primary key)- transaction_date: date- product_id: varchar(50)- amount: decimal(10, 2)- region: varchar(50)Common queries:- Total revenue: SUM(amount)- Revenue by region: GROUP BY region- Sales trend: GROUP BY transaction_date""" },"inventory_management": {"description":"Useful for checking stock levels, product locations, and warehouse management.","content": """You are an Inventory Management Expert.You have access to the 'inventory_items' table.Table Schema:- id: integer (primary key)- product_id: varchar(50)- product_name: varchar(100)- stock_count: integer- warehouse_location: varchar(50)Common queries:- Check stock: WHERE product_name = '...'- Low stock: WHERE stock_count < threshold""" }}# --- Tools ---@tooldefload_skill(skill_name: str)-> str:""" Load the detailed prompt and schema for a specific skill. Available skills: - sales_analytics: For sales, revenue, and transaction analysis. - inventory_management: For stock, products, and warehouse queries. """ skill = SKILLS.get(skill_name)ifnotskill:returnf"Error: Skill '{skill_name}' not found. Available skills:{list(SKILLS.keys())}"returnskill["content"]@tooldefrun_sql_query(query: str)-> str:""" Execute a SQL query against the database. Only use this tool AFTER loading the appropriate skill to understand the schema. """try:returndb.run(query)exceptExceptionase:returnf"Error executing SQL:{e}"@tooldeflist_tables()-> str:"""List all available tables in the database."""returnstr(db.get_usable_table_names())tools = [load_skill, run_sql_query, list_tables]# --- Agent Setup ---llm = ChatOpenAI( base_url=BASE_URL, api_key=API_KEY, model=MODEL_NAME, temperature=0)llm_with_tools = llm.bind_tools(tools)# --- Graph Definition ---classAgentState(MessagesState):# We can add custom state if needed, but MessagesState is sufficient for simple chatpassdefagent_node(state: AgentState): messages = state["messages"] response = llm_with_tools.invoke(messages)return{"messages": [response]}workflow = StateGraph(AgentState)workflow.add_node("agent", agent_node)workflow.add_node(\ke({"messages": messages}) last_msg = final_state["messages"][-1] ifisinstance(last_msg, AIMessage): print(last_msg.content) messages = final_state["messages"]# Update history print("-"*50) exceptExceptionase: print(f"\nError:{e}") break

Published in Technology

You Might Also Like

📝
Technology

Claude Code Buddy แก้ไขคู่มือ: วิธีการรับสัตว์เลี้ยงระดับตำนานที่เปล่งประกาย

Claude Code Buddy แก้ไขคู่มือ: วิธีการรับสัตว์เลี้ยงระดับตำนานที่เปล่งประกาย วันที่ 1 เมษายน 2026, Anthropic ได้เปิดตัวฟ...

Obsidian เปิดตัว Defuddle ยกระดับ Obsidian Web Clipper สู่ระดับใหม่Technology

Obsidian เปิดตัว Defuddle ยกระดับ Obsidian Web Clipper สู่ระดับใหม่

Obsidian เปิดตัว Defuddle ยกระดับ Obsidian Web Clipper สู่ระดับใหม่ ฉันชอบแนวคิดหลักของ Obsidian มาตลอด: เน้นที่การจัดเ...

OpenAI突然宣布"三合一":浏览器+编程+ChatGPT合并,内部承认过去一年走错了Technology

OpenAI突然宣布"三合一":浏览器+编程+ChatGPT合并,内部承认过去一年走错了

OpenAI突然宣布"三合一":浏览器+编程+ChatGPT合并,内部承认过去一年走错了 ในคืนวันที่ 19 มีนาคม 2026 มีการรั่วไหลของบันทึกภายในจากสำนักงานใหญ่ของ Op...

2026,不再逼自己"自律"!做好这8件小事,健康自然来Health

2026,不再逼自己"自律"!做好这8件小事,健康自然来

2026,不再逼自己"自律"!做好这8件小事,健康自然来 ปีใหม่เริ่มต้นขึ้นแล้ว ปีที่แล้วคุณทำตามเป้าหมายที่ตั้งไว้หรือยัง? คุณเคยรู้สึกสับสนระหว่า...

แม่ๆ ที่พยายามลดน้ำหนักแต่ไม่สำเร็จ แน่นอนว่าต้องเจอปัญหานี้Health

แม่ๆ ที่พยายามลดน้ำหนักแต่ไม่สำเร็จ แน่นอนว่าต้องเจอปัญหานี้

แม่ๆ ที่พยายามลดน้ำหนักแต่ไม่สำเร็จ แน่นอนว่าต้องเจอปัญหานี้ เดือนมีนาคมผ่านไปครึ่งหนึ่งแล้ว แผนการลดน้ำหนักของคุณเป็นอ...

📝
Technology

AI Browser 24 ชั่วโมงการทำงานที่เสถียร

AI Browser 24 ชั่วโมงการทำงานที่เสถียร บทแนะนำนี้จะอธิบายวิธีการตั้งค่า สภาพแวดล้อม AI เบราว์เซอร์ที่เสถียรและทำงานได้ยา...