import{s as dl,n as cl,o as hl}from"../chunks/scheduler.65852ee5.js";import{S as ul,i as fl,g as c,s,r as i,A as Ml,h,f as l,c as a,j as rl,u as o,x as u,k as ml,y as bl,a as n,v as p,d as r,t as m,w as d}from"../chunks/index.aa74147d.js";import{D as gl,C as f}from"../chunks/DocNotebookDropdown.479f4286.js";import{H as M,E as wl}from"../chunks/EditOnGithub.4eda6a96.js";function yl(Rt){let b,ve,je,Ie,g,Ge,w,Ce,y,Bt='<em>Authored by: <a href="https://github.com/zc277584121" rel="nofollow">Chen Zhang</a></em>',ke,J,Vt='<a href="https://milvus.io/" rel="nofollow">Milvus</a> is the world’s most advanced open-source vector database, built to power embedding similarity search and AI applications. In this tutorial, we will show you how to build a RAG(Retrieval-Augmented Generation) pipeline with Hugging Face and Milvus.',Ze,T,Nt="The RAG system combines a retrieval system with a generative model to generate new text based on a given prompt. The system first retrieves relevant documents from a corpus using Milvus, and then uses an LLM from Hugging Face to generate new text based on the retrieved documents.",We,$,xe,j,_e,U,Re,v,At="<p>If you are using Google Colab, to enable dependencies just installed, you may need to <strong>restart the runtime</strong> (click on the “Runtime” menu at the top of the screen, and select “Restart session” from the dropdown menu).</p>",Be,I,Et='In addition, we recommend that you configure the <a href="https://huggingface.co/docs/hub/security-tokens" rel="nofollow">Hugging Face User Access Token</a> by your account，and set it in your environment variables, because we will use the LLM from Hugging Face hub. You may get a low limit of requests if you don’t set the token environment variable.',Ve,G,Ne,C,Ae,k,Xt='We use the <a href="https://artificialintelligenceact.eu/wp-content/uploads/2021/08/The-AI-Act.pdf" rel="nofollow">AI Act PDF</a> as the private knowledge in our RAG, which is a regulatory framework for AI, with different risk levels meaning more or less regulation.',Ee,Z,Xe,W,qt='We use the <a href="https://python.langchain.com/v0.1/docs/modules/data_connection/document_loaders/pdf/" rel="nofollow"><code>PyPDFLoader</code></a> from LangChain to extract the text from the PDF, and then split the text into smaller chunks. By default, we set the chunk size as 1000 and the overlap as 200, which means each chunk will nearly have 1000 characters and the overlap between two chunks will be 200 characters.',qe,x,He,_,Ht=`108
`,ze,R,Qe,B,Fe,V,Ye,N,zt='Define a function to generate text embeddings. We use <a href="https://huggingface.co/BAAI/bge-small-en-v1.5" rel="nofollow">BGE embedding model</a> as an example, and you can change it to any other embedding models, where you can find in <a href="https://huggingface.co/spaces/mteb/leaderboard" rel="nofollow">MTEB leaderboard</a> .',Se,A,Le,E,Qt="Generate a test embedding and print its dimension and first few elements.",Pe,X,De,q,Ft=`384
[-0.07660683244466782, 0.025316666811704636, 0.012505513615906239, 0.004595153499394655, 0.025780051946640015, 0.03816710412502289, 0.08050819486379623, 0.003035430097952485, 0.02439221926033497, 0.0048803347162902355]
`,Ke,H,Oe,z,et,Q,tt,F,Yt='<p>As for the argument of <code>MilvusClient</code>:</p> <ul><li>Setting the <code>uri</code> as a local file, e.g.<code>./hf_milvus_demo.db</code>, is the most convenient method, as it automatically utilizes <a href="https://milvus.io/docs/milvus_lite.md" rel="nofollow">Milvus Lite</a> to store all data in this file.</li> <li>If you have large scale of data, you can set up a more performant Milvus server on <a href="https://milvus.io/docs/quickstart.md" rel="nofollow">docker or kubernetes</a>. In this setup, please use the server uri, e.g.<code>http://localhost:19530</code>, as your <code>uri</code>.</li> <li>If you want to use <a href="https://zilliz.com/cloud" rel="nofollow">Zilliz Cloud</a>, the fully managed cloud service for Milvus, adjust the <code>uri</code> and <code>token</code>, which correspond to the <a href="https://docs.zilliz.com/docs/on-zilliz-cloud-console#free-cluster-details" rel="nofollow">Public Endpoint and Api key</a> in Zilliz Cloud.</li></ul>',lt,Y,St="Check if the collection already exists and drop it if it does.",nt,S,st,L,Lt="Create a new collection with specified parameters.",at,P,Pt="If we don’t specify any field information, Milvus will automatically create a default <code>id</code> field for primary key, and a <code>vector</code> field to store the vector data. A reserved JSON field is used to store non-schema-defined fields and their values.",it,D,ot,K,pt,O,Dt="Iterate through the text lines, create embeddings, and then insert the data into Milvus.",rt,ee,Kt="Here is a new field <code>text</code>, which is a non-defined field in the collection schema. It will be automatically added to the reserved JSON dynamic field, which can be treated as a normal field at a high level.",mt,te,dt,le,ct,ne,ht,se,Ot="Let’s specify a question about the proposal.",ut,ae,ft,ie,el="Search for the question in the collection and retrieve the semantic top-3 matches.",Mt,oe,bt,pe,tl="Let’s take a look at the search results of the query",gt,re,wt,me,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,de,Jt,ce,nl="Convert the retrieved documents into a string format.",Tt,he,$t,ue,sl="Define prompts for the Language Model. This prompt is assembled with the retrieved documents from Milvus.",jt,fe,Ut,Me,al='We use the <a href="https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1" rel="nofollow">Mixtral-8x7B-Instruct-v0.1</a> to generate a response based on the prompts.',vt,be,It,ge,il="Format the prompt and generate the answer.",Gt,we,Ct,ye,kt,Je,ol=`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.
`,Zt,Te,pl="Congratulations! You have built an RAG pipeline with Hugging Face and Milvus.",Wt,$e,xt,Ue,_t;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/en/rag_with_hf_and_milvus.ipynb"}]}}),w=new M({props:{title:"Build RAG with Hugging Face and Milvus",local:"build-rag-with-hugging-face-and-milvus",headingTag:"h1"}}),$=new M({props:{title:"Preparation",local:"preparation",headingTag:"h2"}}),j=new M({props:{title:"Dependencies and Environment",local:"dependencies-and-environment",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}}),G=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:"Prepare the data",local:"prepare-the-data",headingTag:"h3"}}),Z=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}}),x=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}}),B=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}}),V=new M({props:{title:"Prepare the Embedding Model",local:"prepare-the-embedding-model",headingTag:"h3"}}),A=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}}),X=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:"Load data into Milvus",local:"load-data-into-milvus",headingTag:"h2"}}),z=new M({props:{title:"Create the Collection",local:"create-the-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:"Insert data",local:"insert-data",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:"Build RAG",local:"build-rag",headingTag:"h2"}}),ne=new M({props:{title:"Retrieve data for a query",local:"retrieve-data-for-a-query",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}}),oe=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}}),re=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}}),de=new M({props:{title:"Use LLM to get an RAG response",local:"use-llm-to-get-an-rag-response",headingTag:"h3"}}),he=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/en/rag_with_hf_and_milvus.md"}}),{c(){b=c("meta"),ve=s(),je=c("p"),Ie=s(),i(g.$$.fragment),Ge=s(),i(w.$$.fragment),Ce=s(),y=c("p"),y.innerHTML=Bt,ke=s(),J=c("p"),J.innerHTML=Vt,Ze=s(),T=c("p"),T.textContent=Nt,We=s(),i($.$$.fragment),xe=s(),i(j.$$.fragment),_e=s(),i(U.$$.fragment),Re=s(),v=c("blockquote"),v.innerHTML=At,Be=s(),I=c("p"),I.innerHTML=Et,Ve=s(),i(G.$$.fragment),Ne=s(),i(C.$$.fragment),Ae=s(),k=c("p"),k.innerHTML=Xt,Ee=s(),i(Z.$$.fragment),Xe=s(),W=c("p"),W.innerHTML=qt,qe=s(),i(x.$$.fragment),He=s(),_=c("pre"),_.textContent=Ht,ze=s(),i(R.$$.fragment),Qe=s(),i(B.$$.fragment),Fe=s(),i(V.$$.fragment),Ye=s(),N=c("p"),N.innerHTML=zt,Se=s(),i(A.$$.fragment),Le=s(),E=c("p"),E.textContent=Qt,Pe=s(),i(X.$$.fragment),De=s(),q=c("pre"),q.textContent=Ft,Ke=s(),i(H.$$.fragment),Oe=s(),i(z.$$.fragment),et=s(),i(Q.$$.fragment),tt=s(),F=c("blockquote"),F.innerHTML=Yt,lt=s(),Y=c("p"),Y.textContent=St,nt=s(),i(S.$$.fragment),st=s(),L=c("p"),L.textContent=Lt,at=s(),P=c("p"),P.innerHTML=Pt,it=s(),i(D.$$.fragment),ot=s(),i(K.$$.fragment),pt=s(),O=c("p"),O.textContent=Dt,rt=s(),ee=c("p"),ee.innerHTML=Kt,mt=s(),i(te.$$.fragment),dt=s(),i(le.$$.fragment),ct=s(),i(ne.$$.fragment),ht=s(),se=c("p"),se.textContent=Ot,ut=s(),i(ae.$$.fragment),ft=s(),ie=c("p"),ie.textContent=el,Mt=s(),i(oe.$$.fragment),bt=s(),pe=c("p"),pe.textContent=tl,gt=s(),i(re.$$.fragment),wt=s(),me=c("pre"),me.textContent=ll,yt=s(),i(de.$$.fragment),Jt=s(),ce=c("p"),ce.textContent=nl,Tt=s(),i(he.$$.fragment),$t=s(),ue=c("p"),ue.textContent=sl,jt=s(),i(fe.$$.fragment),Ut=s(),Me=c("p"),Me.innerHTML=al,vt=s(),i(be.$$.fragment),It=s(),ge=c("p"),ge.textContent=il,Gt=s(),i(we.$$.fragment),Ct=s(),i(ye.$$.fragment),kt=s(),Je=c("pre"),Je.textContent=ol,Zt=s(),Te=c("p"),Te.textContent=pl,Wt=s(),i($e.$$.fragment),xt=s(),Ue=c("p"),this.h()},l(e){const t=Ml("svelte-u9bgzb",document.head);b=h(t,"META",{name:!0,content:!0}),t.forEach(l),ve=a(e),je=h(e,"P",{}),rl(je).forEach(l),Ie=a(e),o(g.$$.fragment,e),Ge=a(e),o(w.$$.fragment,e),Ce=a(e),y=h(e,"P",{"data-svelte-h":!0}),u(y)!=="svelte-1a5wzqz"&&(y.innerHTML=Bt),ke=a(e),J=h(e,"P",{"data-svelte-h":!0}),u(J)!=="svelte-1x6k4uf"&&(J.innerHTML=Vt),Ze=a(e),T=h(e,"P",{"data-svelte-h":!0}),u(T)!=="svelte-19faguc"&&(T.textContent=Nt),We=a(e),o($.$$.fragment,e),xe=a(e),o(j.$$.fragment,e),_e=a(e),o(U.$$.fragment,e),Re=a(e),v=h(e,"BLOCKQUOTE",{"data-svelte-h":!0}),u(v)!=="svelte-okgk74"&&(v.innerHTML=At),Be=a(e),I=h(e,"P",{"data-svelte-h":!0}),u(I)!=="svelte-38dvop"&&(I.innerHTML=Et),Ve=a(e),o(G.$$.fragment,e),Ne=a(e),o(C.$$.fragment,e),Ae=a(e),k=h(e,"P",{"data-svelte-h":!0}),u(k)!=="svelte-8hbydq"&&(k.innerHTML=Xt),Ee=a(e),o(Z.$$.fragment,e),Xe=a(e),W=h(e,"P",{"data-svelte-h":!0}),u(W)!=="svelte-ok4onc"&&(W.innerHTML=qt),qe=a(e),o(x.$$.fragment,e),He=a(e),_=h(e,"PRE",{"data-svelte-h":!0}),u(_)!=="svelte-q6t6yp"&&(_.textContent=Ht),ze=a(e),o(R.$$.fragment,e),Qe=a(e),o(B.$$.fragment,e),Fe=a(e),o(V.$$.fragment,e),Ye=a(e),N=h(e,"P",{"data-svelte-h":!0}),u(N)!=="svelte-1vrm788"&&(N.innerHTML=zt),Se=a(e),o(A.$$.fragment,e),Le=a(e),E=h(e,"P",{"data-svelte-h":!0}),u(E)!=="svelte-1k4nam5"&&(E.textContent=Qt),Pe=a(e),o(X.$$.fragment,e),De=a(e),q=h(e,"PRE",{"data-svelte-h":!0}),u(q)!=="svelte-citxyn"&&(q.textContent=Ft),Ke=a(e),o(H.$$.fragment,e),Oe=a(e),o(z.$$.fragment,e),et=a(e),o(Q.$$.fragment,e),tt=a(e),F=h(e,"BLOCKQUOTE",{"data-svelte-h":!0}),u(F)!=="svelte-1wwr14u"&&(F.innerHTML=Yt),lt=a(e),Y=h(e,"P",{"data-svelte-h":!0}),u(Y)!=="svelte-1gg40t6"&&(Y.textContent=St),nt=a(e),o(S.$$.fragment,e),st=a(e),L=h(e,"P",{"data-svelte-h":!0}),u(L)!=="svelte-hrnx97"&&(L.textContent=Lt),at=a(e),P=h(e,"P",{"data-svelte-h":!0}),u(P)!=="svelte-1t69aou"&&(P.innerHTML=Pt),it=a(e),o(D.$$.fragment,e),ot=a(e),o(K.$$.fragment,e),pt=a(e),O=h(e,"P",{"data-svelte-h":!0}),u(O)!=="svelte-tgjhxg"&&(O.textContent=Dt),rt=a(e),ee=h(e,"P",{"data-svelte-h":!0}),u(ee)!=="svelte-acj9x6"&&(ee.innerHTML=Kt),mt=a(e),o(te.$$.fragment,e),dt=a(e),o(le.$$.fragment,e),ct=a(e),o(ne.$$.fragment,e),ht=a(e),se=h(e,"P",{"data-svelte-h":!0}),u(se)!=="svelte-1k3hh7n"&&(se.textContent=Ot),ut=a(e),o(ae.$$.fragment,e),ft=a(e),ie=h(e,"P",{"data-svelte-h":!0}),u(ie)!=="svelte-16p6vvk"&&(ie.textContent=el),Mt=a(e),o(oe.$$.fragment,e),bt=a(e),pe=h(e,"P",{"data-svelte-h":!0}),u(pe)!=="svelte-1az8z3k"&&(pe.textContent=tl),gt=a(e),o(re.$$.fragment,e),wt=a(e),me=h(e,"PRE",{"data-svelte-h":!0}),u(me)!=="svelte-sx7p7m"&&(me.textContent=ll),yt=a(e),o(de.$$.fragment,e),Jt=a(e),ce=h(e,"P",{"data-svelte-h":!0}),u(ce)!=="svelte-1is24w7"&&(ce.textContent=nl),Tt=a(e),o(he.$$.fragment,e),$t=a(e),ue=h(e,"P",{"data-svelte-h":!0}),u(ue)!=="svelte-108tlj2"&&(ue.textContent=sl),jt=a(e),o(fe.$$.fragment,e),Ut=a(e),Me=h(e,"P",{"data-svelte-h":!0}),u(Me)!=="svelte-1i30iag"&&(Me.innerHTML=al),vt=a(e),o(be.$$.fragment,e),It=a(e),ge=h(e,"P",{"data-svelte-h":!0}),u(ge)!=="svelte-1o7c97n"&&(ge.textContent=il),Gt=a(e),o(we.$$.fragment,e),Ct=a(e),o(ye.$$.fragment,e),kt=a(e),Je=h(e,"PRE",{"data-svelte-h":!0}),u(Je)!=="svelte-18i7g4t"&&(Je.textContent=ol),Zt=a(e),Te=h(e,"P",{"data-svelte-h":!0}),u(Te)!=="svelte-6dbn83"&&(Te.textContent=pl),Wt=a(e),o($e.$$.fragment,e),xt=a(e),Ue=h(e,"P",{}),rl(Ue).forEach(l),this.h()},h(){ml(b,"name","hf:doc:metadata"),ml(b,"content",Jl)},m(e,t){bl(document.head,b),n(e,ve,t),n(e,je,t),n(e,Ie,t),p(g,e,t),n(e,Ge,t),p(w,e,t),n(e,Ce,t),n(e,y,t),n(e,ke,t),n(e,J,t),n(e,Ze,t),n(e,T,t),n(e,We,t),p($,e,t),n(e,xe,t),p(j,e,t),n(e,_e,t),p(U,e,t),n(e,Re,t),n(e,v,t),n(e,Be,t),n(e,I,t),n(e,Ve,t),p(G,e,t),n(e,Ne,t),p(C,e,t),n(e,Ae,t),n(e,k,t),n(e,Ee,t),p(Z,e,t),n(e,Xe,t),n(e,W,t),n(e,qe,t),p(x,e,t),n(e,He,t),n(e,_,t),n(e,ze,t),p(R,e,t),n(e,Qe,t),p(B,e,t),n(e,Fe,t),p(V,e,t),n(e,Ye,t),n(e,N,t),n(e,Se,t),p(A,e,t),n(e,Le,t),n(e,E,t),n(e,Pe,t),p(X,e,t),n(e,De,t),n(e,q,t),n(e,Ke,t),p(H,e,t),n(e,Oe,t),p(z,e,t),n(e,et,t),p(Q,e,t),n(e,tt,t),n(e,F,t),n(e,lt,t),n(e,Y,t),n(e,nt,t),p(S,e,t),n(e,st,t),n(e,L,t),n(e,at,t),n(e,P,t),n(e,it,t),p(D,e,t),n(e,ot,t),p(K,e,t),n(e,pt,t),n(e,O,t),n(e,rt,t),n(e,ee,t),n(e,mt,t),p(te,e,t),n(e,dt,t),p(le,e,t),n(e,ct,t),p(ne,e,t),n(e,ht,t),n(e,se,t),n(e,ut,t),p(ae,e,t),n(e,ft,t),n(e,ie,t),n(e,Mt,t),p(oe,e,t),n(e,bt,t),n(e,pe,t),n(e,gt,t),p(re,e,t),n(e,wt,t),n(e,me,t),n(e,yt,t),p(de,e,t),n(e,Jt,t),n(e,ce,t),n(e,Tt,t),p(he,e,t),n(e,$t,t),n(e,ue,t),n(e,jt,t),p(fe,e,t),n(e,Ut,t),n(e,Me,t),n(e,vt,t),p(be,e,t),n(e,It,t),n(e,ge,t),n(e,Gt,t),p(we,e,t),n(e,Ct,t),p(ye,e,t),n(e,kt,t),n(e,Je,t),n(e,Zt,t),n(e,Te,t),n(e,Wt,t),p($e,e,t),n(e,xt,t),n(e,Ue,t),_t=!0},p:cl,i(e){_t||(r(g.$$.fragment,e),r(w.$$.fragment,e),r($.$$.fragment,e),r(j.$$.fragment,e),r(U.$$.fragment,e),r(G.$$.fragment,e),r(C.$$.fragment,e),r(Z.$$.fragment,e),r(x.$$.fragment,e),r(R.$$.fragment,e),r(B.$$.fragment,e),r(V.$$.fragment,e),r(A.$$.fragment,e),r(X.$$.fragment,e),r(H.$$.fragment,e),r(z.$$.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(ne.$$.fragment,e),r(ae.$$.fragment,e),r(oe.$$.fragment,e),r(re.$$.fragment,e),r(de.$$.fragment,e),r(he.$$.fragment,e),r(fe.$$.fragment,e),r(be.$$.fragment,e),r(we.$$.fragment,e),r(ye.$$.fragment,e),r($e.$$.fragment,e),_t=!0)},o(e){m(g.$$.fragment,e),m(w.$$.fragment,e),m($.$$.fragment,e),m(j.$$.fragment,e),m(U.$$.fragment,e),m(G.$$.fragment,e),m(C.$$.fragment,e),m(Z.$$.fragment,e),m(x.$$.fragment,e),m(R.$$.fragment,e),m(B.$$.fragment,e),m(V.$$.fragment,e),m(A.$$.fragment,e),m(X.$$.fragment,e),m(H.$$.fragment,e),m(z.$$.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(ne.$$.fragment,e),m(ae.$$.fragment,e),m(oe.$$.fragment,e),m(re.$$.fragment,e),m(de.$$.fragment,e),m(he.$$.fragment,e),m(fe.$$.fragment,e),m(be.$$.fragment,e),m(we.$$.fragment,e),m(ye.$$.fragment,e),m($e.$$.fragment,e),_t=!1},d(e){e&&(l(ve),l(je),l(Ie),l(Ge),l(Ce),l(y),l(ke),l(J),l(Ze),l(T),l(We),l(xe),l(_e),l(Re),l(v),l(Be),l(I),l(Ve),l(Ne),l(Ae),l(k),l(Ee),l(Xe),l(W),l(qe),l(He),l(_),l(ze),l(Qe),l(Fe),l(Ye),l(N),l(Se),l(Le),l(E),l(Pe),l(De),l(q),l(Ke),l(Oe),l(et),l(tt),l(F),l(lt),l(Y),l(nt),l(st),l(L),l(at),l(P),l(it),l(ot),l(pt),l(O),l(rt),l(ee),l(mt),l(dt),l(ct),l(ht),l(se),l(ut),l(ft),l(ie),l(Mt),l(bt),l(pe),l(gt),l(wt),l(me),l(yt),l(Jt),l(ce),l(Tt),l($t),l(ue),l(jt),l(Ut),l(Me),l(vt),l(It),l(ge),l(Gt),l(Ct),l(kt),l(Je),l(Zt),l(Te),l(Wt),l(xt),l(Ue)),l(b),d(g,e),d(w,e),d($,e),d(j,e),d(U,e),d(G,e),d(C,e),d(Z,e),d(x,e),d(R,e),d(B,e),d(V,e),d(A,e),d(X,e),d(H,e),d(z,e),d(Q,e),d(S,e),d(D,e),d(K,e),d(te,e),d(le,e),d(ne,e),d(ae,e),d(oe,e),d(re,e),d(de,e),d(he,e),d(fe,e),d(be,e),d(we,e),d(ye,e),d($e,e)}}}const Jl='{"title":"Build RAG with Hugging Face and Milvus","local":"build-rag-with-hugging-face-and-milvus","sections":[{"title":"Preparation","local":"preparation","sections":[{"title":"Dependencies and Environment","local":"dependencies-and-environment","sections":[],"depth":3},{"title":"Prepare the data","local":"prepare-the-data","sections":[],"depth":3},{"title":"Prepare the Embedding Model","local":"prepare-the-embedding-model","sections":[],"depth":3}],"depth":2},{"title":"Load data into Milvus","local":"load-data-into-milvus","sections":[{"title":"Create the Collection","local":"create-the-collection","sections":[],"depth":3},{"title":"Insert data","local":"insert-data","sections":[],"depth":3}],"depth":2},{"title":"Build RAG","local":"build-rag","sections":[{"title":"Retrieve data for a query","local":"retrieve-data-for-a-query","sections":[],"depth":3},{"title":"Use LLM to get an RAG response","local":"use-llm-to-get-an-rag-response","sections":[],"depth":3}],"depth":2}],"depth":1}';function Tl(Rt){return hl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Il extends ul{constructor(b){super(),fl(this,b,Tl,yl,dl,{})}}export{Il as component};
