import{s as ua,n as da,o as Ca}from"../chunks/scheduler.39a43cde.js";import{S as Ia,i as Aa,g as M,s as e,r as j,A as ba,h as p,f as a,c as n,j as ra,u as y,x as i,k as wa,y as ga,a as t,v as o,d as c,t as U,w as J}from"../chunks/index.23ec2204.js";import{D as fa,C as T}from"../chunks/DocNotebookDropdown.7c3158f8.js";import{H as h,E as Ea}from"../chunks/index.724daca9.js";function Ba(_s){let m,El,gl,Bl,r,Zl,w,Vl,u,qs='作者: <a href="https://huggingface.co/lloydmeta" rel="nofollow">lloydmeta</a>',Rl,d,Xs="这个 notebook 将引导你构建一个由 Elasticsearch（ES）和 Hugging Face 模型支持的检索增强生成（RAG）系统，允许你在 ES 向量化（你的 ES 集群在摄取和查询时为你向量化）与自向量化（你在将数据发送到 ES 之前对所有数据进行向量化）之间切换。",Gl,C,vs="你的用例应该使用哪种方式？<em>这取决于</em> 🤷‍♂️。ES 向量化意味着你的客户端不需要实现这一点，所以这里默认使用；但是，如果你没有机器学习节点，或者你自己的嵌入设置更好/更快，可以在下面的“选择数据和查询向量化选项”部分将 <code>USE_ELASTICSEARCH_VECTORISATION</code> 设置为 <code>False</code>！",kl,I,Fs=`<p>[!提示]
这个 notebook 已经使用 ES 8.13.x 和 8.14.x 进行了测试。</p>`,Sl,A,Ql,b,Wl,g,Nl,f,_l,E,$s="这允许你通过 Hugging Face 进行身份验证，以便下载模型和数据集。",ql,B,Xl,Z,vl,V,xs='让我们确保你可以访问你的 Elasticsearch 部署。如果你还没有，可以在 <a href="https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-a-cloud-deployment" rel="nofollow">Elastic Cloud</a>上创建一个。',Fl,R,Hs="确保你已经将 <code>CLOUD_ID</code> 和 <code>ELASTIC_DEPL_API_KEY</code> 保存为 Colab secrets。",$l,G,Ys='<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"/>',xl,k,Hl,S,zs="设置客户端并确保凭据有效。",Yl,Q,zl,W,Ll,N,Ls='本教程中使用的数据来源于 Hugging Face 数据集，特别是来自 <a href="https://huggingface.co/datasets/MongoDB/embedded_movies" rel="nofollow">MongoDB/embedded_movies 数据集</a>。',Dl,_,Pl,q,Ds="以下代码片段中的操作专注于强制数据完整性和质量。",Ol,X,Ps="<li>第一个过程确保每个数据点的 <code>fullplot</code> 属性不为空，因为这是我们在嵌入过程中主要使用的数据。</li> <li>第二步还确保我们移除所有数据点的 <code>plot_embedding</code> 属性，因为这将被一个使用不同的嵌入模型 <code>gte-large</code> 创建的新嵌入所替换。</li>",Kl,v,ls,F,ss,$,as,x,Os="在这里，你需要做出一个决定：你希望 Elasticsearch 对你的数据和查询进行向量化，还是希望你自己来做？",ts,H,Ks="将 <code>USE_ELASTICSEARCH_VECTORISATION</code> 设置为 <code>True</code> 将使本 notebook 的其余部分为你数据和查询设置并使用 ES 托管的向量化，但<strong>请注意</strong>，这要求你的 ES 部署至少有 1 个 ML 节点（我建议在你的云部署上设置自动缩放为 true，以防你选择的模型过大）。",es,Y,la="如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 是 <code>False</code>，这个 notebook 将为你数据和查询向量化设置并使用提供的模型“本地”。",ns,z,sa='在这里，我选择了 <a href="https://huggingface.co/thenlper/gte-small" rel="nofollow">thenlper/gte-small</a> 模型，没有其他原因，只是因为它在另一个指南中使用过，而且对我来说效果足够好。如果你愿意，请随时尝试其他模型——唯一重要的是你根据模型更新 <code>EMBEDDING_DIMENSIONS</code>。',Ms,L,aa="<strong>注意</strong>：如果你更改这些值，你可能需要从这一步重新运行 notebook。",ps,D,is,P,js,O,ta='如果 <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 在后续步骤中对你的查询和数据进行向量化。',ys,K,os,ll,ea="这一步添加了在本地为文本创建嵌入的函数，并通过嵌入丰富了数据集，这样数据就可以作为向量被摄取到 Elasticsearch 中。如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 为 True，则不会运行。",cs,sl,Us,al,Js,tl,na="在这一点上，我们将在 Elasticsearch 中创建一个索引，并设置正确的索引映射来处理向量搜索。",Ts,el,Ma='请点击这里了解更多关于 <a href="https://www.elastic.co/what-is/vector-search" rel="nofollow">Elasticsearch 向量搜索能力</a>的信息。',hs,nl,ms,Ml,pa=`Creating index movies
`,rs,pl,ia="将数据摄取到 Elasticsearch 中最好是批量进行。幸运的是，<code>helpers</code> 提供了一个简单的方法来执行这个操作。",ws,il,us,jl,ja=`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!
`,ds,yl,Cs,ol,ya="以下步骤实现了一个函数，该函数返回一个向量搜索结果。",Is,cl,oa="如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 为 true，文本查询将直接发送到 ES，在那里将首先使用上传的模型对其进行向量化，然后执行向量搜索。如果 <code>USE_ELASTICSEARCH_VECTORISATION</code> 为 false，那么我们在发送查询之前先在本地进行向量化，然后发送查询的向量化形式。",As,Ul,bs,Jl,gs,Tl,fs,hl,ca=`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.
.
`,Es,ml,Ua='加载我们的 LLM (这里我们用 <a href="https://huggingface.co/google/gemma-2b-it" rel="nofollow">google/gemma-2b-lt</a>)',Bs,rl,Zs,wl,Ja="定义一个方法，从 ES 中的向量搜索获取格式化的结果，然后将这些结果传递给 LLM（大型语言模型）以获取我们的结果。",Vs,ul,Rs,dl,Ta=`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.
`,Gs,Cl,ks,Il,ha="这个 notebook 改编自",Ss,Al,ma='<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>',Qs,bl,Ws,fl,Ns;return r=new fa({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 h({props:{title:"构建一个基于 Gemma、Elasticsearch 和 Hugging Face 模型的 RAG 系统",local:"构建一个基于-gemmaelasticsearch-和-hugging-face-模型的-rag-系统",headingTag:"h1"}}),A=new h({props:{title:"步骤 1：安装相关库",local:"步骤-1安装相关库",headingTag:"h2"}}),b=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}}),g=new h({props:{title:"步骤 2：设置",local:"步骤-2设置",headingTag:"h2"}}),f=new h({props:{title:"Hugging Face",local:"hugging-face",headingTag:"h3"}}),B=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}}),Z=new h({props:{title:"Elasticsearch 部署",local:"elasticsearch-部署",headingTag:"h4"}}),k=new T({props:{code:"ZnJvbSUyMGdvb2dsZS5jb2xhYiUyMGltcG9ydCUyMHVzZXJkYXRhJTBBJTBBJTIzJTIwaHR0cHMlM0ElMkYlMkZ3d3cuZWxhc3RpYy5jbyUyRnNlYXJjaC1sYWJzJTJGdHV0b3JpYWxzJTJGaW5zdGFsbC1lbGFzdGljc2VhcmNoJTJGZWxhc3RpYy1jbG91ZCUyM2ZpbmRpbmcteW91ci1jbG91ZC1pZCUwQUNMT1VEX0lEJTIwJTNEJTIwdXNlcmRhdGEuZ2V0KCUyMkNMT1VEX0lEJTIyKSUyMCUyMCUyMyUyMG9yJTIwJTIyJTNDWU9VUiUyMENMT1VEX0lEJTNFJTIyJTBBJTBBJTIzJTIwaHR0cHMlM0ElMkYlMkZ3d3cuZWxhc3RpYy5jbyUyRnNlYXJjaC1sYWJzJTJGdHV0b3JpYWxzJTJGaW5zdGFsbC1lbGFzdGljc2VhcmNoJTJGZWxhc3RpYy1jbG91ZCUyM2NyZWF0aW5nLWFuLWFwaS1rZXklMEFFTEFTVElDX0FQSV9LRVklMjAlM0QlMjB1c2VyZGF0YS5nZXQoJTIyRUxBU1RJQ19ERVBMX0FQSV9LRVklMjIpJTIwJTIwJTIzJTIwb3IlMjAlMjIlM0NZT1VSJTIwQVBJJTIwS0VZJTNFJTIy",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}}),Q=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}}),W=new h({props:{title:"步骤 3：数据获取和准备",local:"步骤-3数据获取和准备",headingTag:"h2"}}),_=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}}),v=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"}}),$=new h({props:{title:"选择数据和查询向量化选项",local:"选择数据和查询向量化选项",headingTag:"h3"}}),D=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}}),P=new h({props:{title:"如有必要，将 Hugging Face 模型加载到 Elasticsearch 中",local:"如有必要将-hugging-face-模型加载到-elasticsearch-中",headingTag:"h3"}}),K=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}}),sl=new T({props:{code:"ZnJvbSUyMHNlbnRlbmNlX3RyYW5zZm9ybWVycyUyMGltcG9ydCUyMFNlbnRlbmNlVHJhbnNmb3JtZXIlMEElMEFpZiUyMG5vdCUyMFVTRV9FTEFTVElDU0VBUkNIX1ZFQ1RPUklTQVRJT04lM0ElMEElMjAlMjAlMjAlMjBlbWJlZGRpbmdfbW9kZWwlMjAlM0QlMjBTZW50ZW5jZVRyYW5zZm9ybWVyKEVNQkVERElOR19NT0RFTF9JRCklMEElMEElMEFkZWYlMjBnZXRfZW1iZWRkaW5nKHRleHQlM0ElMjBzdHIpJTIwLSUzRSUyMGxpc3QlNUJmbG9hdCU1RCUzQSUwQSUyMCUyMCUyMCUyMGlmJTIwVVNFX0VMQVNUSUNTRUFSQ0hfVkVDVE9SSVNBVElPTiUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJhaX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AlMjAlMjAlMjAlMjBmdWxsX3Bsb3RzJTIwJTNEJTIweCU1QiUyMmZ1bGxwbG90JTIyJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTdCJTIyZW1iZWRkaW5nJTIyJTNBJTIwJTVCZ2V0X2VtYmVkZGluZyhmdWxsX3Bsb3QpJTIwZm9yJTIwZnVsbF9wbG90JTIwaW4lMjBmdWxsX3Bsb3RzJTVEJTdEJTBBJTBBJTBBaWYlMjBub3QlMjBVU0VfRUxBU1RJQ1NFQVJDSF9WRUNUT1JJU0FUSU9OJTNBJTBBJTIwJTIwJTIwJTIwZGF0YXNldCUyMCUzRCUyMGRhdGFzZXQubWFwKGFkZF9mdWxscGxvdF9lbWJlZGRpbmclMkMlMjBiYXRjaGVkJTNEVHJ1ZSklMEElMjAlMjAlMjAlMjBkYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQ=",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}}),al=new h({props:{title:"步骤 5：创建一个带有向量搜索映射的搜索索引。",local:"步骤-5创建一个带有向量搜索映射的搜索索引",headingTag:"h2"}}),nl=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}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ticsearch.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}}),yl=new h({props:{title:"步骤 6：对用户查询执行向量搜索",local:"步骤-6对用户查询执行向量搜索",headingTag:"h2"}}),Ul=new T({props:{code:"ZGVmJTIwdmVjdG9yX3NlYXJjaChwbG90X3F1ZXJ5KSUzQSUw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TIwJTIwJTIwJTIwZ2V0X2tub3dsZWRnZSUyMCUzRCUyMHZlY3Rvcl9zZWFyY2gocXVlcnkpJTBBJTBBJTIwJTIwJTIwJTIwc2VhcmNoX3Jlc3VsdCUyMCUzRCUyMCUyMiUyMiUwQSUyMCUyMCUyMCUyMGZvciUyMHJlc3VsdCUyMGluJTIwZ2V0X2tub3dsZWRnZSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHNlYXJjaF9yZXN1bHQlMjAlMkIlM0QlMjBmJTIyVGl0bGUlM0ElMjAlN0JyZXN1bHQuZ2V0KCd0aXRsZSclMkMlMjAnTiUyRkEnKSU3RCUyQyUyMFBsb3QlM0ElMjAlN0JyZXN1bHQuZ2V0KCdmdWxscGxvdCclMkMlMjAnTiUyRkEnKSU3RCU1Q24lMjIlMEElMEElMjAlMjAlMjAlMjByZXR1cm4lMjBzZWFyY2hfcmVzdWx0",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}}),Jl=new h({props:{title:"步骤 7：处理用户查询并加载 Gemma",local:"步骤-7处理用户查询并加载-gemma",headingTag:"h2"}}),Tl=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}}),rl=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}}),ul=new T({props:{code:"ZGVmJTIwcmFnX3F1ZXJ5KHF1ZXJ5KSUzQSUwQSUyMCUyMCUyMCUyMGNvbWJpbmVkX2luZm9ybWF0aW9uJTIwJTNEJTIwY29tYmluZWRfcXVlcnkocXVlcnkpJTBBJTBBJTIwJTIwJTIwJTIwJTIzJTIwTW92aW5nJTIwdGVuc29ycyUyMHRvJTIwR1BVJTBBJTIwJTIwJTIwJTIwaW5wdXRfaWRzJTIwJTNEJTIwdG9rZW5pemVyKGNvbWJpbmVkX2luZm9ybWF0aW9uJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMiklMjAlMjAlMjMlMjAudG8oJTIyY3VkYSUyMiklMjAlMjMlMjBBZGQlMjBpZiUyMHVzaW5nJTIwR1BVJTBBJTIwJTIwJTIwJTIwcmVzcG9uc2UlMjAlM0QlMjBtb2RlbC5nZW5lcmF0ZSgqKmlucHV0X2lkcyUyQyUyMG1heF9uZXdfdG9rZW5zJTNENzAwKSUwQSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMHRva2VuaXplci5kZWNvZGUocmVzcG9uc2UlNUIwJTVEJTJDJTIwc2tpcF9zcGVjaWFsX3Rva2VucyUzRFRydWUpJTBBJTBBJTBBcHJpbnQocmFnX3F1ZXJ5KCUyMldoYXQncyUyMGElMjByb21hbnRpYyUyMG1vdmllJTIwdGhhdCUyMEklMjBjYW4lMjB3YXRjaCUyMHdpdGglMjBteSUyMHdpZmUlM0YlMjIpKQ==",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}}),Cl=new h({props:{title:"致谢",local:"致谢",headingTag:"h2"}}),bl=new Ea({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/zh-CN/rag_with_hugging_face_gemma_elasticsearch.md"}}),{c(){m=M("meta"),El=e(),gl=M("p"),Bl=e(),j(r.$$.fragment),Zl=e(),j(w.$$.fragment),Vl=e(),u=M("p"),u.innerHTML=qs,Rl=e(),d=M("p"),d.textContent=Xs,Gl=e(),C=M("p"),C.innerHTML=vs,kl=e(),I=M("blockquote"),I.innerHTML=Fs,Sl=e(),j(A.$$.fragment),Ql=e(),j(b.$$.fragment),Wl=e(),j(g.$$.fragment),Nl=e(),j(f.$$.fragment),_l=e(),E=M("p"),E.textContent=$s,ql=e(),j(B.$$.fragment),Xl=e(),j(Z.$$.fragment),vl=e(),V=M("p"),V.innerHTML=xs,Fl=e(),R=M("p"),R.innerHTML=Hs,$l=e(),G=M("p"),G.innerHTML=Ys,xl=e(),j(k.$$.fragment),Hl=e(),S=M("p"),S.textContent=zs,Yl=e(),j(Q.$$.fragment),zl=e(),j(W.$$.fragment),Ll=e(),N=M("p"),N.innerHTML=Ls,Dl=e(),j(_.$$.fragment),Pl=e(),q=M("p"),q.textContent=Ds,Ol=e(),X=M("ol"),X.innerHTML=Ps,Kl=e(),j(v.$$.fragment),ls=e(),j(F.$$.fragment),ss=e(),j($.$$.fragment),as=e(),x=M("p"),x.textContent=Os,ts=e(),H=M("p"),H.innerHTML=Ks,es=e(),Y=M("p"),Y.innerHTML=la,ns=e(),z=M("p"),z.innerHTML=sa,Ms=e(),L=M("p"),L.innerHTML=aa,ps=e(),j(D.$$.fragment),is=e(),j(P.$$.fragment),js=e(),O=M("p"),O.innerHTML=ta,ys=e(),j(K.$$.fragment),os=e(),ll=M("p"),ll.innerHTML=ea,cs=e(),j(sl.$$.fragment),Us=e(),j(al.$$.fragment),Js=e(),tl=M("p"),tl.textContent=na,Ts=e(),el=M("p"),el.innerHTML=Ma,hs=e(),j(nl.$$.fragment),ms=e(),Ml=M("pre"),Ml.textContent=pa,rs=e(),pl=M("p"),pl.innerHTML=ia,ws=e(),j(il.$$.fragment),us=e(),jl=M("pre"),jl.textContent=ja,ds=e(),j(yl.$$.fragment),Cs=e(),ol=M("p"),ol.textContent=ya,Is=e(),cl=M("p"),cl.innerHTML=oa,As=e(),j(Ul.$$.fragment),bs=e(),j(Jl.$$.fragment),gs=e(),j(Tl.$$.fragment),fs=e(),hl=M("pre"),hl.textContent=ca,Es=e(),ml=M("p"),ml.innerHTML=Ua,Bs=e(),j(rl.$$.fragment),Zs=e(),wl=M("p"),wl.textContent=Ja,Vs=e(),j(ul.$$.fragment),Rs=e(),dl=M("pre"),dl.textContent=Ta,Gs=e(),j(Cl.$$.fragment),ks=e(),Il=M("p"),Il.textContent=ha,Ss=e(),Al=M("ul"),Al.innerHTML=ma,Qs=e(),j(bl.$$.fragment),Ws=e(),fl=M("p"),this.h()},l(l){const s=ba("svelte-u9bgzb",document.head);m=p(s,"META",{name:!0,content:!0}),s.forEach(a),El=n(l),gl=p(l,"P",{}),ra(gl).forEach(a),Bl=n(l),y(r.$$.fragment,l),Zl=n(l),y(w.$$.fragment,l),Vl=n(l),u=p(l,"P",{"data-svelte-h":!0}),i(u)!=="svelte-pmaiec"&&(u.innerHTML=qs),Rl=n(l),d=p(l,"P",{"data-svelte-h":!0}),i(d)!=="svelte-19067t3"&&(d.textContent=Xs),Gl=n(l),C=p(l,"P",{"data-svelte-h":!0}),i(C)!=="svelte-xudwm6"&&(C.innerHTML=vs),kl=n(l),I=p(l,"BLOCKQUOTE",{"data-svelte-h":!0}),i(I)!=="svelte-lfr17y"&&(I.innerHTML=Fs),Sl=n(l),y(A.$$.fragment,l),Ql=n(l),y(b.$$.fragment,l),Wl=n(l),y(g.$$.fragment,l),Nl=n(l),y(f.$$.fragment,l),_l=n(l),E=p(l,"P",{"data-svelte-h":!0}),i(E)!=="svelte-1d4oa1p"&&(E.textContent=$s),ql=n(l),y(B.$$.fragment,l),Xl=n(l),y(Z.$$.fragment,l),vl=n(l),V=p(l,"P",{"data-svelte-h":!0}),i(V)!=="svelte-vafilm"&&(V.innerHTML=xs),Fl=n(l),R=p(l,"P",{"data-svelte-h":!0}),i(R)!=="svelte-qhaml4"&&(R.innerHTML=Hs),$l=n(l),G=p(l,"P",{"data-svelte-h":!0}),i(G)!=="svelte-1ixe20f"&&(G.innerHTML=Ys),xl=n(l),y(k.$$.fragment,l),Hl=n(l),S=p(l,"P",{"data-svelte-h":!0}),i(S)!=="svelte-4yqdmt"&&(S.textContent=zs),Yl=n(l),y(Q.$$.fragment,l),zl=n(l),y(W.$$.fragment,l),Ll=n(l),N=p(l,"P",{"data-svelte-h":!0}),i(N)!=="svelte-jw93wo"&&(N.innerHTML=Ls),Dl=n(l),y(_.$$.fragment,l),Pl=n(l),q=p(l,"P",{"data-svelte-h":!0}),i(q)!=="svelte-1f7jf4d"&&(q.textContent=Ds),Ol=n(l),X=p(l,"OL",{"data-svelte-h":!0}),i(X)!=="svelte-1jmgevs"&&(X.innerHTML=Ps),Kl=n(l),y(v.$$.fragment,l),ls=n(l),y(F.$$.fragment,l),ss=n(l),y($.$$.fragment,l),as=n(l),x=p(l,"P",{"data-svelte-h":!0}),i(x)!=="svelte-1c1n86z"&&(x.textContent=Os),ts=n(l),H=p(l,"P",{"data-svelte-h":!0}),i(H)!=="svelte-1d13qni"&&(H.innerHTML=Ks),es=n(l),Y=p(l,"P",{"data-svelte-h":!0}),i(Y)!=="svelte-1ybkmte"&&(Y.innerHTML=la),ns=n(l),z=p(l,"P",{"data-svelte-h":!0}),i(z)!=="svelte-1kss47q"&&(z.innerHTML=sa),Ms=n(l),L=p(l,"P",{"data-svelte-h":!0}),i(L)!=="svelte-kf6a8q"&&(L.innerHTML=aa),ps=n(l),y(D.$$.fragment,l),is=n(l),y(P.$$.fragment,l),js=n(l),O=p(l,"P",{"data-svelte-h":!0}),i(O)!=="svelte-ke677j"&&(O.innerHTML=ta),ys=n(l),y(K.$$.fragment,l),os=n(l),ll=p(l,"P",{"data-svelte-h":!0}),i(ll)!=="svelte-1qjun2k"&&(ll.innerHTML=ea),cs=n(l),y(sl.$$.fragment,l),Us=n(l),y(al.$$.fragment,l),Js=n(l),tl=p(l,"P",{"data-svelte-h":!0}),i(tl)!=="svelte-16segge"&&(tl.textContent=na),Ts=n(l),el=p(l,"P",{"data-svelte-h":!0}),i(el)!=="svelte-1usvj88"&&(el.innerHTML=Ma),hs=n(l),y(nl.$$.fragment,l),ms=n(l),Ml=p(l,"PRE",{"data-svelte-h":!0}),i(Ml)!=="svelte-dtnw1a"&&(Ml.textContent=pa),rs=n(l),pl=p(l,"P",{"data-svelte-h":!0}),i(pl)!=="svelte-1e1t9oi"&&(pl.innerHTML=ia),ws=n(l),y(il.$$.fragment,l),us=n(l),jl=p(l,"PRE",{"data-svelte-h":!0}),i(jl)!=="svelte-rnckh4"&&(jl.textContent=ja),ds=n(l),y(yl.$$.fragment,l),Cs=n(l),ol=p(l,"P",{"data-svelte-h":!0}),i(ol)!=="svelte-hr2v6x"&&(ol.textContent=ya),Is=n(l),cl=p(l,"P",{"data-svelte-h":!0}),i(cl)!=="svelte-sw9t8h"&&(cl.innerHTML=oa),As=n(l),y(Ul.$$.fragment,l),bs=n(l),y(Jl.$$.fragment,l),gs=n(l),y(Tl.$$.fragment,l),fs=n(l),hl=p(l,"PRE",{"data-svelte-h":!0}),i(hl)!=="svelte-rtrdii"&&(hl.textContent=ca),Es=n(l),ml=p(l,"P",{"data-svelte-h":!0}),i(ml)!=="svelte-15b150i"&&(ml.innerHTML=Ua),Bs=n(l),y(rl.$$.fragment,l),Zs=n(l),wl=p(l,"P",{"data-svelte-h":!0}),i(wl)!=="svelte-y4njus"&&(wl.textContent=Ja),Vs=n(l),y(ul.$$.fragment,l),Rs=n(l),dl=p(l,"PRE",{"data-svelte-h":!0}),i(dl)!=="svelte-1d2dt07"&&(dl.textContent=Ta),Gs=n(l),y(Cl.$$.fragment,l),ks=n(l),Il=p(l,"P",{"data-svelte-h":!0}),i(Il)!=="svelte-1xg2uqr"&&(Il.textContent=ha),Ss=n(l),Al=p(l,"UL",{"data-svelte-h":!0}),i(Al)!=="svelte-k2eyjn"&&(Al.innerHTML=ma),Qs=n(l),y(bl.$$.fragment,l),Ws=n(l),fl=p(l,"P",{}),ra(fl).forEach(a),this.h()},h(){wa(m,"name","hf:doc:metadata"),wa(m,"content",Za)},m(l,s){ga(document.head,m),t(l,El,s),t(l,gl,s),t(l,Bl,s),o(r,l,s),t(l,Zl,s),o(w,l,s),t(l,Vl,s),t(l,u,s),t(l,Rl,s),t(l,d,s),t(l,Gl,s),t(l,C,s),t(l,kl,s),t(l,I,s),t(l,Sl,s),o(A,l,s),t(l,Ql,s),o(b,l,s),t(l,Wl,s),o(g,l,s),t(l,Nl,s),o(f,l,s),t(l,_l,s),t(l,E,s),t(l,ql,s),o(B,l,s),t(l,Xl,s),o(Z,l,s),t(l,vl,s),t(l,V,s),t(l,Fl,s),t(l,R,s),t(l,$l,s),t(l,G,s),t(l,xl,s),o(k,l,s),t(l,Hl,s),t(l,S,s),t(l,Yl,s),o(Q,l,s),t(l,zl,s),o(W,l,s),t(l,Ll,s),t(l,N,s),t(l,Dl,s),o(_,l,s),t(l,Pl,s),t(l,q,s),t(l,Ol,s),t(l,X,s),t(l,Kl,s),o(v,l,s),t(l,ls,s),o(F,l,s),t(l,ss,s),o($,l,s),t(l,as,s),t(l,x,s),t(l,ts,s),t(l,H,s),t(l,es,s),t(l,Y,s),t(l,ns,s),t(l,z,s),t(l,Ms,s),t(l,L,s),t(l,ps,s),o(D,l,s),t(l,is,s),o(P,l,s),t(l,js,s),t(l,O,s),t(l,ys,s),o(K,l,s),t(l,os,s),t(l,ll,s),t(l,cs,s),o(sl,l,s),t(l,Us,s),o(al,l,s),t(l,Js,s),t(l,tl,s),t(l,Ts,s),t(l,el,s),t(l,hs,s),o(nl,l,s),t(l,ms,s),t(l,Ml,s),t(l,rs,s),t(l,pl,s),t(l,ws,s),o(il,l,s),t(l,us,s),t(l,jl,s),t(l,ds,s),o(yl,l,s),t(l,Cs,s),t(l,ol,s),t(l,Is,s),t(l,cl,s),t(l,As,s),o(Ul,l,s),t(l,bs,s),o(Jl,l,s),t(l,gs,s),o(Tl,l,s),t(l,fs,s),t(l,hl,s),t(l,Es,s),t(l,ml,s),t(l,Bs,s),o(rl,l,s),t(l,Zs,s),t(l,wl,s),t(l,Vs,s),o(ul,l,s),t(l,Rs,s),t(l,dl,s),t(l,Gs,s),o(Cl,l,s),t(l,ks,s),t(l,Il,s),t(l,Ss,s),t(l,Al,s),t(l,Qs,s),o(bl,l,s),t(l,Ws,s),t(l,fl,s),Ns=!0},p:da,i(l){Ns||(c(r.$$.fragment,l),c(w.$$.fragment,l),c(A.$$.fragment,l),c(b.$$.fragment,l),c(g.$$.fragment,l),c(f.$$.fragment,l),c(B.$$.fragment,l),c(Z.$$.fragment,l),c(k.$$.fragment,l),c(Q.$$.fragment,l),c(W.$$.fragment,l),c(_.$$.fragment,l),c(v.$$.fragment,l),c(F.$$.fragment,l),c($.$$.fragment,l),c(D.$$.fragment,l),c(P.$$.fragment,l),c(K.$$.fragment,l),c(sl.$$.fragment,l),c(al.$$.fragment,l),c(nl.$$.fragment,l),c(il.$$.fragment,l),c(yl.$$.fragment,l),c(Ul.$$.fragment,l),c(Jl.$$.fragment,l),c(Tl.$$.fragment,l),c(rl.$$.fragment,l),c(ul.$$.fragment,l),c(Cl.$$.fragment,l),c(bl.$$.fragment,l),Ns=!0)},o(l){U(r.$$.fragment,l),U(w.$$.fragment,l),U(A.$$.fragment,l),U(b.$$.fragment,l),U(g.$$.fragment,l),U(f.$$.fragment,l),U(B.$$.fragment,l),U(Z.$$.fragment,l),U(k.$$.fragment,l),U(Q.$$.fragment,l),U(W.$$.fragment,l),U(_.$$.fragment,l),U(v.$$.fragment,l),U(F.$$.fragment,l),U($.$$.fragment,l),U(D.$$.fragment,l),U(P.$$.fragment,l),U(K.$$.fragment,l),U(sl.$$.fragment,l),U(al.$$.fragment,l),U(nl.$$.fragment,l),U(il.$$.fragment,l),U(yl.$$.fragment,l),U(Ul.$$.fragment,l),U(Jl.$$.fragment,l),U(Tl.$$.fragment,l),U(rl.$$.fragment,l),U(ul.$$.fragment,l),U(Cl.$$.fragment,l),U(bl.$$.fragment,l),Ns=!1},d(l){l&&(a(El),a(gl),a(Bl),a(Zl),a(Vl),a(u),a(Rl),a(d),a(Gl),a(C),a(kl),a(I),a(Sl),a(Ql),a(Wl),a(Nl),a(_l),a(E),a(ql),a(Xl),a(vl),a(V),a(Fl),a(R),a($l),a(G),a(xl),a(Hl),a(S),a(Yl),a(zl),a(Ll),a(N),a(Dl),a(Pl),a(q),a(Ol),a(X),a(Kl),a(ls),a(ss),a(as),a(x),a(ts),a(H),a(es),a(Y),a(ns),a(z),a(Ms),a(L),a(ps),a(is),a(js),a(O),a(ys),a(os),a(ll),a(cs),a(Us),a(Js),a(tl),a(Ts),a(el),a(hs),a(ms),a(Ml),a(rs),a(pl),a(ws),a(us),a(jl),a(ds),a(Cs),a(ol),a(Is),a(cl),a(As),a(bs),a(gs),a(fs),a(hl),a(Es),a(ml),a(Bs),a(Zs),a(wl),a(Vs),a(Rs),a(dl),a(Gs),a(ks),a(Il),a(Ss),a(Al),a(Qs),a(Ws),a(fl)),a(m),J(r,l),J(w,l),J(A,l),J(b,l),J(g,l),J(f,l),J(B,l),J(Z,l),J(k,l),J(Q,l),J(W,l),J(_,l),J(v,l),J(F,l),J($,l),J(D,l),J(P,l),J(K,l),J(sl,l),J(al,l),J(nl,l),J(il,l),J(yl,l),J(Ul,l),J(Jl,l),J(Tl,l),J(rl,l),J(ul,l),J(Cl,l),J(bl,l)}}}const Za='{"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 Va(_s){return Ca(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Qa extends Ia{constructor(m){super(),Aa(this,m,Va,Ba,ua,{})}}export{Qa as component};
