← 返回
AI智能 中文

LangChain

Avoid common LangChain mistakes — LCEL gotchas, memory persistence, RAG chunking, and output parser traps.
避免常见 LangChain 错误:LCEL 陷阱、记忆持久化、RAG 分块和输出解析器陷阱。
ivangdavila
AI智能 clawhub v1.0.0 1 版本 99895.2 Key: 无需
★ 2
Stars
📥 1,866
下载
💾 119
安装
1
版本
#latest

概述

LCEL Basics

  • | pipes output to next — prompt | llm | parser
  • RunnablePassthrough() forwards input unchanged — use in parallel branches
  • RunnableParallel runs branches concurrently — {"a": chain1, "b": chain2}
  • .invoke() for single, .batch() for multiple, .stream() for tokens
  • Input must match expected keys — {"question": x} not just x if prompt expects {question}

Memory Gotchas

  • Memory doesn't auto-persist between sessions — save/load explicitly
  • ConversationBufferMemory grows unbounded — use ConversationSummaryMemory for long chats
  • Memory key must match prompt variable — memory_key="chat_history" needs {chat_history} in prompt
  • return_messages=True for chat models — False returns string for completion models

RAG Chunking

  • Chunk size affects retrieval quality — too small loses context, too large dilutes relevance
  • Chunk overlap prevents cutting mid-sentence — 10-20% overlap typical
  • RecursiveCharacterTextSplitter preserves structure — splits on paragraphs, then sentences
  • Embedding dimension must match vector store — mixing models causes silent failures

Output Parsers

  • PydanticOutputParser needs format instructions in prompt — call .get_format_instructions()
  • Parser failures aren't always loud — malformed JSON may partially parse
  • OutputFixingParser retries with LLM — wraps another parser, fixes errors
  • with_structured_output() on chat models — cleaner than manual parsing for supported models

Retrieval

  • similarity_search returns documents — .page_content for text
  • k parameter controls results count — more isn't always better, noise increases
  • Metadata filtering before similarity — filter={"source": "docs"} in most vector stores
  • max_marginal_relevance_search for diversity — avoids redundant similar chunks

Agents

  • Agents decide tool order dynamically — chains are fixed sequence
  • Tool descriptions matter — agent uses them to decide when to call
  • handle_parsing_errors=True — prevents crash on malformed agent output
  • Max iterations prevents infinite loops — max_iterations=10 default may be too low

Common Mistakes

  • Prompt template variables case-sensitive — {Question}{question}
  • Chat models need message format — ChatPromptTemplate, not PromptTemplate
  • Callbacks not propagating — pass config={"callbacks": [...]} through chain
  • Rate limits crash silently sometimes — wrap in retry logic
  • Token count exceeds context — use trim_messages or summarization for long histories

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-28 23:16 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

ai-intelligence

self-improving agent

pskoett
捕获经验教训、错误和纠正,以实现持续改进。使用时机:(1)命令或操作意外失败;(2)用户纠正……
★ 4,061 📥 799,115
ai-intelligence

Proactive Agent

halthelobster
将AI智能体从任务执行者升级为主动预判需求、持续优化的智能伙伴。集成WAL协议、工作缓冲区、自主定时任务及实战验证模式。Hal Stack核心组件 🦞
★ 838 📥 213,397
ai-intelligence

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,362 📥 318,920