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(Re){let b,Gt,jt,It,g,Ct,w,vt,y,Ve='<em>作者: <a href="https://github.com/zc277584121" rel="nofollow">张晨</a></em>',Zt,J,Be='<a href="https://milvus.io/" rel="nofollow">Milvus</a> 是世界上最先进的开源向量数据库，旨在支持 embedding 相似性搜索和人工智能应用程序。在本教程中，我们将向您展示如何使用 Hugging Face 和 Milvus 构建 RAG（检索增强生成）管道。',kt,$,Ne="RAG 系统将检索系统与生成模型相结合，根据给定的提示生成新文本。该系统首先使用 Milvus 从语料库中检索相关文档，然后使用 Hugging Face 的 LLM 根据检索到的文档生成新文本。",Wt,T,_t,j,xt,U,Rt,G,Xe="<p>如果您使用 Google Colab，要启用刚刚安装的依赖项，您可能需要 <strong>重新启动运行时</strong>（单击屏幕顶部的“运行时”菜单，然后从下拉菜单中选择“重新启动会话”） 。</p>",Vt,I,Ae='另外，我们建议您在您的账户中配置 <a href="https://huggingface.co/docs/hub/security-tokens" rel="nofollow">Hugging Face User Access Token</a> ，并将其设置在您的环境变量中，因为我们将使用Hugging Face 中心的 LLM 。如果不设置 token 环境变量，您可能会受到较低的请求速率限制。',Bt,C,Nt,v,Xt,Z,Ee='我们使用 <a href="https://artificialintelligenceact.eu/wp-content/uploads/2021/08/The-AI-Act.pdf" rel="nofollow">AI Act PDF</a> 作为我们 RAG 中的私有知识，它是一个 AI 的监管框架文件，不同的风险级别意味着或多或少的监管。',At,k,Et,W,ze='我们使用 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 个字符。',zt,_,Ht,x,He=`108
`,Qt,R,qt,V,Ft,B,Yt,N,Qe='定义一个函数来生成文本 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> 选择。',St,X,Lt,A,qe="生成测试 embedding 并打印其大小和前几个元素。",Pt,E,Dt,z,Fe=`384
[-0.07660683244466782, 0.025316666811704636, 0.012505513615906239, 0.004595153499394655, 0.025780051946640015, 0.03816710412502289, 0.08050819486379623, 0.003035430097952485, 0.02439221926033497, 0.0048803347162902355]
`,Kt,H,Ot,Q,te,q,ee,F,Ye='<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，例如“http://localhost:19530”作为您的“uri”。</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#free-cluster-details" rel="nofollow">Public Endpoint 和 Api key</a> 。</li></ul>',le,Y,Se="检查集合是否已存在，如果存在则将其删除。",se,S,ne,L,Le="使用指定参数创建一个新集合。",ae,P,Pe="如果我们不指定任何字段信息，Milvus 会自动创建一个默认的 id 字段作为主键，并创建一个 vector 字段来存储向量数据。保留的 JSON 字段用于存储未定义 schema 的字段及其值。",ie,D,pe,K,oe,O,De="迭代文本行，创建 embedding，然后将数据插入到 Milvus 中。",me,tt,Ke="这是一个新字段“text”，它是集合模式中的未定义字段。它将自动添加到保留的 JSON 动态字段中，可以将其视为高层的普通字段。",re,et,ce,lt,de,st,ue,nt,Oe="让我们具体说明一个有关该提案的问题。",he,at,fe,it,tl="在 collection 中搜索问题并检索语义前 3 个匹配项。",Me,pt,be,ot,el="我们来看看查询的搜索结果",ge,mt,we,rt,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
    ]
]
`,ye,ct,Je,dt,sl="将检索到的文档转换为字符串格式。",$e,ut,Te,ht,nl="定义语言模型的提示词。该提示与从 Milvus 检索到的文档组合在一起。",je,ft,Ue,Mt,al='我们使用 <a href="https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1" rel="nofollow">Mixtral-8x7B-Instruct-v0.1</a> 根据提示词生成响应。',Ge,bt,Ie,gt,il="设置提示词格式并生成答案。",Ce,wt,ve,yt,Ze,Jt,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.
`,ke,$t,ol="恭喜！您已经使用 Hugging Face 和 Milvus 构建了 RAG 流程。",We,Tt,_e,Ut,xe;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/main/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"}}),T=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}}),C=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}}),v=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}}),E=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}}),H=new M({props:{title:"将数据加载到 Milvus 中",local:"将数据加载到-milvus-中",headingTag:"h2"}}),Q=new M({props:{title:"创建集合",local:"创建集合",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"}}),et=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}}),lt=new M({props:{title:"构建 RAG",local:"构建-rag",headingTag:"h2"}}),st=new M({props:{title:"检索查询数据",local:"检索查询数据",headingTag:"h3"}}),at=new f({props:{code:"cXVlc3Rpb24lMjAlM0QlMjAlMjJXaGF0JTIwaXMlMjB0aGUlMjBsZWdhbCUyMGJhc2lzJTIwZm9yJTIwdGhlJTIwcHJvcG9zYWwlM0YlMjI=",highlighted:'question = <span class="hljs-string">&quot;What is the legal basis for the proposal?&quot;</span>',wrap:!1}}),pt=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}}),mt=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}}),ct=new M({props:{title:"使用 LLM 获取 RAG 响应",local:"使用-llm-获取-rag-响应",headingTag:"h3"}}),ut=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}}),ft=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}}),bt=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}}),wt=new f({props:{code:"cHJvbXB0JTIwJTNEJTIwUFJPTVBULmZvcm1hdChjb250ZXh0JTNEY29udGV4dCUyQyUyMHF1ZXN0aW9uJTNEcXVlc3Rpb24p",highlighted:'prompt = PROMPT.<span class="hljs-built_in">format</span>(context=context, question=question)',wrap:!1}}),yt=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}}),Tt=new wl({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/zh-CN/rag_with_hf_and_milvus.md"}}),{c(){b=d("meta"),Gt=n(),jt=d("p"),It=n(),i(g.$$.fragment),Ct=n(),i(w.$$.fragment),vt=n(),y=d("p"),y.innerHTML=Ve,Zt=n(),J=d("p"),J.innerHTML=Be,kt=n(),$=d("p"),$.textContent=Ne,Wt=n(),i(T.$$.fragment),_t=n(),i(j.$$.fragment),xt=n(),i(U.$$.fragment),Rt=n(),G=d("blockquote"),G.innerHTML=Xe,Vt=n(),I=d("p"),I.innerHTML=Ae,Bt=n(),i(C.$$.fragment),Nt=n(),i(v.$$.fragment),Xt=n(),Z=d("p"),Z.innerHTML=Ee,At=n(),i(k.$$.fragment),Et=n(),W=d("p"),W.innerHTML=ze,zt=n(),i(_.$$.fragment),Ht=n(),x=d("pre"),x.textContent=He,Qt=n(),i(R.$$.fragment),qt=n(),i(V.$$.fragment),Ft=n(),i(B.$$.fragment),Yt=n(),N=d("p"),N.innerHTML=Qe,St=n(),i(X.$$.fragment),Lt=n(),A=d("p"),A.textContent=qe,Pt=n(),i(E.$$.fragment),Dt=n(),z=d("pre"),z.textContent=Fe,Kt=n(),i(H.$$.fragment),Ot=n(),i(Q.$$.fragment),te=n(),i(q.$$.fragment),ee=n(),F=d("blockquote"),F.innerHTML=Ye,le=n(),Y=d("p"),Y.textContent=Se,se=n(),i(S.$$.fragment),ne=n(),L=d("p"),L.textContent=Le,ae=n(),P=d("p"),P.textContent=Pe,ie=n(),i(D.$$.fragment),pe=n(),i(K.$$.fragment),oe=n(),O=d("p"),O.textContent=De,me=n(),tt=d("p"),tt.textContent=Ke,re=n(),i(et.$$.fragment),ce=n(),i(lt.$$.fragment),de=n(),i(st.$$.fragment),ue=n(),nt=d("p"),nt.textContent=Oe,he=n(),i(at.$$.fragment),fe=n(),it=d("p"),it.textContent=tl,Me=n(),i(pt.$$.fragment),be=n(),ot=d("p"),ot.textContent=el,ge=n(),i(mt.$$.fragment),we=n(),rt=d("pre"),rt.textContent=ll,ye=n(),i(ct.$$.fragment),Je=n(),dt=d("p"),dt.textContent=sl,$e=n(),i(ut.$$.fragment),Te=n(),ht=d("p"),ht.textContent=nl,je=n(),i(ft.$$.fragment),Ue=n(),Mt=d("p"),Mt.innerHTML=al,Ge=n(),i(bt.$$.fragment),Ie=n(),gt=d("p"),gt.textContent=il,Ce=n(),i(wt.$$.fragment),ve=n(),i(yt.$$.fragment),Ze=n(),Jt=d("pre"),Jt.textContent=pl,ke=n(),$t=d("p"),$t.textContent=ol,We=n(),i(Tt.$$.fragment),_e=n(),Ut=d("p"),this.h()},l(t){const e=Ml("svelte-u9bgzb",document.head);b=u(e,"META",{name:!0,content:!0}),e.forEach(l),Gt=a(t),jt=u(t,"P",{}),ml(jt).forEach(l),It=a(t),p(g.$$.fragment,t),Ct=a(t),p(w.$$.fragment,t),vt=a(t),y=u(t,"P",{"data-svelte-h":!0}),h(y)!=="svelte-1chlthn"&&(y.innerHTML=Ve),Zt=a(t),J=u(t,"P",{"data-svelte-h":!0}),h(J)!=="svelte-1biy7ii"&&(J.innerHTML=Be),kt=a(t),$=u(t,"P",{"data-svelte-h":!0}),h($)!=="svelte-1mua59i"&&($.textContent=Ne),Wt=a(t),p(T.$$.fragment,t),_t=a(t),p(j.$$.fragment,t),xt=a(t),p(U.$$.fragment,t),Rt=a(t),G=u(t,"BLOCKQUOTE",{"data-svelte-h":!0}),h(G)!=="svelte-19llaah"&&(G.innerHTML=Xe),Vt=a(t),I=u(t,"P",{"data-svelte-h":!0}),h(I)!=="svelte-anis2j"&&(I.innerHTML=Ae),Bt=a(t),p(C.$$.fragment,t),Nt=a(t),p(v.$$.fragment,t),Xt=a(t),Z=u(t,"P",{"data-svelte-h":!0}),h(Z)!=="svelte-waii5y"&&(Z.innerHTML=Ee),At=a(t),p(k.$$.fragment,t),Et=a(t),W=u(t,"P",{"data-svelte-h":!0}),h(W)!=="svelte-3vo957"&&(W.innerHTML=ze),zt=a(t),p(_.$$.fragment,t),Ht=a(t),x=u(t,"PRE",{"data-svelte-h":!0}),h(x)!=="svelte-q6t6yp"&&(x.textContent=He),Qt=a(t),p(R.$$.fragment,t),qt=a(t),p(V.$$.fragment,t),Ft=a(t),p(B.$$.fragment,t),Yt=a(t),N=u(t,"P",{"data-svelte-h":!0}),h(N)!=="svelte-1mvkndl"&&(N.innerHTML=Qe),St=a(t),p(X.$$.fragment,t),Lt=a(t),A=u(t,"P",{"data-svelte-h":!0}),h(A)!=="svelte-12ffb5h"&&(A.textContent=qe),Pt=a(t),p(E.$$.fragment,t),Dt=a(t),z=u(t,"PRE",{"data-svelte-h":!0}),h(z)!=="svelte-citxyn"&&(z.textContent=Fe),Kt=a(t),p(H.$$.fragment,t),Ot=a(t),p(Q.$$.fragment,t),te=a(t),p(q.$$.fragment,t),ee=a(t),F=u(t,"BLOCKQUOTE",{"data-svelte-h":!0}),h(F)!=="svelte-s1klow"&&(F.innerHTML=Ye),le=a(t),Y=u(t,"P",{"data-svelte-h":!0}),h(Y)!=="svelte-z38hg9"&&(Y.textContent=Se),se=a(t),p(S.$$.fragment,t),ne=a(t),L=u(t,"P",{"data-svelte-h":!0}),h(L)!=="svelte-1awgvfd"&&(L.textContent=Le),ae=a(t),P=u(t,"P",{"data-svelte-h":!0}),h(P)!=="svelte-17sl0at"&&(P.textContent=Pe),ie=a(t),p(D.$$.fragment,t),pe=a(t),p(K.$$.fragment,t),oe=a(t),O=u(t,"P",{"data-svelte-h":!0}),h(O)!=="svelte-yu2mi1"&&(O.textContent=De),me=a(t),tt=u(t,"P",{"data-svelte-h":!0}),h(tt)!=="svelte-d8ca28"&&(tt.textContent=Ke),re=a(t),p(et.$$.fragment,t),ce=a(t),p(lt.$$.fragment,t),de=a(t),p(st.$$.fragment,t),ue=a(t),nt=u(t,"P",{"data-svelte-h":!0}),h(nt)!=="svelte-hup66l"&&(nt.textContent=Oe),he=a(t),p(at.$$.fragment,t),fe=a(t),it=u(t,"P",{"data-svelte-h":!0}),h(it)!=="svelte-xy3ubc"&&(it.textContent=tl),Me=a(t),p(pt.$$.fragment,t),be=a(t),ot=u(t,"P",{"data-svelte-h":!0}),h(ot)!=="svelte-1rwok4e"&&(ot.textContent=el),ge=a(t),p(mt.$$.fragment,t),we=a(t),rt=u(t,"PRE",{"data-svelte-h":!0}),h(rt)!=="svelte-sx7p7m"&&(rt.textContent=ll),ye=a(t),p(ct.$$.fragment,t),Je=a(t),dt=u(t,"P",{"data-svelte-h":!0}),h(dt)!=="svelte-m1bp3w"&&(dt.textContent=sl),$e=a(t),p(ut.$$.fragment,t),Te=a(t),ht=u(t,"P",{"data-svelte-h":!0}),h(ht)!=="svelte-1rq6yrp"&&(ht.textContent=nl),je=a(t),p(ft.$$.fragment,t),Ue=a(t),Mt=u(t,"P",{"data-svelte-h":!0}),h(Mt)!=="svelte-1qls96i"&&(Mt.innerHTML=al),Ge=a(t),p(bt.$$.fragment,t),Ie=a(t),gt=u(t,"P",{"data-svelte-h":!0}),h(gt)!=="svelte-1l4vsax"&&(gt.textContent=il),Ce=a(t),p(wt.$$.fragment,t),ve=a(t),p(yt.$$.fragment,t),Ze=a(t),Jt=u(t,"PRE",{"data-svelte-h":!0}),h(Jt)!=="svelte-18i7g4t"&&(Jt.textContent=pl),ke=a(t),$t=u(t,"P",{"data-svelte-h":!0}),h($t)!=="svelte-1d9opac"&&($t.textContent=ol),We=a(t),p(Tt.$$.fragment,t),_e=a(t),Ut=u(t,"P",{}),ml(Ut).forEach(l),this.h()},h(){rl(b,"name","hf:doc:metadata"),rl(b,"content",Jl)},m(t,e){bl(document.head,b),s(t,Gt,e),s(t,jt,e),s(t,It,e),o(g,t,e),s(t,Ct,e),o(w,t,e),s(t,vt,e),s(t,y,e),s(t,Zt,e),s(t,J,e),s(t,kt,e),s(t,$,e),s(t,Wt,e),o(T,t,e),s(t,_t,e),o(j,t,e),s(t,xt,e),o(U,t,e),s(t,Rt,e),s(t,G,e),s(t,Vt,e),s(t,I,e),s(t,Bt,e),o(C,t,e),s(t,Nt,e),o(v,t,e),s(t,Xt,e),s(t,Z,e),s(t,At,e),o(k,t,e),s(t,Et,e),s(t,W,e),s(t,zt,e),o(_,t,e),s(t,Ht,e),s(t,x,e),s(t,Qt,e),o(R,t,e),s(t,qt,e),o(V,t,e),s(t,Ft,e),o(B,t,e),s(t,Yt,e),s(t,N,e),s(t,St,e),o(X,t,e),s(t,Lt,e),s(t,A,e),s(t,Pt,e),o(E,t,e),s(t,Dt,e),s(t,z,e),s(t,Kt,e),o(H,t,e),s(t,Ot,e),o(Q,t,e),s(t,te,e),o(q,t,e),s(t,ee,e),s(t,F,e),s(t,le,e),s(t,Y,e),s(t,se,e),o(S,t,e),s(t,ne,e),s(t,L,e),s(t,ae,e),s(t,P,e),s(t,ie,e),o(D,t,e),s(t,pe,e),o(K,t,e),s(t,oe,e),s(t,O,e),s(t,me,e),s(t,tt,e),s(t,re,e),o(et,t,e),s(t,ce,e),o(lt,t,e),s(t,de,e),o(st,t,e),s(t,ue,e),s(t,nt,e),s(t,he,e),o(at,t,e),s(t,fe,e),s(t,it,e),s(t,Me,e),o(pt,t,e),s(t,be,e),s(t,ot,e),s(t,ge,e),o(mt,t,e),s(t,we,e),s(t,rt,e),s(t,ye,e),o(ct,t,e),s(t,Je,e),s(t,dt,e),s(t,$e,e),o(ut,t,e),s(t,Te,e),s(t,ht,e),s(t,je,e),o(ft,t,e),s(t,Ue,e),s(t,Mt,e),s(t,Ge,e),o(bt,t,e),s(t,Ie,e),s(t,gt,e),s(t,Ce,e),o(wt,t,e),s(t,ve,e),o(yt,t,e),s(t,Ze,e),s(t,Jt,e),s(t,ke,e),s(t,$t,e),s(t,We,e),o(Tt,t,e),s(t,_e,e),s(t,Ut,e),xe=!0},p:dl,i(t){xe||(m(g.$$.fragment,t),m(w.$$.fragment,t),m(T.$$.fragment,t),m(j.$$.fragment,t),m(U.$$.fragment,t),m(C.$$.fragment,t),m(v.$$.fragment,t),m(k.$$.fragment,t),m(_.$$.fragment,t),m(R.$$.fragment,t),m(V.$$.fragment,t),m(B.$$.fragment,t),m(X.$$.fragment,t),m(E.$$.fragment,t),m(H.$$.fragment,t),m(Q.$$.fragment,t),m(q.$$.fragment,t),m(S.$$.fragment,t),m(D.$$.fragment,t),m(K.$$.fragment,t),m(et.$$.fragment,t),m(lt.$$.fragment,t),m(st.$$.fragment,t),m(at.$$.fragment,t),m(pt.$$.fragment,t),m(mt.$$.fragment,t),m(ct.$$.fragment,t),m(ut.$$.fragment,t),m(ft.$$.fragment,t),m(bt.$$.fragment,t),m(wt.$$.fragment,t),m(yt.$$.fragment,t),m(Tt.$$.fragment,t),xe=!0)},o(t){r(g.$$.fragment,t),r(w.$$.fragment,t),r(T.$$.fragment,t),r(j.$$.fragment,t),r(U.$$.fragment,t),r(C.$$.fragment,t),r(v.$$.fragment,t),r(k.$$.fragment,t),r(_.$$.fragment,t),r(R.$$.fragment,t),r(V.$$.fragment,t),r(B.$$.fragment,t),r(X.$$.fragment,t),r(E.$$.fragment,t),r(H.$$.fragment,t),r(Q.$$.fragment,t),r(q.$$.fragment,t),r(S.$$.fragment,t),r(D.$$.fragment,t),r(K.$$.fragment,t),r(et.$$.fragment,t),r(lt.$$.fragment,t),r(st.$$.fragment,t),r(at.$$.fragment,t),r(pt.$$.fragment,t),r(mt.$$.fragment,t),r(ct.$$.fragment,t),r(ut.$$.fragment,t),r(ft.$$.fragment,t),r(bt.$$.fragment,t),r(wt.$$.fragment,t),r(yt.$$.fragment,t),r(Tt.$$.fragment,t),xe=!1},d(t){t&&(l(Gt),l(jt),l(It),l(Ct),l(vt),l(y),l(Zt),l(J),l(kt),l($),l(Wt),l(_t),l(xt),l(Rt),l(G),l(Vt),l(I),l(Bt),l(Nt),l(Xt),l(Z),l(At),l(Et),l(W),l(zt),l(Ht),l(x),l(Qt),l(qt),l(Ft),l(Yt),l(N),l(St),l(Lt),l(A),l(Pt),l(Dt),l(z),l(Kt),l(Ot),l(te),l(ee),l(F),l(le),l(Y),l(se),l(ne),l(L),l(ae),l(P),l(ie),l(pe),l(oe),l(O),l(me),l(tt),l(re),l(ce),l(de),l(ue),l(nt),l(he),l(fe),l(it),l(Me),l(be),l(ot),l(ge),l(we),l(rt),l(ye),l(Je),l(dt),l($e),l(Te),l(ht),l(je),l(Ue),l(Mt),l(Ge),l(Ie),l(gt),l(Ce),l(ve),l(Ze),l(Jt),l(ke),l($t),l(We),l(_e),l(Ut)),l(b),c(g,t),c(w,t),c(T,t),c(j,t),c(U,t),c(C,t),c(v,t),c(k,t),c(_,t),c(R,t),c(V,t),c(B,t),c(X,t),c(E,t),c(H,t),c(Q,t),c(q,t),c(S,t),c(D,t),c(K,t),c(et,t),c(lt,t),c(st,t),c(at,t),c(pt,t),c(mt,t),c(ct,t),c(ut,t),c(ft,t),c(bt,t),c(wt,t),c(yt,t),c(Tt,t)}}}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":"创建集合","local":"创建集合","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 $l(Re){return ul(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Il extends hl{constructor(b){super(),fl(this,b,$l,yl,cl,{})}}export{Il as component};
