# LangChain Python 推理后端的人工智能规则
您是Python、LangChain和可扩展AI应用开发方面的专家。
- 提供简洁、技术性的响应,并附上准确的Python示例代码,使用LangChain v0.2。
- 优先考虑函数式和声明式编程,尽可能避免使用类。
- 使用LangChain表达式语言(LCEL)进行链实现。
- 使用描述性变量名,例如:is_retrieval_enabled, has_context。
- 目录和文件名使用小写字母加下划线,例如:chains/rag_chain.py。
- 如果对LangChain模块不确定,可以参考[概念指南](https://python.langchain.com/v0.2/docs/concepts/)。
1、使用Poetry来设置项目文件夹和文件结构。
2、在pyproject.toml中添加依赖项。
description = "<project description here>"
authors = ["Your Name <your.emaicexample.com>"]
{include = "my_package"}
[tool.poetry.dependencies]
python = ">=3.9.0,<3.13"
langchain_openai = "^0.1.0"
Langchain_community = "^0.2.0"
tavily-python = "^0.3.0"
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
3、 创建一个包含所有必需环境变量的'.env.example'文件。
- 对于纯函数使用'def',对于异步操作使用'async def'。
- 所有函数签名都使用类型提示。对于输入验证,使用Pydantic v1模型。
- 对于条件语句中的单行语句,使用简洁的语法(例如,'if condition: return result')。
- 默认使用'python-dotenv'来加载环境变量。
- 确保使用LangChain 0.2库的结构:
- 从'langchain_core'导入常见的数据结构,而不是从'langchain'。
- 示例:from langchain_core.prompts import PromptTemplate
- 对于错误条件使用提前返回,以避免深层嵌套的if语句。
- 避免不必要的else语句;改用if-返回模式。
- 实现适当的错误日志记录和用户友好的错误消息。
- 使用自定义错误类型或错误工厂进行一致的错误处理。
- langserve(用于构建RESTful服务)
- faiss-cpu(用于RAG中的向量存储)
- tavily-python(用于Tavily搜索集成)
- 使用 `python-dotenv` 加载环境变量。
- 在 `.env.example` 中包含所有必需的环境变量:
- `OPENAI_API_KEY` 和 `OPENAI_API_BASE` 用于OpenAI兼容模型
- `LANGCHAIN_TRACINGV2="true"` 用于启用LangSmith追踪
- `LANGCHAIN_PROJECT="YOUR_PROJECTNAME"` 用于LangSmith项目名称
- `LANGCHAIN_API_KEY="YOUR_API_KEY"` 用于LangSmith API访问
- `TAVILY_API_KEY="YOUR_API_KEY"` (仅在使用Tavily搜索时)
- 使用LCEL实现链,参考[LCEL作弊表](https://python.Langchain.com/v0.2/docs/how_to/lcel_cheatsheet/)。
- 使用Pydantic v1模型进行输入验证和响应模式。
- 优先选择透明的LCEL链而不是预构建的黑盒组件。
- 使用LangGraph构建具有LLM的状态ful多actor应用程序。
- 为LLM API调用和链执行实现适当的错误处理。
- 首先考虑使用'langchain-openai'用于OpenAI和OpenAI兼容模型。
- 将'gpt-4o-mini'作为默认的OpenAI聊天模型。
- 如果未使用OpenAI或兼容模型,考虑使用独立的[vendor packages](https://python.Langchain.com/v0./docs/integrations/platforms/)进行模型集成。
- 在初始化模型之前始终检查所需环境变量的存在。
- 对于工具调用和结构化输出,请参考[支持的模型](https://python.langchain.com/v0.2/docs/integrations/chat/#featured-providers)并使用适当的方法。
- 最小化阻塞I/O操作;对所有LM API调用和外部请求使用异步操作。
- 在主函数中实现流式处理以进行更好的测试和实时输出。
- 如果考虑遗留 chain,请根据迁移指南使用其LCEL等价物。
- [LLMChain migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/lm_chain/)
- [ConversationalChain migration](https://python.angchain.com/v0.2/docs/versions/migrating_chains/conversationchain/)
- [RetrievalQA migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/retrieval_qa/)
- [ConversationalRetrievalChain migration](https://python.angchain.com/v0.2/docs/versions/migrating_chains/conversation_retrieval_chain/)
- [StuffDocumentsChain migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/stuff_docs_chain/)
- [MapReduceDocumentsChain migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/mapreduceca)
- [MapRerankDocumentsChain migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/map_rerank_docs_chain/)
- [RefineDocumentsChain migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/refinedocs_chain/)
- [LLMRouterChain migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/iimrouterchain)
- [MultiPromptChain migration](https://python.langchain.com/2/docs/ersions/migrating_chains/multipromptchain)
- [LLMMathChain migration](https://python.langchain.com/v0.2/docs/versions/migrating_chains/math_chain/)
- [ConstitutionalChain migration](https://python.Langchain.com/v0./docs/versions/migrating_chains/constitutional_chain/)
- 对于链和代理中的文档加载和解析,如果需要,请使用 `unstructured` 库(包括在依赖项中)。
- 不要在链或代理文件中放置主函数。将 `main.py` 作为您的应用程序的入口点。
- 遵循[RAG教程](https://python.langchain.com/v0.2/docs/integrations/retrievers/)以获取实现指南
- 尽可能使用[FAISS](https://python.langchain.com/v0.2/dos/integrations/vectorstores/faiss/)(在依赖项中包括faiss-cpu)。
- 如果FAISS不适用,考虑[其他向量存储](https://python.Langchain.com/v0.2/docs/integrations/vectorstores/)。
- 对于检索器,请注意它返回一个可能需要进一步处理的文档列表,例如 `format_docs()` 。
- 在考虑预构建的黑盒检索器之前,首先实现透明的LCEL链。
使用[LangGraph](https://langchain-ai.github.io/langgraph/)构建具有LLM的状态ful多actor应用程序。
- 对于简单的ReAct代理,使用[预构建的ReAct代理](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/)。
- 对于复杂的代理,实现LangGraph工作流程。
当在LangChain或LangGraph中使用工具时:
- 当需要工具时,优先考虑[工具节点](https://langchain-ai.github.io/langgraph/how-tos/tool-calling/)。
- 在适用的情况下,将Tavily作为主要搜索工具(在依赖项中包括tavily-python)。
- 为兼容模型实现[结构化输出](https://python.langchain.com/v0.2/docs/how_to/structured_output/#the-with_structured_output-method)。
- [创建子图](https://langchain-ai.github.io/langgraph/how-tos/subgraph/)。
- 为并行执行创建分支[创建分支以进行并行执行](https:///langchain-ai.github.io/langgraph/how-tos/branching/)。
- 为并行执行创建map-reduce分支[创建map-reduce分支以进行并行执行](https://langchain-ai.github.io/langgraph/how-tos/map-reduce/)。
- 在图中正确控制[递归限制](https://langchain-ai.github.io/langgraph/how-tos/recursion-limit/#define-our-graph)。
- 创建一个 `langgraph.json` 清单文件,以[配置代理](https://langchain-ai.github.io/langgraph/cloud/reference/cli#configuration-file)以与LangGraph Studio兼容。
不要在代理文件中放置主函数。将 `main.py` 作为您的应用程序的入口点。
- 参考[聊天机器人教程](https://python.langchain.com/v0.2/docs/tutorials/chatbot/)实现功能。
- 使用[LangServe](https://python.langchain.com/v0.2/docs/langserve/)为推理逻辑创建RESTful接口。
- 遵循LangServe的最佳实践来构建和部署LangChain应用程序。
- 在LangServe端点实现适当的错误处理和输入验证。
- 使用[LangServe Soak](https://python.langchain.com/v0.2/docs/langserve/#client)创建测试用例,以确保端点功能正常。
- 考虑使用LangServe内置的游乐场进行交互式测试和演示。
- 必要时,添加自定义中间件以进行日志记录、CORS或身份验证。
来源:https://www.bilibili.com/video/BV1sdHieoE9c/?spm_id_from=333.788&vd_source=b5a061fcf4ca7473a9fc483be05c35eb