{"id":87719,"date":"2026-07-30T19:40:28","date_gmt":"2026-07-30T11:40:28","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/87719.html"},"modified":"2026-07-30T19:40:28","modified_gmt":"2026-07-30T11:40:28","slug":"langchain-embedding%e4%b8%8e%e5%90%91%e9%87%8f%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/87719.html","title":{"rendered":"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93"},"content":{"rendered":"<p>\u6458\u8981<\/p>\n<p>\u6587\u6863\u5207\u597d\u4e86\u5f97\u5b58\u8d77\u6765&#xff0c;\u8fd8\u5f97\u80fd\u6839\u636e\u95ee\u9898\u627e\u51fa\u6765\u3002Embedding\u628a\u6587\u5b57\u8f6c\u6210\u5411\u91cf&#xff0c;\u5411\u91cf\u6570\u636e\u5e93\u8d1f\u8d23\u5b58\u548c\u67e5\u3002\u672c\u6587\u8bb2\u672c\u5730\u4e2d\u6587Embedding\u6a21\u578b\u7684\u4f7f\u7528\u3001Chroma\/Milvus\u7684\u57fa\u672c\u64cd\u4f5c&#xff0c;\u4ee5\u53ca\u600e\u4e48\u7528\u5143\u6570\u636e\u8fc7\u6ee4\u7f29\u5c0f\u68c0\u7d22\u8303\u56f4\u3002<\/p>\n<p>\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981Embedding<\/p>\n<p>\u7528\u6237\u95ee&#034;\u516c\u53f8\u600e\u4e48\u5904\u7406\u8fdf\u5230&#034;&#xff0c;\u6587\u6863\u91cc\u5199\u7684\u662f&#034;\u5458\u5de5\u5e94\u6309\u65f6\u51fa\u52e4&#xff0c;\u8fdd\u8005\u6309\u5236\u5ea6\u5904\u7406&#034;\u3002\u5173\u952e\u8bcd\u4e0d\u4e00\u6837&#xff0c;\u610f\u601d\u4e00\u6837\u3002<\/p>\n<p>\u666e\u901a\u5173\u952e\u8bcd\u641c\u4e0d\u5230&#xff0c;\u5f97\u9760\u8bed\u4e49\u68c0\u7d22\u3002<\/p>\n<p>Embedding\u7684\u4f5c\u7528\u5c31\u662f\u628a\u6587\u672c\u8f6c\u6210\u4e00\u7ec4\u6570\u5b57&#xff08;\u5411\u91cf&#xff09;\u3002\u610f\u601d\u8d8a\u63a5\u8fd1\u7684\u6587\u672c&#xff0c;\u5411\u91cf\u8ddd\u79bb\u8d8a\u8fd1\u3002<\/p>\n<p>python<\/p>\n<p># \u4f2a\u4ee3\u7801\u793a\u610f<br \/>\n&#034;\u4eba\u5de5\u667a\u80fd&#034; \u2192 [0.12, -0.35, 0.78, &#8230;]<br \/>\n&#034;\u673a\u5668\u5b66\u4e60&#034; \u2192 [0.11, -0.33, 0.80, &#8230;]  # \u8ddd\u79bb\u8fd1<br \/>\n&#034;\u7ea2\u70e7\u8089&#034;   \u2192 [0.89, 0.45, -0.23, &#8230;]  # \u8ddd\u79bb\u8fdc<\/p>\n<p>\u4e8c\u3001Embedding\u548c\u804a\u5929\u6a21\u578b\u7684\u533a\u522b<\/p>\n<table>\n<tr>\u7c7b\u578b\u8f93\u51fa\u7528\u9014<\/tr>\n<tbody>\n<tr>\n<td>\u804a\u5929\u6a21\u578b&#xff08;DeepSeek&#xff09;<\/td>\n<td>\u6587\u672c<\/td>\n<td>\u56de\u7b54\u95ee\u9898\u3001\u751f\u6210\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>Embedding\u6a21\u578b<\/td>\n<td>\u5411\u91cf&#xff08;\u6570\u5b57\u5217\u8868&#xff09;<\/td>\n<td>\u8ba1\u7b97\u6587\u672c\u76f8\u4f3c\u5ea6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u77e5\u8bc6\u5e93\u9879\u76ee\u4e2d\u4e24\u8005\u914d\u5408&#xff1a;Embedding\u627e\u76f8\u5173\u6587\u6863&#xff0c;\u804a\u5929\u6a21\u578b\u8bfb\u6587\u6863\u540e\u751f\u6210\u56de\u7b54\u3002<\/p>\n<p>\u4e09\u3001\u5b89\u88c5\u4f9d\u8d56<\/p>\n<p>bash<\/p>\n<p>pip install langchain-community chromadb pymilvus dashscope<\/p>\n<p>\u4f7f\u7528\u672c\u5730\u4e2d\u6587Embedding\u6a21\u578b&#xff08;bge-small-zh&#xff09;&#xff0c;\u6216\u8005\u963f\u91cc\u4e91DashScope\u7684text-embedding-v4\u3002\u8bfe\u5802\u7528\u672c\u5730\u6a21\u578b&#xff0c;\u4e0d\u6d88\u8017API\u989d\u5ea6\u3002<\/p>\n<p>\u56db\u3001\u6848\u4f8b\u4e00&#xff1a;\u628a\u6587\u672c\u8f6c\u6362\u6210\u5411\u91cf<\/p>\n<p>python<\/p>\n<p>from langchain_community.embeddings import HuggingFaceEmbeddings<\/p>\n<p>embeddings &#061; HuggingFaceEmbeddings(<br \/>\n    model_name&#061;&#034;BAAI\/bge-small-zh-v1.5&#034;,<br \/>\n    model_kwargs&#061;{&#034;device&#034;: &#034;cpu&#034;},<br \/>\n    encode_kwargs&#061;{&#034;normalize_embeddings&#034;: True}<br \/>\n)<\/p>\n<p>texts &#061; [&#034;\u4eba\u5de5\u667a\u80fd\u6b63\u5728\u6539\u53d8\u4e16\u754c&#034;, &#034;\u673a\u5668\u5b66\u4e60\u662fAI\u7684\u4e00\u4e2a\u5206\u652f&#034;]<\/p>\n<p># \u6279\u91cf\u751f\u6210\u6587\u6863\u5411\u91cf<br \/>\nvectors &#061; embeddings.embed_documents(texts)<br \/>\nprint(len(vectors))<br \/>\nprint(len(vectors[0]))  # \u5411\u91cf\u7ef4\u5ea6<\/p>\n<p># \u751f\u6210\u67e5\u8be2\u5411\u91cf<br \/>\nquery_vec &#061; embeddings.embed_query(&#034;\u4ec0\u4e48\u662fAI&#034;)<br \/>\nprint(len(query_vec))<\/p>\n<p>embed_documents\u7528\u4e8e\u6587\u6863\u5217\u8868&#xff0c;embed_query\u7528\u4e8e\u7528\u6237\u95ee\u9898\u3002\u4e24\u8005\u4f7f\u7528\u7684\u6a21\u578b\u5fc5\u987b\u4e00\u81f4\u3002<\/p>\n<p>\u4e94\u3001\u6848\u4f8b\u4e8c&#xff1a;\u6279\u91cf\u751f\u6210\u6587\u672c\u5411\u91cf<\/p>\n<p>\u5728model_factory.py\u4e2d\u5c01\u88c5Embedding\u65b9\u6cd5&#xff1a;<\/p>\n<p>python<\/p>\n<p>from langchain_community.embeddings import HuggingFaceEmbeddings<\/p>\n<p>def get_embeddings(model_name&#061;&#034;BAAI\/bge-small-zh-v1.5&#034;):<br \/>\n    return HuggingFaceEmbeddings(<br \/>\n        model_name&#061;model_name,<br \/>\n        model_kwargs&#061;{&#034;device&#034;: &#034;cpu&#034;},<br \/>\n        encode_kwargs&#061;{&#034;normalize_embeddings&#034;: True}<br \/>\n    )<\/p>\n<p>\u4f7f\u7528&#xff1a;<\/p>\n<p>python<\/p>\n<p>from utils.model_factory import get_embeddings<\/p>\n<p>embeddings &#061; get_embeddings()<\/p>\n<p>chunks &#061; [&#034;\u6587\u6863\u57571&#034;, &#034;\u6587\u6863\u57572&#034;, &#034;\u6587\u6863\u57573&#034;]<br \/>\nvectors &#061; embeddings.embed_documents(chunks)<\/p>\n<p>query &#061; &#034;\u7528\u6237\u95ee\u9898&#034;<br \/>\nquery_vector &#061; embeddings.embed_query(query)<\/p>\n<p>\u516d\u3001\u4ec0\u4e48\u662f\u5411\u91cf\u6570\u636e\u5e93<\/p>\n<p>\u6587\u672c\u8f6c\u6210\u5411\u91cf\u540e\u9700\u8981\u5b58\u8d77\u6765&#xff0c;\u8fd8\u8981\u652f\u6301\u6839\u636e\u5411\u91cf\u627e\u76f8\u4f3c\u5185\u5bb9\u3002\u8fd9\u5c31\u662f\u5411\u91cf\u6570\u636e\u5e93\u3002<\/p>\n<p>\u5411\u91cf\u6570\u636e\u5e93\u4fdd\u5b58\u4e09\u7c7b\u6570\u636e&#xff1a;<\/p>\n<p>\u6587\u672c\u5185\u5bb9&#xff08;\u6587\u6863\u5757\u6b63\u6587&#xff09;<br \/>\n\u5411\u91cf&#xff08;\u6570\u5b57\u5217\u8868&#xff09;<br \/>\n\u5143\u6570\u636e&#xff08;\u6587\u4ef6\u540d\u3001\u9875\u7801\u3001\u5206\u7c7b\u7b49&#xff09;<\/p>\n<p>\u68c0\u7d22\u6d41\u7a0b&#xff1a;<\/p>\n<p>\u7528\u6237\u95ee\u9898 \u2192 \u8f6c\u6210\u67e5\u8be2\u5411\u91cf \u2192 \u5411\u91cf\u5e93\u641c\u7d22\u6700\u76f8\u4f3c\u7684\u6587\u6863\u5411\u91cf \u2192 \u8fd4\u56de\u5bf9\u5e94\u7684\u6587\u672c\u548c\u5143\u6570\u636e<\/p>\n<p>\u4e03\u3001\u5411\u91cf\u6570\u636e\u5e93\u9009\u578b<\/p>\n<table>\n<tr>\u7c7b\u578b\u4ea7\u54c1\u9002\u7528\u573a\u666f<\/tr>\n<tbody>\n<tr>\n<td>\u5f00\u6e90\u672c\u5730<\/td>\n<td>Chroma<\/td>\n<td>\u5f00\u53d1\u6d4b\u8bd5\u3001\u8f7b\u91cf\u7ea7RAG<\/td>\n<\/tr>\n<tr>\n<td>\u5f00\u6e90\u5206\u5e03\u5f0f<\/td>\n<td>Milvus<\/td>\n<td>\u5927\u89c4\u6a21\u751f\u4ea7\u73af\u5883<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u5efa\u7ec4\u4ef6<\/td>\n<td>FAISS<\/td>\n<td>\u5d4c\u5165\u81ea\u5efa\u7cfb\u7edf<\/td>\n<\/tr>\n<tr>\n<td>\u4e91\u6258\u7ba1<\/td>\n<td>Pinecone\u3001\u963f\u91cc\u4e91\u5411\u91cf\u670d\u52a1<\/td>\n<td>\u4f01\u4e1a\u7ea7\u6258\u7ba1\u65b9\u6848<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8bfe\u7a0b\u7528Chroma\u505a\u6f14\u793a&#xff0c;Milvus\u505a\u6848\u4f8b\u3002<\/p>\n<p>\u516b\u3001Chroma\u5411\u91cf\u6570\u636e\u5e93<\/p>\n<p>python<\/p>\n<p>from langchain_chroma import Chroma<br \/>\nfrom utils.model_factory import get_embeddings<\/p>\n<p>embeddings &#061; get_embeddings()<\/p>\n<p># \u4ece\u6587\u6863\u521b\u5efa\u5411\u91cf\u5e93<br \/>\nvector_store &#061; Chroma.from_documents(<br \/>\n    documents&#061;chunks,  # Document\u5217\u8868<br \/>\n    embedding&#061;embeddings,<br \/>\n    persist_directory&#061;&#034;.\/chroma_db&#034;  # \u6301\u4e45\u5316\u76ee\u5f55<br \/>\n)<\/p>\n<p># \u68c0\u7d22<br \/>\nresults &#061; vector_store.similarity_search(&#034;\u5458\u5de5\u8fdf\u5230\u600e\u4e48\u5904\u7406&#034;, k&#061;3)<br \/>\nfor doc in results:<br \/>\n    print(doc.page_content)<br \/>\n    print(doc.metadata)<\/p>\n<p>persist_directory\u6307\u5b9a\u6301\u4e45\u5316\u76ee\u5f55&#xff0c;\u4e0b\u6b21\u542f\u52a8\u65f6\u53ef\u4ee5\u4ece\u78c1\u76d8\u52a0\u8f7d&#xff0c;\u4e0d\u7528\u91cd\u65b0\u5efa\u5e93\u3002<\/p>\n<p>\u52a0\u8f7d\u5df2\u6709\u5411\u91cf\u5e93&#xff1a;<\/p>\n<p>python<\/p>\n<p>vector_store &#061; Chroma(<br \/>\n    persist_directory&#061;&#034;.\/chroma_db&#034;,<br \/>\n    embedding_function&#061;embeddings<br \/>\n)<\/p>\n<p>\u4e5d\u3001\u76f8\u4f3c\u5ea6\u5206\u6570<\/p>\n<p>similarity_search\u53ea\u8fd4\u56de\u6587\u6863&#xff0c;\u4e0d\u8fd4\u56de\u5206\u6570\u3002\u7528similarity_search_with_score\u67e5\u770b\u76f8\u4f3c\u5ea6&#xff1a;<\/p>\n<p>python<\/p>\n<p>results &#061; vector_store.similarity_search_with_score(&#034;\u5458\u5de5\u8fdf\u5230\u600e\u4e48\u5904\u7406&#034;, k&#061;3)<\/p>\n<p>for doc, score in results:<br \/>\n    print(f&#034;\u5206\u6570&#xff1a;{score}&#034;)<br \/>\n    print(f&#034;\u5185\u5bb9&#xff1a;{doc.page_content[:100]}&#034;)<br \/>\n    print(&#034;-&#034; * 40)<\/p>\n<p>\u4e0d\u540c\u5411\u91cf\u5e93\u7684\u5206\u6570\u542b\u4e49\u4e0d\u540c&#xff0c;\u4e0d\u8981\u53ea\u51ed\u56fa\u5b9a\u6570\u5b57\u505a\u5224\u65ad\u3002\u7ed3\u5408Top K\u7ed3\u679c\u3001\u6587\u6863\u5185\u5bb9\u3001\u6d4b\u8bd5\u96c6\u6548\u679c\u7efc\u5408\u8bc4\u4f30\u3002<\/p>\n<p>\u5341\u3001Milvus\u5411\u91cf\u6570\u636e\u5e93<\/p>\n<p>Milvus\u662f\u5f00\u6e90\u5206\u5e03\u5f0f\u5411\u91cf\u6570\u636e\u5e93&#xff0c;\u9002\u5408\u5927\u89c4\u6a21\u751f\u4ea7\u73af\u5883\u3002<\/p>\n<p>\u5b89\u88c5&#xff1a;<\/p>\n<p>bash<\/p>\n<p>pip install pymilvus langchain-milvus<\/p>\n<p>\u786e\u4fddMilvus\u670d\u52a1\u5df2\u542f\u52a8&#xff08;Docker\u6216\u672c\u5730\u90e8\u7f72&#xff09;\u3002<\/p>\n<p>python<\/p>\n<p>from langchain_milvus import Milvus<br \/>\nfrom utils.model_factory import get_embeddings<\/p>\n<p>embeddings &#061; get_embeddings()<\/p>\n<p># \u8fde\u63a5Milvus\u5e76\u5199\u5165<br \/>\nvector_store &#061; Milvus.from_documents(<br \/>\n    documents&#061;chunks,<br \/>\n    embedding&#061;embeddings,<br \/>\n    connection_args&#061;{&#034;host&#034;: &#034;localhost&#034;, &#034;port&#034;: &#034;19530&#034;},<br \/>\n    collection_name&#061;&#034;knowledge_base&#034;,<br \/>\n    drop_old&#061;True,<br \/>\n    auto_id&#061;True,<br \/>\n    index_params&#061;{<br \/>\n        &#034;index_type&#034;: &#034;HNSW&#034;,<br \/>\n        &#034;metric_type&#034;: &#034;COSINE&#034;,<br \/>\n        &#034;params&#034;: {&#034;M&#034;: 16, &#034;efConstruction&#034;: 128}<br \/>\n    }<br \/>\n)<\/p>\n<p># \u68c0\u7d22<br \/>\nresults &#061; vector_store.similarity_search(&#034;\u5458\u5de5\u8fdf\u5230\u600e\u4e48\u5904\u7406&#034;, k&#061;3)<\/p>\n<p>index_params\u53c2\u6570\u8bf4\u660e&#xff1a;<\/p>\n<p>index_type&#061;&#034;HNSW&#034;&#xff1a;\u5206\u5c42\u5bfc\u822a\u5c0f\u4e16\u754c\u56fe&#xff0c;\u68c0\u7d22\u901f\u5ea6\u5feb&#xff0c;\u9002\u5408\u767e\u4e07\u7ea7\u4ee5\u5185\u5411\u91cf\u3002<br \/>\nmetric_type&#061;&#034;COSINE&#034;&#xff1a;\u4f59\u5f26\u76f8\u4f3c\u5ea6&#xff0c;\u4e2d\u6587Embedding\u9996\u9009\u3002<br \/>\nM&#061;16&#xff1a;\u6bcf\u4e2a\u8282\u70b9\u6700\u5927\u8fde\u63a5\u90bb\u5c45\u6570&#xff0c;\u8d8a\u5927\u5185\u5b58\u5360\u7528\u8d8a\u9ad8\u3002<br \/>\nefConstruction&#061;128&#xff1a;\u6784\u5efa\u65f6\u5019\u9009\u90bb\u5c45\u6570&#xff0c;\u8d8a\u5927\u7d22\u5f15\u8d28\u91cf\u8d8a\u597d\u3002<\/p>\n<p>\u5e26\u5206\u6570\u7684\u68c0\u7d22&#xff1a;<\/p>\n<p>python<\/p>\n<p>results &#061; vector_store.similarity_search_with_score(&#034;\u5458\u5de5\u8fdf\u5230\u600e\u4e48\u5904\u7406&#034;, k&#061;3)<\/p>\n<p>\u8fd4\u56de\u7684score\u662f\u8ddd\u79bb\u503c&#xff0c;\u8d8a\u5c0f\u8868\u793a\u8d8a\u76f8\u4f3c\u3002<\/p>\n<p>\u5341\u4e00\u3001\u5143\u6570\u636e\u8fc7\u6ee4<\/p>\n<p>\u77e5\u8bc6\u5e93\u53ef\u80fd\u6709\u591a\u4e2a\u5206\u7c7b&#xff0c;\u68c0\u7d22\u65f6\u53ef\u4ee5\u6309\u5143\u6570\u636e\u8fc7\u6ee4\u3002<\/p>\n<p>python<\/p>\n<p># \u53ea\u68c0\u7d22\u4eba\u4e8b\u5236\u5ea6<br \/>\nresults &#061; vector_store.similarity_search(<br \/>\n    &#034;\u8fdf\u5230\u600e\u4e48\u5904\u7406&#034;,<br \/>\n    k&#061;3,<br \/>\n    expr&#061;&#039;file_name &#061;&#061; &#034;employee_handbook.txt&#034;&#039;<br \/>\n)<\/p>\n<p># \u53ea\u68c0\u7d22PDF\u6587\u4ef6<br \/>\nresults &#061; vector_store.similarity_search(<br \/>\n    &#034;\u4ea7\u54c1\u53c2\u6570&#034;,<br \/>\n    k&#061;3,<br \/>\n    expr&#061;&#039;file_type &#061;&#061; &#034;pdf&#034;&#039;<br \/>\n)<\/p>\n<p>\u6ce8\u610fMilvus\u4f7f\u7528expr\u8868\u8fbe\u5f0f\u8bed\u6cd5&#xff0c;\u4e0d\u662ffilter\u53c2\u6570\u3002<\/p>\n<p>\u5143\u6570\u636e\u8fc7\u6ee4\u9002\u5408&#xff1a;\u53ea\u68c0\u7d22\u67d0\u4e2a\u90e8\u95e8\u7684\u8d44\u6599\u3001\u53ea\u68c0\u7d22\u67d0\u4e2a\u4ea7\u54c1\u7ebf\u7684\u624b\u518c\u3001\u53ea\u68c0\u7d22\u67d0\u4e2a\u6587\u4ef6\u6765\u6e90\u3002<\/p>\n<p>\u5341\u4e8c\u3001\u4f01\u4e1a\u6848\u4f8b&#xff1a;\u77e5\u8bc6\u5e93\u68c0\u7d22\u7d22\u5f15<\/p>\n<p>\u9879\u76ee\u7ed3\u6784&#xff1a;<\/p>\n<p>text<\/p>\n<p>knowledge_base\/<br \/>\n\u251c\u2500\u2500 hr\/<br \/>\n\u2502   \u2514\u2500\u2500 employee_handbook.txt<br \/>\n\u251c\u2500\u2500 customer_service\/<br \/>\n\u2502   \u2514\u2500\u2500 refund_policy.md<br \/>\n\u2514\u2500\u2500 product\/<br \/>\n    \u2514\u2500\u2500 product_manual.pdf<\/p>\n<p>build_index.py<br \/>\nsearch_knowledge.py<br \/>\ndocument_loader.py<br \/>\nembedding_factory.py<\/p>\n<p>embedding_factory.py&#xff1a;<\/p>\n<p>python<\/p>\n<p>from langchain_community.embeddings import HuggingFaceEmbeddings<\/p>\n<p>def get_embeddings():<br \/>\n    return HuggingFaceEmbeddings(<br \/>\n        model_name&#061;&#034;BAAI\/bge-small-zh-v1.5&#034;,<br \/>\n        model_kwargs&#061;{&#034;device&#034;: &#034;cpu&#034;},<br \/>\n        encode_kwargs&#061;{&#034;normalize_embeddings&#034;: True}<br \/>\n    )<\/p>\n<p>document_loader.py&#xff1a;<\/p>\n<p>python<\/p>\n<p>from pathlib import Path<br \/>\nfrom langchain_community.document_loaders import TextLoader, PyPDFLoader<br \/>\nfrom langchain_text_splitters import RecursiveCharacterTextSplitter<\/p>\n<p>def load_and_split(directory_path, chunk_size&#061;500, chunk_overlap&#061;50):<br \/>\n    docs &#061; []<br \/>\n    directory &#061; Path(directory_path)<\/p>\n<p>    for file_path in sorted(directory.rglob(&#034;*&#034;)):<br \/>\n        if not file_path.is_file():<br \/>\n            continue<br \/>\n        suffix &#061; file_path.suffix.lower()<br \/>\n        try:<br \/>\n            if suffix &#061;&#061; &#034;.txt&#034;:<br \/>\n                loader &#061; TextLoader(str(file_path), encoding&#061;&#034;utf-8&#034;)<br \/>\n            elif suffix &#061;&#061; &#034;.md&#034;:<br \/>\n                loader &#061; TextLoader(str(file_path), encoding&#061;&#034;utf-8&#034;)<br \/>\n            elif suffix &#061;&#061; &#034;.pdf&#034;:<br \/>\n                loader &#061; PyPDFLoader(str(file_path))<br \/>\n            else:<br \/>\n                continue<br \/>\n            loaded &#061; loader.load()<br \/>\n            for doc in loaded:<br \/>\n                doc.metadata[&#034;file_name&#034;] &#061; file_path.name<br \/>\n                doc.metadata[&#034;file_type&#034;] &#061; suffix[1:]<br \/>\n                doc.metadata[&#034;category&#034;] &#061; file_path.parent.name<br \/>\n            docs.extend(loaded)<br \/>\n        except Exception as e:<br \/>\n            print(f&#034;\u52a0\u8f7d\u5931\u8d25&#xff1a;{file_path.name}&#xff0c;\u9519\u8bef&#xff1a;{e}&#034;)<\/p>\n<p>    splitter &#061; RecursiveCharacterTextSplitter(<br \/>\n        chunk_size&#061;chunk_size,<br \/>\n        chunk_overlap&#061;chunk_overlap<br \/>\n    )<br \/>\n    return splitter.split_documents(docs)<\/p>\n<p>build_index.py&#xff1a;<\/p>\n<p>python<\/p>\n<p>from langchain_milvus import Milvus<br \/>\nfrom embedding_factory import get_embeddings<br \/>\nfrom document_loader import load_and_split<\/p>\n<p>print(&#034;\u52a0\u8f7d\u5e76\u5207\u5206\u6587\u6863&#8230;&#034;)<br \/>\nchunks &#061; load_and_split(&#034;knowledge_base&#034;)<br \/>\nprint(f&#034;\u751f\u6210{len(chunks)}\u4e2a\u6587\u6863\u5757&#034;)<\/p>\n<p>print(&#034;\u5199\u5165Milvus&#8230;&#034;)<br \/>\nembeddings &#061; get_embeddings()<\/p>\n<p>vector_store &#061; Milvus.from_documents(<br \/>\n    documents&#061;chunks,<br \/>\n    embedding&#061;embeddings,<br \/>\n    connection_args&#061;{&#034;host&#034;: &#034;localhost&#034;, &#034;port&#034;: &#034;19530&#034;},<br \/>\n    collection_name&#061;&#034;knowledge_base&#034;,<br \/>\n    drop_old&#061;True,<br \/>\n    index_params&#061;{<br \/>\n        &#034;index_type&#034;: &#034;HNSW&#034;,<br \/>\n        &#034;metric_type&#034;: &#034;COSINE&#034;,<br \/>\n        &#034;params&#034;: {&#034;M&#034;: 16, &#034;efConstruction&#034;: 128}<br \/>\n    }<br \/>\n)<\/p>\n<p>print(&#034;\u7d22\u5f15\u6784\u5efa\u5b8c\u6210&#034;)<\/p>\n<p>search_knowledge.py&#xff1a;<\/p>\n<p>python<\/p>\n<p>from langchain_milvus import Milvus<br \/>\nfrom embedding_factory import get_embeddings<\/p>\n<p>embeddings &#061; get_embeddings()<\/p>\n<p>vector_store &#061; Milvus(<br \/>\n    embedding_function&#061;embeddings,<br \/>\n    connection_args&#061;{&#034;host&#034;: &#034;localhost&#034;, &#034;port&#034;: &#034;19530&#034;},<br \/>\n    collection_name&#061;&#034;knowledge_base&#034;<br \/>\n)<\/p>\n<p>while True:<br \/>\n    question &#061; input(&#034;\\\\n\u8bf7\u8f93\u5165\u95ee\u9898&#xff08;\u8f93\u5165q\u9000\u51fa&#xff09;&#xff1a;&#034;)<br \/>\n    if question.lower() &#061;&#061; &#034;q&#034;:<br \/>\n        break<\/p>\n<p>    results &#061; vector_store.similarity_search_with_score(question, k&#061;3)<\/p>\n<p>    print(f&#034;\\\\n\u76f8\u5173\u6587\u6863\u5757&#xff1a;&#034;)<br \/>\n    for i, (doc, score) in enumerate(results, 1):<br \/>\n        print(f&#034;{i}. \u6765\u6e90&#xff1a;{doc.metadata.get(&#039;file_name&#039;)}&#034;)<br \/>\n        print(f&#034;   \u5206\u7c7b&#xff1a;{doc.metadata.get(&#039;category&#039;)}&#034;)<br \/>\n        print(f&#034;   \u76f8\u4f3c\u5ea6&#xff1a;{score:.4f}&#034;)<br \/>\n        print(f&#034;   \u5185\u5bb9&#xff1a;{doc.page_content[:150]}&#8230;&#034;)<br \/>\n        print()<\/p>\n<p>\u5341\u4e09\u3001\u68c0\u7d22\u6548\u679c\u8c03\u4f18<\/p>\n<p>\u4e0d\u51c6\u7684\u8bdd\u68c0\u67e5\u8fd9\u4e9b&#xff1a;<\/p>\n<p>\u6587\u6863\u5757\u592a\u77ed\u6216\u592a\u957f&#xff1a;\u8c03\u6574chunk_size<br \/>\n\u4e0a\u4e0b\u6587\u88ab\u5207\u65ad&#xff1a;\u589e\u5927chunk_overlap<br \/>\n\u6807\u9898\u548c\u6b63\u6587\u5206\u79bb&#xff1a;\u8c03\u6574\u5206\u9694\u7b26<br \/>\n\u67e5\u8be2\u548c\u6587\u6863\u8868\u8fbe\u5dee\u5f02\u5927&#xff1a;\u589e\u52a0\u540c\u4e49\u8868\u8fbe<br \/>\nEmbedding\u6a21\u578b\u4e0d\u9002\u5408\u4e2d\u6587&#xff1a;\u6362\u6a21\u578b<\/p>\n<p>\u51c6\u5907\u4e00\u7ec4\u6d4b\u8bd5\u95ee\u9898&#xff0c;\u9010\u4e2a\u89c2\u5bdfTop 3\u7ed3\u679c\u662f\u5426\u76f8\u5173\u3002\u8fd9\u662f\u6700\u65e9\u671f\u7684\u8bc4\u4f30\u65b9\u5f0f\u3002<\/p>\n<p>\u5e38\u89c1\u95ee\u9898<\/p>\n<p>DeepSeek\u80fd\u505aEmbedding\u5417<\/p>\n<p>DeepSeek\u662f\u804a\u5929\u6a21\u578b&#xff0c;\u4e0d\u505aEmbedding\u3002\u672c\u8bfe\u7a0b\u7528\u672c\u5730\u4e2d\u6587\u6a21\u578b&#xff0c;\u4e0d\u6d88\u8017API\u989d\u5ea6\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u7528\u540c\u4e00\u4e2aEmbedding\u6a21\u578b<\/p>\n<p>\u5efa\u5e93\u548c\u67e5\u8be2\u5fc5\u987b\u7528\u540c\u4e00\u4e2a\u6a21\u578b&#xff0c;\u5426\u5219\u5411\u91cf\u7a7a\u95f4\u4e0d\u4e00\u81f4&#xff0c;\u68c0\u7d22\u6548\u679c\u5dee\u3002<\/p>\n<p>\u76f8\u4f3c\u5ea6\u5206\u6570\u9608\u503c\u8bbe\u591a\u5c11\u5408\u9002<\/p>\n<p>\u4e0d\u8981\u5199\u6b7b\u3002\u4e0d\u540c\u6a21\u578b\u3001\u4e0d\u540c\u5411\u91cf\u5e93\u7684\u5206\u6570\u542b\u4e49\u4e0d\u540c\u3002\u5148\u89c2\u5bdf\u771f\u5b9e\u95ee\u9898\u7ed3\u679c\u518d\u5b9a\u3002<\/p>\n<p>\u7b2c\u4e00\u6b21\u8fd0\u884c\u6162<\/p>\n<p>\u9996\u6b21\u8fd0\u884c\u4f1a\u4e0b\u8f7d\u6a21\u578b\u6587\u4ef6\u5230\u672c\u5730\u7f13\u5b58&#xff0c;\u540e\u7eed\u5c31\u5feb\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981\u6587\u6863\u5207\u597d\u4e86\u5f97\u5b58\u8d77\u6765&#xff0c;\u8fd8\u5f97\u80fd\u6839\u636e\u95ee\u9898\u627e\u51fa\u6765\u3002Embedding\u628a\u6587\u5b57\u8f6c\u6210\u5411\u91cf&#xff0c;\u5411\u91cf\u6570\u636e\u5e93\u8d1f\u8d23\u5b58\u548c\u67e5\u3002\u672c\u6587\u8bb2\u672c\u5730\u4e2d\u6587Embedding\u6a21\u578b\u7684\u4f7f\u7528\u3001Chroma\/Milvus\u7684\u57fa\u672c\u64cd\u4f5c&#xff0c;\u4ee5\u53ca\u600e\u4e48\u7528\u5143\u6570\u636e\u8fc7\u6ee4\u7f29\u5c0f\u68c0\u7d22\u8303\u56f4\u3002\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981Embedding\u7528\u6237\u95ee\\&#8221;\u516c\u53f8\u600e\u4e48\u5904\u7406\u8fdf\u5230\\&#8221;&#xff0c;\u6587\u6863\u91cc\u5199\u7684\u662f\\&#8221;\u5458\u5de5\u5e94\u6309\u65f6\u51fa\u52e4&#xff0c;\u8fdd\u8005\u6309\u5236\u5ea6\u5904\u7406\\&#8221;\u3002\u5173\u952e\u8bcd\u4e0d\u4e00\u6837&#xff0c;\u610f\u601d\u4e00\u6837\u3002\u666e\u901a\u5173\u952e\u8bcd\u641c\u4e0d\u5230&#xff0c;\u5f97\u9760\u8bed\u4e49\u68c0\u7d22\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[346,997,81,50,100],"topic":[],"class_list":["post-87719","post","type-post","status-publish","format-standard","hentry","category-server","tag-embedding","tag-langchain","tag-python","tag-50","tag-100"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/87719.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6458\u8981\u6587\u6863\u5207\u597d\u4e86\u5f97\u5b58\u8d77\u6765&#xff0c;\u8fd8\u5f97\u80fd\u6839\u636e\u95ee\u9898\u627e\u51fa\u6765\u3002Embedding\u628a\u6587\u5b57\u8f6c\u6210\u5411\u91cf&#xff0c;\u5411\u91cf\u6570\u636e\u5e93\u8d1f\u8d23\u5b58\u548c\u67e5\u3002\u672c\u6587\u8bb2\u672c\u5730\u4e2d\u6587Embedding\u6a21\u578b\u7684\u4f7f\u7528\u3001Chroma\/Milvus\u7684\u57fa\u672c\u64cd\u4f5c&#xff0c;\u4ee5\u53ca\u600e\u4e48\u7528\u5143\u6570\u636e\u8fc7\u6ee4\u7f29\u5c0f\u68c0\u7d22\u8303\u56f4\u3002\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981Embedding\u7528\u6237\u95ee&quot;\u516c\u53f8\u600e\u4e48\u5904\u7406\u8fdf\u5230&quot;&#xff0c;\u6587\u6863\u91cc\u5199\u7684\u662f&quot;\u5458\u5de5\u5e94\u6309\u65f6\u51fa\u52e4&#xff0c;\u8fdd\u8005\u6309\u5236\u5ea6\u5904\u7406&quot;\u3002\u5173\u952e\u8bcd\u4e0d\u4e00\u6837&#xff0c;\u610f\u601d\u4e00\u6837\u3002\u666e\u901a\u5173\u952e\u8bcd\u641c\u4e0d\u5230&#xff0c;\u5f97\u9760\u8bed\u4e49\u68c0\u7d22\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/87719.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-30T11:40:28+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/87719.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/87719.html\",\"name\":\"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-30T11:40:28+00:00\",\"dateModified\":\"2026-07-30T11:40:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/87719.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/87719.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/87719.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/87719.html","og_locale":"zh_CN","og_type":"article","og_title":"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6458\u8981\u6587\u6863\u5207\u597d\u4e86\u5f97\u5b58\u8d77\u6765&#xff0c;\u8fd8\u5f97\u80fd\u6839\u636e\u95ee\u9898\u627e\u51fa\u6765\u3002Embedding\u628a\u6587\u5b57\u8f6c\u6210\u5411\u91cf&#xff0c;\u5411\u91cf\u6570\u636e\u5e93\u8d1f\u8d23\u5b58\u548c\u67e5\u3002\u672c\u6587\u8bb2\u672c\u5730\u4e2d\u6587Embedding\u6a21\u578b\u7684\u4f7f\u7528\u3001Chroma\/Milvus\u7684\u57fa\u672c\u64cd\u4f5c&#xff0c;\u4ee5\u53ca\u600e\u4e48\u7528\u5143\u6570\u636e\u8fc7\u6ee4\u7f29\u5c0f\u68c0\u7d22\u8303\u56f4\u3002\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981Embedding\u7528\u6237\u95ee\"\u516c\u53f8\u600e\u4e48\u5904\u7406\u8fdf\u5230\"&#xff0c;\u6587\u6863\u91cc\u5199\u7684\u662f\"\u5458\u5de5\u5e94\u6309\u65f6\u51fa\u52e4&#xff0c;\u8fdd\u8005\u6309\u5236\u5ea6\u5904\u7406\"\u3002\u5173\u952e\u8bcd\u4e0d\u4e00\u6837&#xff0c;\u610f\u601d\u4e00\u6837\u3002\u666e\u901a\u5173\u952e\u8bcd\u641c\u4e0d\u5230&#xff0c;\u5f97\u9760\u8bed\u4e49\u68c0\u7d22\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/87719.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-30T11:40:28+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/87719.html","url":"https:\/\/www.wsisp.com\/helps\/87719.html","name":"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-30T11:40:28+00:00","dateModified":"2026-07-30T11:40:28+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/87719.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/87719.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/87719.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"LangChain Embedding\u4e0e\u5411\u91cf\u6570\u636e\u5e93"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/87719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=87719"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/87719\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=87719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=87719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=87719"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=87719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}