{"id":16,"date":"2026-02-15T10:00:00","date_gmt":"2026-02-15T02:00:00","guid":{"rendered":"http:\/\/www.gmrea.com\/?p=16"},"modified":"2026-02-15T10:00:00","modified_gmt":"2026-02-15T02:00:00","slug":"langchain-04-document-analyzer","status":"publish","type":"post","link":"https:\/\/gmrea.com\/?p=16","title":{"rendered":"LangChain \u5b9e\u6218\uff1a\u6784\u5efa\u6587\u6863\u5206\u6790\u5de5\u5177"},"content":{"rendered":"<p>\u672c\u6559\u7a0b\u5c06\u5e26\u4f60\u4f7f\u7528 LangChain \u6784\u5efa\u4e00\u4e2a\u667a\u80fd\u6587\u6863\u5206\u6790\u5de5\u5177\uff0c\u80fd\u591f\u81ea\u52a8\u603b\u7ed3\u3001\u63d0\u53d6\u4fe1\u606f\u548c\u5206\u6790\u6587\u6863\u3002<\/p>\n<p>## \u9879\u76ee\u6982\u8ff0<br \/>\n\u6784\u5efa\u4e00\u4e2a\u6587\u6863\u5206\u6790\u5de5\u5177\uff0c\u5b83\u80fd\u591f\uff1a\u52a0\u8f7d\u591a\u79cd\u683c\u5f0f\u7684\u6587\u6863\uff08PDF\u3001Word\u3001TXT\uff09\u3001\u81ea\u52a8\u751f\u6210\u6458\u8981\u3001\u63d0\u53d6\u5173\u952e\u4fe1\u606f\u3001\u5206\u6790\u60c5\u611f\u548c\u4e3b\u9898\u3001\u751f\u6210\u7ed3\u6784\u5316\u62a5\u544a\u3002<\/p>\n<p>## \u51c6\u5907\u5de5\u4f5c<br \/>\n&#8220;`bash<br \/>\npip install langchain langchain-openai langchain-community python-docx PyPDF2 spacy<br \/>\npython -m spacy download zh_core_web_sm<br \/>\n&#8220;`<\/p>\n<p>## \u7b2c\u4e00\u6b65\uff1a\u6587\u6863\u52a0\u8f7d\u5668<br \/>\n\u652f\u6301 PDF\u3001Word\uff08.docx\uff09\u3001TXT\/MD \u683c\u5f0f\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom langchain_core.documents import Document<br \/>\nfrom pathlib import Path<\/p>\n<p>def load_pdf(file_path: str):<br \/>\n    import PyPDF2<br \/>\n    with open(file_path, &#8216;rb&#8217;) as file:<br \/>\n        pdf_reader = PyPDF2.PdfReader(file)<br \/>\n        text = &#8220;&#8221;<br \/>\n        for page in pdf_reader.pages:<br \/>\n            text += page.extract_text() + &#8220;n&#8221;<br \/>\n    return [Document(page_content=text.strip(), metadata={&#8220;source&#8221;: file_path})]<\/p>\n<p>def load_docx(file_path: str):<br \/>\n    import docx<br \/>\n    doc = docx.Document(file_path)<br \/>\n    text = &#8220;n&#8221;.join([para.text for para in doc.paragraphs])<br \/>\n    return [Document(page_content=text.strip(), metadata={&#8220;source&#8221;: file_path})]<\/p>\n<p>def load_txt(file_path: str):<br \/>\n    with open(file_path, &#8216;r&#8217;, encoding=&#8217;utf-8&#8242;) as f:<br \/>\n        text = f.read()<br \/>\n    return [Document(page_content=text.strip(), metadata={&#8220;source&#8221;: file_path})]<br \/>\n&#8220;`<\/p>\n<p>## \u7b2c\u4e8c\u6b65\uff1a\u6587\u6863\u6458\u8981\u5668<br \/>\n&#8220;`python<br \/>\nfrom langchain_openai import ChatOpenAI<br \/>\nfrom langchain_core.prompts import ChatPromptTemplate<br \/>\nfrom langchain_core.output_parsers import StrOutputParser<\/p>\n<p>def summarize_document(document, model_name=&#8221;gpt-4o-mini&#8221;, summary_type=&#8221;detailed&#8221;):<br \/>\n    llm = ChatOpenAI(model=model_name, temperature=0)<\/p>\n<p>    if summary_type == &#8220;detailed&#8221;:<br \/>\n        template = &#8220;\u8bf7\u8be6\u7ec6\u603b\u7ed3\u4ee5\u4e0b\u6587\u6863\u5185\u5bb9\u3002nn\u6587\u6863\u5185\u5bb9:n{content}&#8221;<br \/>\n    elif summary_type == &#8220;brief&#8221;:<br \/>\n        template = &#8220;\u7528 3-5 \u53e5\u8bdd\u7b80\u8981\u603b\u7ed3\u4ee5\u4e0b\u6587\u6863\u3002nn{content}&#8221;<br \/>\n    elif summary_type == &#8220;bullet_points&#8221;:<br \/>\n        template = &#8220;\u7528\u8981\u70b9\u5217\u8868\u603b\u7ed3\u4ee5\u4e0b\u6587\u6863\u3002nn{content}&#8221;<\/p>\n<p>    prompt = ChatPromptTemplate.from_template(template)<br \/>\n    chain = prompt | llm | StrOutputParser()<br \/>\n    return chain.invoke({&#8220;content&#8221;: document.page_content})<br \/>\n&#8220;`<\/p>\n<p>## \u7b2c\u4e09\u6b65\uff1a\u4fe1\u606f\u63d0\u53d6\u5668<br \/>\n&#8220;`python<br \/>\nfrom langchain_core.output_parsers import JsonOutputParser<\/p>\n<p>def extract_entities(document):<br \/>\n    &#8220;&#8221;&#8221;\u63d0\u53d6\u5b9e\u4f53\uff08\u4eba\u7269\u3001\u5730\u70b9\u3001\u7ec4\u7ec7\u7b49\uff09&#8221;&#8221;&#8221;<br \/>\n    llm = ChatOpenAI(model=&#8221;gpt-4o-mini&#8221;, temperature=0)<br \/>\n    prompt = ChatPromptTemplate.from_template(&#8220;&#8221;&#8221;<br \/>\n\u4ece\u4ee5\u4e0b\u6587\u6863\u4e2d\u63d0\u53d6\u5b9e\u4f53\u4fe1\u606f\uff0c\u7528 JSON \u683c\u5f0f\u8fd4\u56de\uff1a<br \/>\n{{<br \/>\n    &#8220;\u4eba\u7269&#8221;: [&#8230;],<br \/>\n    &#8220;\u5730\u70b9&#8221;: [&#8230;],<br \/>\n    &#8220;\u7ec4\u7ec7&#8221;: [&#8230;],<br \/>\n    &#8220;\u65e5\u671f&#8221;: [&#8230;],<br \/>\n    &#8220;\u91d1\u989d&#8221;: [&#8230;]<br \/>\n}}<\/p>\n<p>\u6587\u6863\u5185\u5bb9:<br \/>\n{content}<br \/>\n&#8220;&#8221;&#8221;)<br \/>\n    chain = prompt | llm | JsonOutputParser()<br \/>\n    return chain.invoke({&#8220;content&#8221;: document.page_content})<\/p>\n<p>def extract_key_information(document):<br \/>\n    &#8220;&#8221;&#8221;\u63d0\u53d6\u5173\u952e\u4fe1\u606f&#8221;&#8221;&#8221;<br \/>\n    llm = ChatOpenAI(model=&#8221;gpt-4o-mini&#8221;, temperature=0)<br \/>\n    prompt = ChatPromptTemplate.from_template(&#8220;&#8221;&#8221;<br \/>\n\u4ece\u4ee5\u4e0b\u6587\u6863\u4e2d\u63d0\u53d6\u5173\u952e\u4fe1\u606f\uff08\u4e3b\u8981\u7ed3\u8bba\u3001\u91cd\u8981\u6570\u636e\u3001\u884c\u52a8\u9879\u3001\u98ce\u9669\u3001\u673a\u9047\uff09\uff0c\u7528 JSON \u683c\u5f0f\u8fd4\u56de\u3002<\/p>\n<p>\u6587\u6863\u5185\u5bb9:<br \/>\n{content}<br \/>\n&#8220;&#8221;&#8221;)<br \/>\n    chain = prompt | llm | JsonOutputParser()<br \/>\n    return chain.invoke({&#8220;content&#8221;: document.page_content})<br \/>\n&#8220;`<\/p>\n<p>## \u7b2c\u56db\u6b65\uff1a\u60c5\u611f\u5206\u6790<br \/>\n&#8220;`python<br \/>\ndef analyze_sentiment(document):<br \/>\n    llm = ChatOpenAI(model=&#8221;gpt-4o-mini&#8221;, temperature=0)<br \/>\n    prompt = ChatPromptTemplate.from_template(&#8220;&#8221;&#8221;<br \/>\n\u5206\u6790\u4ee5\u4e0b\u6587\u6863\u7684\u60c5\u611f\u503e\u5411\uff08\u6574\u4f53\u60c5\u611f\u3001\u60c5\u611f\u5f3a\u5ea6\u3001\u4e3b\u8981\u60c5\u7eea\u3001\u5173\u952e\u60c5\u611f\u8bcd\u8bed\uff09\uff0c\u7528 JSON \u683c\u5f0f\u8fd4\u56de\u3002<\/p>\n<p>\u6587\u6863\u5185\u5bb9:<br \/>\n{content}<br \/>\n&#8220;&#8221;&#8221;)<br \/>\n    chain = prompt | llm | JsonOutputParser()<br \/>\n    return chain.invoke({&#8220;content&#8221;: document.page_content})<br \/>\n&#8220;`<\/p>\n<p>## \u7b2c\u4e94\u6b65\uff1a\u4e3b\u7a0b\u5e8f\u96c6\u6210<br \/>\n\u6574\u5408\u6240\u6709\u5206\u6790\u529f\u80fd\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u6587\u6863\u5206\u6790\u6d41\u7a0b\u4e2d\uff0c\u652f\u6301\u547d\u4ee4\u884c\u548c\u6279\u5904\u7406\u6a21\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6559\u7a0b\u5c06\u5e26\u4f60\u4f7f\u7528 LangChain \u6784\u5efa\u4e00\u4e2a\u667a\u80fd\u6587\u6863\u5206\u6790\u5de5\u5177\uff0c\u80fd\u591f\u81ea\u52a8\u603b\u7ed3\u3001\u63d0\u53d6\u4fe1\u606f\u548c\u5206\u6790\u6587\u6863\u3002 ## \u9879\u76ee [&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-16","post","type-post","status-publish","format-standard","hentry","category-langchain"],"_links":{"self":[{"href":"https:\/\/gmrea.com\/index.php?rest_route=\/wp\/v2\/posts\/16","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=16"}],"version-history":[{"count":0,"href":"https:\/\/gmrea.com\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/gmrea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gmrea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gmrea.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}