{"id":17,"date":"2026-02-20T10:00:00","date_gmt":"2026-02-20T02:00:00","guid":{"rendered":"http:\/\/www.gmrea.com\/?p=17"},"modified":"2026-02-20T10:00:00","modified_gmt":"2026-02-20T02:00:00","slug":"langchain-05-best-practices","status":"publish","type":"post","link":"https:\/\/gmrea.com\/?p=17","title":{"rendered":"LangChain \u9ad8\u7ea7\u6280\u5de7\u4e0e\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p>\u672c\u6587\u603b\u7ed3 LangChain \u7684\u9ad8\u7ea7\u6280\u5de7\u3001\u6027\u80fd\u4f18\u5316\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u4f60\u6784\u5efa\u66f4\u5f3a\u5927\u7684 LLM \u5e94\u7528\u3002<\/p>\n<p>## 1. \u6027\u80fd\u4f18\u5316<\/p>\n<p>### 1.1 \u5e76\u53d1\u5904\u7406<br \/>\n&#8220;`python<br \/>\nfrom langchain_core.runnables import RunnableParallel<\/p>\n<p>parallel_chain = RunnableParallel({<br \/>\n    &#8220;summary&#8221;: summarize_prompt | llm,<br \/>\n    &#8220;translation&#8221;: translate_prompt | llm,<br \/>\n    &#8220;sentiment&#8221;: sentiment_prompt | llm<br \/>\n})<br \/>\nresult = parallel_chain.invoke({&#8220;text&#8221;: &#8220;\u4eca\u5929\u5929\u6c14\u4e0d\u9519&#8221;})<br \/>\n# \u6bd4\u987a\u5e8f\u8c03\u7528\u5feb 3 \u500d<br \/>\n&#8220;`<\/p>\n<p>### 1.2 \u6279\u5904\u7406<br \/>\n&#8220;`python<br \/>\nquestions = [&#8220;\u4ec0\u4e48\u662f Python\uff1f&#8221;, &#8220;\u4ec0\u4e48\u662f JavaScript\uff1f&#8221;, &#8220;\u4ec0\u4e48\u662f AI\uff1f&#8221;]<br \/>\nresponses = llm.batch(questions)  # \u4e00\u6b21\u8c03\u7528\uff0c\u5904\u7406\u591a\u4e2a\u8f93\u5165<br \/>\n&#8220;`<\/p>\n<p>### 1.3 \u6d41\u5f0f\u5904\u7406<br \/>\n&#8220;`python<br \/>\nfor chunk in llm.stream(&#8220;\u8bb2\u4e00\u4e2a\u5f88\u957f\u7684\u6545\u4e8b&#8221;):<br \/>\n    print(chunk.content, end=&#8221;&#8221;, flush=True)<br \/>\n&#8220;`<\/p>\n<p>### 1.4 \u7f13\u5b58\u673a\u5236<br \/>\n&#8220;`python<br \/>\nfrom langchain.cache import InMemoryCache<\/p>\n<p>cache = InMemoryCache()<br \/>\nllm = ChatOpenAI(model=&#8221;gpt-4o-mini&#8221;, cache=cache)<br \/>\n# \u76f8\u540c\u5185\u5bb9\u7684\u7b2c\u4e8c\u6b21\u8c03\u7528\u4ece\u7f13\u5b58\u8fd4\u56de<br \/>\n&#8220;`<\/p>\n<p>## 2. \u9ad8\u7ea7\u94fe\u5f0f\u7ec4\u5408<\/p>\n<p>### 2.1 \u6761\u4ef6\u8def\u7531<br \/>\n&#8220;`python<br \/>\nfrom langchain_core.runnables import RunnableBranch<\/p>\n<p>branch = RunnableBranch(<br \/>\n    (lambda x: x[&#8220;route&#8221;] == &#8220;math&#8221;, math_chain),<br \/>\n    (lambda x: x[&#8220;route&#8221;] == &#8220;translate&#8221;, translate_chain),<br \/>\n)<br \/>\n&#8220;`<\/p>\n<p>### 2.2 \u5faa\u73af\u94fe<br \/>\n\u5bf9\u4e8e\u9700\u8981\u8fed\u4ee3\u4f18\u5316\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u5b9e\u73b0 RunnableLoop \u6a21\u5f0f\uff0c\u53cd\u590d\u6267\u884c\u76f4\u5230\u6ee1\u8db3\u6761\u4ef6\u3002<\/p>\n<p>## 3. \u9519\u8bef\u5904\u7406\u548c\u91cd\u8bd5<br \/>\n&#8220;`python<br \/>\nfrom tenacity import retry, stop_after_attempt, wait_exponential<\/p>\n<p>@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=10))<br \/>\ndef safe_invoke(chain, input_data):<br \/>\n    return chain.invoke(input_data)<br \/>\n&#8220;`<\/p>\n<p>## 4. \u6210\u672c\u4f18\u5316<br \/>\n&#8211; Token \u4f7f\u7528\u76d1\u63a7\uff1a\u4f7f\u7528 `get_openai_callback()` \u8ddf\u8e2a\u6210\u672c<br \/>\n&#8211; \u667a\u80fd\u6a21\u578b\u9009\u62e9\uff1a\u7b80\u5355\u4efb\u52a1\u7528 gpt-4o-mini\uff0c\u590d\u6742\u4efb\u52a1\u7528 gpt-4o<br \/>\n&#8211; \u63d0\u793a\u8bcd\u4f18\u5316\uff1a\u7cbe\u7b80\u63d0\u793a\u8bcd\uff0c\u51cf\u5c11 token \u6d88\u8017<\/p>\n<p>## 5. \u5b89\u5168\u6700\u4f73\u5b9e\u8df5<br \/>\n&#8211; \u8f93\u5165\u9a8c\u8bc1\uff1a\u68c0\u67e5\u8f93\u5165\u957f\u5ea6\u3001\u683c\u5f0f\uff0c\u9632\u6b62\u6ce8\u5165\u653b\u51fb<br \/>\n&#8211; \u8f93\u51fa\u8fc7\u6ee4\uff1a\u8fc7\u6ee4\u654f\u611f\u4fe1\u606f\uff08\u7535\u8bdd\u53f7\u7801\u3001\u8eab\u4efd\u8bc1\u3001\u90ae\u7bb1\uff09<br \/>\n&#8211; \u6743\u9650\u63a7\u5236\uff1a\u9650\u5236 Agent \u53ef\u8bbf\u95ee\u7684\u5de5\u5177\u548c\u6570\u636e\u8303\u56f4<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u603b\u7ed3 LangChain \u7684\u9ad8\u7ea7\u6280\u5de7\u3001\u6027\u80fd\u4f18\u5316\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9\u4f60\u6784\u5efa\u66f4\u5f3a\u5927\u7684 LLM \u5e94\u7528\u3002 ## 1.  [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-langchain"],"_links":{"self":[{"href":"https:\/\/gmrea.com\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gmrea.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gmrea.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/gmrea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":0,"href":"https:\/\/gmrea.com\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/gmrea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gmrea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gmrea.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}