import{s as cl,n as dl,o as ul}from"../chunks/scheduler.39a43cde.js";import{S as hl,i as fl,g as d,s as n,r as i,A as Ml,h as u,f as l,c as a,j as ml,u as p,x as h,k as rl,y as bl,a as s,v as o,d as m,t as r,w as c}from"../chunks/index.23ec2204.js";import{D as gl,C as f}from"../chunks/DocNotebookDropdown.21a75b74.js";import{H as M,E as wl}from"../chunks/EditOnGithub.f2d09226.js";function yl(Rt){let b,Ge,je,Ie,g,ve,w,Ce,y,Vt='<em>作者: <a href="https://github.com/zc277584121" rel="nofollow">张晨</a></em>',Ze,J,Bt='<a href="https://milvus.io/" rel="nofollow">Milvus</a> 是一个广受欢迎的开源向量数据库，为人工智能应用提供高性能和可扩展的向量相似性搜索。在本教程中，我们将向您展示如何使用 Hugging Face 和 Milvus 构建 RAG（检索增强生成）流程。',ke,T,Nt="RAG 系统将检索系统与 LLM 相结合。该系统首先使用向量数据库 Milvus 从语料库中检索相关文档，然后使用 Hugging Face 的 LLM 根据检索到的文档生成回答。",We,$,_e,j,xe,U,Re,G,Xt="<p>如果您使用 Google Colab，要启用刚刚安装的依赖项，您可能需要 <strong>重新启动运行时</strong>（单击屏幕顶部的“运行时”菜单，然后从下拉菜单中选择“重新启动会话”） 。</p>",Ve,I,Ht='另外，我们建议您配置您的 <a href="https://huggingface.co/docs/hub/security-tokens" rel="nofollow">Hugging Face User Access Token</a>，并将其设置在您的环境变量中，因为我们将使用 Hugging Face Hub 的 LLM 。如果不设置 token 环境变量，您可能会受到较低的请求速率限制。',Be,v,Ne,C,Xe,Z,At='我们使用 <a href="https://artificialintelligenceact.eu/wp-content/uploads/2021/08/The-AI-Act.pdf" rel="nofollow">AI Act PDF</a> 作为我们 RAG 中的私有知识。这是一个针对人工智能的监管框架，具有不同风险级别，对应或多或少的监管。',He,k,Ae,W,Et='我们使用 LangChain 的 <a href="https://python.langchain.com/v0.1/docs/modules/data_connection/document_loaders/pdf/" rel="nofollow"><code>PyPDFLoader</code></a> 从 PDF 中提取文本，然后将文本拆分为更小的块。默认情况下，我们将块大小设置为 1000，重叠设置为 200，这意味着每个块将有近 1000 个字符，两个块之间的重叠将是 200 个字符。',Ee,_,ze,x,zt=`108
`,Qe,R,qe,V,Fe,B,Ye,N,Qt='定义一个函数来生成文本 embedding。我们使用 <a href="https://huggingface.co/BAAI/bge-small-en-v1.5" rel="nofollow">BGE embedding模型</a> 作为示例，但您也可以将其更改为任何其他 embedding 模型，例如在 <a href="https://huggingface.co/spaces/mteb/leaderboard" rel="nofollow">MTEB排行榜</a> 上选择。',Se,X,Le,H,qt="生成测试 embedding 并打印其大小和前几个元素。",Pe,A,De,E,Ft=`384
[-0.07660683244466782, 0.025316666811704636, 0.012505513615906239, 0.004595153499394655, 0.025780051946640015, 0.03816710412502289, 0.08050819486379623, 0.003035430097952485, 0.02439221926033497, 0.0048803347162902355]
`,Ke,z,Oe,Q,et,q,tt,F,Yt='<p>对于 <code>MilvusClient</code> 的参数：</p> <ul><li>将 <code>uri</code> 设置为本地文件，例如 <code>./hf_milvus_demo.db</code> ，是最方便的方法，因为它会自动使用 <a href="https://milvus.io/docs/milvus_lite.md" rel="nofollow">Milvus Lite</a> 将所有数据存储在此文件中。</li> <li>如果您有大量数据，例如超过一百万个向量，您可以在 <a href="https://milvus.io/docs/quickstart.md" rel="nofollow">Docker 或 Kubernetes</a> 上设置性能更高的 Milvus 服务器。在此设置中，请使用服务器 uri，例如 <code>http://localhost:19530</code> 作为您的 <code>uri</code> 。</li> <li>如果您想使用 Milvus 的全托管云服务 <a href="https://zilliz.com/cloud" rel="nofollow">Zilliz Cloud</a> ，请调整 <code>uri</code> 和 <code>token</code>，分别对应 Zilliz Cloud 中 <a href="https://docs.zilliz.com/docs/on-zilliz-cloud-console#cluster-details" rel="nofollow">Public Endpoint 和 Api key</a> 。</li></ul>',lt,Y,St="检查 collection 是否已存在，如果存在则将其删除。",st,S,nt,L,Lt="使用指定参数创建一个新 collection。",at,P,Pt="如果我们不指定任何字段信息，Milvus 会自动创建一个默认的 <code>id</code> 字段作为主键，并创建一个 <code>vector</code> 字段来存储向量数据。保留的 JSON 字段用于存储未定义 schema 的字段及其值。",it,D,pt,K,ot,O,Dt="迭代文本行，创建 embedding，然后将数据插入到 Milvus 中。",mt,ee,Kt="这是一个新字段 <code>text</code>，它是 collection schema 中的未定义字段。它将自动添加到保留的 JSON 动态字段中，从更高的层次看来，它可被视为普通字段。",rt,te,ct,le,dt,se,ut,ne,Ot="让我们具体指定一个有关该语料的问题。",ht,ae,ft,ie,el="在 collection 中搜索问题并检索语义前 3 个匹配项。",Mt,pe,bt,oe,tl="我们来看看查询的搜索结果",gt,me,wt,re,ll=`[
    [
        "EN 6  EN 2. LEGAL  BASIS,  SUBSIDIARITY  AND  PROPORTIONALITY  \\n2.1. Legal  basis  \\nThe legal basis for the proposal is in the first place Article 114 of the Treaty on the \\nFunctioning of the European Union (TFEU), which provides for the adoption of measures to \\nensure the establishment and f unctioning of the internal market.  \\nThis proposal constitutes a core part of the EU digital single market strategy. The primary \\nobjective of this proposal is to ensure the proper functioning of the internal market by setting \\nharmonised rules in particular on the development, placing on the Union market and the use \\nof products and services making use of AI technologies or provided as stand -alone AI \\nsystems. Some Member States are already considering national rules to ensure that AI is safe \\nand is developed a nd used in compliance with fundamental rights obligations. This will likely \\nlead to two main problems: i) a fragmentation of the internal market on essential elements",
        0.7412998080253601
    ],
    [
        "applications and prevent market fragmentation.  \\nTo achieve those objectives, this proposal presents a balanced and proportionate horizontal \\nregulatory approach to AI that is limited to the minimum necessary requirements to address \\nthe risks and problems linked to AI, withou t unduly constraining or hindering technological \\ndevelopment or otherwise disproportionately increasing the cost of placing AI solutions on \\nthe market.  The proposal sets a robust and flexible legal framework. On the one hand, it is \\ncomprehensive and future -proof in its fundamental regulatory choices, including the \\nprinciple -based requirements that AI systems should comply with. On the other hand, it puts \\nin place a proportionate regulatory system centred on a well -defined risk -based regulatory \\napproach that  does not create unnecessary restrictions to trade, whereby legal intervention is \\ntailored to those concrete situations where there is a justified cause for concern or where such",
        0.696428656578064
    ],
    [
        "approach that  does not create unnecessary restrictions to trade, whereby legal intervention is \\ntailored to those concrete situations where there is a justified cause for concern or where such \\nconcern can reasonably be anticipated in the near future. At the same time, t he legal \\nframework includes flexible mechanisms that enable it to be dynamically adapted as the \\ntechnology evolves and new concerning situations emerge.  \\nThe proposal sets harmonised rules for the development, placement on the market and use of \\nAI systems i n the Union following a proportionate risk -based approach. It proposes a single \\nfuture -proof definition of AI. Certain particularly harmful AI practices are prohibited as \\ncontravening Union values, while specific restrictions and safeguards are proposed in  relation \\nto certain uses of remote biometric identification systems for the purpose of law enforcement. \\nThe proposal lays down a solid risk methodology to define \\u201chigh -risk\\u201d AI systems that pose",
        0.6891457438468933
    ]
]
`,yt,ce,Jt,de,sl="在合并到 LLM 提示词之前，我们首先将检索到的文档列表展平为纯字符串。",Tt,ue,$t,he,nl="定义语言模型的提示词。该提示与从 Milvus 检索到的文档组合在一起。",jt,fe,Ut,Me,al='我们使用部署在 Hugging Face 推理服务上的 <a href="https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1" rel="nofollow">Mixtral-8x7B-Instruct-v0.1</a> 根据提示词生成响应。',Gt,be,It,ge,il="我们设置提示词格式并生成最后的回答。",vt,we,Ct,ye,Zt,Je,pl=`The legal basis for the proposal is Article 114 of the Treaty on the Functioning of the European Union (TFEU), which provides for the adoption of measures to ensure the establishment and functioning of the internal market. The proposal aims to establish harmonized rules for the development, placing on the market, and use of AI systems in the Union following a proportionate risk-based approach.
`,kt,Te,ol="恭喜！您已经使用 Hugging Face 和 Milvus 构建了 RAG 流程。",Wt,$e,_t,Ue,xt;return g=new gl({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/cookbook/blob/dataset_issues/notebooks/zh-CN/rag_with_hf_and_milvus.ipynb"}]}}),w=new M({props:{title:"使用 Hugging Face 和 Milvus 构建 RAG 系统",local:"使用-hugging-face-和-milvus-构建-rag-系统",headingTag:"h1"}}),$=new M({props:{title:"准备",local:"准备",headingTag:"h2"}}),j=new M({props:{title:"依赖关系和环境",local:"依赖关系和环境",headingTag:"h3"}}),U=new f({props:{code:"ISUyMHBpcCUyMGluc3RhbGwlMjAtLXVwZ3JhZGUlMjBweW1pbHZ1cyUyMHNlbnRlbmNlLXRyYW5zZm9ybWVycyUyMGh1Z2dpbmdmYWNlLWh1YiUyMGxhbmdjaGFpbl9jb21tdW5pdHklMjBsYW5nY2hhaW4tdGV4dC1zcGxpdHRlcnMlMjBweXBkZiUyMHRxZG0=",highlighted:"! pip install --upgrade pymilvus sentence-transformers huggingface-hub langchain_community langchain-text-splitters pypdf tqdm",wrap:!1}}),v=new f({props:{code:"aW1wb3J0JTIwb3MlMEElMEFvcy5lbnZpcm9uJTVCJTIySEZfVE9LRU4lMjIlNUQlMjAlM0QlMjAlMjJoZl8uLi4lMjI=",highlighted:`<span class="hljs-keyword">import</span> os

os.environ[<span class="hljs-string">&quot;HF_TOKEN&quot;</span>] = <span class="hljs-string">&quot;hf_...&quot;</span>`,wrap:!1}}),C=new M({props:{title:"准备数据",local:"准备数据",headingTag:"h3"}}),k=new f({props:{code:"JTI1JTI1YmFzaCUwQSUwQWlmJTIwJTVCJTIwISUyMC1mJTIwJTIyVGhlLUFJLUFjdC5wZGYlMjIlMjAlNUQlM0IlMjB0aGVuJTBBJTIwJTIwJTIwJTIwd2dldCUyMC1xJTIwaHR0cHMlM0ElMkYlMkZhcnRpZmljaWFsaW50ZWxsaWdlbmNlYWN0LmV1JTJGd3AtY29udGVudCUyRnVwbG9hZHMlMkYyMDIxJTJGMDglMkZUaGUtQUktQWN0LnBkZiUwQWZp",highlighted:`%%bash

<span class="hljs-keyword">if</span> [ ! -f <span class="hljs-string">&quot;The-AI-Act.pdf&quot;</span> ]; then
    wget -q https://artificialintelligenceact.eu/wp-content/uploads/<span class="hljs-number">2021</span>/08/The-AI-Act.pdf
fi`,wrap:!1}}),_=new f({props:{code:"ZnJvbSUyMGxhbmdjaGFpbl9jb21tdW5pdHkuZG9jdW1lbnRfbG9hZGVycyUyMGltcG9ydCUyMFB5UERGTG9hZGVyJTBBJTBBbG9hZGVyJTIwJTNEJTIwUHlQREZMb2FkZXIoJTIyVGhlLUFJLUFjdC5wZGYlMjIpJTBBZG9jcyUyMCUzRCUyMGxvYWRlci5sb2FkKCklMEFwcmludChsZW4oZG9jcykp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> langchain_community.document_loaders <span class="hljs-keyword">import</span> PyPDFLoader

<span class="hljs-meta">&gt;&gt;&gt; </span>loader = PyPDFLoader(<span class="hljs-string">&quot;The-AI-Act.pdf&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>docs = loader.load()
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-built_in">len</span>(docs))`,wrap:!1}}),R=new f({props:{code:"ZnJvbSUyMGxhbmdjaGFpbl90ZXh0X3NwbGl0dGVycyUyMGltcG9ydCUyMFJlY3Vyc2l2ZUNoYXJhY3RlclRleHRTcGxpdHRlciUwQSUwQXRleHRfc3BsaXR0ZXIlMjAlM0QlMjBSZWN1cnNpdmVDaGFyYWN0ZXJUZXh0U3BsaXR0ZXIoY2h1bmtfc2l6ZSUzRDEwMDAlMkMlMjBjaHVua19vdmVybGFwJTNEMjAwKSUwQWNodW5rcyUyMCUzRCUyMHRleHRfc3BsaXR0ZXIuc3BsaXRfZG9jdW1lbnRzKGRvY3Mp",highlighted:`<span class="hljs-keyword">from</span> langchain_text_splitters <span class="hljs-keyword">import</span> RecursiveCharacterTextSplitter

text_splitter = RecursiveCharacterTextSplitter(chunk_size=<span class="hljs-number">1000</span>, chunk_overlap=<span class="hljs-number">200</span>)
chunks = text_splitter.split_documents(docs)`,wrap:!1}}),V=new f({props:{code:"dGV4dF9saW5lcyUyMCUzRCUyMCU1QmNodW5rLnBhZ2VfY29udGVudCUyMGZvciUyMGNodW5rJTIwaW4lMjBjaHVua3MlNUQ=",highlighted:'text_lines = [chunk.page_content <span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> chunks]',wrap:!1}}),B=new M({props:{title:"准备 embedding 模型",local:"准备-embedding-模型",headingTag:"h3"}}),X=new f({props:{code:"ZnJvbSUyMHNlbnRlbmNlX3RyYW5zZm9ybWVycyUyMGltcG9ydCUyMFNlbnRlbmNlVHJhbnNmb3JtZXIlMEElMEFlbWJlZGRpbmdfbW9kZWwlMjAlM0QlMjBTZW50ZW5jZVRyYW5zZm9ybWVyKCUyMkJBQUklMkZiZ2Utc21hbGwtZW4tdjEuNSUyMiklMEElMEElMEFkZWYlMjBlbWJfdGV4dCh0ZXh0KSUzQSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMGVtYmVkZGluZ19tb2RlbC5lbmNvZGUoJTVCdGV4dCU1RCUyQyUyMG5vcm1hbGl6ZV9lbWJlZGRpbmdzJTNEVHJ1ZSkudG9saXN0KCklNUIwJTVE",highlighted:`<span class="hljs-keyword">from</span> sentence_transformers <span class="hljs-keyword">import</span> SentenceTransformer

embedding_model = SentenceTransformer(<span class="hljs-string">&quot;BAAI/bge-small-en-v1.5&quot;</span>)


<span class="hljs-keyword">def</span> <span class="hljs-title function_">emb_text</span>(<span class="hljs-params">text</span>):
    <span class="hljs-keyword">return</span> embedding_model.encode([text], normalize_embeddings=<span class="hljs-literal">True</span>).tolist()[<span class="hljs-number">0</span>]`,wrap:!1}}),A=new f({props:{code:"dGVzdF9lbWJlZGRpbmclMjAlM0QlMjBlbWJfdGV4dCglMjJUaGlzJTIwaXMlMjBhJTIwdGVzdCUyMiklMEFlbWJlZGRpbmdfZGltJTIwJTNEJTIwbGVuKHRlc3RfZW1iZWRkaW5nKSUwQXByaW50KGVtYmVkZGluZ19kaW0pJTBBcHJpbnQodGVzdF9lbWJlZGRpbmclNUIlM0ExMCU1RCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>test_embedding = emb_text(<span class="hljs-string">&quot;This is a test&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>embedding_dim = <span class="hljs-built_in">len</span>(test_embedding)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(embedding_dim)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(test_embedding[:<span class="hljs-number">10</span>])`,wrap:!1}}),z=new M({props:{title:"将数据加载到 Milvus 中",local:"将数据加载到-milvus-中",headingTag:"h2"}}),Q=new M({props:{title:"创建 collection",local:"创建-collection",headingTag:"h3"}}),q=new f({props:{code:"ZnJvbSUyMHB5bWlsdnVzJTIwaW1wb3J0JTIwTWlsdnVzQ2xpZW50JTBBJTBBbWlsdnVzX2NsaWVudCUyMCUzRCUyME1pbHZ1c0NsaWVudCh1cmklM0QlMjIuJTJGaGZfbWlsdnVzX2RlbW8uZGIlMjIpJTBBJTBBY29sbGVjdGlvbl9uYW1lJTIwJTNEJTIwJTIycmFnX2NvbGxlY3Rpb24lMjI=",highlighted:`<span class="hljs-keyword">from</span> pymilvus <span class="hljs-keyword">import</span> MilvusClient

milvus_client = MilvusClient(uri=<span class="hljs-string">&quot;./hf_milvus_demo.db&quot;</span>)

collection_name = <span class="hljs-string">&quot;rag_collection&quot;</span>`,wrap:!1}}),S=new f({props:{code:"aWYlMjBtaWx2dXNfY2xpZW50Lmhhc19jb2xsZWN0aW9uKGNvbGxlY3Rpb25fbmFtZSklM0ElMEElMjAlMjAlMjAlMjBtaWx2dXNfY2xpZW50LmRyb3BfY29sbGVjdGlvbihjb2xsZWN0aW9uX25hbWUp",highlighted:`<span class="hljs-keyword">if</span> milvus_client.has_collection(collection_name):
    milvus_client.drop_collection(collection_name)`,wrap:!1}}),D=new f({props:{code:"bWlsdnVzX2NsaWVudC5jcmVhdGVfY29sbGVjdGlvbiglMEElMjAlMjAlMjAlMjBjb2xsZWN0aW9uX25hbWUlM0Rjb2xsZWN0aW9uX25hbWUlMkMlMEElMjAlMjAlMjAlMjBkaW1lbnNpb24lM0RlbWJlZGRpbmdfZGltJTJDJTBBJTIwJTIwJTIwJTIwbWV0cmljX3R5cGUlM0QlMjJJUCUyMiUyQyUyMCUyMCUyMyUyMElubmVyJTIwcHJvZHVjdCUyMGRpc3RhbmNlJTBBJTIwJTIwJTIwJTIwY29uc2lzdGVuY3lfbGV2ZWwlM0QlMjJTdHJvbmclMjIlMkMlMjAlMjAlMjMlMjBTdHJvbmclMjBjb25zaXN0ZW5jeSUyMGxldmVsJTBBKQ==",highlighted:`milvus_client.create_collection(
    collection_name=collection_name,
    dimension=embedding_dim,
    metric_type=<span class="hljs-string">&quot;IP&quot;</span>,  <span class="hljs-comment"># Inner product distance</span>
    consistency_level=<span class="hljs-string">&quot;Strong&quot;</span>,  <span class="hljs-comment"># Strong consistency level</span>
)`,wrap:!1}}),K=new M({props:{title:"插入数据",local:"插入数据",headingTag:"h3"}}),te=new f({props:{code:"ZnJvbSUyMHRxZG0lMjBpbXBvcnQlMjB0cWRtJTBBJTBBZGF0YSUyMCUzRCUyMCU1QiU1RCUwQSUwQWZvciUyMGklMkMlMjBsaW5lJTIwaW4lMjBlbnVtZXJhdGUodHFkbSh0ZXh0X2xpbmVzJTJDJTIwZGVzYyUzRCUyMkNyZWF0aW5nJTIwZW1iZWRkaW5ncyUyMikpJTNBJTBBJTIwJTIwJTIwJTIwZGF0YS5hcHBlbmQoJTdCJTIyaWQlMjIlM0ElMjBpJTJDJTIwJTIydmVjdG9yJTIyJTNBJTIwZW1iX3RleHQobGluZSklMkMlMjAlMjJ0ZXh0JTIyJTNBJTIwbGluZSU3RCklMEElMEFpbnNlcnRfcmVzJTIwJTNEJTIwbWlsdnVzX2NsaWVudC5pbnNlcnQoY29sbGVjdGlvbl9uYW1lJTNEY29sbGVjdGlvbl9uYW1lJTJDJTIwZGF0YSUzRGRhdGEpJTBBaW5zZXJ0X3JlcyU1QiUyMmluc2VydF9jb3VudCUyMiU1RA==",highlighted:`<span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm

data = []

<span class="hljs-keyword">for</span> i, line <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(tqdm(text_lines, desc=<span class="hljs-string">&quot;Creating embeddings&quot;</span>)):
    data.append({<span class="hljs-string">&quot;id&quot;</span>: i, <span class="hljs-string">&quot;vector&quot;</span>: emb_text(line), <span class="hljs-string">&quot;text&quot;</span>: line})

insert_res = milvus_client.insert(collection_name=collection_name, data=data)
insert_res[<span class="hljs-string">&quot;insert_count&quot;</span>]`,wrap:!1}}),le=new M({props:{title:"构建 RAG",local:"构建-rag",headingTag:"h2"}}),se=new M({props:{title:"检索查询数据",local:"检索查询数据",headingTag:"h3"}}),ae=new f({props:{code:"cXVlc3Rpb24lMjAlM0QlMjAlMjJXaGF0JTIwaXMlMjB0aGUlMjBsZWdhbCUyMGJhc2lzJTIwZm9yJTIwdGhlJTIwcHJvcG9zYWwlM0YlMjI=",highlighted:'question = <span class="hljs-string">&quot;What is the legal basis for the proposal?&quot;</span>',wrap:!1}}),pe=new f({props:{code:"c2VhcmNoX3JlcyUyMCUzRCUyMG1pbHZ1c19jbGllbnQuc2VhcmNoKCUwQSUyMCUyMCUyMCUyMGNvbGxlY3Rpb25fbmFtZSUzRGNvbGxlY3Rpb25fbmFtZSUyQyUwQSUyMCUyMCUyMCUyMGRhdGElM0QlNUJlbWJfdGV4dChxdWVzdGlvbiklNUQlMkMlMjAlMjAlMjMlMjBVc2UlMjB0aGUlMjAlNjBlbWJfdGV4dCU2MCUyMGZ1bmN0aW9uJTIwdG8lMjBjb252ZXJ0JTIwdGhlJTIwcXVlc3Rpb24lMjB0byUyMGFuJTIwZW1iZWRkaW5nJTIwdmVjdG9yJTBBJTIwJTIwJTIwJTIwbGltaXQlM0QzJTJDJTIwJTIwJTIzJTIwUmV0dXJuJTIwdG9wJTIwMyUyMHJlc3VsdHMlMEElMjAlMjAlMjAlMjBzZWFyY2hfcGFyYW1zJTNEJTdCJTIybWV0cmljX3R5cGUlMjIlM0ElMjAlMjJJUCUyMiUyQyUyMCUyMnBhcmFtcyUyMiUzQSUyMCU3QiU3RCU3RCUyQyUyMCUyMCUyMyUyMElubmVyJTIwcHJvZHVjdCUyMGRpc3RhbmNlJTBBJTIwJTIwJTIwJTIwb3V0cHV0X2ZpZWxkcyUzRCU1QiUyMnRleHQlMjIlNUQlMkMlMjAlMjAlMjMlMjBSZXR1cm4lMjB0aGUlMjB0ZXh0JTIwZmllbGQlMEEp",highlighted:`search_res = milvus_client.search(
    collection_name=collection_name,
    data=[emb_text(question)],  <span class="hljs-comment"># Use the \`emb_text\` function to convert the question to an embedding vector</span>
    limit=<span class="hljs-number">3</span>,  <span class="hljs-comment"># Return top 3 results</span>
    search_params={<span class="hljs-string">&quot;metric_type&quot;</span>: <span class="hljs-string">&quot;IP&quot;</span>, <span class="hljs-string">&quot;params&quot;</span>: {}},  <span class="hljs-comment"># Inner product distance</span>
    output_fields=[<span class="hljs-string">&quot;text&quot;</span>],  <span class="hljs-comment"># Return the text field</span>
)`,wrap:!1}}),me=new f({props:{code:"aW1wb3J0JTIwanNvbiUwQSUwQXJldHJpZXZlZF9saW5lc193aXRoX2Rpc3RhbmNlcyUyMCUzRCUyMCU1QihyZXMlNUIlMjJlbnRpdHklMjIlNUQlNUIlMjJ0ZXh0JTIyJTVEJTJDJTIwcmVzJTVCJTIyZGlzdGFuY2UlMjIlNUQpJTIwZm9yJTIwcmVzJTIwaW4lMjBzZWFyY2hfcmVzJTVCMCU1RCU1RCUwQXByaW50KGpzb24uZHVtcHMocmV0cmlldmVkX2xpbmVzX3dpdGhfZGlzdGFuY2VzJTJDJTIwaW5kZW50JTNENCkp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> json

<span class="hljs-meta">&gt;&gt;&gt; </span>retrieved_lines_with_distances = [(res[<span class="hljs-string">&quot;entity&quot;</span>][<span class="hljs-string">&quot;text&quot;</span>], res[<span class="hljs-string">&quot;distance&quot;</span>]) <span class="hljs-keyword">for</span> res <span class="hljs-keyword">in</span> search_res[<span class="hljs-number">0</span>]]
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(json.dumps(retrieved_lines_with_distances, indent=<span class="hljs-number">4</span>))`,wrap:!1}}),ce=new M({props:{title:"使用 LLM 获取 RAG 回答",local:"使用-llm-获取-rag-回答",headingTag:"h3"}}),ue=new f({props:{code:"Y29udGV4dCUyMCUzRCUyMCUyMiU1Q24lMjIuam9pbiglNUJsaW5lX3dpdGhfZGlzdGFuY2UlNUIwJTVEJTIwZm9yJTIwbGluZV93aXRoX2Rpc3RhbmNlJTIwaW4lMjByZXRyaWV2ZWRfbGluZXNfd2l0aF9kaXN0YW5jZXMlNUQp",highlighted:'context = <span class="hljs-string">&quot;\\n&quot;</span>.join([line_with_distance[<span class="hljs-number">0</span>] <span class="hljs-keyword">for</span> line_with_distance <span class="hljs-keyword">in</span> retrieved_lines_with_distances])',wrap:!1}}),fe=new f({props:{code:"UFJPTVBUJTIwJTNEJTIwJTIyJTIyJTIyJTBBVXNlJTIwdGhlJTIwZm9sbG93aW5nJTIwcGllY2VzJTIwb2YlMjBpbmZvcm1hdGlvbiUyMGVuY2xvc2VkJTIwaW4lMjAlM0Njb250ZXh0JTNFJTIwdGFncyUyMHRvJTIwcHJvdmlkZSUyMGFuJTIwYW5zd2VyJTIwdG8lMjB0aGUlMjBxdWVzdGlvbiUyMGVuY2xvc2VkJTIwaW4lMjAlM0NxdWVzdGlvbiUzRSUyMHRhZ3MuJTBBJTNDY29udGV4dCUzRSUwQSU3QmNvbnRleHQlN0QlMEElM0MlMkZjb250ZXh0JTNFJTBBJTNDcXVlc3Rpb24lM0UlMEElN0JxdWVzdGlvbiU3RCUwQSUzQyUyRnF1ZXN0aW9uJTNFJTBBJTIyJTIyJTIy",highlighted:`PROMPT = <span class="hljs-string">&quot;&quot;&quot;
Use the following pieces of information enclosed in &lt;context&gt; tags to provide an answer to the question enclosed in &lt;question&gt; tags.
&lt;context&gt;
{context}
&lt;/context&gt;
&lt;question&gt;
{question}
&lt;/question&gt;
&quot;&quot;&quot;</span>`,wrap:!1}}),be=new f({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMEluZmVyZW5jZUNsaWVudCUwQSUwQXJlcG9faWQlMjAlM0QlMjAlMjJtaXN0cmFsYWklMkZNaXh0cmFsLTh4N0ItSW5zdHJ1Y3QtdjAuMSUyMiUwQSUwQWxsbV9jbGllbnQlMjAlM0QlMjBJbmZlcmVuY2VDbGllbnQobW9kZWwlM0RyZXBvX2lkJTJDJTIwdGltZW91dCUzRDEyMCk=",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient

repo_id = <span class="hljs-string">&quot;mistralai/Mixtral-8x7B-Instruct-v0.1&quot;</span>

llm_client = InferenceClient(model=repo_id, timeout=<span class="hljs-number">120</span>)`,wrap:!1}}),we=new f({props:{code:"cHJvbXB0JTIwJTNEJTIwUFJPTVBULmZvcm1hdChjb250ZXh0JTNEY29udGV4dCUyQyUyMHF1ZXN0aW9uJTNEcXVlc3Rpb24p",highlighted:'prompt = PROMPT.<span class="hljs-built_in">format</span>(context=context, question=question)',wrap:!1}}),ye=new f({props:{code:"YW5zd2VyJTIwJTNEJTIwbGxtX2NsaWVudC50ZXh0X2dlbmVyYXRpb24oJTBBJTIwJTIwJTIwJTIwcHJvbXB0JTJDJTBBJTIwJTIwJTIwJTIwbWF4X25ld190b2tlbnMlM0QxMDAwJTJDJTBBKS5zdHJpcCgpJTBBcHJpbnQoYW5zd2VyKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>answer = llm_client.text_generation(
<span class="hljs-meta">... </span>    prompt,
<span class="hljs-meta">... </span>    max_new_tokens=<span class="hljs-number">1000</span>,
<span class="hljs-meta">... </span>).strip()
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(answer)`,wrap:!1}}),$e=new wl({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/zh-CN/rag_with_hf_and_milvus.md"}}),{c(){b=d("meta"),Ge=n(),je=d("p"),Ie=n(),i(g.$$.fragment),ve=n(),i(w.$$.fragment),Ce=n(),y=d("p"),y.innerHTML=Vt,Ze=n(),J=d("p"),J.innerHTML=Bt,ke=n(),T=d("p"),T.textContent=Nt,We=n(),i($.$$.fragment),_e=n(),i(j.$$.fragment),xe=n(),i(U.$$.fragment),Re=n(),G=d("blockquote"),G.innerHTML=Xt,Ve=n(),I=d("p"),I.innerHTML=Ht,Be=n(),i(v.$$.fragment),Ne=n(),i(C.$$.fragment),Xe=n(),Z=d("p"),Z.innerHTML=At,He=n(),i(k.$$.fragment),Ae=n(),W=d("p"),W.innerHTML=Et,Ee=n(),i(_.$$.fragment),ze=n(),x=d("pre"),x.textContent=zt,Qe=n(),i(R.$$.fragment),qe=n(),i(V.$$.fragment),Fe=n(),i(B.$$.fragment),Ye=n(),N=d("p"),N.innerHTML=Qt,Se=n(),i(X.$$.fragment),Le=n(),H=d("p"),H.textContent=qt,Pe=n(),i(A.$$.fragment),De=n(),E=d("pre"),E.textContent=Ft,Ke=n(),i(z.$$.fragment),Oe=n(),i(Q.$$.fragment),et=n(),i(q.$$.fragment),tt=n(),F=d("blockquote"),F.innerHTML=Yt,lt=n(),Y=d("p"),Y.textContent=St,st=n(),i(S.$$.fragment),nt=n(),L=d("p"),L.textContent=Lt,at=n(),P=d("p"),P.innerHTML=Pt,it=n(),i(D.$$.fragment),pt=n(),i(K.$$.fragment),ot=n(),O=d("p"),O.textContent=Dt,mt=n(),ee=d("p"),ee.innerHTML=Kt,rt=n(),i(te.$$.fragment),ct=n(),i(le.$$.fragment),dt=n(),i(se.$$.fragment),ut=n(),ne=d("p"),ne.textContent=Ot,ht=n(),i(ae.$$.fragment),ft=n(),ie=d("p"),ie.textContent=el,Mt=n(),i(pe.$$.fragment),bt=n(),oe=d("p"),oe.textContent=tl,gt=n(),i(me.$$.fragment),wt=n(),re=d("pre"),re.textContent=ll,yt=n(),i(ce.$$.fragment),Jt=n(),de=d("p"),de.textContent=sl,Tt=n(),i(ue.$$.fragment),$t=n(),he=d("p"),he.textContent=nl,jt=n(),i(fe.$$.fragment),Ut=n(),Me=d("p"),Me.innerHTML=al,Gt=n(),i(be.$$.fragment),It=n(),ge=d("p"),ge.textContent=il,vt=n(),i(we.$$.fragment),Ct=n(),i(ye.$$.fragment),Zt=n(),Je=d("pre"),Je.textContent=pl,kt=n(),Te=d("p"),Te.textContent=ol,Wt=n(),i($e.$$.fragment),_t=n(),Ue=d("p"),this.h()},l(e){const t=Ml("svelte-u9bgzb",document.head);b=u(t,"META",{name:!0,content:!0}),t.forEach(l),Ge=a(e),je=u(e,"P",{}),ml(je).forEach(l),Ie=a(e),p(g.$$.fragment,e),ve=a(e),p(w.$$.fragment,e),Ce=a(e),y=u(e,"P",{"data-svelte-h":!0}),h(y)!=="svelte-1chlthn"&&(y.innerHTML=Vt),Ze=a(e),J=u(e,"P",{"data-svelte-h":!0}),h(J)!=="svelte-17b5h5k"&&(J.innerHTML=Bt),ke=a(e),T=u(e,"P",{"data-svelte-h":!0}),h(T)!=="svelte-s7wopg"&&(T.textContent=Nt),We=a(e),p($.$$.fragment,e),_e=a(e),p(j.$$.fragment,e),xe=a(e),p(U.$$.fragment,e),Re=a(e),G=u(e,"BLOCKQUOTE",{"data-svelte-h":!0}),h(G)!=="svelte-19llaah"&&(G.innerHTML=Xt),Ve=a(e),I=u(e,"P",{"data-svelte-h":!0}),h(I)!=="svelte-lusge4"&&(I.innerHTML=Ht),Be=a(e),p(v.$$.fragment,e),Ne=a(e),p(C.$$.fragment,e),Xe=a(e),Z=u(e,"P",{"data-svelte-h":!0}),h(Z)!=="svelte-nbxe55"&&(Z.innerHTML=At),He=a(e),p(k.$$.fragment,e),Ae=a(e),W=u(e,"P",{"data-svelte-h":!0}),h(W)!=="svelte-3vo957"&&(W.innerHTML=Et),Ee=a(e),p(_.$$.fragment,e),ze=a(e),x=u(e,"PRE",{"data-svelte-h":!0}),h(x)!=="svelte-q6t6yp"&&(x.textContent=zt),Qe=a(e),p(R.$$.fragment,e),qe=a(e),p(V.$$.fragment,e),Fe=a(e),p(B.$$.fragment,e),Ye=a(e),N=u(e,"P",{"data-svelte-h":!0}),h(N)!=="svelte-101d5kf"&&(N.innerHTML=Qt),Se=a(e),p(X.$$.fragment,e),Le=a(e),H=u(e,"P",{"data-svelte-h":!0}),h(H)!=="svelte-12ffb5h"&&(H.textContent=qt),Pe=a(e),p(A.$$.fragment,e),De=a(e),E=u(e,"PRE",{"data-svelte-h":!0}),h(E)!=="svelte-citxyn"&&(E.textContent=Ft),Ke=a(e),p(z.$$.fragment,e),Oe=a(e),p(Q.$$.fragment,e),et=a(e),p(q.$$.fragment,e),tt=a(e),F=u(e,"BLOCKQUOTE",{"data-svelte-h":!0}),h(F)!=="svelte-1fnjc2l"&&(F.innerHTML=Yt),lt=a(e),Y=u(e,"P",{"data-svelte-h":!0}),h(Y)!=="svelte-lb7voj"&&(Y.textContent=St),st=a(e),p(S.$$.fragment,e),nt=a(e),L=u(e,"P",{"data-svelte-h":!0}),h(L)!=="svelte-1x8w3ez"&&(L.textContent=Lt),at=a(e),P=u(e,"P",{"data-svelte-h":!0}),h(P)!=="svelte-1w83t7l"&&(P.innerHTML=Pt),it=a(e),p(D.$$.fragment,e),pt=a(e),p(K.$$.fragment,e),ot=a(e),O=u(e,"P",{"data-svelte-h":!0}),h(O)!=="svelte-yu2mi1"&&(O.textContent=Dt),mt=a(e),ee=u(e,"P",{"data-svelte-h":!0}),h(ee)!=="svelte-b8v197"&&(ee.innerHTML=Kt),rt=a(e),p(te.$$.fragment,e),ct=a(e),p(le.$$.fragment,e),dt=a(e),p(se.$$.fragment,e),ut=a(e),ne=u(e,"P",{"data-svelte-h":!0}),h(ne)!=="svelte-15ym3a8"&&(ne.textContent=Ot),ht=a(e),p(ae.$$.fragment,e),ft=a(e),ie=u(e,"P",{"data-svelte-h":!0}),h(ie)!=="svelte-xy3ubc"&&(ie.textContent=el),Mt=a(e),p(pe.$$.fragment,e),bt=a(e),oe=u(e,"P",{"data-svelte-h":!0}),h(oe)!=="svelte-1rwok4e"&&(oe.textContent=tl),gt=a(e),p(me.$$.fragment,e),wt=a(e),re=u(e,"PRE",{"data-svelte-h":!0}),h(re)!=="svelte-sx7p7m"&&(re.textContent=ll),yt=a(e),p(ce.$$.fragment,e),Jt=a(e),de=u(e,"P",{"data-svelte-h":!0}),h(de)!=="svelte-14bvkzq"&&(de.textContent=sl),Tt=a(e),p(ue.$$.fragment,e),$t=a(e),he=u(e,"P",{"data-svelte-h":!0}),h(he)!=="svelte-1rq6yrp"&&(he.textContent=nl),jt=a(e),p(fe.$$.fragment,e),Ut=a(e),Me=u(e,"P",{"data-svelte-h":!0}),h(Me)!=="svelte-1ubdl5e"&&(Me.innerHTML=al),Gt=a(e),p(be.$$.fragment,e),It=a(e),ge=u(e,"P",{"data-svelte-h":!0}),h(ge)!=="svelte-fobs8u"&&(ge.textContent=il),vt=a(e),p(we.$$.fragment,e),Ct=a(e),p(ye.$$.fragment,e),Zt=a(e),Je=u(e,"PRE",{"data-svelte-h":!0}),h(Je)!=="svelte-18i7g4t"&&(Je.textContent=pl),kt=a(e),Te=u(e,"P",{"data-svelte-h":!0}),h(Te)!=="svelte-1d9opac"&&(Te.textContent=ol),Wt=a(e),p($e.$$.fragment,e),_t=a(e),Ue=u(e,"P",{}),ml(Ue).forEach(l),this.h()},h(){rl(b,"name","hf:doc:metadata"),rl(b,"content",Jl)},m(e,t){bl(document.head,b),s(e,Ge,t),s(e,je,t),s(e,Ie,t),o(g,e,t),s(e,ve,t),o(w,e,t),s(e,Ce,t),s(e,y,t),s(e,Ze,t),s(e,J,t),s(e,ke,t),s(e,T,t),s(e,We,t),o($,e,t),s(e,_e,t),o(j,e,t),s(e,xe,t),o(U,e,t),s(e,Re,t),s(e,G,t),s(e,Ve,t),s(e,I,t),s(e,Be,t),o(v,e,t),s(e,Ne,t),o(C,e,t),s(e,Xe,t),s(e,Z,t),s(e,He,t),o(k,e,t),s(e,Ae,t),s(e,W,t),s(e,Ee,t),o(_,e,t),s(e,ze,t),s(e,x,t),s(e,Qe,t),o(R,e,t),s(e,qe,t),o(V,e,t),s(e,Fe,t),o(B,e,t),s(e,Ye,t),s(e,N,t),s(e,Se,t),o(X,e,t),s(e,Le,t),s(e,H,t),s(e,Pe,t),o(A,e,t),s(e,De,t),s(e,E,t),s(e,Ke,t),o(z,e,t),s(e,Oe,t),o(Q,e,t),s(e,et,t),o(q,e,t),s(e,tt,t),s(e,F,t),s(e,lt,t),s(e,Y,t),s(e,st,t),o(S,e,t),s(e,nt,t),s(e,L,t),s(e,at,t),s(e,P,t),s(e,it,t),o(D,e,t),s(e,pt,t),o(K,e,t),s(e,ot,t),s(e,O,t),s(e,mt,t),s(e,ee,t),s(e,rt,t),o(te,e,t),s(e,ct,t),o(le,e,t),s(e,dt,t),o(se,e,t),s(e,ut,t),s(e,ne,t),s(e,ht,t),o(ae,e,t),s(e,ft,t),s(e,ie,t),s(e,Mt,t),o(pe,e,t),s(e,bt,t),s(e,oe,t),s(e,gt,t),o(me,e,t),s(e,wt,t),s(e,re,t),s(e,yt,t),o(ce,e,t),s(e,Jt,t),s(e,de,t),s(e,Tt,t),o(ue,e,t),s(e,$t,t),s(e,he,t),s(e,jt,t),o(fe,e,t),s(e,Ut,t),s(e,Me,t),s(e,Gt,t),o(be,e,t),s(e,It,t),s(e,ge,t),s(e,vt,t),o(we,e,t),s(e,Ct,t),o(ye,e,t),s(e,Zt,t),s(e,Je,t),s(e,kt,t),s(e,Te,t),s(e,Wt,t),o($e,e,t),s(e,_t,t),s(e,Ue,t),xt=!0},p:dl,i(e){xt||(m(g.$$.fragment,e),m(w.$$.fragment,e),m($.$$.fragment,e),m(j.$$.fragment,e),m(U.$$.fragment,e),m(v.$$.fragment,e),m(C.$$.fragment,e),m(k.$$.fragment,e),m(_.$$.fragment,e),m(R.$$.fragment,e),m(V.$$.fragment,e),m(B.$$.fragment,e),m(X.$$.fragment,e),m(A.$$.fragment,e),m(z.$$.fragment,e),m(Q.$$.fragment,e),m(q.$$.fragment,e),m(S.$$.fragment,e),m(D.$$.fragment,e),m(K.$$.fragment,e),m(te.$$.fragment,e),m(le.$$.fragment,e),m(se.$$.fragment,e),m(ae.$$.fragment,e),m(pe.$$.fragment,e),m(me.$$.fragment,e),m(ce.$$.fragment,e),m(ue.$$.fragment,e),m(fe.$$.fragment,e),m(be.$$.fragment,e),m(we.$$.fragment,e),m(ye.$$.fragment,e),m($e.$$.fragment,e),xt=!0)},o(e){r(g.$$.fragment,e),r(w.$$.fragment,e),r($.$$.fragment,e),r(j.$$.fragment,e),r(U.$$.fragment,e),r(v.$$.fragment,e),r(C.$$.fragment,e),r(k.$$.fragment,e),r(_.$$.fragment,e),r(R.$$.fragment,e),r(V.$$.fragment,e),r(B.$$.fragment,e),r(X.$$.fragment,e),r(A.$$.fragment,e),r(z.$$.fragment,e),r(Q.$$.fragment,e),r(q.$$.fragment,e),r(S.$$.fragment,e),r(D.$$.fragment,e),r(K.$$.fragment,e),r(te.$$.fragment,e),r(le.$$.fragment,e),r(se.$$.fragment,e),r(ae.$$.fragment,e),r(pe.$$.fragment,e),r(me.$$.fragment,e),r(ce.$$.fragment,e),r(ue.$$.fragment,e),r(fe.$$.fragment,e),r(be.$$.fragment,e),r(we.$$.fragment,e),r(ye.$$.fragment,e),r($e.$$.fragment,e),xt=!1},d(e){e&&(l(Ge),l(je),l(Ie),l(ve),l(Ce),l(y),l(Ze),l(J),l(ke),l(T),l(We),l(_e),l(xe),l(Re),l(G),l(Ve),l(I),l(Be),l(Ne),l(Xe),l(Z),l(He),l(Ae),l(W),l(Ee),l(ze),l(x),l(Qe),l(qe),l(Fe),l(Ye),l(N),l(Se),l(Le),l(H),l(Pe),l(De),l(E),l(Ke),l(Oe),l(et),l(tt),l(F),l(lt),l(Y),l(st),l(nt),l(L),l(at),l(P),l(it),l(pt),l(ot),l(O),l(mt),l(ee),l(rt),l(ct),l(dt),l(ut),l(ne),l(ht),l(ft),l(ie),l(Mt),l(bt),l(oe),l(gt),l(wt),l(re),l(yt),l(Jt),l(de),l(Tt),l($t),l(he),l(jt),l(Ut),l(Me),l(Gt),l(It),l(ge),l(vt),l(Ct),l(Zt),l(Je),l(kt),l(Te),l(Wt),l(_t),l(Ue)),l(b),c(g,e),c(w,e),c($,e),c(j,e),c(U,e),c(v,e),c(C,e),c(k,e),c(_,e),c(R,e),c(V,e),c(B,e),c(X,e),c(A,e),c(z,e),c(Q,e),c(q,e),c(S,e),c(D,e),c(K,e),c(te,e),c(le,e),c(se,e),c(ae,e),c(pe,e),c(me,e),c(ce,e),c(ue,e),c(fe,e),c(be,e),c(we,e),c(ye,e),c($e,e)}}}const Jl='{"title":"使用 Hugging Face 和 Milvus 构建 RAG 系统","local":"使用-hugging-face-和-milvus-构建-rag-系统","sections":[{"title":"准备","local":"准备","sections":[{"title":"依赖关系和环境","local":"依赖关系和环境","sections":[],"depth":3},{"title":"准备数据","local":"准备数据","sections":[],"depth":3},{"title":"准备 embedding 模型","local":"准备-embedding-模型","sections":[],"depth":3}],"depth":2},{"title":"将数据加载到 Milvus 中","local":"将数据加载到-milvus-中","sections":[{"title":"创建 collection","local":"创建-collection","sections":[],"depth":3},{"title":"插入数据","local":"插入数据","sections":[],"depth":3}],"depth":2},{"title":"构建 RAG","local":"构建-rag","sections":[{"title":"检索查询数据","local":"检索查询数据","sections":[],"depth":3},{"title":"使用 LLM 获取 RAG 回答","local":"使用-llm-获取-rag-回答","sections":[],"depth":3}],"depth":2}],"depth":1}';function Tl(Rt){return ul(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Il extends hl{constructor(b){super(),fl(this,b,Tl,yl,cl,{})}}export{Il as component};
