import{s as Ca,n as Ia,o as Aa}from"../chunks/scheduler.1f027d5a.js";import{S as ba,i as ga,e as M,s as e,c as j,h as fa,a as p,d as a,b as n,f as ua,g as y,j as i,k as da,l as Ea,m as t,n as U,t as o,o as c,p as J}from"../chunks/index.6117a65d.js";import{C as Ba}from"../chunks/CopyLLMTxtMenu.610b106b.js";import{D as Za,C as T}from"../chunks/DocNotebookDropdown.4ae0d470.js";import{H as h,E as Va}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.bd18ae98.js";function Ra(Xs){let m,Bl,fl,Zl,r,Vl,w,Rl,u,Gl,d,vs='作者: <a href="https://huggingface.co/lloydmeta" rel="nofollow">lloydmeta</a>',kl,C,$s="这个 notebook 将引导你构建一个由 Elasticsearch（ES）和 Hugging Face 模型支持的检索增强生成（RAG）系统，允许你在 ES 向量化（你的 ES 集群在摄取和查询时为你向量化）与自向量化（你在将数据发送到 ES 之前对所有数据进行向量化）之间切换。",Sl,I,Fs="你的用例应该使用哪种方式？<em>这取决于</em> 🤷‍♂️。ES 向量化意味着你的客户端不需要实现这一点，所以这里默认使用；但是，如果你没有机器学习节点，或者你自己的嵌入设置更好/更快，可以在下面的“选择数据和查询向量化选项”部分将 <code>USE_ELASTICSEARCH_VECTORISATION</code> 设置为 <code>False</code>！",Ql,A,xs=`<p>[!提示]
这个 notebook 已经使用 ES 8.13.x 和 8.14.x 进行了测试。</p>`,Wl,b,Nl,g,_l,f,ql,E,Xl,B,Hs="这允许你通过 Hugging Face 进行身份验证，以便下载模型和数据集。",vl,Z,$l,V,Fl,R,Ys='让我们确保你可以访问你的 Elasticsearch 部署。如果你还没有，可以在 <a href="https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-a-cloud-deployment" rel="nofollow">Elastic Cloud</a>上创建一个。',xl,G,zs="确保你已经将 <code>CLOUD_ID</code> 和 <code>ELASTIC_DEPL_API_KEY</code> 保存为 Colab secrets。",Hl,k,Ls='<img src="https://huggingface.co/datasets/huggingface/cookbook-images/resolve/main/colab-secrets.jpeg" alt="Image of how to set up secrets using Google Colab"/>',Yl,S,zl,Q,Ds="设置客户端并确保凭据有效。",Ll,W,Dl,N,Pl,_,Ps='本教程中使用的数据来源于 Hugging Face 数据集，特别是来自 <a href="https://huggingface.co/datasets/MongoDB/embedded_movies" rel="nofollow">MongoDB/embedded_movies 数据集</a>。',Ol,q,Kl,X,Os="以下代码片段中的操作专注于强制数据完整性和质量。",ls,v,Ks="<li>第一个过程确保每个数据点的 <code>fullplot</code> 属性不为空，因为这是我们在嵌入过程中主要使用的数据。</li> <li>第二步还确保我们移除所有数据点的 <code>plot_embedding</code> 属性，因为这将被一个使用不同的嵌入模型 <code>gte-large</code> 创建的新嵌入所替换。</li>",ss,$,as,F,ts,x,es,H,la="在这里，你需要做出一个决定：你希望 Elasticsearch 对你的数据和查询进行向量化，还是希望你自己来做？",ns,Y,sa="将 <code>USE_ELASTICSEARCH_VECTORISATION</code> 设置为 <code>True</code> 将使本 notebook 的其余部分为你数据和查询设置并使用 ES 托管的向量化，但<strong>请注意</strong>，这要求你的 ES 部署至少有 1 个 ML 节点（我建议在你的云部署上设置自动缩放为 true，以防你选择的模型过大）。",Ms,z,aa="如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 是 <code>False</code>，这个 notebook 将为你数据和查询向量化设置并使用提供的模型“本地”。",ps,L,ta='在这里，我选择了 <a href="https://huggingface.co/thenlper/gte-small" rel="nofollow">thenlper/gte-small</a> 模型，没有其他原因，只是因为它在另一个指南中使用过，而且对我来说效果足够好。如果你愿意，请随时尝试其他模型——唯一重要的是你根据模型更新 <code>EMBEDDING_DIMENSIONS</code>。',is,D,ea="<strong>注意</strong>：如果你更改这些值，你可能需要从这一步重新运行 notebook。",js,P,ys,O,Us,K,na='如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 设置为 <code>True</code>，这一步将使用 <a href="https://eland.readthedocs.io/en/v8.12.1/" rel="nofollow">Eland</a>将 Hugging Face 模型加载并部署到 Elasticsearch 中。这允许 Elasticsearch 在后续步骤中对你的查询和数据进行向量化。',os,ll,cs,sl,Ma="这一步添加了在本地为文本创建嵌入的函数，并通过嵌入丰富了数据集，这样数据就可以作为向量被摄取到 Elasticsearch 中。如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 为 True，则不会运行。",Js,al,Ts,tl,hs,el,pa="在这一点上，我们将在 Elasticsearch 中创建一个索引，并设置正确的索引映射来处理向量搜索。",ms,nl,ia='请点击这里了解更多关于 <a href="https://www.elastic.co/what-is/vector-search" rel="nofollow">Elasticsearch 向量搜索能力</a>的信息。',rs,Ml,ws,pl,ja=`Creating index movies
`,us,il,ya="将数据摄取到 Elasticsearch 中最好是批量进行。幸运的是，<code>helpers</code> 提供了一个简单的方法来执行这个操作。",ds,jl,Cs,yl,Ua=`batch: start [0], end [100]
batch: start [100], end [200]
batch: start [200], end [300]
batch: start [300], end [400]
batch: start [400], end [500]
batch: start [500], end [600]
batch: start [600], end [700]
batch: start [700], end [800]
batch: start [800], end [900]
batch: start [900], end [1000]
batch: start [1000], end [1100]
batch: start [1100], end [1200]
batch: start [1200], end [1300]
batch: start [1300], end [1400]
batch: start [1400], end [1452]
Data ingestion into Elasticsearch complete!
`,Is,Ul,As,ol,oa="以下步骤实现了一个函数，该函数返回一个向量搜索结果。",bs,cl,ca="如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 为 true，文本查询将直接发送到 ES，在那里将首先使用上传的模型对其进行向量化，然后执行向量搜索。如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 为 false，那么我们在发送查询之前先在本地进行向量化，然后发送查询的向量化形式。",gs,Jl,fs,Tl,Es,hl,Bs,ml,Ja=`Query: What is the best romantic movie to watch and why?
Continue to answer the query by using these Search Results:
Title: Shut Up and Kiss Me!, Plot: Ryan and Pete are 27-year old best friends in Miami, born on the same day and each searching for the perfect woman. Ryan is a rookie stockbroker living with his psychic Mom. Pete is a slick surfer dude yet to find commitment. Each meets the women of their dreams on the same day. Ryan knocks heads in an elevator with the gorgeous Jessica, passing out before getting her number. Pete falls for the insatiable Tiara, but Tiara's uncle is mob boss Vincent Bublione, charged with her protection. This high-energy romantic comedy asks to what extent will you go for true love?
Title: Titanic, Plot: The plot focuses on the romances of two couples upon the doomed ship's maiden voyage. Isabella Paradine (Catherine Zeta-Jones) is a wealthy woman mourning the loss of her aunt, who reignites a romance with former flame Wynn Park (Peter Gallagher). Meanwhile, a charming ne'er-do-well named Jamie Perse (Mike Doyle) steals a ticket for the ship, and falls for a sweet innocent Irish girl on board. But their romance is threatened by the villainous Simon Doonan (Tim Curry), who has discovered about the ticket and makes Jamie his unwilling accomplice, as well as having sinister plans for the girl.
Title: Dark Blue World, Plot: March 15, 1939: Germany invades Czechoslovakia. Czech and Slovak pilots flee to England, joining the RAF. After the war, back home, they are put in labor camps, suspected of anti-Communist ideas. This film cuts between a post-war camp where Franta is a prisoner and England during the war, where Franta is like a big brother to Karel, a very young pilot. On maneuvers, Karel crash lands by the rural home of Susan, an English woman whose husband is MIA. She spends one night with Karel, and he thinks he's found the love of his life. It's complicated by Susan's attraction to Franta. How will the three handle innocence, Eros, friendship, and the heat of battle? When war ends, what then?
Title: Dark Blue World, Plot: March 15, 1939: Germany invades Czechoslovakia. Czech and Slovak pilots flee to England, joining the RAF. After the war, back home, they are put in labor camps, suspected of anti-Communist ideas. This film cuts between a post-war camp where Franta is a prisoner and England during the war, where Franta is like a big brother to Karel, a very young pilot. On maneuvers, Karel crash lands by the rural home of Susan, an English woman whose husband is MIA. She spends one night with Karel, and he thinks he's found the love of his life. It's complicated by Susan's attraction to Franta. How will the three handle innocence, Eros, friendship, and the heat of battle? When war ends, what then?
Title: No Good Deed, Plot: About a police detective, Jack, who, while doing a friend a favor and searching for a runaway teenager on Turk Street, stumbles upon a bizarre band of criminals about to pull off a bank robbery. Jack finds himself being held hostage while the criminals decide what to do with him, and the leader's beautiful girlfriend, Erin, is left alone to watch Jack. Erin, who we discover is a master manipulator of the men in the gang, reveals another side to Jack - a melancholy romantic who could have been a classical cellist. She finds Jack's captivity an irresistible turn-on and he can't figure out if she's for real, or manipulating him, too. Before the gang returns, Jack and Erin's connection intensifies and who ends up with the money is anyone's guess.
.
`,Zs,rl,Ta='加载我们的 LLM (这里我们用 <a href="https://huggingface.co/google/gemma-2b-it" rel="nofollow">google/gemma-2b-lt</a>)',Vs,wl,Rs,ul,ha="定义一个方法，从 ES 中的向量搜索获取格式化的结果，然后将这些结果传递给 LLM（大型语言模型）以获取我们的结果。",Gs,dl,ks,Cl,ma=`Query: What's a romantic movie that I can watch with my wife?
Continue to answer the query by using these Search Results:
Title: King Solomon's Mines, Plot: Guide Allan Quatermain helps a young lady (Beth) find her lost husband somewhere in Africa. It's a spectacular adventure story with romance, because while they fight with wild animals and cannibals, they fall in love. Will they find the lost husband and finish the nice connection?
Title: Shut Up and Kiss Me!, Plot: Ryan and Pete are 27-year old best friends in Miami, born on the same day and each searching for the perfect woman. Ryan is a rookie stockbroker living with his psychic Mom. Pete is a slick surfer dude yet to find commitment. Each meets the women of their dreams on the same day. Ryan knocks heads in an elevator with the gorgeous Jessica, passing out before getting her number. Pete falls for the insatiable Tiara, but Tiara's uncle is mob boss Vincent Bublione, charged with her protection. This high-energy romantic comedy asks to what extent will you go for true love?
Title: Titanic, Plot: The plot focuses on the romances of two couples upon the doomed ship's maiden voyage. Isabella Paradine (Catherine Zeta-Jones) is a wealthy woman mourning the loss of her aunt, who reignites a romance with former flame Wynn Park (Peter Gallagher). Meanwhile, a charming ne'er-do-well named Jamie Perse (Mike Doyle) steals a ticket for the ship, and falls for a sweet innocent Irish girl on board. But their romance is threatened by the villainous Simon Doonan (Tim Curry), who has discovered about the ticket and makes Jamie his unwilling accomplice, as well as having sinister plans for the girl.
Title: Fortress, Plot: A futuristic prison movie. Protagonist and wife are nabbed at a future US emigration point with an illegal baby during population control. The resulting prison experience is the subject of the movie. The prison is a futuristic one run by a private corporation bent on mind control in various ways.
Title: Varalaaru, Plot: Relationships become entangled in an emotional web.
.

Which movie would you recommend for a romantic evening with your wife?

From the provided titles, the movie that would be recommended for a romantic evening with your wife is **King Solomon's Mines**. It's a romantic adventure story with romance, and it's a great choice for a date night.
`,Ss,Il,Qs,Al,ra="这个 notebook 改编自",Ws,bl,wa='<li><a href="https://huggingface.co/learn/cookbook/rag_with_hugging_face_gemma_mongodb" rel="nofollow">MongoDB 的 RAG 指南</a></li> <li>OpenAI 的 <a href="https://github.com/openai/openai-cookbook/blob/main/examples/vector_databases/elasticsearch/elasticsearch-retrieval-augmented-generation.ipynb" rel="nofollow">ES RAG 指南</a></li> <li>Elasticsearch-labs 的 <a href="https://github.com/elastic/elasticsearch-labs/blob/main/notebooks/integrations/hugging-face/loading-model-from-hugging-face.ipynb" rel="nofollow">从-Hugging-Face-加载模型-cookbook</a></li>',Ns,gl,_s,El,qs;return r=new Za({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_hugging_face_gemma_elasticsearch.ipynb"}]}}),w=new Ba({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),u=new h({props:{title:"构建一个基于 Gemma、Elasticsearch 和 Hugging Face 模型的 RAG 系统",local:"构建一个基于-gemmaelasticsearch-和-hugging-face-模型的-rag-系统",headingTag:"h1"}}),b=new h({props:{title:"步骤 1：安装相关库",local:"步骤-1安装相关库",headingTag:"h2"}}),g=new T({props:{code:"IXBpcCUyMGluc3RhbGwlMjBlbGFzdGljc2VhcmNoJTIwc2VudGVuY2VfdHJhbnNmb3JtZXJzJTIwdHJhbnNmb3JtZXJzJTIwZWxhbmQlM0QlM0Q4LjEyLjElMjAlMjMlMjBhY2NlbGVyYXRlJTIwJTIzJTIwdW5jb21tZW50JTIwaWYlMjB1c2luZyUyMEdQVSUwQSFwaXAlMjBpbnN0YWxsJTIwZGF0YXNldHMlM0QlM0QyLjE5LjIlMjAlMjMlMjBSZW1vdmUlMjB2ZXJzaW9uJTIwbG9jayUyMGlmJTIwaHR0cHMlM0ElMkYlMkZnaXRodWIuY29tJTJGaHVnZ2luZ2ZhY2UlMkZkYXRhc2V0cyUyRnB1bGwlMkY2OTc4JTIwaGFzJTIwYmVlbiUyMHJlbGVhc2Vk",highlighted:`!pip install elasticsearch sentence_transformers transformers eland==<span class="hljs-number">8.12</span><span class="hljs-number">.1</span> <span class="hljs-comment"># accelerate # uncomment if using GPU</span>
!pip install datasets==<span class="hljs-number">2.19</span><span class="hljs-number">.2</span> <span class="hljs-comment"># Remove version lock if https://github.com/huggingface/datasets/pull/6978 has been released</span>`,wrap:!1}}),f=new h({props:{title:"步骤 2：设置",local:"步骤-2设置",headingTag:"h2"}}),E=new h({props:{title:"Hugging Face",local:"hugging-face",headingTag:"h3"}}),Z=new T({props:{code:"ZnJvbSUyMGh1Z2dpbmdmYWNlX2h1YiUyMGltcG9ydCUyMG5vdGVib29rX2xvZ2luJTBBJTBBbm90ZWJvb2tfbG9naW4oKQ==",highlighted:`<span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> notebook_login

notebook_login()`,wrap:!1}}),V=new h({props:{title:"Elasticsearch 部署",local:"elasticsearch-部署",headingTag:"h4"}}),S=new T({props:{code:"ZnJvbSUyMGdvb2dsZS5jb2xhYiUyMGltcG9ydCUyMHVzZXJkYXRhJTBBJTBBJTIzJTIwaHR0cHMlM0ElMkYlMkZ3d3cuZWxhc3RpYy5jbyUyRnNlYXJjaC1sYWJzJTJGdHV0b3JpYWxzJTJGaW5zdGFsbC1lbGFzdGljc2VhcmNoJTJGZWxhc3RpYy1jbG91ZCUyM2ZpbmRpbmcteW91ci1jbG91ZC1pZCUwQUNMT1VEX0lEJTIwJTNEJTIwdXNlcmRhdGEuZ2V0KCUyMkNMT1VEX0lEJTIyKSUyMCUyMyUyMG9yJTIwJTIyJTNDWU9VUiUyMENMT1VEX0lEJTNFJTIyJTBBJTBBJTIzJTIwaHR0cHMlM0ElMkYlMkZ3d3cuZWxhc3RpYy5jbyUyRnNlYXJjaC1sYWJzJTJGdHV0b3JpYWxzJTJGaW5zdGFsbC1lbGFzdGljc2VhcmNoJTJGZWxhc3RpYy1jbG91ZCUyM2NyZWF0aW5nLWFuLWFwaS1rZXklMEFFTEFTVElDX0FQSV9LRVklMjAlM0QlMjB1c2VyZGF0YS5nZXQoJTIyRUxBU1RJQ19ERVBMX0FQSV9LRVklMjIpJTIwJTIwJTIzJTIwb3IlMjAlMjIlM0NZT1VSJTIwQVBJJTIwS0VZJTNFJTIy",highlighted:`<span class="hljs-keyword">from</span> google.colab <span class="hljs-keyword">import</span> userdata

<span class="hljs-comment"># https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#finding-your-cloud-id</span>
CLOUD_ID = userdata.get(<span class="hljs-string">&quot;CLOUD_ID&quot;</span>) <span class="hljs-comment"># or &quot;&lt;YOUR CLOUD_ID&gt;&quot;</span>

<span class="hljs-comment"># https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-an-api-key</span>
ELASTIC_API_KEY = userdata.get(<span class="hljs-string">&quot;ELASTIC_DEPL_API_KEY&quot;</span>)  <span class="hljs-comment"># or &quot;&lt;YOUR API KEY&gt;&quot;</span>`,wrap:!1}}),W=new T({props:{code:"ZnJvbSUyMGVsYXN0aWNzZWFyY2glMjBpbXBvcnQlMjBFbGFzdGljc2VhcmNoJTJDJTIwaGVscGVycyUwQSUwQSUyMyUyMENyZWF0ZSUyMHRoZSUyMGNsaWVudCUyMGluc3RhbmNlJTBBY2xpZW50JTIwJTNEJTIwRWxhc3RpY3NlYXJjaChjbG91ZF9pZCUzRENMT1VEX0lEJTJDJTIwYXBpX2tleSUzREVMQVNUSUNfQVBJX0tFWSklMEElMEElMjMlMjBTdWNjZXNzZnVsJTIwcmVzcG9uc2UhJTBBY2xpZW50LmluZm8oKQ==",highlighted:`<span class="hljs-keyword">from</span> elasticsearch <span class="hljs-keyword">import</span> Elasticsearch, helpers

<span class="hljs-comment"># Create the client instance</span>
client = Elasticsearch(cloud_id=CLOUD_ID, api_key=ELASTIC_API_KEY)

<span class="hljs-comment"># Successful response!</span>
client.info()`,wrap:!1}}),N=new h({props:{title:"步骤 3：数据获取和准备",local:"步骤-3数据获取和准备",headingTag:"h2"}}),q=new T({props:{code:"JTIzJTIwTG9hZCUyMERhdGFzZXQlMEFmcm9tJTIwZGF0YXNldHMlMjBpbXBvcnQlMjBsb2FkX2RhdGFzZXQlMEElMEElMjMlMjBodHRwcyUzQSUyRiUyRmh1Z2dpbmdmYWNlLmNvJTJGZGF0YXNldHMlMkZNb25nb0RCJTJGZW1iZWRkZWRfbW92aWVzJTBBZGF0YXNldCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJNb25nb0RCJTJGZW1iZWRkZWRfbW92aWVzJTIyKSUwQSUwQWRhdGFzZXQ=",highlighted:`<span class="hljs-comment"># Load Dataset</span>
<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

<span class="hljs-comment"># https://huggingface.co/datasets/MongoDB/embedded_movies</span>
dataset = load_dataset(<span class="hljs-string">&quot;MongoDB/embedded_movies&quot;</span>)

dataset`,wrap:!1}}),$=new T({props:{code:"JTIzJTIwRGF0YSUyMFByZXBhcmF0aW9uJTBBJTBBJTIzJTIwUmVtb3ZlJTIwZGF0YSUyMHBvaW50JTIwd2hlcmUlMjBwbG90JTIwY29sb3VtbiUyMGlzJTIwbWlzc2luZyUwQWRhdGFzZXQlMjAlM0QlMjBkYXRhc2V0LmZpbHRlcihsYW1iZGElMjB4JTNBJTIweCU1QiUyMmZ1bGxwbG90JTIyJTVEJTIwaXMlMjBub3QlMjBOb25lKSUwQSUwQWlmJTIwJTIycGxvdF9lbWJlZGRpbmclMjIlMjBpbiUyMHN1bShkYXRhc2V0LmNvbHVtbl9uYW1lcy52YWx1ZXMoKSUyQyUyMCU1QiU1RCklM0ElMEElMjAlMjAlMjAlMjAlMjMlMjBSZW1vdmUlMjB0aGUlMjBwbG90X2VtYmVkZGluZyUyMGZyb20lMjBlYWNoJTIwZGF0YSUyMHBvaW50JTIwaW4lMjB0aGUlMjBkYXRhc2V0JTIwYXMlMjB3ZSUyMGFyZSUyMGdvaW5nJTIwdG8lMjBjcmVhdGUlMjBuZXclMjBlbWJlZGRpbmdzJTIwd2l0aCUyMGFuJTIwb3BlbiUyMHNvdXJjZSUyMGVtYmVkZGluZyUyMG1vZGVsJTIwZnJvbSUyMEh1Z2dpbmclMjBGYWNlJTBBJTIwJTIwJTIwJTIwZGF0YXNldCUyMCUzRCUyMGRhdGFzZXQucmVtb3ZlX2NvbHVtbnMoJTIycGxvdF9lbWJlZGRpbmclMjIpJTBBJTBBZGF0YXNldCU1QiUyMnRyYWluJTIyJTVE",highlighted:`<span class="hljs-comment"># Data Preparation</span>

<span class="hljs-comment"># Remove data point where plot coloumn is missing</span>
dataset = dataset.<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> x: x[<span class="hljs-string">&quot;fullplot&quot;</span>] <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>)

<span class="hljs-keyword">if</span> <span class="hljs-string">&quot;plot_embedding&quot;</span> <span class="hljs-keyword">in</span> <span class="hljs-built_in">sum</span>(dataset.column_names.values(), []):
    <span class="hljs-comment"># Remove the plot_embedding from each data point in the dataset as we are going to create new embeddings with an open source embedding model from Hugging Face</span>
    dataset = dataset.remove_columns(<span class="hljs-string">&quot;plot_embedding&quot;</span>)

dataset[<span class="hljs-string">&quot;train&quot;</span>]`,wrap:!1}}),F=new h({props:{title:"步骤 4：使用向量化数据加载 Elasticsearch",local:"步骤-4使用向量化数据加载-elasticsearch",headingTag:"h2"}}),x=new h({props:{title:"选择数据和查询向量化选项",local:"选择数据和查询向量化选项",headingTag:"h3"}}),P=new T({props:{code:"VVNFX0VMQVNUSUNTRUFSQ0hfVkVDVE9SSVNBVElPTiUyMCUzRCUyMFRydWUlMEElMEFFTUJFRERJTkdfTU9ERUxfSUQlMjAlM0QlMjAlMjJ0aGVubHBlciUyRmd0ZS1zbWFsbCUyMiUwQSUyMyUyMGh0dHBzJTNBJTJGJTJGaHVnZ2luZ2ZhY2UuY28lMkZ0aGVubHBlciUyRmd0ZS1zbWFsbCdzJTIwcGFnZSUyMHNob3dzJTIwdGhlJTIwZGltZW5zaW9ucyUyMG9mJTIwdGhlJTIwbW9kZWwlMEElMjMlMjBJZiUyMHlvdSUyMHVzZSUyMHRoZSUyMCU2MGd0ZS1iYXNlJTYwJTIwb3IlMjAlNjBndGUtbGFyZ2UlNjAlMjBlbWJlZGRpbmclMjBtb2RlbHMlMkMlMjB0aGUlMjBudW1EaW1lbnNpb24lMEElMjMlMjB2YWx1ZSUyMGluJTIwdGhlJTIwdmVjdG9yJTIwc2VhcmNoJTIwaW5kZXglMjBtdXN0JTIwYmUlMjBzZXQlMjB0byUyMDc2OCUyMGFuZCUyMDEwMjQlMkMlMjByZXNwZWN0aXZlbHkuJTBBRU1CRURESU5HX0RJTUVOU0lPTlMlMjAlM0QlMjAzODQ=",highlighted:`USE_ELASTICSEARCH_VECTORISATION = <span class="hljs-literal">True</span>

EMBEDDING_MODEL_ID = <span class="hljs-string">&quot;thenlper/gte-small&quot;</span>
<span class="hljs-comment"># https://huggingface.co/thenlper/gte-small&#x27;s page shows the dimensions of the model</span>
<span class="hljs-comment"># If you use the \`gte-base\` or \`gte-large\` embedding models, the numDimension</span>
<span class="hljs-comment"># value in the vector search index must be set to 768 and 1024, respectively.</span>
EMBEDDING_DIMENSIONS = <span class="hljs-number">384</span>`,wrap:!1}}),O=new h({props:{title:"如有必要，将 Hugging Face 模型加载到 Elasticsearch 中",local:"如有必要将-hugging-face-模型加载到-elasticsearch-中",headingTag:"h3"}}),ll=new T({props:{code:"aW1wb3J0JTIwbG9jYWxlJTBBbG9jYWxlLmdldHByZWZlcnJlZGVuY29kaW5nJTIwJTNEJTIwbGFtYmRhJTNBJTIwJTIyVVRGLTglMjIlMEEhKGlmJTIwJTVCJTIwJTIyVHJ1ZSUyMiUyMCUzRCUzRCUyMCUyNFVTRV9FTEFTVElDU0VBUkNIX1ZFQ1RPUklTQVRJT04lMjAlNUQlM0IlMjB0aGVuJTIwJTVDJTBBJTIwJTIwZWxhbmRfaW1wb3J0X2h1Yl9tb2RlbCUyMC0tY2xvdWQtaWQlMjAlMjRDTE9VRF9JRCUyMC0taHViLW1vZGVsLWlkJTIwJTI0RU1CRURESU5HX01PREVMX0lEJTIwLS10YXNrLXR5cGUlMjB0ZXh0X2VtYmVkZGluZyUyMC0tZXMtYXBpLWtleSUyMCUyNEVMQVNUSUNfQVBJX0tFWSUyMC0tc3RhcnQlMjAtLWNsZWFyLXByZXZpb3VzJTNCJTIwJTVDJTBBZmkp",highlighted:`<span class="hljs-keyword">import</span> locale
locale.getpreferredencoding = <span class="hljs-keyword">lambda</span>: <span class="hljs-string">&quot;UTF-8&quot;</span>
!(<span class="hljs-keyword">if</span> [ <span class="hljs-string">&quot;True&quot;</span> == $USE_ELASTICSEARCH_VECTORISATION ]; then \\
  eland_import_hub_model --cloud-<span class="hljs-built_in">id</span> $CLOUD_ID --hub-model-<span class="hljs-built_in">id</span> $EMBEDDING_MODEL_ID --task-<span class="hljs-built_in">type</span> text_embedding --es-api-key $ELASTIC_API_KEY --start --clear-previous; \\
fi)`,wrap:!1}}),al=new T({props:{code:"ZnJvbSUyMHNlbnRlbmNlX3RyYW5zZm9ybWVycyUyMGltcG9ydCUyMFNlbnRlbmNlVHJhbnNmb3JtZXIlMEElMEFpZiUyMG5vdCUyMFVTRV9FTEFTVElDU0VBUkNIX1ZFQ1RPUklTQVRJT04lM0ElMEElMjAlMjAlMjAlMjBlbWJlZGRpbmdfbW9kZWwlMjAlM0QlMjBTZW50ZW5jZVRyYW5zZm9ybWVyKEVNQkVERElOR19NT0RFTF9JRCklMEElMEElMEFkZWYlMjBnZXRfZW1iZWRkaW5nKHRleHQlM0ElMjBzdHIpJTIwLSUzRSUyMGxpc3QlNUJmbG9hdCU1RCUzQS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0aW9uKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGYlMjJEaXNhYmxlZCUyMHdoZW4lMjBVU0VfRUxBU1RJQ1NFQVJDSF9WRUNUT1JJU0FUSU9OJTIwaXMlMjAlNUIlN0JVU0VfRUxBU1RJQ1NFQVJDSF9WRUNUT1JJU0FUSU9OJTdEJTVEJTIyJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKSUwQSUyMCUyMCUyMCUyMGVsc2UlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmdWxsX3Bsb3RzJTIwJTNEJTIweCU1QiUyMmZ1bGxwbG90JTIyJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTdCJTIyZW1iZWRkaW5nJTIyJTNBJTIwJTVCZ2V0X2VtYmVkZGluZyhmdWxsX3Bsb3QpJTIwZm9yJTIwZnVsbF9wbG90JTIwaW4lMjBmdWxsX3Bsb3RzJTVEJTdEJTBBJTBBJTBBaWYlMjBub3QlMjBVU0VfRUxBU1RJQ1NFQVJDSF9WRUNUT1JJU0FUSU9OJTNBJTBBJTIwJTIwJTIwJTIwZGF0YXNldCUyMCUzRCUyMGRhdGFzZXQubWFwKGFkZF9mdWxscGxvdF9lbWJlZGRpbmclMkMlMjBiYXRjaGVkJTNEVHJ1ZSklMEElMjAlMjAlMjAlMjBkYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQ=",highlighted:`<span class="hljs-keyword">from</span> sentence_transformers <span class="hljs-keyword">import</span> SentenceTransformer

<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> USE_ELASTICSEARCH_VECTORISATION:
    embedding_model = SentenceTransformer(EMBEDDING_MODEL_ID)


<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_embedding</span>(<span class="hljs-params">text: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-built_in">list</span>[<span class="hljs-built_in">float</span>]:
    <span class="hljs-keyword">if</span> USE_ELASTICSEARCH_VECTORISATION:
        <span class="hljs-keyword">raise</span> Exception(
            <span class="hljs-string">f&quot;Disabled when USE_ELASTICSEARCH_VECTORISATION is [<span class="hljs-subst">{USE_ELASTICSEARCH_VECTORISATION}</span>]&quot;</span>
        )
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> text.strip():
            <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Attempted to get embedding for empty text.&quot;</span>)
            <span class="hljs-keyword">return</span> []

        embedding = embedding_model.encode(text)
        <span class="hljs-keyword">return</span> embedding.tolist()


<span class="hljs-keyword">def</span> <span class="hljs-title function_">add_fullplot_embedding</span>(<span class="hljs-params">x</span>):
    <span class="hljs-keyword">if</span> USE_ELASTICSEARCH_VECTORISATION:
        <span class="hljs-keyword">raise</span> Exception(
            <span class="hljs-string">f&quot;Disabled when USE_ELASTICSEARCH_VECTORISATION is [<span class="hljs-subst">{USE_ELASTICSEARCH_VECTORISATION}</span>]&quot;</span>
        )
    <span class="hljs-keyword">else</span>:
        full_plots = x[<span class="hljs-string">&quot;fullplot&quot;</span>]
        <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;embedding&quot;</span>: [get_embedding(full_plot) <span class="hljs-keyword">for</span> full_plot <span class="hljs-keyword">in</span> full_plots]}


<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> USE_ELASTICSEARCH_VECTORISATION:
    dataset = dataset.<span class="hljs-built_in">map</span>(add_fullplot_embedding, batched=<span class="hljs-literal">True</span>)
    dataset[<span class="hljs-string">&quot;train&quot;</span>]`,wrap:!1}}),tl=new h({props:{title:"步骤 5：创建一个带有向量搜索映射的搜索索引。",local:"步骤-5创建一个带有向量搜索映射的搜索索引",headingTag:"h2"}}),Ml=new T({props:{code:" ",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Needs to match the id returned from Eland</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># in general for Hugging Face models, you just replace the forward slash with</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># double underscore</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>model_id = EMBEDDING_MODEL_ID.replace(<span class="hljs-string">&quot;/&quot;</span>, <span class="hljs-string">&quot;__&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>index_name = <span class="hljs-string">&quot;movies&quot;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>index_mapping = {
<span class="hljs-meta">... </span>    <span class="hljs-string">&quot;properties&quot;</span>: {
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;fullplot&quot;</span>: {<span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;text&quot;</span>},
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;plot&quot;</span>: {<span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;text&quot;</span>},
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;title&quot;</span>: {<span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;text&quot;</span>},
<span class="hljs-meta">... </span>    }
<span class="hljs-meta">... </span>}
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># define index mapping</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> USE_ELASTICSEARCH_VECTORISATION:
<span class="hljs-meta">... </span>    index_mapping[<span class="hljs-string">&quot;properties&quot;</span>][<span class="hljs-string">&quot;embedding&quot;</span>] = {
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;properties&quot;</span>: {
<span class="hljs-meta">... </span>            <span class="hljs-string">&quot;is_truncated&quot;</span>: {<span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;boolean&quot;</span>},
<span class="hljs-meta">... </span>            <span class="hljs-string">&quot;model_id&quot;</span>: {
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;text&quot;</span>,
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;fields&quot;</span>: {<span class="hljs-string">&quot;keyword&quot;</span>: {<span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;keyword&quot;</span>, <span class="hljs-string">&quot;ignore_above&quot;</span>: <span class="hljs-number">256</span>}},
<span class="hljs-meta">... </span>            },
<span class="hljs-meta">... </span>            <span class="hljs-string">&quot;predicted_value&quot;</span>: {
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;dense_vector&quot;</span>,
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;dims&quot;</span>: EMBEDDING_DIMENSIONS,
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;index&quot;</span>: <span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;similarity&quot;</span>: <span class="hljs-string">&quot;cosine&quot;</span>,
<span class="hljs-meta">... </span>            },
<span class="hljs-meta">... </span>        }
<span class="hljs-meta">... </span>    }
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    index_mapping[<span class="hljs-string">&quot;properties&quot;</span>][<span class="hljs-string">&quot;embedding&quot;</span>] = {
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;type&quot;</span>: <span class="hljs-string">&quot;dense_vector&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;dims&quot;</span>: EMBEDDING_DIMENSIONS,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;index&quot;</span>: <span class="hljs-string">&quot;true&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;similarity&quot;</span>: <span class="hljs-string">&quot;cosine&quot;</span>,
<span class="hljs-meta">... </span>    }

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># flag to check if index has to be deleted before creating</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>should_delete_index = <span class="hljs-literal">True</span>

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># check if we want to delete index before creating the index</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> should_delete_index:
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> client.indices.exists(index=index_name):
<span class="hljs-meta">... </span>        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Deleting existing %s&quot;</span> % index_name)
<span class="hljs-meta">... </span>        client.indices.delete(index=index_name, ignore=[<span class="hljs-number">400</span>, <span class="hljs-number">404</span>])

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Creating index %s&quot;</span> % index_name)


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># ingest pipeline definition</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> USE_ELASTICSEARCH_VECTORISATION:
<span class="hljs-meta">... </span>    pipeline_id = <span class="hljs-string">&quot;vectorize_fullplots&quot;</span>

<span class="hljs-meta">... </span>    client.ingest.put_pipeline(
<span class="hljs-meta">... </span>        <span class="hljs-built_in">id</span>=pipeline_id,
<span class="hljs-meta">... </span>        processors=[
<span class="hljs-meta">... </span>            {
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;inference&quot;</span>: {
<span class="hljs-meta">... </span>                    <span class="hljs-string">&quot;model_id&quot;</span>: model_id,
<span class="hljs-meta">... </span>                    <span class="hljs-string">&quot;target_field&quot;</span>: <span class="hljs-string">&quot;embedding&quot;</span>,
<span class="hljs-meta">... </span>                    <span class="hljs-string">&quot;field_map&quot;</span>: {<span class="hljs-string">&quot;fullplot&quot;</span>: <span class="hljs-string">&quot;text_field&quot;</span>},
<span class="hljs-meta">... </span>                }
<span class="hljs-meta">... </span>            }
<span class="hljs-meta">... </span>        ],
<span class="hljs-meta">... </span>    )

<span class="hljs-meta">... </span>    index_settings = {
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;index&quot;</span>: {
<span class="hljs-meta">... </span>            <span class="hljs-string">&quot;default_pipeline&quot;</span>: pipeline_id,
<span class="hljs-meta">... </span>        }
<span class="hljs-meta">... </span>    }
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    index_settings = {}

<span class="hljs-meta">&gt;&gt;&gt; </span>client.options(ignore_status=[<span class="hljs-number">400</span>, <span class="hljs-number">404</span>]).indices.create(
<span class="hljs-meta">... </span>    index=index_name, mappings=index_mapping, settings=index_settings
<span class="hljs-meta">... </span>)`,wrap:!1}}),jl=new T({props:{code:"ZnJvbSUyMGVsYXN0aWNzZWFyY2guaGVscGVycyUyMGltcG9ydCUyMEJ1bGtJbmRleEVycm9yJTBBJTBBZGVmJTIwYmF0Y2hfdG9fYnVsa19hY3Rpb25zKGJhdGNoKSUzQSUwQSUyMCUyMCUyMCUyMGZvciUyMHJlY29yZCUyMGluJTIwYmF0Y2glM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBhY3Rpb24lMjAlM0QlMjAlN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJfaW5kZXglMjIlM0ElMjAlMjJtb3ZpZXMlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJfc291cmNlJTIyJTNBJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIydGl0bGUlMjIlM0ElMjByZWNvcmQlNUIlMjJ0aXRsZSUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmZ1bGxwbG90JTIyJTNBJTIwcmVjb3JkJTVCJTIyZnVsbHBsb3QlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMj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TIwJTIwYnVsa19pbmRleChkYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQpJTBBZXhjZXB0JTIwQnVsa0luZGV4RXJyb3IlMjBhcyUyMGUlM0ElMEElMjAlMjAlMjAlMjBwcmludChmJTIyJTdCZS5lcnJvcnMlN0QlMjIpJTBBJTBBcHJpbnQoJTIyRGF0YSUyMGluZ2VzdGlvbiUyMGludG8lMjBFbGFzdGljc2VhcmNoJTIwY29tcGxldGUhJTIyKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> elasticsearch.helpers <span class="hljs-keyword">import</span> BulkIndexError

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">batch_to_bulk_actions</span>(<span class="hljs-params">batch</span>):
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> record <span class="hljs-keyword">in</span> batch:
<span class="hljs-meta">... </span>        action = {
<span class="hljs-meta">... </span>            <span class="hljs-string">&quot;_index&quot;</span>: <span class="hljs-string">&quot;movies&quot;</span>,
<span class="hljs-meta">... </span>            <span class="hljs-string">&quot;_source&quot;</span>: {
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;title&quot;</span>: record[<span class="hljs-string">&quot;title&quot;</span>],
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;fullplot&quot;</span>: record[<span class="hljs-string">&quot;fullplot&quot;</span>],
<span class="hljs-meta">... </span>                <span class="hljs-string">&quot;plot&quot;</span>: record[<span class="hljs-string">&quot;plot&quot;</span>],
<span class="hljs-meta">... </span>            },
<span class="hljs-meta">... </span>        }
<span class="hljs-meta">... </span>        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> USE_ELASTICSEARCH_VECTORISATION:
<span class="hljs-meta">... </span>            action[<span class="hljs-string">&quot;_source&quot;</span>][<span class="hljs-string">&quot;embedding&quot;</span>] = record[<span class="hljs-string">&quot;embedding&quot;</span>]
<span class="hljs-meta">... </span>        <span class="hljs-keyword">yield</span> action


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">bulk_index</span>(<span class="hljs-params">ds</span>):
<span class="hljs-meta">... </span>    start = <span class="hljs-number">0</span>
<span class="hljs-meta">... </span>    end = <span class="hljs-built_in">len</span>(ds)
<span class="hljs-meta">... </span>    batch_size = <span class="hljs-number">100</span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> USE_ELASTICSEARCH_VECTORISATION:
<span class="hljs-meta">... </span>        <span class="hljs-comment"># If using auto-embedding, bulk requests can take a lot longer,</span>
<span class="hljs-meta">... </span>        <span class="hljs-comment"># so pass a longer request_timeout here (defaults to 10s), otherwise</span>
<span class="hljs-meta">... </span>        <span class="hljs-comment"># we could get Connection timeouts</span>
<span class="hljs-meta">... </span>        batch_client = client.options(request_timeout=<span class="hljs-number">600</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>        batch_client = client
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> batch_start <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(start, end, batch_size):
<span class="hljs-meta">... </span>        batch_end = <span class="hljs-built_in">min</span>(batch_start + batch_size, end)
<span class="hljs-meta">... </span>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;batch: start [<span class="hljs-subst">{batch_start}</span>], end [<span class="hljs-subst">{batch_end}</span>]&quot;</span>)
<span class="hljs-meta">... </span>        batch = ds.select(<span class="hljs-built_in">range</span>(batch_start, batch_end))
<span class="hljs-meta">... </span>        actions = batch_to_bulk_actions(batch)
<span class="hljs-meta">... </span>        helpers.bulk(batch_client, actions)


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">try</span>:
<span class="hljs-meta">... </span>    bulk_index(dataset[<span class="hljs-string">&quot;train&quot;</span>])
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">except</span> BulkIndexError <span class="hljs-keyword">as</span> e:
<span class="hljs-meta">... </span>    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;<span class="hljs-subst">{e.errors}</span>&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Data ingestion into Elasticsearch complete!&quot;</span>)`,wrap:!1}}),Ul=new h({props:{title:"步骤 6：对用户查询执行向量搜索",local:"步骤-6对用户查询执行向量搜索",headingTag:"h2"}}),Jl=new T({props:{code:"ZGVmJTIwdmVjdG9yX3NlYXJjaChwbG90X3F1ZXJ5KSUzQSUwQSUyMCUyMCUyMCUyMGlmJTIwVVNFX0VMQVNUSUNTRUFSQ0hfVkVDVE9SSVNBVElPTiUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGtubiUyMCUzRCUyMCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmZpZWxkJTIyJTNBJTIwJTIyZW1iZWRkaW5nLnByZWRpY3RlZF92YWx1ZSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmslMjIlM0ElMjAxMCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnF1ZXJ5X3ZlY3Rvcl9idWlsZGVyJTIyJTNBJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIydGV4dF9lbWJlZGRpbmclMjIlM0ElMjAlN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJtb2RlbF9pZCUyMiUzQSUyMG1vZGVsX2lkJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTI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yQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmZ1bGxwbG90JTIyJTNBJTIwZnVsbHBsb3QlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlN0QlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZXN1bHRzLmFwcGVuZChyZXN1bHQpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwcmVzdWx0cyUwQSUwQWRlZiUyMHByZXR0eV9zZWFyY2gocXVlcnkpJTNBJTBBJTBBJTIwJTIwJTIwJTIwZ2V0X2tub3dsZWRnZSUyMCUzRCUyMHZlY3Rvcl9zZWFyY2gocXVlcnkpJTBBJTBBJTIwJTIwJTIwJTIwc2VhcmNoX3Jlc3VsdCUyMCUzRCUyMCUyMiUyMiUwQSUyMCUyMCUyMCUyMGZvciUyMHJlc3VsdCUyMGluJTIwZ2V0X2tub3dsZWRnZSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHNlYXJjaF9yZXN1bHQlMjAlMkIlM0QlMjBmJTIyVGl0bGUlM0ElMjAlN0JyZXN1bHQuZ2V0KCd0aXRsZSclMkMlMjAnTiUyRkEnKSU3RCUyQyUyMFBsb3QlM0ElMjAlN0JyZXN1bHQuZ2V0KCdmdWxscGxvdCclMkMlMjAnTiUyRkEnKSU3RCU1Q24lMjIlMEElMEElMjAlMjAlMjAlMjByZXR1cm4lMjBzZWFyY2hfcmVzdWx0",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">vector_search</span>(<span class="hljs-params">plot_query</span>):
    <span class="hljs-keyword">if</span> USE_ELASTICSEARCH_VECTORISATION:
        knn = {
            <span class="hljs-string">&quot;field&quot;</span>: <span class="hljs-string">&quot;embedding.predicted_value&quot;</span>,
            <span class="hljs-string">&quot;k&quot;</span>: <span class="hljs-number">10</span>,
            <span class="hljs-string">&quot;query_vector_builder&quot;</span>: {
                <span class="hljs-string">&quot;text_embedding&quot;</span>: {
                    <span class="hljs-string">&quot;model_id&quot;</span>: model_id,
                    <span class="hljs-string">&quot;model_text&quot;</span>: plot_query,
                }
            },
            <span class="hljs-string">&quot;num_candidates&quot;</span>: <span class="hljs-number">150</span>,
        }
    <span class="hljs-keyword">else</span>:
        question_embedding = get_embedding(plot_query)
        knn = {
            <span class="hljs-string">&quot;field&quot;</span>: <span class="hljs-string">&quot;embedding&quot;</span>,
            <span class="hljs-string">&quot;query_vector&quot;</span>: question_embedding,
            <span class="hljs-string">&quot;k&quot;</span>: <span class="hljs-number">10</span>,
            <span class="hljs-string">&quot;num_candidates&quot;</span>: <span class="hljs-number">150</span>,
        }

    response = client.search(index=<span class="hljs-string">&quot;movies&quot;</span>, knn=knn, size=<span class="hljs-number">5</span>)
    results = []
    <span class="hljs-keyword">for</span> hit <span class="hljs-keyword">in</span> response[<span class="hljs-string">&quot;hits&quot;</span>][<span class="hljs-string">&quot;hits&quot;</span>]:
        <span class="hljs-built_in">id</span> = hit[<span class="hljs-string">&quot;_id&quot;</span>]
        score = hit[<span class="hljs-string">&quot;_score&quot;</span>]
        title = hit[<span class="hljs-string">&quot;_source&quot;</span>][<span class="hljs-string">&quot;title&quot;</span>]
        plot = hit[<span class="hljs-string">&quot;_source&quot;</span>][<span class="hljs-string">&quot;plot&quot;</span>]
        fullplot = hit[<span class="hljs-string">&quot;_source&quot;</span>][<span class="hljs-string">&quot;fullplot&quot;</span>]
        result = {
            <span class="hljs-string">&quot;id&quot;</span>: <span class="hljs-built_in">id</span>,
            <span class="hljs-string">&quot;_score&quot;</span>: score,
            <span class="hljs-string">&quot;title&quot;</span>: title,
            <span class="hljs-string">&quot;plot&quot;</span>: plot,
            <span class="hljs-string">&quot;fullplot&quot;</span>: fullplot,
        }
        results.append(result)
    <span class="hljs-keyword">return</span> results

<span class="hljs-keyword">def</span> <span class="hljs-title function_">pretty_search</span>(<span class="hljs-params">query</span>):

    get_knowledge = vector_search(query)

    search_result = <span class="hljs-string">&quot;&quot;</span>
    <span class="hljs-keyword">for</span> result <span class="hljs-keyword">in</span> get_knowledge:
        search_result += <span class="hljs-string">f&quot;Title: <span class="hljs-subst">{result.get(<span class="hljs-string">&#x27;title&#x27;</span>, <span class="hljs-string">&#x27;N/A&#x27;</span>)}</span>, Plot: <span class="hljs-subst">{result.get(<span class="hljs-string">&#x27;fullplot&#x27;</span>, <span class="hljs-string">&#x27;N/A&#x27;</span>)}</span>\\n&quot;</span>

    <span class="hljs-keyword">return</span> search_result`,wrap:!1}}),Tl=new h({props:{title:"步骤 7：处理用户查询并加载 Gemma",local:"步骤-7处理用户查询并加载-gemma",headingTag:"h2"}}),hl=new T({props:{code:"JTIzJTIwQ29uZHVjdCUyMHF1ZXJ5JTIwd2l0aCUyMHJldHJpdmFsJTIwb2YlMjBzb3VyY2VzJTJDJTIwY29tYmluaW5nJTIwcmVzdWx0cyUyMGludG8lMjBzb21ldGhpbmclMjB0aGF0JTBBJTIzJTIwd2UlMjBjYW4lMjBmZWVkJTIwdG8lMjBHZW1tYSUwQWRlZiUyMGNvbWJpbmVkX3F1ZXJ5KHF1ZXJ5KSUzQSUwQSUyMCUyMCUyMCUyMHNvdXJjZV9pbmZvcm1hdGlvbiUyMCUzRCUyMHByZXR0eV9zZWFyY2gocXVlcnkpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwZiUyMlF1ZXJ5JTNBJTIwJTdCcXVlcnklN0QlNUNuQ29udGludWUlMjB0byUyMGFuc3dlciUyMHRoZSUyMHF1ZXJ5JTIwYnklMjB1c2luZyUyMHRoZXNlJTIwU2VhcmNoJTIwUmVzdWx0cyUzQSU1Q24lN0Jzb3VyY2VfaW5mb3JtYXRpb24lN0QuJTIyJTBBJTBBJTBBcXVlcnklMjAlM0QlMjAlMjJXaGF0JTIwaXMlMjB0aGUlMjBiZXN0JTIwcm9tYW50aWMlMjBtb3ZpZSUyMHRvJTIwd2F0Y2glMjBhbmQlMjB3aHklM0YlMjIlMEFjb21iaW5lZF9yZXN1bHRzJTIwJTNEJTIwY29tYmluZWRfcXVlcnkocXVlcnkpJTBBJTBBcHJpbnQoY29tYmluZWRfcmVzdWx0cyk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Conduct query with retrival of sources, combining results into something that</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># we can feed to Gemma</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">combined_query</span>(<span class="hljs-params">query</span>):
<span class="hljs-meta">... </span>    source_information = pretty_search(query)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Query: <span class="hljs-subst">{query}</span>\\nContinue to answer the query by using these Search Results:\\n<span class="hljs-subst">{source_information}</span>.&quot;</span>


<span class="hljs-meta">&gt;&gt;&gt; </span>query = <span class="hljs-string">&quot;What is the best romantic movie to watch and why?&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>combined_results = combined_query(query)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(combined_results)`,wrap:!1}}),wl=new T({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMkMlMjBBdXRvTW9kZWxGb3JDYXVzYWxMTSUwQSUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZSUyRmdlbW1hLTJiLWl0JTIyKSUwQSUyMyUyMENQVSUyMEVuYWJsZWQlMjB1bmNvbW1lbnQlMjBiZWxvdyUyMCVGMCU5RiU5MSU4NyVGMCU5RiU4RiVCRCUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yQ2F1c2FsTE0uZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZSUyRmdlbW1hLTJiLWl0JTIyKSUwQSUyMyUyMEdQVSUyMEVuYWJsZWQlMjB1c2UlMjBiZWxvdyUyMCVGMCU5RiU5MSU4NyVGMCU5RiU4RiVCRCUwQSUyMyUyMG1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yQ2F1c2FsTE0uZnJvbV9wcmV0cmFpbmVkKCUyMmdvb2dsZSUyRmdlbW1hLTJiLWl0JTIyJTJDJTIwZGV2aWNlX21hcCUzRCUyMmF1dG8lMjIp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;google/gemma-2b-it&quot;</span>)
<span class="hljs-comment"># CPU Enabled uncomment below 👇🏽</span>
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;google/gemma-2b-it&quot;</span>)
<span class="hljs-comment"># GPU Enabled use below 👇🏽</span>
<span class="hljs-comment"># model = AutoModelForCausalLM.from_pretrained(&quot;google/gemma-2b-it&quot;, device_map=&quot;auto&quot;)</span>`,wrap:!1}}),dl=new T({props:{code:"ZGVmJTIwcmFnX3F1ZXJ5KHF1ZXJ5KSUzQSUwQSUyMCUyMCUyMCUyMGNvbWJpbmVkX2luZm9ybWF0aW9uJTIwJTNEJTIwY29tYmluZWRfcXVlcnkocXVlcnkpJTBBJTBBJTIwJTIwJTIwJTIwJTIzJTIwTW92aW5nJTIwdGVuc29ycyUyMHRvJTIwR1BVJTBBJTIwJTIwJTIwJTIwaW5wdXRfaWRzJTIwJTNEJTIwdG9rZW5pemVyKGNvbWJpbmVkX2luZm9ybWF0aW9uJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMiklMjAlMjMlMjAudG8oJTIyY3VkYSUyMiklMjAlMjMlMjBBZGQlMjBpZiUyMHVzaW5nJTIwR1BVJTBBJTIwJTIwJTIwJTIwcmVzcG9uc2UlMjAlM0QlMjBtb2RlbC5nZW5lcmF0ZSgqKmlucHV0X2lkcyUyQyUyMG1heF9uZXdfdG9rZW5zJTNENzAwKSUwQSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMHRva2VuaXplci5kZWNvZGUocmVzcG9uc2UlNUIwJTVEJTJDJTIwc2tpcF9zcGVjaWFsX3Rva2VucyUzRFRydWUpJTBBJTBBJTBBcHJpbnQocmFnX3F1ZXJ5KCUyMldoYXQncyUyMGElMjByb21hbnRpYyUyMG1vdmllJTIwdGhhdCUyMEklMjBjYW4lMjB3YXRjaCUyMHdpdGglMjBteSUyMHdpZmUlM0YlMjIpKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">rag_query</span>(<span class="hljs-params">query</span>):
<span class="hljs-meta">... </span>    combined_information = combined_query(query)

<span class="hljs-meta">... </span>    <span class="hljs-comment"># Moving tensors to GPU</span>
<span class="hljs-meta">... </span>    input_ids = tokenizer(combined_information, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>) <span class="hljs-comment"># .to(&quot;cuda&quot;) # Add if using GPU</span>
<span class="hljs-meta">... </span>    response = model.generate(**input_ids, max_new_tokens=<span class="hljs-number">700</span>)

<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> tokenizer.decode(response[<span class="hljs-number">0</span>], skip_special_tokens=<span class="hljs-literal">True</span>)


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(rag_query(<span class="hljs-string">&quot;What&#x27;s a romantic movie that I can watch with my wife?&quot;</span>))`,wrap:!1}}),Il=new h({props:{title:"致谢",local:"致谢",headingTag:"h2"}}),gl=new Va({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/zh-CN/rag_with_hugging_face_gemma_elasticsearch.md"}}),{c(){m=M("meta"),Bl=e(),fl=M("p"),Zl=e(),j(r.$$.fragment),Vl=e(),j(w.$$.fragment),Rl=e(),j(u.$$.fragment),Gl=e(),d=M("p"),d.innerHTML=vs,kl=e(),C=M("p"),C.textContent=$s,Sl=e(),I=M("p"),I.innerHTML=Fs,Ql=e(),A=M("blockquote"),A.innerHTML=xs,Wl=e(),j(b.$$.fragment),Nl=e(),j(g.$$.fragment),_l=e(),j(f.$$.fragment),ql=e(),j(E.$$.fragment),Xl=e(),B=M("p"),B.textContent=Hs,vl=e(),j(Z.$$.fragment),$l=e(),j(V.$$.fragment),Fl=e(),R=M("p"),R.innerHTML=Ys,xl=e(),G=M("p"),G.innerHTML=zs,Hl=e(),k=M("p"),k.innerHTML=Ls,Yl=e(),j(S.$$.fragment),zl=e(),Q=M("p"),Q.textContent=Ds,Ll=e(),j(W.$$.fragment),Dl=e(),j(N.$$.fragment),Pl=e(),_=M("p"),_.innerHTML=Ps,Ol=e(),j(q.$$.fragment),Kl=e(),X=M("p"),X.textContent=Os,ls=e(),v=M("ol"),v.innerHTML=Ks,ss=e(),j($.$$.fragment),as=e(),j(F.$$.fragment),ts=e(),j(x.$$.fragment),es=e(),H=M("p"),H.textContent=la,ns=e(),Y=M("p"),Y.innerHTML=sa,Ms=e(),z=M("p"),z.innerHTML=aa,ps=e(),L=M("p"),L.innerHTML=ta,is=e(),D=M("p"),D.innerHTML=ea,js=e(),j(P.$$.fragment),ys=e(),j(O.$$.fragment),Us=e(),K=M("p"),K.innerHTML=na,os=e(),j(ll.$$.fragment),cs=e(),sl=M("p"),sl.innerHTML=Ma,Js=e(),j(al.$$.fragment),Ts=e(),j(tl.$$.fragment),hs=e(),el=M("p"),el.textContent=pa,ms=e(),nl=M("p"),nl.innerHTML=ia,rs=e(),j(Ml.$$.fragment),ws=e(),pl=M("pre"),pl.textContent=ja,us=e(),il=M("p"),il.innerHTML=ya,ds=e(),j(jl.$$.fragment),Cs=e(),yl=M("pre"),yl.textContent=Ua,Is=e(),j(Ul.$$.fragment),As=e(),ol=M("p"),ol.textContent=oa,bs=e(),cl=M("p"),cl.innerHTML=ca,gs=e(),j(Jl.$$.fragment),fs=e(),j(Tl.$$.fragment),Es=e(),j(hl.$$.fragment),Bs=e(),ml=M("pre"),ml.textContent=Ja,Zs=e(),rl=M("p"),rl.innerHTML=Ta,Vs=e(),j(wl.$$.fragment),Rs=e(),ul=M("p"),ul.textContent=ha,Gs=e(),j(dl.$$.fragment),ks=e(),Cl=M("pre"),Cl.textContent=ma,Ss=e(),j(Il.$$.fragment),Qs=e(),Al=M("p"),Al.textContent=ra,Ws=e(),bl=M("ul"),bl.innerHTML=wa,Ns=e(),j(gl.$$.fragment),_s=e(),El=M("p"),this.h()},l(l){const s=fa("svelte-u9bgzb",document.head);m=p(s,"META",{name:!0,content:!0}),s.forEach(a),Bl=n(l),fl=p(l,"P",{}),ua(fl).forEach(a),Zl=n(l),y(r.$$.fragment,l),Vl=n(l),y(w.$$.fragment,l),Rl=n(l),y(u.$$.fragment,l),Gl=n(l),d=p(l,"P",{"data-svelte-h":!0}),i(d)!=="svelte-pmaiec"&&(d.innerHTML=vs),kl=n(l),C=p(l,"P",{"data-svelte-h":!0}),i(C)!=="svelte-19067t3"&&(C.textContent=$s),Sl=n(l),I=p(l,"P",{"data-svelte-h":!0}),i(I)!=="svelte-xudwm6"&&(I.innerHTML=Fs),Ql=n(l),A=p(l,"BLOCKQUOTE",{"data-svelte-h":!0}),i(A)!=="svelte-lfr17y"&&(A.innerHTML=xs),Wl=n(l),y(b.$$.fragment,l),Nl=n(l),y(g.$$.fragment,l),_l=n(l),y(f.$$.fragment,l),ql=n(l),y(E.$$.fragment,l),Xl=n(l),B=p(l,"P",{"data-svelte-h":!0}),i(B)!=="svelte-1d4oa1p"&&(B.textContent=Hs),vl=n(l),y(Z.$$.fragment,l),$l=n(l),y(V.$$.fragment,l),Fl=n(l),R=p(l,"P",{"data-svelte-h":!0}),i(R)!=="svelte-vafilm"&&(R.innerHTML=Ys),xl=n(l),G=p(l,"P",{"data-svelte-h":!0}),i(G)!=="svelte-qhaml4"&&(G.innerHTML=zs),Hl=n(l),k=p(l,"P",{"data-svelte-h":!0}),i(k)!=="svelte-1ixe20f"&&(k.innerHTML=Ls),Yl=n(l),y(S.$$.fragment,l),zl=n(l),Q=p(l,"P",{"data-svelte-h":!0}),i(Q)!=="svelte-4yqdmt"&&(Q.textContent=Ds),Ll=n(l),y(W.$$.fragment,l),Dl=n(l),y(N.$$.fragment,l),Pl=n(l),_=p(l,"P",{"data-svelte-h":!0}),i(_)!=="svelte-jw93wo"&&(_.innerHTML=Ps),Ol=n(l),y(q.$$.fragment,l),Kl=n(l),X=p(l,"P",{"data-svelte-h":!0}),i(X)!=="svelte-1f7jf4d"&&(X.textContent=Os),ls=n(l),v=p(l,"OL",{"data-svelte-h":!0}),i(v)!=="svelte-1jmgevs"&&(v.innerHTML=Ks),ss=n(l),y($.$$.fragment,l),as=n(l),y(F.$$.fragment,l),ts=n(l),y(x.$$.fragment,l),es=n(l),H=p(l,"P",{"data-svelte-h":!0}),i(H)!=="svelte-1c1n86z"&&(H.textContent=la),ns=n(l),Y=p(l,"P",{"data-svelte-h":!0}),i(Y)!=="svelte-1d13qni"&&(Y.innerHTML=sa),Ms=n(l),z=p(l,"P",{"data-svelte-h":!0}),i(z)!=="svelte-1ybkmte"&&(z.innerHTML=aa),ps=n(l),L=p(l,"P",{"data-svelte-h":!0}),i(L)!=="svelte-1kss47q"&&(L.innerHTML=ta),is=n(l),D=p(l,"P",{"data-svelte-h":!0}),i(D)!=="svelte-kf6a8q"&&(D.innerHTML=ea),js=n(l),y(P.$$.fragment,l),ys=n(l),y(O.$$.fragment,l),Us=n(l),K=p(l,"P",{"data-svelte-h":!0}),i(K)!=="svelte-ke677j"&&(K.innerHTML=na),os=n(l),y(ll.$$.fragment,l),cs=n(l),sl=p(l,"P",{"data-svelte-h":!0}),i(sl)!=="svelte-1qjun2k"&&(sl.innerHTML=Ma),Js=n(l),y(al.$$.fragment,l),Ts=n(l),y(tl.$$.fragment,l),hs=n(l),el=p(l,"P",{"data-svelte-h":!0}),i(el)!=="svelte-16segge"&&(el.textContent=pa),ms=n(l),nl=p(l,"P",{"data-svelte-h":!0}),i(nl)!=="svelte-1usvj88"&&(nl.innerHTML=ia),rs=n(l),y(Ml.$$.fragment,l),ws=n(l),pl=p(l,"PRE",{"data-svelte-h":!0}),i(pl)!=="svelte-dtnw1a"&&(pl.textContent=ja),us=n(l),il=p(l,"P",{"data-svelte-h":!0}),i(il)!=="svelte-1e1t9oi"&&(il.innerHTML=ya),ds=n(l),y(jl.$$.fragment,l),Cs=n(l),yl=p(l,"PRE",{"data-svelte-h":!0}),i(yl)!=="svelte-rnckh4"&&(yl.textContent=Ua),Is=n(l),y(Ul.$$.fragment,l),As=n(l),ol=p(l,"P",{"data-svelte-h":!0}),i(ol)!=="svelte-hr2v6x"&&(ol.textContent=oa),bs=n(l),cl=p(l,"P",{"data-svelte-h":!0}),i(cl)!=="svelte-sw9t8h"&&(cl.innerHTML=ca),gs=n(l),y(Jl.$$.fragment,l),fs=n(l),y(Tl.$$.fragment,l),Es=n(l),y(hl.$$.fragment,l),Bs=n(l),ml=p(l,"PRE",{"data-svelte-h":!0}),i(ml)!=="svelte-rtrdii"&&(ml.textContent=Ja),Zs=n(l),rl=p(l,"P",{"data-svelte-h":!0}),i(rl)!=="svelte-15b150i"&&(rl.innerHTML=Ta),Vs=n(l),y(wl.$$.fragment,l),Rs=n(l),ul=p(l,"P",{"data-svelte-h":!0}),i(ul)!=="svelte-y4njus"&&(ul.textContent=ha),Gs=n(l),y(dl.$$.fragment,l),ks=n(l),Cl=p(l,"PRE",{"data-svelte-h":!0}),i(Cl)!=="svelte-1d2dt07"&&(Cl.textContent=ma),Ss=n(l),y(Il.$$.fragment,l),Qs=n(l),Al=p(l,"P",{"data-svelte-h":!0}),i(Al)!=="svelte-1xg2uqr"&&(Al.textContent=ra),Ws=n(l),bl=p(l,"UL",{"data-svelte-h":!0}),i(bl)!=="svelte-k2eyjn"&&(bl.innerHTML=wa),Ns=n(l),y(gl.$$.fragment,l),_s=n(l),El=p(l,"P",{}),ua(El).forEach(a),this.h()},h(){da(m,"name","hf:doc:metadata"),da(m,"content",Ga)},m(l,s){Ea(document.head,m),t(l,Bl,s),t(l,fl,s),t(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,s),t(l,ks,s),t(l,Cl,s),t(l,Ss,s),U(Il,l,s),t(l,Qs,s),t(l,Al,s),t(l,Ws,s),t(l,bl,s),t(l,Ns,s),U(gl,l,s),t(l,_s,s),t(l,El,s),qs=!0},p:Ia,i(l){qs||(o(r.$$.fragment,l),o(w.$$.fragment,l),o(u.$$.fragment,l),o(b.$$.fragment,l),o(g.$$.fragment,l),o(f.$$.fragment,l),o(E.$$.fragment,l),o(Z.$$.fragment,l),o(V.$$.fragment,l),o(S.$$.fragment,l),o(W.$$.fragment,l),o(N.$$.fragment,l),o(q.$$.fragment,l),o($.$$.fragment,l),o(F.$$.fragment,l),o(x.$$.fragment,l),o(P.$$.fragment,l),o(O.$$.fragment,l),o(ll.$$.fragment,l),o(al.$$.fragment,l),o(tl.$$.fragment,l),o(Ml.$$.fragment,l),o(jl.$$.fragment,l),o(Ul.$$.fragment,l),o(Jl.$$.fragment,l),o(Tl.$$.fragment,l),o(hl.$$.fragment,l),o(wl.$$.fragment,l),o(dl.$$.fragment,l),o(Il.$$.fragment,l),o(gl.$$.fragment,l),qs=!0)},o(l){c(r.$$.fragment,l),c(w.$$.fragment,l),c(u.$$.fragment,l),c(b.$$.fragment,l),c(g.$$.fragment,l),c(f.$$.fragment,l),c(E.$$.fragment,l),c(Z.$$.fragment,l),c(V.$$.fragment,l),c(S.$$.fragment,l),c(W.$$.fragment,l),c(N.$$.fragment,l),c(q.$$.fragment,l),c($.$$.fragment,l),c(F.$$.fragment,l),c(x.$$.fragment,l),c(P.$$.fragment,l),c(O.$$.fragment,l),c(ll.$$.fragment,l),c(al.$$.fragment,l),c(tl.$$.fragment,l),c(Ml.$$.fragment,l),c(jl.$$.fragment,l),c(Ul.$$.fragment,l),c(Jl.$$.fragment,l),c(Tl.$$.fragment,l),c(hl.$$.fragment,l),c(wl.$$.fragment,l),c(dl.$$.fragment,l),c(Il.$$.fragment,l),c(gl.$$.fragment,l),qs=!1},d(l){l&&(a(Bl),a(fl),a(Zl),a(Vl),a(Rl),a(Gl),a(d),a(kl),a(C),a(Sl),a(I),a(Ql),a(A),a(Wl),a(Nl),a(_l),a(ql),a(Xl),a(B),a(vl),a($l),a(Fl),a(R),a(xl),a(G),a(Hl),a(k),a(Yl),a(zl),a(Q),a(Ll),a(Dl),a(Pl),a(_),a(Ol),a(Kl),a(X),a(ls),a(v),a(ss),a(as),a(ts),a(es),a(H),a(ns),a(Y),a(Ms),a(z),a(ps),a(L),a(is),a(D),a(js),a(ys),a(Us),a(K),a(os),a(cs),a(sl),a(Js),a(Ts),a(hs),a(el),a(ms),a(nl),a(rs),a(ws),a(pl),a(us),a(il),a(ds),a(Cs),a(yl),a(Is),a(As),a(ol),a(bs),a(cl),a(gs),a(fs),a(Es),a(Bs),a(ml),a(Zs),a(rl),a(Vs),a(Rs),a(ul),a(Gs),a(ks),a(Cl),a(Ss),a(Qs),a(Al),a(Ws),a(bl),a(Ns),a(_s),a(El)),a(m),J(r,l),J(w,l),J(u,l),J(b,l),J(g,l),J(f,l),J(E,l),J(Z,l),J(V,l),J(S,l),J(W,l),J(N,l),J(q,l),J($,l),J(F,l),J(x,l),J(P,l),J(O,l),J(ll,l),J(al,l),J(tl,l),J(Ml,l),J(jl,l),J(Ul,l),J(Jl,l),J(Tl,l),J(hl,l),J(wl,l),J(dl,l),J(Il,l),J(gl,l)}}}const Ga='{"title":"构建一个基于 Gemma、Elasticsearch 和 Hugging Face 模型的 RAG 系统","local":"构建一个基于-gemmaelasticsearch-和-hugging-face-模型的-rag-系统","sections":[{"title":"步骤 1：安装相关库","local":"步骤-1安装相关库","sections":[],"depth":2},{"title":"步骤 2：设置","local":"步骤-2设置","sections":[{"title":"Hugging Face","local":"hugging-face","sections":[{"title":"Elasticsearch 部署","local":"elasticsearch-部署","sections":[],"depth":4}],"depth":3}],"depth":2},{"title":"步骤 3：数据获取和准备","local":"步骤-3数据获取和准备","sections":[],"depth":2},{"title":"步骤 4：使用向量化数据加载 Elasticsearch","local":"步骤-4使用向量化数据加载-elasticsearch","sections":[{"title":"选择数据和查询向量化选项","local":"选择数据和查询向量化选项","sections":[],"depth":3},{"title":"如有必要，将 Hugging Face 模型加载到 Elasticsearch 中","local":"如有必要将-hugging-face-模型加载到-elasticsearch-中","sections":[],"depth":3}],"depth":2},{"title":"步骤 5：创建一个带有向量搜索映射的搜索索引。","local":"步骤-5创建一个带有向量搜索映射的搜索索引","sections":[],"depth":2},{"title":"步骤 6：对用户查询执行向量搜索","local":"步骤-6对用户查询执行向量搜索","sections":[],"depth":2},{"title":"步骤 7：处理用户查询并加载 Gemma","local":"步骤-7处理用户查询并加载-gemma","sections":[],"depth":2},{"title":"致谢","local":"致谢","sections":[],"depth":2}],"depth":1}';function ka(Xs){return Aa(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class qa extends ba{constructor(m){super(),ga(this,m,ka,Ra,Ca,{})}}export{qa as component};
