import{s as ja,f as Qs,n as ca,o as wa}from"../chunks/scheduler.39a43cde.js";import{S as Ta,i as fa,g as A,s as n,r as p,A as Ja,h as i,f as e,c as a,j as ra,u as M,x as o,k as Ct,y as ua,a as s,v as U,d as r,t as j,w as c}from"../chunks/index.23ec2204.js";import{D as ya,C as w}from"../chunks/DocNotebookDropdown.21a75b74.js";import{H as T,E as ma}from"../chunks/EditOnGithub.f2d09226.js";function da(xs){let f,Bt,bt,vt,J,It,u,gt,y,Hs='<em>作者: <a href="https://huggingface.co/m-ric" rel="nofollow">Aymeric Roucher</a></em>',Et,m,qs="这个 notebook 主要讲述了你怎么构建一个高级的 RAG，用于回答一个关于特定知识库的问题（这里，是 HuggingFace 文档），使用 LangChain。",Kt,d,Ps='对于 RAG 的介绍，你可以查看<a href="rag_zephyr_langch">这个教程</a>',Vt,R,Gs="RAG 系统是复杂的，它有许多组块:这里画一个简单的 RAG 图表，其中用蓝色标注了所有系统增强的可能性。",Nt,C,Ws,Xt,h,zs="<p>💡 可以看到，这个架构中有许多步骤可以调整：正确调整系统将带来显著的性能提升。</p>",kt,b,Zs="在这个 notebook 中，我们将研究许多这些蓝色标注的部分，看看如何调整你的 RAG 系统以获得最佳性能。",Qt,F,Ls="<strong>让我们深入研究模型架构吧！</strong> 首先，安装所需的模型依赖项。",xt,B,Ht,v,qt,I,Pt,g,Gt,E,Wt,K,zt,V,Zt,N,Ss="<strong>检索器的作用类似于内部搜索引擎</strong>：给定用户查询，它从你的知识库中返回几个相关的片段。",Lt,X,Ds="这些片段随后将被输入到阅读器模型中，以帮助其生成答案。",St,k,Ys="所以 <strong>我们的目标在这里是，给定一个用户问题，从我们的知识库中找到最多的片段来回答这个问题。</strong>",Dt,Q,_s="这是一个宽泛的目标，它留下了一些问题。我们应该检索多少片段？这个参数将被命名为<code>top_k</code>。",Yt,x,Os="这些片段应该有多长？这被称为 <code>chunk size</code> （片段大小）。没有一刀切的答案，但这里有一些要点：",_t,H,$s='<li>🔀 你的 <code>chunk size</code> 允许从一段片段到另一段片段有所不同。</li> <li>由于你的检索中总会存在一些噪音，增加 <code>top_k</code> 可以提高你检索到的片段中包含相关元素的概率。🎯 射更多的箭增加了你命中目标的概率。</li> <li>同时，你检索到的文档的总长度不应过高：例如，对于大多数当前模型来说，16k 个 token 可能会因为<a href="https://huggingface.co/papers/2307.03172" rel="nofollow">中间丢失现象</a>而在信息中淹没你的阅读器模型。🎯 只给你的阅读器模型提供最相关的见解，而不是一堆书！</li>',Ot,q,ln="<p>在这个 notebook 中，我们使用 Langchain 库，因为 <strong>它为向量数据库提供了大量的选项，并允许我们在整个处理过程中保留文档的元数据</strong>。</p>",$t,P,le,G,tn="<li>在这一部分，<strong>我们将知识库中的文档拆分成更小的片段</strong>，这些片段将是喂给阅读器 LLM 生成答案的片段。</li> <li>目标是准备一组<strong>语义上相关的片段</strong>。因此，它们的大小应该适配确切的想法：太小会截断想法，太大则会稀释它们。</li>",te,W,en='💡 <em>对于文本拆分存在许多选项：按单词拆分，按句子边界拆分，递归拆分以树状方式处理文档以保留结构信息… 要了解更多关于拆分的信息，我建议你阅读<a href="https://github.com/FullStackRetrieval-com/RetrievalTutorials/blob/main/5_Levels_Of_Text_Splitting.ipynb" rel="nofollow">这个很棒的 notebook</a>，这是由 Greg Kamradt 编写的。</em>',ee,z,sn="<li><p><strong>递归拆分</strong>使用给定的一组分隔符逐步将文本分解为更小的部分，这些分隔符按从最重要到最不重要的顺序排序。如果第一次拆分没有给出正确大小或形状的片段，该方法会使用不同的分隔符在新的片段上重复自身。例如，使用分隔符列表<code>[&quot;\\n\\n&quot;, &quot;\\n&quot;, &quot;.&quot;, &quot;&quot;]</code>：</p> <ul><li>该方法首先在出现双行中断<code>&quot;\\n\\n&quot;</code>的任何地方拆分文档。</li> <li>结果文档将在简单的行中断<code>&quot;\\n&quot;</code>处再次拆分，然后在句子结尾<code>&quot;.&quot;</code>处拆分。</li> <li>最后，如果有些片段仍然太大，它们将在超过最大大小时拆分。</li></ul></li> <li><p>使用这种方法，整体结构得到了很好的保留，代价是片段大小会有轻微的变化。</p></li>",se,Z,nn='<p><a href="https://huggingface.co/spaces/A-Roucher/chunk_visualizer" rel="nofollow">这个空间</a>让你可视化不同的拆分选项如何影响你得到的片段。</p>',ne,L,an="🔬 让我们用片段大小做一些实验，从任意大小开始，看看拆分是如何工作的。我们使用 Langchain 的 <code>RecursiveCharacterTextSplitter</code> 实现递归拆分。",ae,S,An="<li>参数 <code>chunk_size</code> 控制单个片段的长度：这个长度默认计算为片段中的字符数。</li> <li>参数 <code>chunk_overlap</code> 允许相邻片段彼此有一些重叠。这减少了想法被两个相邻片段之间的拆分切割成两半的概率。我们武断地将这个设置为片段大小的1/10，你可以尝试不同的值！</li>",Ae,D,ie,Y,on="我们还必须记住，当我们嵌入文档时，我们将使用一个接受特定最大序列长度 <code>max_seq_length</code> 的嵌入模型。",oe,_,pn="因此，我们应该确保我们的片段大小低于这个限制，因为任何更长的片段在处理之前都会被截断，从而失去相关性。",pe,O,Me,$,Mn=`Model's maximum sequence length: 512
`,Ue,ll,Un="👀 可以看到，<strong>片段长度与我们的 512 个 token 的限制不匹配</strong>，并且有些文档超出了限制，因此它们的一部分将在截断中丢失！",re,tl,rn="<li>因此，我们应该更改 <code>RecursiveCharacterTextSplitter</code> 类，以计算 token 数量而不是字符数量。</li> <li>然后，我们可以选择一个特定的片段大小，这里我们会选择低于 512 的阈值：<ul><li>较小的文档可能允许拆分更专注于特定想法的内容。</li> <li>但太小的片段会拆分句子，从而再次失去意义：适当的调整是一个平衡的问题。</li></ul></li>",je,el,ce,ht,jn,we,sl,cn="➡️ 现在分块长度分布看起来好多了!",Te,nl,wn="### 1.2 构建向量数据库",fe,al,Tn='我们希望为我们知识库的所有片段计算嵌入向量：要了解更多关于句子嵌入的信息，我们建议阅读<a href="https://osanseviero.github.io/hackerllama/blog/posts/sentence_embeddings/" rel="nofollow">这个指南</a>。',Je,Al,fn="#### 检索的工作原理",ue,il,Jn="一旦所有片段都被嵌入，我们就将它们存储到一个向量数据库中。当用户输入一个查询时，它会被之前使用的同一模型嵌入，并且相似性搜索会返回向量数据库中最接近的文档。",ye,ol,un="因此，技术挑战在于，给定一个查询向量，快速找到向量数据库中这个向量的最近邻。为此，我们需要选择两件事：一个距离度量，以及一个搜索算法，以便在成千上万的记录数据库中快速找到最近邻。",me,pl,yn="##### 最近邻搜索算法",de,Ml,mn='最近邻搜索算法有很多选择：我们选择 Facebook 的 <a href="https://github.com/facebookresearch/faiss" rel="nofollow">FAISS</a>，因为 FAISS 对于大多数用例来说性能足够好，而且它广为人知，因此被广泛实现。',Re,Ul,dn="##### 距离度量",Ce,rl,Rn='关于距离度量，你可以在<a href="https://osanseviero.github.io/hackerllama/blog/posts/sentence_embeddings/#distance-between-embeddings" rel="nofollow">这里</a>找到一个很好的指南。简而言之：',he,jl,Cn="<li><strong>余弦相似度</strong>计算两个向量之间的相似性，作为它们相对角度的余弦值：它允许我们比较向量的方向，而不考虑它们的幅度。使用它需要对所有向量进行归一化，将它们重新缩放到单位范数。</li> <li><strong>点积</strong>考虑幅度，有时会有不希望的效果，即增加向量的长度会使它与所有其他向量更相似。</li> <li><strong>欧氏距离</strong>是向量末端之间的距离。</li>",be,cl,hn='你可以尝试<a href="https://developers.google.com/machine-learning/clustering/similarity/check-your-understanding" rel="nofollow">这个小测</a>来检查你对这些概念的理解。但是一旦向量被归一化，<a href="https://platform.openai.com/docs/guides/embeddings/which-distance-function-should-i-use" rel="nofollow">选择特定的距离度量并不重要</a>。',Fe,wl,bn="我们的特定模型与余弦相似度配合得很好，所以我们选择这个距离度量，并在嵌入模型中以及 FAISS 索引的 <code>distance_strategy</code> 参数中设置它。使用余弦相似度，我们需要归一化我们的嵌入向量。",Be,Tl,Fn="🚨👇 下面的单元格需要在 A10G 上运行几分钟！",ve,fl,Ie,Jl,Bn="👀 为了可视化搜索最接近的文档，我们使用 PaCMAP 将我们的嵌入向量从 384 维降至 2 维。",ge,ul,vn='💡 <em>我们选择 PaCMAP 而不是其他技术，如 t-SNE 或 UMAP，因为<a href="https://www.nature.com/articles/s42003-022-03628-x#Abs1" rel="nofollow">它效率高（保留局部和全局结构），对初始化参数鲁棒且速度快</a>。</em>',Ee,yl,Ke,ml,Ve,dl,Ne,Rl,In,Xe,Cl,gn="➡️ 在上面的图表中，你可以看到知识库文档的空间表示。由于向量嵌入代表了文档的含义，它们在意义上的接近应该在它们的嵌入的接近程度上反映出来。",ke,hl,En="用户查询的嵌入也被显示出来：我们想要找到意义最接近的 <code>k</code> 个文档，因此我们选择最接近的 <code>k</code> 个向量。",Qe,bl,Kn="在 LangChain 向量数据库实现中，这个搜索操作是由方法 <code>vector_database.similarity_search(query)</code> 执行的。",xe,Fl,Vn="这里是结果：",He,Bl,qe,vl,Nn=`Starting retrieval for user_query='How to create a pipeline object?'...

==================================Top document==================================
\`\`\`

## Available Pipelines:
==================================Metadata==================================
&#123;'source': 'huggingface/diffusers/blob/main/docs/source/en/api/pipelines/deepfloyd_if.md', 'start_index': 16887}
`,Pe,Il,Ge,gl,Xn="在这一部分，<strong>LLM 阅读器读取检索到的上下文以形成其答案。</strong>",We,El,kn="实际上有多个可以调整的子步骤：",ze,Kl,Qn="<li>检索到的文档内容被聚合并放入“上下文”中，这其中有许多处理选项，如<em>提示压缩</em>。</li> <li>上下文和用户查询被聚合并形成一个提示(prompt)，然后交给 LLM 生成其答案。</li>",Ze,Vl,Le,Nl,xn="在选择阅读器模型时，有几个方面很重要：",Se,Xl,Hn="<li>阅读器模型的 <code>max_seq_length</code> 必须适应我们的提示(prompt)，其中包括检索器调用输出的上下文：上下文包括 5 个每份 512 个 token 的文档，所以我们至少需要 4k 个 token 的上下文长度。</li> <li>阅读器模型</li>",De,kl,qn='在这个例子中，我们选择了 <a href="https://huggingface.co/HuggingFaceH4/zephyr-7b-beta" rel="nofollow"><code>HuggingFaceH4/zephyr-7b-beta</code></a>，这是一个小而强大的模型。',Ye,Ql,Pn='由于每周都会发布许多模型，你可能想要用最新最好的模型替换这个模型。跟踪开源 LLM 的最佳方式是查看 <a href="https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard" rel="nofollow">Open-source LLM leaderboard</a>。',_e,xl,Gn="为了加速推理，我们将加载模型的量化版本：",Oe,Hl,$e,ql,ls,Pl,ts,Gl,Wn="下面的 RAG 提示模板是我们将要提供给阅读器 LLM 的内容：需要将其格式化为阅读器 LLM 的聊天模板,这点非常重要。",es,Wl,zn="我们向其提供我们的上下文和用户的问题。",ss,zl,ns,Zl,Zn=`<|system|>
Using the information contained in the context, 
give a comprehensive answer to the question.
Respond only to the question asked, response should be concise and relevant to the question.
Provide the number of the source document when relevant.
If the answer cannot be deduced from the context, do not give an answer.
<|user|>
Context:
&#123;context}
---
Now here is the question you need to answer.

Question: &#123;question}
<|assistant|>
`,as,Ll,Ln="让我们在之前检索的文档上测试我们的阅读器!",As,Sl,is,Dl,Sn=`To create a pipeline object, follow these steps:

1. Define the inputs and outputs of your pipeline. These could be strings, dictionaries, or any other format that best suits your use case.

2. Inherit the \`Pipeline\` class from the \`transformers\` module and implement the following methods:

   - \`preprocess\`: This method takes the raw inputs and returns a preprocessed dictionary that can be passed to the model.

   - \`_forward\`: This method performs the actual inference using the model and returns the output tensor.

   - \`postprocess\`: This method takes the output tensor and returns the final output in the desired format.

   - \`_sanitize_parameters\`: This method is used to sanitize the input parameters before passing them to the model.

3. Load the necessary components, such as the model and scheduler, into the pipeline object.

4. Instantiate the pipeline object and return it.

Here's an example implementation based on the given context:

\`\`\`python
from transformers import Pipeline
import torch
from diffusers import StableDiffusionPipeline

class MyPipeline(Pipeline):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.pipe = StableDiffusionPipeline.from_pretrained("my_model")

    def preprocess(self, inputs):
        &num; Preprocess the inputs as needed
        return &#123;"input_ids":...}

    def _forward(self, inputs):
        &num; Run the forward pass of the model
        return self.pipe(**inputs).images[0]

    def postprocess(self, outputs):
        &num; Postprocess the outputs as needed
        return outputs["sample"]

    def _sanitize_parameters(self, params):
        &num; Sanitize the input parameters
        return params

my_pipeline = MyPipeline()
result = my_pipeline("My input string")
print(result)
\`\`\`

Note that this implementation assumes that the model and scheduler are already loaded into memory. If they need to be loaded dynamically, you can modify the \`__init__\` method accordingly.
`,os,Yl,ps,_l,Dn="对于 RAG 来说，通常更好的选择会最终检索出比你想要的更多的文档，然后在保留 <code>top_k</code> 之前，使用更强大的检索模型对结果进行重新排序。",Ms,Ol,Yn='为此，<a href="https://arxiv.org/abs/2112.01488" rel="nofollow">Colbertv2</a>是一个很好的选择：它不是像我们传统的嵌入模型那样的双向编码器，而是一个交叉编码器，它计算查询 token 与每个文档 token 之间更细致的交互。',Us,$l,_n='由于有了 <a href="https://github.com/bclavie/RAGatouille" rel="nofollow">RAGatouille 库</a>，它的使用变得非常简单。',rs,lt,js,tt,cs,et,ws,st,On="让我们看看我们的 RAG 流水线是怎么回答用户的询问的。",Ts,nt,fs,at,$n=`=> Retrieving documents...
`,Js,At,us,it,la=`==================================Answer==================================
To create a pipeline object, follow these steps:

1. Import the \`pipeline\` function from the \`transformers\` module:

   \`\`\`python
   from transformers import pipeline
   \`\`\`

2. Choose the task you want to perform, such as object detection, sentiment analysis, or image generation, and pass it as an argument to the \`pipeline\` function:

   - For object detection:

     \`\`\`python
     >>> object_detector = pipeline('object-detection')
     >>> object_detector(image)
     [&#123;'score': 0.9982201457023621,
       'label':'remote',
       'box': &#123;'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
     ...]
     \`\`\`

   - For sentiment analysis:

     \`\`\`python
     >>> classifier = pipeline("sentiment-analysis")
     >>> classifier("This is a great product!")
     &#123;'labels': ['POSITIVE'],'scores': tensor([0.9999], device='cpu', dtype=torch.float32)}
     \`\`\`

   - For image generation:

     \`\`\`python
     >>> image = pipeline(
    ... "stained glass of darth vader, backlight, centered composition, masterpiece, photorealistic, 8k"
    ... ).images[0]
     >>> image
     PILImage mode RGB size 7680x4320 at 0 DPI
     \`\`\`

Note that the exact syntax may vary depending on the specific pipeline being used. Refer to the documentation for more details on how to use each pipeline.

In general, the process involves importing the necessary modules, selecting the desired pipeline task, and passing it to the \`pipeline\` function along with any required arguments. The resulting pipeline object can then be used to perform the selected task on input data.
==================================Source docs==================================
Document 0------------------------------------------------------------
# Allocate a pipeline for object detection
>>> object_detector = pipeline('object-detection')
>>> object_detector(image)
[&#123;'score': 0.9982201457023621,
  'label': 'remote',
  'box': &#123;'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
 &#123;'score': 0.9960021376609802,
  'label': 'remote',
  'box': &#123;'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
 &#123;'score': 0.9954745173454285,
  'label': 'couch',
  'box': &#123;'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
 &#123;'score': 0.9988006353378296,
  'label': 'cat',
  'box': &#123;'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
 &#123;'score': 0.9986783862113953,
  'label': 'cat',
  'box': &#123;'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}}]
Document 1------------------------------------------------------------
# Allocate a pipeline for object detection
>>> object_detector = pipeline('object_detection')
>>> object_detector(image)
[&#123;'score': 0.9982201457023621,
  'label': 'remote',
  'box': &#123;'xmin': 40, 'ymin': 70, 'xmax': 175, 'ymax': 117}},
 &#123;'score': 0.9960021376609802,
  'label': 'remote',
  'box': &#123;'xmin': 333, 'ymin': 72, 'xmax': 368, 'ymax': 187}},
 &#123;'score': 0.9954745173454285,
  'label': 'couch',
  'box': &#123;'xmin': 0, 'ymin': 1, 'xmax': 639, 'ymax': 473}},
 &#123;'score': 0.9988006353378296,
  'label': 'cat',
  'box': &#123;'xmin': 13, 'ymin': 52, 'xmax': 314, 'ymax': 470}},
 &#123;'score': 0.9986783862113953,
  'label': 'cat',
  'box': &#123;'xmin': 345, 'ymin': 23, 'xmax': 640, 'ymax': 368}}]
Document 2------------------------------------------------------------
Start by creating an instance of [\`pipeline\`] and specifying a task you want to use it for. In this guide, you'll use the [\`pipeline\`] for sentiment analysis as an example:

\`\`\`py
>>> from transformers import pipeline

>>> classifier = pipeline("sentiment-analysis")
Document 3------------------------------------------------------------
\`\`\`

## Add the pipeline to 🤗 Transformers

If you want to contribute your pipeline to 🤗 Transformers, you will need to add a new module in the \`pipelines\` submodule
with the code of your pipeline, then add it to the list of tasks defined in \`pipelines/__init__.py\`.

Then you will need to add tests. Create a new file \`tests/test_pipelines_MY_PIPELINE.py\` with examples of the other tests.

The \`run_pipeline_test\` function will be very generic and run on small random models on every possible
architecture as defined by \`model_mapping\` and \`tf_model_mapping\`.

This is very important to test future compatibility, meaning if someone adds a new model for
\`XXXForQuestionAnswering\` then the pipeline test will attempt to run on it. Because the models are random it's
impossible to check for actual values, that's why there is a helper \`ANY\` that will simply attempt to match the
output of the pipeline TYPE.

You also *need* to implement 2 (ideally 4) tests.

- \`test_small_model_pt\` : Define 1 small model for this pipeline (doesn't matter if the results don't make sense)
  and test the pipeline outputs. The results should be the same as \`test_small_model_tf\`.
- \`test_small_model_tf\` : Define 1 small model for this pipeline (doesn't matter if the results don't make sense)
  and test the pipeline outputs. The results should be the same as \`test_small_model_pt\`.
- \`test_large_model_pt\` (\`optional\`): Tests the pipeline on a real pipeline where the results are supposed to
  make sense. These tests are slow and should be marked as such. Here the goal is to showcase the pipeline and to make
  sure there is no drift in future releases.
- \`test_large_model_tf\` (\`optional\`): Tests the pipeline on a real pipeline where the results are supposed to
  make sense. These tests are slow and should be marked as such. Here the goal is to showcase the pipeline and to make
  sure there is no drift in future releases.
Document 4------------------------------------------------------------
\`\`\`

2. Pass a prompt to the pipeline to generate an image:

\`\`\`py
image = pipeline(
	"stained glass of darth vader, backlight, centered composition, masterpiece, photorealistic, 8k"
).images[0]
image
`,ys,ot,ta="✅ 现在我们已经拥有了一个完整且性能出色的 RAG 系统。今天的教程就到这里！恭喜你坚持到了最后 🥳",ms,pt,ds,Mt,ea="这并不是旅程的终点！你可以尝试许多步骤来改进你的 RAG 系统。我们建议以迭代的方式进行：对系统进行小的更改，看看哪些可以提升性能。",Rs,Ut,Cs,rt,sa="<li>💬 “你不能改进你没有衡量的模型性能”，甘地说过… 或者至少 Llama2 告诉我他这么说过。无论如何，你绝对应该从衡量性能开始：这意味着构建一个小的评估数据集，然后在评估数据集上监控你的 RAG 系统的性能。</li>",hs,jt,bs,ct,na="🛠️ <strong>你可以使用这些选项来调整结果：</strong>",Fs,wt,aa='<li><p>调整分块方法：</p> <ul><li>片段的大小</li> <li>方法：使用不同的分隔符进行拆分，使用<a href="https://python.langchain.com/docs/modules/data_connection/document_transformers/semantic-chunker" rel="nofollow">语义分块</a>…</li></ul></li> <li><p>更改嵌入模型</p></li>',Bs,Tt,Aa="👷‍♀️ <strong>还可以考虑以下事项：</strong>",vs,ft,ia="<li>尝试另一种分块方法，如语义分块</li> <li>更改使用的索引（这里使用的是 FAISS）</li> <li>查询扩展：以略微不同的方式重新构建用户查询以检索更多文档。</li>",Is,Jt,gs,ut,oa="🛠️ <strong>这里你可以尝试以下选项来改善结果：</strong>",Es,yt,pa="<li>调整提示</li> <li>开启/关闭重排序</li> <li>选择一个更强大的阅读器模型</li>",Ks,mt,Ma="💡 <strong>这里有许多选项可以考虑以进一步改善结果：</strong>",Vs,dt,Ua="<li>压缩检索到的上下文，只保留与回答查询最相关的部分。</li> <li>扩展 RAG 系统，使其更加用户友好：<ul><li>引用来源</li> <li>使其能够进行对话</li></ul></li>",Ns,Rt,Xs,Ft,ks;return J=new ya({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/advanced_rag.ipynb"}]}}),u=new T({props:{title:"使用 LangChain 在 HuggingFace 文档上构建高级 RAG",local:"使用-langchain-在-huggingface-文档上构建高级-rag",headingTag:"h1"}}),B=new w({props:{code:"IXBpcCUyMGluc3RhbGwlMjAtcSUyMHRvcmNoJTIwdHJhbnNmb3JtZXJzJTIwdHJhbnNmb3JtZXJzJTIwYWNjZWxlcmF0ZSUyMGJpdHNhbmRieXRlcyUyMGxhbmdjaGFpbiUyMHNlbnRlbmNlLXRyYW5zZm9ybWVycyUyMGZhaXNzLWdwdSUyMG9wZW5weXhsJTIwcGFjbWFw",highlighted:"!pip install -q torch transformers transformers accelerate bitsandbytes langchain sentence-transformers faiss-gpu openpyxl pacmap",wrap:!1}}),v=new w({props:{code:"JTI1cmVsb2FkX2V4dCUyMGRvdGVudiUwQSUyNWRvdGVudg==",highlighted:`%reload_ext dotenv
%dotenv`,wrap:!1}}),I=new w({props:{code:"ZnJvbSUyMHRxZG0ubm90ZWJvb2slMjBpbXBvcnQlMjB0cWRtJTBBaW1wb3J0JTIwcGFuZGFzJTIwYXMlMjBwZCUwQWZyb20lMjB0eXBpbmclMjBpbXBvcnQlMjBPcHRpb25hbCUyQyUyMExpc3QlMkMlMjBUdXBsZSUwQWZyb20lMjBkYXRhc2V0cyUyMGltcG9ydCUyMERhdGFzZXQlMEFpbXBvcnQlMjBtYXRwbG90bGliLnB5cGxvdCUyMGFzJTIwcGx0JTBBJTBBcGQuc2V0X29wdGlvbiglMjJkaXNwbGF5Lm1heF9jb2x3aWR0aCUyMiUyQyUyME5vbmUpJTIwJTIwJTIzJTIwdGhpcyUyMHdpbGwlMjBiZSUyMGhlbHBmdWwlMjB3aGVuJTIwdmlzdWFsaXppbmclMjByZXRyaWV2ZXIlMjBvdXRwdXRz",highlighted:`<span class="hljs-keyword">from</span> tqdm.notebook <span class="hljs-keyword">import</span> tqdm
<span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd
<span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">Optional</span>, <span class="hljs-type">List</span>, <span class="hljs-type">Tuple</span>
<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt

pd.set_option(<span class="hljs-string">&quot;display.max_colwidth&quot;</span>, <span class="hljs-literal">None</span>)  <span class="hljs-comment"># this will be helpful when visualizing retriever outputs</span>`,wrap:!1}}),g=new T({props:{title:"加载你的知识基础",local:"加载你的知识基础",headingTag:"h3"}}),E=new w({props:{code:"aW1wb3J0JTIwZGF0YXNldHMlMEElMEFkcyUyMCUzRCUyMGRhdGFzZXRzLmxvYWRfZGF0YXNldCglMjJtLXJpYyUyRmh1Z2dpbmdmYWNlX2RvYyUyMiUyQyUyMHNwbGl0JTNEJTIydHJhaW4lMjIp",highlighted:`<span class="hljs-keyword">import</span> datasets

ds = datasets.load_dataset(<span class="hljs-string">&quot;m-ric/huggingface_doc&quot;</span>, split=<span class="hljs-string">&quot;train&quot;</span>)`,wrap:!1}}),K=new w({props:{code:"ZnJvbSUyMGxhbmdjaGFpbi5kb2NzdG9yZS5kb2N1bWVudCUyMGltcG9ydCUyMERvY3VtZW50JTIwYXMlMjBMYW5nY2hhaW5Eb2N1bWVudCUwQSUwQVJBV19LTk9XTEVER0VfQkFTRSUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMExhbmdjaGFpbkRvY3VtZW50KHBhZ2VfY29udGVudCUzRGRvYyU1QiUyMnRleHQlMjIlNUQlMkMlMjBtZXRhZGF0YSUzRCU3QiUyMnNvdXJjZSUyMiUzQSUyMGRvYyU1QiUyMnNvdXJjZSUyMiU1RCU3RCklMjBmb3IlMjBkb2MlMjBpbiUyMHRxZG0oZHMpJTBBJTVE",highlighted:`<span class="hljs-keyword">from</span> langchain.docstore.document <span class="hljs-keyword">import</span> Document <span class="hljs-keyword">as</span> LangchainDocument

RAW_KNOWLEDGE_BASE = [
    LangchainDocument(page_content=doc[<span class="hljs-string">&quot;text&quot;</span>], metadata={<span class="hljs-string">&quot;source&quot;</span>: doc[<span class="hljs-string">&quot;source&quot;</span>]}) <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> tqdm(ds)
]`,wrap:!1}}),V=new T({props:{title:"1. 检索器- 嵌入 🗂️",local:"1-检索器--嵌入-",headingTag:"h1"}}),P=new T({props:{title:"1.1 将文档拆分为片段(chuncks)",local:"11-将文档拆分为片段chuncks",headingTag:"h3"}}),D=new w({props:{code:"ZnJvbSUyMGxhbmdjaGFpbi50ZXh0X3NwbGl0dGVyJTIwaW1wb3J0JTIwUmVjdXJzaXZlQ2hhcmFjdGVyVGV4dFNwbGl0dGVyJTBBJTBBJTIzJTIwV2UlMjB1c2UlMjBhJTIwaGllcmFyY2hpY2FsJTIwbGlzdCUyMG9mJTIwc2VwYXJhdG9ycyUyMHNwZWNpZmljYWxseSUyMHRhaWxvcmVkJTIwZm9yJTIwc3BsaXR0aW5nJTIwTWFya2Rvd24lMjBkb2N1bWVudHMlMEElMjMlMjBUaGlzJTIwbGlzdCUyMGlzJTIwdGFrZW4lMjBmcm9tJTIwTGFuZ0NoYWluJ3MlMjBNYXJrZG93blRleHRTcGxpdHRlciUyMGNsYXNzLiUwQU1BUktET1dOX1NFUEFSQVRPUlMlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlMjIlNUNuJTIzJTdCMSUyQzYlN0QlMjAlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIlNjAlNjAlNjAlNUNuJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTVDbiU1QyU1QyolNUMlNUMqJTVDJTVDKiUyQiU1Q24lMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIlNUNuLS0tJTJCJTVDbiUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMiU1Q25fX18lMkIlNUNuJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTVDbiU1Q24lMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIlNUNuJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTIwJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTIyJTJDJTBBJTVEJTBBJTBBdGV4dF9zcGxpdHRlciUyMCUzRCUyMFJlY3Vyc2l2ZUNoYXJhY3RlclRleHRTcGxpd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ljs-keyword">import</span> RecursiveCharacterTextSplitter

<span class="hljs-comment"># We use a hierarchical list of separators specifically tailored for splitting Markdown documents</span>
<span class="hljs-comment"># This list is taken from LangChain&#x27;s MarkdownTextSplitter class.</span>
MARKDOWN_SEPARATORS = [
    <span class="hljs-string">&quot;\\n#{1,6} &quot;</span>,
    <span class="hljs-string">&quot;\`\`\`\\n&quot;</span>,
    <span class="hljs-string">&quot;\\n\\\\*\\\\*\\\\*+\\n&quot;</span>,
    <span class="hljs-string">&quot;\\n---+\\n&quot;</span>,
    <span class="hljs-string">&quot;\\n___+\\n&quot;</span>,
    <span class="hljs-string">&quot;\\n\\n&quot;</span>,
    <span class="hljs-string">&quot;\\n&quot;</span>,
    <span class="hljs-string">&quot; &quot;</span>,
    <span class="hljs-string">&quot;&quot;</span>,
]

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=<span class="hljs-number">1000</span>,  <span class="hljs-comment"># the maximum number of characters in a chunk: we selected this value arbitrarily</span>
    chunk_overlap=<span class="hljs-number">100</span>,  <span class="hljs-comment"># the number of characters to overlap between chunks</span>
    add_start_index=<span class="hljs-literal">True</span>,  <span class="hljs-comment"># If \`True\`, includes chunk&#x27;s start index in metadata</span>
    strip_whitespace=<span class="hljs-literal">True</span>,  <span class="hljs-comment"># If \`True\`, strips whitespace from the start and end of every document</span>
    separators=MARKDOWN_SEPARATORS,
)

docs_processed = []
<span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> RAW_KNOWLEDGE_BASE:
    docs_processed += text_splitter.split_documents([doc])`,wrap:!1}}),O=new w({props:{code:"ZnJvbSUyMHNlbnRlbmNlX3RyYW5zZm9ybWVycyUyMGltcG9ydCUyMFNlbnRlbmNlVHJhbnNmb3JtZXIlMEElMEElMjMlMjBUbyUyMGdldCUyMHRoZSUyMHZhbHVlJTIwb2YlMjB0aGUlMjBtYXglMjBzZXF1ZW5jZV9sZW5ndGglMkMlMjB3ZSUyMHdpbGwlMjBxdWVyeSUyMHRoZSUyMHVuZGVybHlpbmclMjAlNjBTZW50ZW5jZVRyYW5zZm9ybWVyJTYwJTIwb2JqZWN0JTIwdXNlZCUyMGluJTIwdGhlJTIwUmVjdXJzaXZlQ2hhcmFjdGVyVGV4dFNwbGl0dGVyLiUwQXByaW50KGYlMjJNb2RlbCdzJTIwbWF4aW11bSUyMHNlcXVlbmNlJTIwbGVuZ3RoJTNBJTIwJTdCU2VudGVuY2VUcmFuc2Zvcm1lcigndGhlbmxwZXIlMkZndGUtc21hbGwnKS5tYXhfc2VxX2xlbmd0aCU3RCUyMiklMEElMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUwQSUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKCUyMnRoZW5scGVyJTJGZ3RlLXNtYWxsJTIyKSUwQWxlbmd0aHMlMjAlM0QlMjAlNUJsZW4odG9rZW5pemVyLmVuY29kZShkb2MucGFnZV9jb250ZW50KSklMjBmb3IlMjBkb2MlMjBpbiUyMHRxZG0oZG9jc19wcm9jZXNzZWQpJTVEJTBBJTBBJTIzJTIwUGxvdCUyMHRoZSUyMGRpc3RydWJ1dGlvbiUyMG9mJTIwZG9jdW1lbnQlMjBsZW5ndGhzJTJDJTIwY291bnRlZCUyMGFzJTIwdGhlJTIwbnVtYmVyJTIwb2YlMjB0b2tlbnMlMEFmaWclMjAlM0QlMjBwZC5TZXJpZXMobGVuZ3RocykuaGlzdCgpJTBBcGx0LnRpdGxlKCUyMkRpc3RyaWJ1dGlvbiUyMG9mJTIwZG9jdW1lbnQlMjBsZW5ndGhzJTIwaW4lMjB0aGUlMjBrbm93bGVkZ2UlMjBiYXNlJTIwKGluJTIwY291bnQlMjBvZiUyMHRva2VucyklMjIpJTBBcGx0LnNob3coKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> sentence_transformers <span class="hljs-keyword">import</span> SentenceTransformer

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># To get the value of the max sequence_length, we will query the underlying \`SentenceTransformer\` object used in the RecursiveCharacterTextSplitter.</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model&#x27;s maximum sequence length: <span class="hljs-subst">{SentenceTransformer(<span class="hljs-string">&#x27;thenlper/gte-small&#x27;</span>).max_seq_length}</span>&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;thenlper/gte-small&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>lengths = [<span class="hljs-built_in">len</span>(tokenizer.encode(doc.page_content)) <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> tqdm(docs_processed)]

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Plot the distrubution of document lengths, counted as the number of tokens</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>fig = pd.Series(lengths).hist()
<span class="hljs-meta">&gt;&gt;&gt; </span>plt.title(<span class="hljs-string">&quot;Distribution of document lengths in the knowledge base (in count of tokens)&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>plt.show()`,wrap:!1}}),el=new w({props:{code:"ZnJvbSUyMGxhbmdjaGFpbi50ZXh0X3NwbGl0dGVyJTIwaW1wb3J0JTIwUmVjdXJzaXZlQ2hhcmFjdGVyVGV4dFNwbGl0dGVyJTBBZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEFFTUJFRERJTkdfTU9ERUxfTkFNRSUyMCUzRCUyMCUyMnRoZW5scGVyJTJGZ3RlLXNtYWxsJTIyJTBBJTBBJTBBZGVmJTIwc3BsaXRfZG9jdW1lbnRzK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ZCUyMCUzRCUyMHNwbGl0X2RvY3VtZW50cyglMEElMjAlMjAlMjAlMjA1MTIlMkMlMjAlMjAlMjMlMjBXZSUyMGNob29zZSUyMGElMjBjaHVuayUyMHNpemUlMjBhZGFwdGVkJTIwdG8lMjBvdXIlMjBtb2RlbCUwQSUyMCUyMCUyMCUyMFJBV19LTk9XTEVER0VfQkFTRSUyQyUwQSUyMCUyMCUyMCUyMHRva2VuaXplcl9uYW1lJTNERU1CRURESU5HX01PREVMX05BTUUlMkMlMEEpJTBBJTBBJTIzJTIwTGV0J3MlMjB2aXN1YWxpemUlMjB0aGUlMjBjaHVuayUyMHNpemVzJTIwd2UlMjB3b3VsZCUyMGhhdmUlMjBpbiUyMHRva2VucyUyMGZyb20lMjBhJTIwY29tbW9uJTIwbW9kZWwlMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUwQSUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKEVNQkVERElOR19NT0RFTF9OQU1FKSUwQWxlbmd0aHMlMjAlM0QlMjAlNUJsZW4odG9rZW5pemVyLmVuY29kZShkb2MucGFnZV9jb250ZW50KSklMjBmb3IlMjBkb2MlMjBpbiUyMHRxZG0oZG9jc19wcm9jZXNzZWQpJTVEJTBBZmlnJTIwJTNEJTIwcGQuU2VyaWVzKGxlbmd0aHMpLmhpc3QoKSUwQXBsdC50aXRsZSglMjJEaXN0cmlidXRpb24lMjBvZiUyMGRvY3VtZW50JTIwbGVuZ3RocyUyMGluJTIwdGhlJTIwa25vd2xlZGdlJTIwYmFzZSUyMChpbiUyMGNvdW50JTIwb2YlMjB0b2tlbnMpJTIyKSUwQXBsdC5zaG93KCk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> langchain.text_splitter <span class="hljs-keyword">import</span> RecursiveCharacterTextSplitter
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

<span class="hljs-meta">&gt;&gt;&gt; </span>EMBEDDING_MODEL_NAME = <span class="hljs-string">&quot;thenlper/gte-small&quot;</span>


<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">split_documents</span>(<span class="hljs-params">
<span class="hljs-meta">... </span>    chunk_size: <span class="hljs-built_in">int</span>,
<span class="hljs-meta">... </span>    knowledge_base: <span class="hljs-type">List</span>[LangchainDocument],
<span class="hljs-meta">... </span>    tokenizer_name: <span class="hljs-type">Optional</span>[<span class="hljs-built_in">str</span>] = EMBEDDING_MODEL_NAME,
<span class="hljs-meta">... </span></span>) -&gt; <span class="hljs-type">List</span>[LangchainDocument]:
<span class="hljs-meta">... </span>    <span class="hljs-string">&quot;&quot;&quot;
<span class="hljs-meta">... </span>    Split documents into chunks of maximum size \`chunk_size\` tokens and return a list of documents.
<span class="hljs-meta">... </span>    &quot;&quot;&quot;</span>
<span class="hljs-meta">... </span>    text_splitter = RecursiveCharacterTextSplitter.from_huggingface_tokenizer(
<span class="hljs-meta">... </span>        AutoTokenizer.from_pretrained(tokenizer_name),
<span class="hljs-meta">... </span>        chunk_size=chunk_size,
<span class="hljs-meta">... </span>        chunk_overlap=<span class="hljs-built_in">int</span>(chunk_size / <span class="hljs-number">10</span>),
<span class="hljs-meta">... </span>        add_start_index=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>        strip_whitespace=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>        separators=MARKDOWN_SEPARATORS,
<span class="hljs-meta">... </span>    )

<span class="hljs-meta">... </span>    docs_processed = []
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> knowledge_base:
<span class="hljs-meta">... </span>        docs_processed += text_splitter.split_documents([doc])

<span class="hljs-meta">... </span>    <span class="hljs-comment"># Remove duplicates</span>
<span class="hljs-meta">... </span>    unique_texts = {}
<span class="hljs-meta">... </span>    docs_processed_unique = []
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> docs_processed:
<span class="hljs-meta">... </span>        <span class="hljs-keyword">if</span> doc.page_content <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> unique_texts:
<span class="hljs-meta">... </span>            unique_texts[doc.page_content] = <span class="hljs-literal">True</span>
<span class="hljs-meta">... </span>            docs_processed_unique.append(doc)

<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> docs_processed_unique


<span class="hljs-meta">&gt;&gt;&gt; </span>docs_processed = split_documents(
<span class="hljs-meta">... </span>    <span class="hljs-number">512</span>,  <span class="hljs-comment"># We choose a chunk size adapted to our model</span>
<span class="hljs-meta">... </span>    RAW_KNOWLEDGE_BASE,
<span class="hljs-meta">... </span>    tokenizer_name=EMBEDDING_MODEL_NAME,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Let&#x27;s visualize the chunk sizes we would have in tokens from a common model</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(EMBEDDING_MODEL_NAME)
<span class="hljs-meta">&gt;&gt;&gt; </span>lengths = [<span class="hljs-built_in">len</span>(tokenizer.encode(doc.page_content)) <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> tqdm(docs_processed)]
<span class="hljs-meta">&gt;&gt;&gt; </span>fig = pd.Series(lengths).hist()
<span class="hljs-meta">&gt;&gt;&gt; </span>plt.title(<span class="hljs-string">&quot;Distribution of document lengths in the knowledge base (in count of tokens)&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>plt.show()`,wrap:!1}}),fl=new w({props:{code:"ZnJvbSUyMGxhbmdjaGFpbi52ZWN0b3JzdG9yZXMlMjBpbXBvcnQlMjBGQUlTUyUwQWZyb20lMjBsYW5nY2hhaW5fY29tbXVuaXR5LmVtYmVkZGluZ3MlMjBpbXBvcnQlMjBIdWdnaW5nRmFjZUVtYmVkZGluZ3MlMEFmcm9tJTIwbGFuZ2NoYWluX2NvbW11bml0eS52ZWN0b3JzdG9yZXMudXRpbHMlMjBpbXBvcnQlMjBEaXN0YW5jZVN0cmF0ZWd5JTBBJTBBZW1iZWRkaW5nX21vZGVsJTIwJTNEJTIwSHVnZ2luZ0ZhY2VFbWJlZGRpbmdzKCUwQSUyMCUyMCUyMCUyMG1vZGVsX25hbWUlM0RFTUJFRERJTkdfTU9ERUxfTkFNRSUyQyUwQSUyMCUyMCUyMCUyMG11bHRpX3Byb2Nlc3MlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwbW9kZWxfa3dhcmdzJTNEJTdCJTIyZGV2aWNlJTIyJTNBJTIwJTIyY3VkYSUyMiU3RCUyQyUwQSUyMCUyMCUyMCUyMGVuY29kZV9rd2FyZ3MlM0QlN0IlMjJub3JtYWxpemVfZW1iZWRkaW5ncyUyMiUzQSUyMFRydWUlN0QlMkMlMjAlMjAlMjMlMjBzZXQlMjBUcnVlJTIwZm9yJTIwY29zaW5lJTIwc2ltaWxhcml0eSUwQSklMEElMEFLTk9XTEVER0VfVkVDVE9SX0RBVEFCQVNFJTIwJTNEJTIwRkFJU1MuZnJvbV9kb2N1bWVudHMoJTBBJTIwJTIwJTIwJTIwZG9jc19wcm9jZXNzZWQlMkMlMjBlbWJlZGRpbmdfbW9kZWwlMkMlMjBkaXN0YW5jZV9zdHJhdGVneSUzRERpc3RhbmNlU3RyYXRlZ3kuQ09TSU5FJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> langchain.vectorstores <span class="hljs-keyword">import</span> FAISS
<span class="hljs-keyword">from</span> langchain_community.embeddings <span class="hljs-keyword">import</span> HuggingFaceEmbeddings
<span class="hljs-keyword">from</span> langchain_community.vectorstores.utils <span class="hljs-keyword">import</span> DistanceStrategy

embedding_model = HuggingFaceEmbeddings(
    model_name=EMBEDDING_MODEL_NAME,
    multi_process=<span class="hljs-literal">True</span>,
    model_kwargs={<span class="hljs-string">&quot;device&quot;</span>: <span class="hljs-string">&quot;cuda&quot;</span>},
    encode_kwargs={<span class="hljs-string">&quot;normalize_embeddings&quot;</span>: <span class="hljs-literal">True</span>},  <span class="hljs-comment"># set True for cosine similarity</span>
)

KNOWLEDGE_VECTOR_DATABASE = FAISS.from_documents(
    docs_processed, embedding_model, distance_strategy=DistanceStrategy.COSINE
)`,wrap:!1}}),yl=new w({props:{code:"JTIzJTIwZW1iZWQlMjBhJTIwdXNlciUyMHF1ZXJ5JTIwaW4lMjB0aGUlMjBzYW1lJTIwc3BhY2UlMEF1c2VyX3F1ZXJ5JTIwJTNEJTIwJTIySG93JTIwdG8lMjBjcmVhdGUlMjBhJTIwcGlwZWxpbmUlMjBvYmplY3QlM0YlMjIlMEFxdWVyeV92ZWN0b3IlMjAlM0QlMjBlbWJlZGRpbmdfbW9kZWwuZW1iZWRfcXVlcnkodXNlcl9xdWVyeSk=",highlighted:`<span class="hljs-comment"># embed a user query in the same space</span>
user_query = <span class="hljs-string">&quot;How to create a pipeline object?&quot;</span>
query_vector = embedding_model.embed_query(user_query)`,wrap:!1}}),ml=new w({props:{code:"aW1wb3J0JTIwcGFjbWFwJTBBaW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBaW1wb3J0JTIwcGxvdGx5LmV4cHJlc3MlMjBhcyUyMHB4JTBBJTBBZW1iZWRkaW5nX3Byb2plY3RvciUyMCUzRCUyMHBhY21hcC5QYUNNQVAobl9jb21wb25lbnRzJTNEMiUyQyUyMG5fbmVpZ2hib3JzJTNETm9uZSUyQyUyME1OX3JhdGlvJTNEMC41JTJDJTIwRlBfcmF0aW8lM0QyLjAlMkMlMjByYW5kb21fc3RhdGUlM0QxKSUwQSUwQWVtYmVkZGluZ3NfMmQlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjBsaXN0KEtOT1dMRURHRV9WRUNUT1JfREFUQUJBU0UuaW5kZXgucmVjb25zdHJ1Y3RfbihpZHglMkMlMjAxKSU1QjAlNUQpJTIwZm9yJTIwaWR4JTIwaW4lMjByYW5nZShsZW4oZG9jc19wcm9jZXNzZWQpKSUwQSU1RCUyMCUyQiUyMCU1QnF1ZXJ5X3ZlY3RvciU1RCUwQSUwQSUyMyUyMGZpdCUyMHRoZSUyMGRhdGElMjAoVGhlJTIwaW5kZXglMjBvZiUyMHRyYW5zZm9ybWVkJTIwZGF0YSUyMGNvcnJlc3BvbmRzJTIwdG8lMjB0aGUlMjBpbmRleCUyMG9mJTIwdGhlJTIwb3JpZ2luYWwlMjBkYXRhKSUwQWRvY3VtZW50c19wcm9qZWN0ZWQlMjAlM0QlMjBlbWJlZGRpbmdfcHJvamVjdG9yLmZpdF90cmFuc2Zvcm0obnAuYXJyYXkoZW1iZWRkaW5nc18yZCklMkMlMjBpbml0JTNEJTIycGNhJTIyKQ==",highlighted:`<span class="hljs-keyword">import</span> pacmap
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> plotly.express <span class="hljs-keyword">as</span> px

embedding_projector = pacmap.PaCMAP(n_components=<span class="hljs-number">2</span>, n_neighbors=<span class="hljs-literal">None</span>, MN_ratio=<span class="hljs-number">0.5</span>, FP_ratio=<span class="hljs-number">2.0</span>, random_state=<span class="hljs-number">1</span>)

embeddings_2d = [
    <span class="hljs-built_in">list</span>(KNOWLEDGE_VECTOR_DATABASE.index.reconstruct_n(idx, <span class="hljs-number">1</span>)[<span class="hljs-number">0</span>]) <span class="hljs-keyword">for</span> idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(docs_processed))
] + [query_vector]

<span class="hljs-comment"># fit the data (The index of transformed data corresponds to the index of the original data)</span>
documents_projected = embedding_projector.fit_transform(np.array(embeddings_2d), init=<span class="hljs-string">&quot;pca&quot;</span>)`,wrap:!1}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lMEElMjAlMjAlMjAlMjBzaXplJTNEJTIyc2l6ZV9jb2wlMjIlMkMlMEElMjAlMjAlMjAlMjBzeW1ib2wlM0QlMjJzeW1ib2wlMjIlMkMlMEElMjAlMjAlMjAlMjBjb2xvcl9kaXNjcmV0ZV9tYXAlM0QlN0IlMjJVc2VyJTIwcXVlcnklMjIlM0ElMjAlMjJibGFjayUyMiU3RCUyQyUwQSUyMCUyMCUyMCUyMHdpZHRoJTNEMTAwMCUyQyUwQSUyMCUyMCUyMCUyMGhlaWdodCUzRDcwMCUyQyUwQSklMEFmaWcudXBkYXRlX3RyYWNlcyhtYXJrZXIlM0RkaWN0KG9wYWNpdHklM0QxJTJDJTIwbGluZSUzRGRpY3Qod2lkdGglM0QwJTJDJTIwY29sb3IlM0QlMjJEYXJrU2xhdGVHcmV5JTIyKSklMkMlMjBzZWxlY3RvciUzRGRpY3QobW9kZSUzRCUyMm1hcmtlcnMlMjIpKSUwQWZpZy51cGRhdGVfbGF5b3V0KCUwQSUyMCUyMCUyMCUyMGxlZ2VuZF90aXRsZV90ZXh0JTNEJTIyJTNDYiUzRUNodW5rJTIwc291cmNlJTNDJTJGYiUzRSUyMiUyQyUwQSUyMCUyMCUyMCUyMHRpdGxlJTNEJTIyJTNDYiUzRTJEJTIwUHJvamVjdGlvbiUyMG9mJTIwQ2h1bmslMjBFbWJlZGRpbmdzJTIwdmlhJTIwUGFDTUFQJTNDJTJGYiUzRSUyMiUyQyUwQSklMEFmaWcuc2hvdygp",highlighted:`df = pd.DataFrame.from_dict(
    [
        {
            <span class="hljs-string">&quot;x&quot;</span>: documents_projected[i, <span class="hljs-number">0</span>],
            <span class="hljs-string">&quot;y&quot;</span>: documents_projected[i, <span class="hljs-number">1</span>],
            <span class="hljs-string">&quot;source&quot;</span>: docs_processed[i].metadata[<span class="hljs-string">&quot;source&quot;</span>].split(<span class="hljs-string">&quot;/&quot;</span>)[<span class="hljs-number">1</span>],
            <span class="hljs-string">&quot;extract&quot;</span>: docs_processed[i].page_content[:<span class="hljs-number">100</span>] + <span class="hljs-string">&quot;...&quot;</span>,
            <span class="hljs-string">&quot;symbol&quot;</span>: <span class="hljs-string">&quot;circle&quot;</span>,
            <span class="hljs-string">&quot;size_col&quot;</span>: <span class="hljs-number">4</span>,
        }
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(docs_processed))
    ]
    + [
        {
            <span class="hljs-string">&quot;x&quot;</span>: documents_projected[-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>],
            <span class="hljs-string">&quot;y&quot;</span>: documents_projected[-<span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
            <span class="hljs-string">&quot;source&quot;</span>: <span class="hljs-string">&quot;User query&quot;</span>,
            <span class="hljs-string">&quot;extract&quot;</span>: user_query,
            <span class="hljs-string">&quot;size_col&quot;</span>: <span class="hljs-number">100</span>,
            <span class="hljs-string">&quot;symbol&quot;</span>: <span class="hljs-string">&quot;star&quot;</span>,
        }
    ]
)

<span class="hljs-comment"># visualize the embedding</span>
fig = px.scatter(
    df,
    x=<span class="hljs-string">&quot;x&quot;</span>,
    y=<span class="hljs-string">&quot;y&quot;</span>,
    color=<span class="hljs-string">&quot;source&quot;</span>,
    hover_data=<span class="hljs-string">&quot;extract&quot;</span>,
    size=<span class="hljs-string">&quot;size_col&quot;</span>,
    symbol=<span class="hljs-string">&quot;symbol&quot;</span>,
    color_discrete_map={<span class="hljs-string">&quot;User query&quot;</span>: <span class="hljs-string">&quot;black&quot;</span>},
    width=<span class="hljs-number">1000</span>,
    height=<span class="hljs-number">700</span>,
)
fig.update_traces(marker=<span class="hljs-built_in">dict</span>(opacity=<span class="hljs-number">1</span>, line=<span class="hljs-built_in">dict</span>(width=<span class="hljs-number">0</span>, color=<span class="hljs-string">&quot;DarkSlateGrey&quot;</span>)), selector=<span class="hljs-built_in">dict</span>(mode=<span class="hljs-string">&quot;markers&quot;</span>))
fig.update_layout(
    legend_title_text=<span class="hljs-string">&quot;&lt;b&gt;Chunk source&lt;/b&gt;&quot;</span>,
    title=<span class="hljs-string">&quot;&lt;b&gt;2D Projection of Chunk Embeddings via PaCMAP&lt;/b&gt;&quot;</span>,
)
fig.show()`,wrap:!1}}),Bl=new w({props:{code:"cHJpbnQoZiUyMiU1Q25TdGFydGluZyUyMHJldHJpZXZhbCUyMGZvciUyMCU3QnVzZXJfcXVlcnklM0QlN0QuLi4lMjIpJTBBcmV0cmlldmVkX2RvY3MlMjAlM0QlMjBLTk9XTEVER0VfVkVDVE9SX0RBVEFCQVNFLnNpbWlsYXJpdHlfc2VhcmNoKHF1ZXJ5JTNEdXNlcl9xdWVyeSUyQyUyMGslM0Q1KSUwQXByaW50KCUyMiU1Q24lM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0QlM0RUb3AlMjBkb2N1bWVudCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUyMiklMEFwcmludChyZXRyaWV2ZWRfZG9jcyU1QjAlNUQucGFnZV9jb250ZW50KSUwQXByaW50KCUyMiUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRE1ldGFkYXRhJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTIyKSUwQXByaW50KHJldHJpZXZlZF9kb2NzJTVCMCU1RC5tZXRhZGF0YSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\\nStarting retrieval for <span class="hljs-subst">{user_query=}</span>...&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>retrieved_docs = KNOWLEDGE_VECTOR_DATABASE.similarity_search(query=user_query, k=<span class="hljs-number">5</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\\n==================================Top document==================================&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(retrieved_docs[<span class="hljs-number">0</span>].page_content)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;==================================Metadata==================================&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(retrieved_docs[<span class="hljs-number">0</span>].metadata)`,wrap:!1}}),Il=new T({props:{title:"2. 阅读器- LLM 💬",local:"2-阅读器--llm-",headingTag:"h1"}}),Vl=new T({props:{title:"2.1. 阅读器模型",local:"21-阅读器模型",headingTag:"h3"}}),Hl=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBaW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUyQyUyMEF1dG9Nb2RlbEZvckNhdXNhbExNJTJDJTIwQml0c0FuZEJ5dGVzQ29uZmlnJTBBJTBBUkVBREVSX01PREVMX05BTUUlMjAlM0QlMjAlMjJIdWdnaW5nRmFjZUg0JTJGemVwaHlyLTdiLWJldGElMjIlMEElMEFibmJfY29uZmlnJTIwJTNEJTIwQml0c0FuZEJ5dGVzQ29uZmlnKCUwQSUyMCUyMCUyMCUyMGxvYWRfaW5fNGJpdCUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBibmJfNGJpdF91c2VfZG91YmxlX3F1YW50JTNEVHJ1ZSUyQyUwQSUyMCUyMCUyMCUyMGJuYl80Yml0X3F1YW50X3R5cGUlM0QlMjJuZjQlMjIlMkMlMEElMjAlMjAlMjAlMjBibmJfNGJpdF9jb21wdXRlX2R0eXBlJTNEdG9yY2guYmZsb2F0MTYlMkMlMEEpJTBBbW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JDYXVzYWxMTS5mcm9tX3ByZXRyYWluZWQoUkVBREVSX01PREVMX05BTUUlMkMlMjBxdWFudGl6YXRpb25fY29uZmlnJTNEYm5iX2NvbmZpZyklMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZChSRUFERVJfTU9ERUxfTkFNRSklMEElMEFSRUFERVJfTExNJTIwJTNEJTIwcGlwZWxpbmUoJTBBJTIwJTIwJTIwJTIwbW9kZWwlM0Rtb2RlbCUyQyUwQSUyMCUyMCUyMCUyMHRva2VuaXplciUzRHRva2VuaXplciUyQyUwQSUyMCUyMCUyMCUyMHRhc2slM0QlMjJ0ZXh0LWdlbmVyYXRpb24lMjIlMkMlMEElMjAlMjAlMjAlMjBkb19zYW1wbGUlM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwdGVtcGVyYXR1cmUlM0QwLjIlMkMlMEElMjAlMjAlMjAlMjByZXBldGl0aW9uX3BlbmFsdHklM0QxLjElMkMlMEElMjAlMjAlMjAlMjByZXR1cm5fZnVsbF90ZXh0JTNERmFsc2UlMkMlMEElMjAlMjAlMjAlMjBtYXhfbmV3X3Rva2VucyUzRDUwMCUyQyUwQSk=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig

READER_MODEL_NAME = <span class="hljs-string">&quot;HuggingFaceH4/zephyr-7b-beta&quot;</span>

bnb_config = BitsAndBytesConfig(
    load_in_4bit=<span class="hljs-literal">True</span>,
    bnb_4bit_use_double_quant=<span class="hljs-literal">True</span>,
    bnb_4bit_quant_type=<span class="hljs-string">&quot;nf4&quot;</span>,
    bnb_4bit_compute_dtype=torch.bfloat16,
)
model = AutoModelForCausalLM.from_pretrained(READER_MODEL_NAME, quantization_config=bnb_config)
tokenizer = AutoTokenizer.from_pretrained(READER_MODEL_NAME)

READER_LLM = pipeline(
    model=model,
    tokenizer=tokenizer,
    task=<span class="hljs-string">&quot;text-generation&quot;</span>,
    do_sample=<span class="hljs-literal">True</span>,
    temperature=<span class="hljs-number">0.2</span>,
    repetition_penalty=<span class="hljs-number">1.1</span>,
    return_full_text=<span class="hljs-literal">False</span>,
    max_new_tokens=<span class="hljs-number">500</span>,
)`,wrap:!1}}),ql=new w({props:{code:"UkVBREVSX0xMTSglMjJXaGF0JTIwaXMlMjA0JTJCNCUzRiUyMEFuc3dlciUzQSUyMik=",highlighted:'READER_LLM(<span class="hljs-string">&quot;What is 4+4? Answer:&quot;</span>)',wrap:!1}}),Pl=new T({props:{title:"2.2. 提示(Prompt)",local:"22-提示prompt",headingTag:"h3"}}),zl=new w({props:{code:"cHJvbXB0X2luX2NoYXRfZm9ybWF0JTIwJTNEJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIycm9sZSUyMiUzQSUyMCUyMnN5c3RlbSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmNvbnRlbnQlMjIlM0ElMjAlMjIlMjIlMjJVc2luZyUyMHRoZSUyMGluZm9ybWF0aW9uJTIwY29udGFpbmVkJTIwaW4lMjB0aGUlMjBjb250ZXh0JTJDJTBBZ2l2ZSUyMGElMjBjb21wcmVoZW5zaXZlJTIwYW5z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FkZF9nZW5lcmF0aW9uX3Byb21wdCUzRFRydWUlMEEpJTBBcHJpbnQoUkFHX1BST01QVF9URU1QTEFURSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>prompt_in_chat_format = [
<span class="hljs-meta">... </span>    {
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;&quot;&quot;Using the information contained in the context,
<span class="hljs-meta">... </span>give a comprehensive answer to the question.
<span class="hljs-meta">... </span>Respond only to the question asked, response should be concise and relevant to the question.
<span class="hljs-meta">... </span>Provide the number of the source document when relevant.
<span class="hljs-meta">... </span>If the answer cannot be deduced from the context, do not give an answer.&quot;&quot;&quot;</span>,
<span class="hljs-meta">... </span>    },
<span class="hljs-meta">... </span>    {
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;&quot;&quot;Context:
<span class="hljs-meta">... </span>{context}
<span class="hljs-meta">... </span>---
<span class="hljs-meta">... </span>Now here is the question you need to answer.

<span class="hljs-meta">... </span>Question: {question}&quot;&quot;&quot;</span>,
<span class="hljs-meta">... </span>    },
<span class="hljs-meta">... </span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>RAG_PROMPT_TEMPLATE = tokenizer.apply_chat_template(
<span class="hljs-meta">... </span>    prompt_in_chat_format, tokenize=<span class="hljs-literal">False</span>, add_generation_prompt=<span class="hljs-literal">True</span>
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(RAG_PROMPT_TEMPLATE)`,wrap:!1}}),Sl=new w({props:{code:"cmV0cmlldmVkX2RvY3NfdGV4dCUyMCUzRCUyMCU1QmRvYy5wYWdlX2NvbnRlbnQlMjBmb3IlMjBkb2MlMjBpbiUyMHJldHJpZXZlZF9kb2NzJTVEJTIwJTIwJTIzJTIwd2UlMjBvbmx5JTIwbmVlZCUyMHRoZSUyMHRleHQlMjBvZiUyMHRoZSUyMGRvY3VtZW50cyUwQWNvbnRleHQlMjAlM0QlMjAlMjIlNUNuRXh0cmFjdGVkJTIwZG9jdW1lbnRzJTNBJTVDbiUyMiUwQWNvbnRleHQlMjAlMkIlM0QlMjAlMjIlMjIuam9pbiglNUJmJTIyRG9jdW1lbnQlMjAlN0JzdHIoaSklN0QlM0ElM0ElM0ElNUNuJTIyJTIwJTJCJTIwZG9jJTIwZm9yJTIwaSUyQyUyMGRvYyUyMGluJTIwZW51bWVyYXRlKHJldHJpZXZlZF9kb2NzX3RleHQpJTVEKSUwQSUwQWZpbmFsX3Byb21wdCUyMCUzRCUyMFJBR19QUk9NUFRfVEVNUExBVEUuZm9ybWF0KHF1ZXN0aW9uJTNEJTIySG93JTIwdG8lMjBjcmVhdGUlMjBhJTIwcGlwZWxpbmUlMjBvYmplY3QlM0YlMjIlMkMlMjBjb250ZXh0JTNEY29udGV4dCklMEElMEElMjMlMjBSZWRhY3QlMjBhbiUyMGFuc3dlciUwQWFuc3dlciUyMCUzRCUyMFJFQURFUl9MTE0oZmluYWxfcHJvbXB0KSU1QjAlNUQlNUIlMjJnZW5lcmF0ZWRfdGV4dCUyMiU1RCUwQXByaW50KGFuc3dlcik=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>retrieved_docs_text = [doc.page_content <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> retrieved_docs]  <span class="hljs-comment"># we only need the text of the documents</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>context = <span class="hljs-string">&quot;\\nExtracted documents:\\n&quot;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>context += <span class="hljs-string">&quot;&quot;</span>.join([<span class="hljs-string">f&quot;Document <span class="hljs-subst">{<span class="hljs-built_in">str</span>(i)}</span>:::\\n&quot;</span> + doc <span class="hljs-keyword">for</span> i, doc <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(retrieved_docs_text)])

<span class="hljs-meta">&gt;&gt;&gt; </span>final_prompt = RAG_PROMPT_TEMPLATE.<span class="hljs-built_in">format</span>(question=<span class="hljs-string">&quot;How to create a pipeline object?&quot;</span>, context=context)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Redact an answer</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>answer = READER_LLM(final_prompt)[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;generated_text&quot;</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(answer)`,wrap:!1}}),Yl=new T({props:{title:"2.3. 重排序(rerank)",local:"23-重排序rerank",headingTag:"h3"}}),lt=new w({props:{code:"ZnJvbSUyMHJhZ2F0b3VpbGxlJTIwaW1wb3J0JTIwUkFHUHJldHJhaW5lZE1vZGVsJTBBJTBBUkVSQU5LRVIlMjAlM0QlMjBSQUdQcmV0cmFpbmVkTW9kZWwuZnJvbV9wcmV0cmFpbmVkKCUyMmNvbGJlcnQtaXIlMkZjb2xiZXJ0djIuMCUyMik=",highlighted:`<span class="hljs-keyword">from</span> ragatouille <span class="hljs-keyword">import</span> RAGPretrainedModel

RERANKER = RAGPretrainedModel.from_pretrained(<span class="hljs-string">&quot;colbert-ir/colbertv2.0&quot;</span>)`,wrap:!1}}),tt=new T({props:{title:"3. 集成所有组件",local:"3-集成所有组件",headingTag:"h1"}}),et=new w({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMFBpcGVsaW5lJTBBJTBBJTBBZGVmJTIwYW5zd2VyX3dpdGhfcmFnKCUwQSUyMCUyMCUyMCUyMHF1ZXN0aW9uJTNBJTIwc3RyJTJDJTBBJTIwJTIwJTIwJTIwbGxtJTNBJTIwUGlwZWxpbmUlMkMlMEElMjAlMjAlMjAlMjBrbm93bGVkZ2VfaW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IlMjAlM0QlMjBsbG0oZmluYWxfcHJvbXB0KSU1QjAlNUQlNUIlMjJnZW5lcmF0ZWRfdGV4dCUyMiU1RCUwQSUwQSUyMCUyMCUyMCUyMHJldHVybiUyMGFuc3dlciUyQyUyMHJlbGV2YW50X2RvY3M=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Pipeline


<span class="hljs-keyword">def</span> <span class="hljs-title function_">answer_with_rag</span>(<span class="hljs-params">
    question: <span class="hljs-built_in">str</span>,
    llm: Pipeline,
    knowledge_index: FAISS,
    reranker: <span class="hljs-type">Optional</span>[RAGPretrainedModel] = <span class="hljs-literal">None</span>,
    num_retrieved_docs: <span class="hljs-built_in">int</span> = <span class="hljs-number">30</span>,
    num_docs_final: <span class="hljs-built_in">int</span> = <span class="hljs-number">5</span>,
</span>) -&gt; <span class="hljs-type">Tuple</span>[<span class="hljs-built_in">str</span>, <span class="hljs-type">List</span>[LangchainDocument]]:
    <span class="hljs-comment"># Gather documents with retriever</span>
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;=&gt; Retrieving documents...&quot;</span>)
    relevant_docs = knowledge_index.similarity_search(query=question, k=num_retrieved_docs)
    relevant_docs = [doc.page_content <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> relevant_docs]  <span class="hljs-comment"># keep only the text</span>

    <span class="hljs-comment"># Optionally rerank results</span>
    <span class="hljs-keyword">if</span> reranker:
        <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;=&gt; Reranking documents...&quot;</span>)
        relevant_docs = reranker.rerank(question, relevant_docs, k=num_docs_final)
        relevant_docs = [doc[<span class="hljs-string">&quot;content&quot;</span>] <span class="hljs-keyword">for</span> doc <span class="hljs-keyword">in</span> relevant_docs]

    relevant_docs = relevant_docs[:num_docs_final]

    <span class="hljs-comment"># Build the final prompt</span>
    context = <span class="hljs-string">&quot;\\nExtracted documents:\\n&quot;</span>
    context += <span class="hljs-string">&quot;&quot;</span>.join([<span class="hljs-string">f&quot;Document <span class="hljs-subst">{<span class="hljs-built_in">str</span>(i)}</span>:::\\n&quot;</span> + doc <span class="hljs-keyword">for</span> i, doc <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(relevant_docs)])

    final_prompt = RAG_PROMPT_TEMPLATE.<span class="hljs-built_in">format</span>(question=question, context=context)

    <span class="hljs-comment"># Redact an answer</span>
    <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;=&gt; Generating answer...&quot;</span>)
    answer = llm(final_prompt)[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;generated_text&quot;</span>]

    <span class="hljs-keyword">return</span> answer, relevant_docs`,wrap:!1}}),nt=new w({props:{code:"cXVlc3Rpb24lMjAlM0QlMjAlMjJob3clMjB0byUyMGNyZWF0ZSUyMGElMjBwaXBlbGluZSUyMG9iamVjdCUzRiUyMiUwQSUwQWFuc3dlciUyQyUyMHJlbGV2YW50X2RvY3MlMjAlM0QlMjBhbnN3ZXJfd2l0aF9yYWcocXVlc3Rpb24lMkMlMjBSRUFERVJfTExNJTJDJTIwS05PV0xFREdFX1ZFQ1RPUl9EQVRBQkFTRSUyQyUyMHJlcmFua2VyJTNEUkVSQU5LRVIp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>question = <span class="hljs-string">&quot;how to create a pipeline object?&quot;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>answer, relevant_docs = answer_with_rag(question, READER_LLM, KNOWLEDGE_VECTOR_DATABASE, reranker=RERANKER)`,wrap:!1}}),At=new w({props:{code:"cHJpbnQoJTIyJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEQW5zd2VyJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTIyKSUwQXByaW50KGYlMjIlN0JhbnN3ZXIlN0QlMjIpJTBBcHJpbnQoJTIyJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEJTNEU291cmNlJTIwZG9jcyUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUzRCUyMiklMEFmb3IlMjBpJTJDJTIwZG9jJTIwaW4lMjBlbnVtZXJhdGUocmVsZXZhbnRfZG9jcyklM0ElMEElMjAlMjAlMjAlMjBwcmludChmJTIyRG9jdW1lbnQlMjAlN0JpJTdELS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tJTIyKSUwQSUyMCUyMCUyMCUyMHByaW50KGRvYyk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;==================================Answer==================================&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;<span class="hljs-subst">{answer}</span>&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;==================================Source docs==================================&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i, doc <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(relevant_docs):
<span class="hljs-meta">... </span>    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Document <span class="hljs-subst">{i}</span>------------------------------------------------------------&quot;</span>)
<span class="hljs-meta">... </span>    <span class="hljs-built_in">print</span>(doc)`,wrap:!1}}),pt=new T({props:{title:"进一步探索 🗺️",local:"进一步探索-",headingTag:"h1"}}),Ut=new T({props:{title:"设置评估流水线",local:"设置评估流水线",headingTag:"h3"}}),jt=new T({props:{title:"改进检索器",local:"改进检索器",headingTag:"h3"}}),Jt=new T({props:{title:"改进阅读器",local:"改进阅读器",headingTag:"h3"}}),Rt=new ma({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/zh-CN/advanced_rag.md"}}),{c(){f=A("meta"),Bt=n(),bt=A("p"),vt=n(),p(J.$$.fragment),It=n(),p(u.$$.fragment),gt=n(),y=A("p"),y.innerHTML=Hs,Et=n(),m=A("p"),m.textContent=qs,Kt=n(),d=A("p"),d.innerHTML=Ps,Vt=n(),R=A("p"),R.textContent=Gs,Nt=n(),C=A("img"),Xt=n(),h=A("blockquote"),h.innerHTML=zs,kt=n(),b=A("p"),b.textContent=Zs,Qt=n(),F=A("p"),F.innerHTML=Ls,xt=n(),p(B.$$.fragment),Ht=n(),p(v.$$.fragment),qt=n(),p(I.$$.fragment),Pt=n(),p(g.$$.fragment),Gt=n(),p(E.$$.fragment),Wt=n(),p(K.$$.fragment),zt=n(),p(V.$$.fragment),Zt=n(),N=A("p"),N.innerHTML=Ss,Lt=n(),X=A("p"),X.textContent=Ds,St=n(),k=A("p"),k.innerHTML=Ys,Dt=n(),Q=A("p"),Q.innerHTML=_s,Yt=n(),x=A("p"),x.innerHTML=Os,_t=n(),H=A("ul"),H.innerHTML=$s,Ot=n(),q=A("blockquote"),q.innerHTML=ln,$t=n(),p(P.$$.fragment),le=n(),G=A("ul"),G.innerHTML=tn,te=n(),W=A("p"),W.innerHTML=en,ee=n(),z=A("ul"),z.innerHTML=sn,se=n(),Z=A("blockquote"),Z.innerHTML=nn,ne=n(),L=A("p"),L.innerHTML=an,ae=n(),S=A("ul"),S.innerHTML=An,Ae=n(),p(D.$$.fragment),ie=n(),Y=A("p"),Y.innerHTML=on,oe=n(),_=A("p"),_.textContent=pn,pe=n(),p(O.$$.fragment),Me=n(),$=A("pre"),$.textContent=Mn,Ue=n(),ll=A("p"),ll.innerHTML=Un,re=n(),tl=A("ul"),tl.innerHTML=rn,je=n(),p(el.$$.fragment),ce=n(),ht=A("img"),we=n(),sl=A("p"),sl.textContent=cn,Te=n(),nl=A("p"),nl.textContent=wn,fe=n(),al=A("p"),al.innerHTML=Tn,Je=n(),Al=A("p"),Al.textContent=fn,ue=n(),il=A("p"),il.textContent=Jn,ye=n(),ol=A("p"),ol.textContent=un,me=n(),pl=A("p"),pl.textContent=yn,de=n(),Ml=A("p"),Ml.innerHTML=mn,Re=n(),Ul=A("p"),Ul.textContent=dn,Ce=n(),rl=A("p"),rl.innerHTML=Rn,he=n(),jl=A("ul"),jl.innerHTML=Cn,be=n(),cl=A("p"),cl.innerHTML=hn,Fe=n(),wl=A("p"),wl.innerHTML=bn,Be=n(),Tl=A("p"),Tl.textContent=Fn,ve=n(),p(fl.$$.fragment),Ie=n(),Jl=A("p"),Jl.textContent=Bn,ge=n(),ul=A("p"),ul.innerHTML=vn,Ee=n(),p(yl.$$.fragment),Ke=n(),p(ml.$$.fragment),Ve=n(),p(dl.$$.fragment),Ne=n(),Rl=A("img"),Xe=n(),Cl=A("p"),Cl.textContent=gn,ke=n(),hl=A("p"),hl.innerHTML=En,Qe=n(),bl=A("p"),bl.innerHTML=Kn,xe=n(),Fl=A("p"),Fl.textContent=Vn,He=n(),p(Bl.$$.fragment),qe=n(),vl=A("pre"),vl.textContent=Nn,Pe=n(),p(Il.$$.fragment),Ge=n(),gl=A("p"),gl.innerHTML=Xn,We=n(),El=A("p"),El.textContent=kn,ze=n(),Kl=A("ol"),Kl.innerHTML=Qn,Ze=n(),p(Vl.$$.fragment),Le=n(),Nl=A("p"),Nl.textContent=xn,Se=n(),Xl=A("ul"),Xl.innerHTML=Hn,De=n(),kl=A("p"),kl.innerHTML=qn,Ye=n(),Ql=A("p"),Ql.innerHTML=Pn,_e=n(),xl=A("p"),xl.textContent=Gn,Oe=n(),p(Hl.$$.fragment),$e=n(),p(ql.$$.fragment),ls=n(),p(Pl.$$.fragment),ts=n(),Gl=A("p"),Gl.textContent=Wn,es=n(),Wl=A("p"),Wl.textContent=zn,ss=n(),p(zl.$$.fragment),ns=n(),Zl=A("pre"),Zl.textContent=Zn,as=n(),Ll=A("p"),Ll.textContent=Ln,As=n(),p(Sl.$$.fragment),is=n(),Dl=A("pre"),Dl.textContent=Sn,os=n(),p(Yl.$$.fragment),ps=n(),_l=A("p"),_l.innerHTML=Dn,Ms=n(),Ol=A("p"),Ol.innerHTML=Yn,Us=n(),$l=A("p"),$l.innerHTML=_n,rs=n(),p(lt.$$.fragment),js=n(),p(tt.$$.fragment),cs=n(),p(et.$$.fragment),ws=n(),st=A("p"),st.textContent=On,Ts=n(),p(nt.$$.fragment),fs=n(),at=A("pre"),at.textContent=$n,Js=n(),p(At.$$.fragment),us=n(),it=A("pre"),it.textContent=la,ys=n(),ot=A("p"),ot.textContent=ta,ms=n(),p(pt.$$.fragment),ds=n(),Mt=A("p"),Mt.textContent=ea,Rs=n(),p(Ut.$$.fragment),Cs=n(),rt=A("ul"),rt.innerHTML=sa,hs=n(),p(jt.$$.fragment),bs=n(),ct=A("p"),ct.innerHTML=na,Fs=n(),wt=A("ul"),wt.innerHTML=aa,Bs=n(),Tt=A("p"),Tt.innerHTML=Aa,vs=n(),ft=A("ul"),ft.innerHTML=ia,Is=n(),p(Jt.$$.fragment),gs=n(),ut=A("p"),ut.innerHTML=oa,Es=n(),yt=A("ul"),yt.innerHTML=pa,Ks=n(),mt=A("p"),mt.innerHTML=Ma,Vs=n(),dt=A("ul"),dt.innerHTML=Ua,Ns=n(),p(Rt.$$.fragment),Xs=n(),Ft=A("p"),this.h()},l(l){const t=Ja("svelte-u9bgzb",document.head);f=i(t,"META",{name:!0,content:!0}),t.forEach(e),Bt=a(l),bt=i(l,"P",{}),ra(bt).forEach(e),vt=a(l),M(J.$$.fragment,l),It=a(l),M(u.$$.fragment,l),gt=a(l),y=i(l,"P",{"data-svelte-h":!0}),o(y)!=="svelte-1ouqjzx"&&(y.innerHTML=Hs),Et=a(l),m=i(l,"P",{"data-svelte-h":!0}),o(m)!=="svelte-1y8o57u"&&(m.textContent=qs),Kt=a(l),d=i(l,"P",{"data-svelte-h":!0}),o(d)!=="svelte-cgsx28"&&(d.innerHTML=Ps),Vt=a(l),R=i(l,"P",{"data-svelte-h":!0}),o(R)!=="svelte-45g62z"&&(R.textContent=Gs),Nt=a(l),C=i(l,"IMG",{src:!0,height:!0}),Xt=a(l),h=i(l,"BLOCKQUOTE",{"data-svelte-h":!0}),o(h)!=="svelte-oiyp3w"&&(h.innerHTML=zs),kt=a(l),b=i(l,"P",{"data-svelte-h":!0}),o(b)!=="svelte-1c77kj2"&&(b.textContent=Zs),Qt=a(l),F=i(l,"P",{"data-svelte-h":!0}),o(F)!=="svelte-16f9kjd"&&(F.innerHTML=Ls),xt=a(l),M(B.$$.fragment,l),Ht=a(l),M(v.$$.fragment,l),qt=a(l),M(I.$$.fragment,l),Pt=a(l),M(g.$$.fragment,l),Gt=a(l),M(E.$$.fragment,l),Wt=a(l),M(K.$$.fragment,l),zt=a(l),M(V.$$.fragment,l),Zt=a(l),N=i(l,"P",{"data-svelte-h":!0}),o(N)!=="svelte-ql0xxw"&&(N.innerHTML=Ss),Lt=a(l),X=i(l,"P",{"data-svelte-h":!0}),o(X)!=="svelte-79k71u"&&(X.textContent=Ds),St=a(l),k=i(l,"P",{"data-svelte-h":!0}),o(k)!=="svelte-gfu2zq"&&(k.innerHTML=Ys),Dt=a(l),Q=i(l,"P",{"data-svelte-h":!0}),o(Q)!=="svelte-c8pzbv"&&(Q.innerHTML=_s),Yt=a(l),x=i(l,"P",{"data-svelte-h":!0}),o(x)!=="svelte-1ylwvta"&&(x.innerHTML=Os),_t=a(l),H=i(l,"UL",{"data-svelte-h":!0}),o(H)!=="svelte-1ynnvg9"&&(H.innerHTML=$s),Ot=a(l),q=i(l,"BLOCKQUOTE",{"data-svelte-h":!0}),o(q)!=="svelte-1jq112o"&&(q.innerHTML=ln),$t=a(l),M(P.$$.fragment,l),le=a(l),G=i(l,"UL",{"data-svelte-h":!0}),o(G)!=="svelte-18g8ey5"&&(G.innerHTML=tn),te=a(l),W=i(l,"P",{"data-svelte-h":!0}),o(W)!=="svelte-uaz899"&&(W.innerHTML=en),ee=a(l),z=i(l,"UL",{"data-svelte-h":!0}),o(z)!=="svelte-1j9j687"&&(z.innerHTML=sn),se=a(l),Z=i(l,"BLOCKQUOTE",{"data-svelte-h":!0}),o(Z)!=="svelte-nls6mz"&&(Z.innerHTML=nn),ne=a(l),L=i(l,"P",{"data-svelte-h":!0}),o(L)!=="svelte-164rw2z"&&(L.innerHTML=an),ae=a(l),S=i(l,"UL",{"data-svelte-h":!0}),o(S)!=="svelte-j32ek"&&(S.innerHTML=An),Ae=a(l),M(D.$$.fragment,l),ie=a(l),Y=i(l,"P",{"data-svelte-h":!0}),o(Y)!=="svelte-y33335"&&(Y.innerHTML=on),oe=a(l),_=i(l,"P",{"data-svelte-h":!0}),o(_)!=="svelte-1yay3kj"&&(_.textContent=pn),pe=a(l),M(O.$$.fragment,l),Me=a(l),$=i(l,"PRE",{"data-svelte-h":!0}),o($)!=="svelte-h6hpdk"&&($.textContent=Mn),Ue=a(l),ll=i(l,"P",{"data-svelte-h":!0}),o(ll)!=="svelte-1k6h3dv"&&(ll.innerHTML=Un),re=a(l),tl=i(l,"UL",{"data-svelte-h":!0}),o(tl)!=="svelte-j2u3py"&&(tl.innerHTML=rn),je=a(l),M(el.$$.fragment,l),ce=a(l),ht=i(l,"IMG",{src:!0}),we=a(l),sl=i(l,"P",{"data-svelte-h":!0}),o(sl)!=="svelte-1su99xg"&&(sl.textContent=cn),Te=a(l),nl=i(l,"P",{"data-svelte-h":!0}),o(nl)!=="svelte-1ybi5r3"&&(nl.textContent=wn),fe=a(l),al=i(l,"P",{"data-svelte-h":!0}),o(al)!=="svelte-1q1eqv0"&&(al.innerHTML=Tn),Je=a(l),Al=i(l,"P",{"data-svelte-h":!0}),o(Al)!=="svelte-4l6ji"&&(Al.textContent=fn),ue=a(l),il=i(l,"P",{"data-svelte-h":!0}),o(il)!=="svelte-120j1u7"&&(il.textContent=Jn),ye=a(l),ol=i(l,"P",{"data-svelte-h":!0}),o(ol)!=="svelte-187cxhr"&&(ol.textContent=un),me=a(l),pl=i(l,"P",{"data-svelte-h":!0}),o(pl)!=="svelte-13r1nip"&&(pl.textContent=yn),de=a(l),Ml=i(l,"P",{"data-svelte-h":!0}),o(Ml)!=="svelte-mw3wdi"&&(Ml.innerHTML=mn),Re=a(l),Ul=i(l,"P",{"data-svelte-h":!0}),o(Ul)!=="svelte-zgqoei"&&(Ul.textContent=dn),Ce=a(l),rl=i(l,"P",{"data-svelte-h":!0}),o(rl)!=="svelte-b66gqb"&&(rl.innerHTML=Rn),he=a(l),jl=i(l,"UL",{"data-svelte-h":!0}),o(jl)!=="svelte-1wwyqqn"&&(jl.innerHTML=Cn),be=a(l),cl=i(l,"P",{"data-svelte-h":!0}),o(cl)!=="svelte-1jcvwk2"&&(cl.innerHTML=hn),Fe=a(l),wl=i(l,"P",{"data-svelte-h":!0}),o(wl)!=="svelte-ibu9nm"&&(wl.innerHTML=bn),Be=a(l),Tl=i(l,"P",{"data-svelte-h":!0}),o(Tl)!=="svelte-fspy7m"&&(Tl.textContent=Fn),ve=a(l),M(fl.$$.fragment,l),Ie=a(l),Jl=i(l,"P",{"data-svelte-h":!0}),o(Jl)!=="svelte-1wro3yx"&&(Jl.textContent=Bn),ge=a(l),ul=i(l,"P",{"data-svelte-h":!0}),o(ul)!=="svelte-17l8q8z"&&(ul.innerHTML=vn),Ee=a(l),M(yl.$$.fragment,l),Ke=a(l),M(ml.$$.fragment,l),Ve=a(l),M(dl.$$.fragment,l),Ne=a(l),Rl=i(l,"IMG",{src:!0,height:!0}),Xe=a(l),Cl=i(l,"P",{"data-svelte-h":!0}),o(Cl)!=="svelte-1k4v1uw"&&(Cl.textContent=gn),ke=a(l),hl=i(l,"P",{"data-svelte-h":!0}),o(hl)!=="svelte-1nzosig"&&(hl.innerHTML=En),Qe=a(l),bl=i(l,"P",{"data-svelte-h":!0}),o(bl)!=="svelte-1mlsch3"&&(bl.innerHTML=Kn),xe=a(l),Fl=i(l,"P",{"data-svelte-h":!0}),o(Fl)!=="svelte-1l1mro3"&&(Fl.textContent=Vn),He=a(l),M(Bl.$$.fragment,l),qe=a(l),vl=i(l,"PRE",{"data-svelte-h":!0}),o(vl)!=="svelte-1q4z1pg"&&(vl.textContent=Nn),Pe=a(l),M(Il.$$.fragment,l),Ge=a(l),gl=i(l,"P",{"data-svelte-h":!0}),o(gl)!=="svelte-1jbxnsj"&&(gl.innerHTML=Xn),We=a(l),El=i(l,"P",{"data-svelte-h":!0}),o(El)!=="svelte-15oxcnc"&&(El.textContent=kn),ze=a(l),Kl=i(l,"OL",{"data-svelte-h":!0}),o(Kl)!=="svelte-1x6zhcr"&&(Kl.innerHTML=Qn),Ze=a(l),M(Vl.$$.fragment,l),Le=a(l),Nl=i(l,"P",{"data-svelte-h":!0}),o(Nl)!=="svelte-jar50q"&&(Nl.textContent=xn),Se=a(l),Xl=i(l,"UL",{"data-svelte-h":!0}),o(Xl)!=="svelte-btzwg9"&&(Xl.innerHTML=Hn),De=a(l),kl=i(l,"P",{"data-svelte-h":!0}),o(kl)!=="svelte-1lm1kj0"&&(kl.innerHTML=qn),Ye=a(l),Ql=i(l,"P",{"data-svelte-h":!0}),o(Ql)!=="svelte-me1why"&&(Ql.innerHTML=Pn),_e=a(l),xl=i(l,"P",{"data-svelte-h":!0}),o(xl)!=="svelte-6joo08"&&(xl.textContent=Gn),Oe=a(l),M(Hl.$$.fragment,l),$e=a(l),M(ql.$$.fragment,l),ls=a(l),M(Pl.$$.fragment,l),ts=a(l),Gl=i(l,"P",{"data-svelte-h":!0}),o(Gl)!=="svelte-npbw3t"&&(Gl.textContent=Wn),es=a(l),Wl=i(l,"P",{"data-svelte-h":!0}),o(Wl)!=="svelte-h6ikmf"&&(Wl.textContent=zn),ss=a(l),M(zl.$$.fragment,l),ns=a(l),Zl=i(l,"PRE",{"data-svelte-h":!0}),o(Zl)!=="svelte-aa4a7a"&&(Zl.textContent=Zn),as=a(l),Ll=i(l,"P",{"data-svelte-h":!0}),o(Ll)!=="svelte-l9rg7w"&&(Ll.textContent=Ln),As=a(l),M(Sl.$$.fragment,l),is=a(l),Dl=i(l,"PRE",{"data-svelte-h":!0}),o(Dl)!=="svelte-bk4o47"&&(Dl.textContent=Sn),os=a(l),M(Yl.$$.fragment,l),ps=a(l),_l=i(l,"P",{"data-svelte-h":!0}),o(_l)!=="svelte-13uoub8"&&(_l.innerHTML=Dn),Ms=a(l),Ol=i(l,"P",{"data-svelte-h":!0}),o(Ol)!=="svelte-vhdu58"&&(Ol.innerHTML=Yn),Us=a(l),$l=i(l,"P",{"data-svelte-h":!0}),o($l)!=="svelte-1b58ltx"&&($l.innerHTML=_n),rs=a(l),M(lt.$$.fragment,l),js=a(l),M(tt.$$.fragment,l),cs=a(l),M(et.$$.fragment,l),ws=a(l),st=i(l,"P",{"data-svelte-h":!0}),o(st)!=="svelte-352kfz"&&(st.textContent=On),Ts=a(l),M(nt.$$.fragment,l),fs=a(l),at=i(l,"PRE",{"data-svelte-h":!0}),o(at)!=="svelte-drvpbm"&&(at.textContent=$n),Js=a(l),M(At.$$.fragment,l),us=a(l),it=i(l,"PRE",{"data-svelte-h":!0}),o(it)!=="svelte-8jp8ub"&&(it.textContent=la),ys=a(l),ot=i(l,"P",{"data-svelte-h":!0}),o(ot)!=="svelte-wgk4la"&&(ot.textContent=ta),ms=a(l),M(pt.$$.fragment,l),ds=a(l),Mt=i(l,"P",{"data-svelte-h":!0}),o(Mt)!=="svelte-1vytx7u"&&(Mt.textContent=ea),Rs=a(l),M(Ut.$$.fragment,l),Cs=a(l),rt=i(l,"UL",{"data-svelte-h":!0}),o(rt)!=="svelte-1reib3"&&(rt.innerHTML=sa),hs=a(l),M(jt.$$.fragment,l),bs=a(l),ct=i(l,"P",{"data-svelte-h":!0}),o(ct)!=="svelte-1dq1tqp"&&(ct.innerHTML=na),Fs=a(l),wt=i(l,"UL",{"data-svelte-h":!0}),o(wt)!=="svelte-euuksf"&&(wt.innerHTML=aa),Bs=a(l),Tt=i(l,"P",{"data-svelte-h":!0}),o(Tt)!=="svelte-1ykhfl1"&&(Tt.innerHTML=Aa),vs=a(l),ft=i(l,"UL",{"data-svelte-h":!0}),o(ft)!=="svelte-1csot73"&&(ft.innerHTML=ia),Is=a(l),M(Jt.$$.fragment,l),gs=a(l),ut=i(l,"P",{"data-svelte-h":!0}),o(ut)!=="svelte-pzw0dh"&&(ut.innerHTML=oa),Es=a(l),yt=i(l,"UL",{"data-svelte-h":!0}),o(yt)!=="svelte-1v6dvb"&&(yt.innerHTML=pa),Ks=a(l),mt=i(l,"P",{"data-svelte-h":!0}),o(mt)!=="svelte-fpwmko"&&(mt.innerHTML=Ma),Vs=a(l),dt=i(l,"UL",{"data-svelte-h":!0}),o(dt)!=="svelte-16c8sbr"&&(dt.innerHTML=Ua),Ns=a(l),M(Rt.$$.fragment,l),Xs=a(l),Ft=i(l,"P",{}),ra(Ft).forEach(e),this.h()},h(){Ct(f,"name","hf:doc:metadata"),Ct(f,"content",Ra),Qs(C.src,Ws="https://huggingface.co/datasets/huggingface/cookbook-images/resolve/main/RAG_workflow.png")||Ct(C,"src",Ws),Ct(C,"height","700"),Qs(ht.src,jn="data:image/jpeg;base64, ")||Ct(ht,"src",jn),Qs(Rl.src,In="https://huggingface.co/datasets/huggingface/cookbook-images/resolve/main/PaCMAP_embeddings.png")||Ct(Rl,"src",In),Ct(Rl,"height","700")},m(l,t){ua(document.head,f),s(l,Bt,t),s(l,bt,t),s(l,vt,t),U(J,l,t),s(l,It,t),U(u,l,t),s(l,gt,t),s(l,y,t),s(l,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ca,i(l){ks||(r(J.$$.fragment,l),r(u.$$.fragment,l),r(B.$$.fragment,l),r(v.$$.fragment,l),r(I.$$.fragment,l),r(g.$$.fragment,l),r(E.$$.fragment,l),r(K.$$.fragment,l),r(V.$$.fragment,l),r(P.$$.fragment,l),r(D.$$.fragment,l),r(O.$$.fragment,l),r(el.$$.fragment,l),r(fl.$$.fragment,l),r(yl.$$.fragment,l),r(ml.$$.fragment,l),r(dl.$$.fragment,l),r(Bl.$$.fragment,l),r(Il.$$.fragment,l),r(Vl.$$.fragment,l),r(Hl.$$.fragment,l),r(ql.$$.fragment,l),r(Pl.$$.fragment,l),r(zl.$$.fragment,l),r(Sl.$$.fragment,l),r(Yl.$$.fragment,l),r(lt.$$.fragment,l),r(tt.$$.fragment,l),r(et.$$.fragment,l),r(nt.$$.fragment,l),r(At.$$.fragment,l),r(pt.$$.fragment,l),r(Ut.$$.fragment,l),r(jt.$$.fragment,l),r(Jt.$$.fragment,l),r(Rt.$$.fragment,l),ks=!0)},o(l){j(J.$$.fragment,l),j(u.$$.fragment,l),j(B.$$.fragment,l),j(v.$$.fragment,l),j(I.$$.fragment,l),j(g.$$.fragment,l),j(E.$$.fragment,l),j(K.$$.fragment,l),j(V.$$.fragment,l),j(P.$$.fragment,l),j(D.$$.fragment,l),j(O.$$.fragment,l),j(el.$$.fragment,l),j(fl.$$.fragment,l),j(yl.$$.fragment,l),j(ml.$$.fragment,l),j(dl.$$.fragment,l),j(Bl.$$.fragment,l),j(Il.$$.fragment,l),j(Vl.$$.fragment,l),j(Hl.$$.fragment,l),j(ql.$$.fragment,l),j(Pl.$$.fragment,l),j(zl.$$.fragment,l),j(Sl.$$.fragment,l),j(Yl.$$.fragment,l),j(lt.$$.fragment,l),j(tt.$$.fragment,l),j(et.$$.fragment,l),j(nt.$$.fragment,l),j(At.$$.fragment,l),j(pt.$$.fragment,l),j(Ut.$$.fragment,l),j(jt.$$.fragment,l),j(Jt.$$.fragment,l),j(Rt.$$.fragment,l),ks=!1},d(l){l&&(e(Bt),e(bt),e(vt),e(It),e(gt),e(y),e(Et),e(m),e(Kt),e(d),e(Vt),e(R),e(Nt),e(C),e(Xt),e(h),e(kt),e(b),e(Qt),e(F),e(xt),e(Ht),e(qt),e(Pt),e(Gt),e(Wt),e(zt),e(Zt),e(N),e(Lt),e(X),e(St),e(k),e(Dt),e(Q),e(Yt),e(x),e(_t),e(H),e(Ot),e(q),e($t),e(le),e(G),e(te),e(W),e(ee),e(z),e(se),e(Z),e(ne),e(L),e(ae),e(S),e(Ae),e(ie),e(Y),e(oe),e(_),e(pe),e(Me),e($),e(Ue),e(ll),e(re),e(tl),e(je),e(ce),e(ht),e(we),e(sl),e(Te),e(nl),e(fe),e(al),e(Je),e(Al),e(ue),e(il),e(ye),e(ol),e(me),e(pl),e(de),e(Ml),e(Re),e(Ul),e(Ce),e(rl),e(he),e(jl),e(be),e(cl),e(Fe),e(wl),e(Be),e(Tl),e(ve),e(Ie),e(Jl),e(ge),e(ul),e(Ee),e(Ke),e(Ve),e(Ne),e(Rl),e(Xe),e(Cl),e(ke),e(hl),e(Qe),e(bl),e(xe),e(Fl),e(He),e(qe),e(vl),e(Pe),e(Ge),e(gl),e(We),e(El),e(ze),e(Kl),e(Ze),e(Le),e(Nl),e(Se),e(Xl),e(De),e(kl),e(Ye),e(Ql),e(_e),e(xl),e(Oe),e($e),e(ls),e(ts),e(Gl),e(es),e(Wl),e(ss),e(ns),e(Zl),e(as),e(Ll),e(As),e(is),e(Dl),e(os),e(ps),e(_l),e(Ms),e(Ol),e(Us),e($l),e(rs),e(js),e(cs),e(ws),e(st),e(Ts),e(fs),e(at),e(Js),e(us),e(it),e(ys),e(ot),e(ms),e(ds),e(Mt),e(Rs),e(Cs),e(rt),e(hs),e(bs),e(ct),e(Fs),e(wt),e(Bs),e(Tt),e(vs),e(ft),e(Is),e(gs),e(ut),e(Es),e(yt),e(Ks),e(mt),e(Vs),e(dt),e(Ns),e(Xs),e(Ft)),e(f),c(J,l),c(u,l),c(B,l),c(v,l),c(I,l),c(g,l),c(E,l),c(K,l),c(V,l),c(P,l),c(D,l),c(O,l),c(el,l),c(fl,l),c(yl,l),c(ml,l),c(dl,l),c(Bl,l),c(Il,l),c(Vl,l),c(Hl,l),c(ql,l),c(Pl,l),c(zl,l),c(Sl,l),c(Yl,l),c(lt,l),c(tt,l),c(et,l),c(nt,l),c(At,l),c(pt,l),c(Ut,l),c(jt,l),c(Jt,l),c(Rt,l)}}}const Ra='{"title":"使用 LangChain 在 HuggingFace 文档上构建高级 RAG","local":"使用-langchain-在-huggingface-文档上构建高级-rag","sections":[{"title":"加载你的知识基础","local":"加载你的知识基础","sections":[],"depth":3}],"depth":1}';function Ca(xs){return wa(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class va extends Ta{constructor(f){super(),fa(this,f,Ca,da,ja,{})}}export{va as component};
