Okay, so we're asking who went to Manchester University and we're going to retrieve 20 documents and 好的，我们要询问谁去了曼彻斯特大学，我们将检索 20 份文件
see how we do. 看看我们怎么做。
And I do believe if you do this before we do the semantic chunking and you try it with the prior version, 我相信如果你在我们进行语义分块之前这样做并且你尝试使用之前的版本，
it doesn't get it right and it's lower than than ten, the top ten that we pull. 它做得不对，而且低于我们拉的前十名。
But as of now, because we've done the semantic chunking, it doesn't do terribly. 但到目前为止，因为我们已经完成了语义分块，所以效果还不是很好。
It does bring it back. 它确实把它带回来了。
It's the fourth document along. 这是第四份文件。
And in fact that is that is index four. 事实上，这就是索引四。
It actually means it's the fifth document along because uh, zero is the first document. 它实际上意味着它是第五个文档，因为呃，零是第一个文档。
So the fifth document down in the stack contained the word Manchester. 因此，堆栈中的第五个文档包含单词曼彻斯特。
Uh, so, uh, it did find it eventually, but it was quite far down. 呃,所以呃,最终还是找到了,但是还差得很远.
Now, what happens if we rerank we call the rerank to reorder things. 现在，如果我们重新排序会发生什么，我们称之为重新排序以重新排序。
You can see that it's put the fifth document at the top now. 您可以看到它现在将第五个文档放在顶部。
So now if we get it to say where is Manchester? 那么现在如果我们得到它说曼彻斯特在哪里？
You'll see that Manchester is in the very top spot. 您会发现曼彻斯特位居榜首。
It was able to sift all of these documents and shove Manchester right up to the top. 它能够筛选所有这些文件，并将曼彻斯特推上榜首。
And so let's see what is in the content of this first document. 让我们看看第一个文档的内容是什么。
You can see that this is about Jessica Liu. 你可以看到这是关于Jessica Liu的。
I think Jessica Yu is Jessica Liu. 我认为余洁西卡就是刘洁西卡。
That's right. 这是正确的。
Is the name of the person who was, uh, described as going to University of Manchester. 是那个被描述为去曼彻斯特大学的人的名字。
So that's working very nicely. 所以效果非常好。
Okay. 好的。
Uh, then everything else is just going to be so easy. 呃，那么其他一切都会变得很容易。
Fetch context is just something which will take the unranked version and then rerank it. 获取上下文只是获取未排名版本然后重新排名的东西。
And it's designed to sound just like the fetch context that we had before. 它的设计听起来就像我们之前的获取上下文一样。
And now we're really in the end game. 现在我们真的进入了最后的阶段。
It's time to to answer the question system prompt. 是时候回答系统提示的问题了。
You're a knowledgeable, friendly assistant representing the company. 你是代表公司的知识渊博、友善的助理。
And sure. 当然。
Um, you're chatting about ensure Elm now. 嗯，您现在正在谈论确保 Elm。
I hope you remember one of the points was about improving your prompts. 我希望您记住其中一点是关于改进您的提示。
And so I thought, well, I should do my own take my own advice. 所以我想，好吧，我应该采取我自己的建议。
So I put in here. 所以我就放在这里了。
Your answer will be evaluated for accuracy, relevance and completeness. 我们将评估您的答案的准确性、相关性和完整性。
So make sure it answers the question and fully answers it and blah blah blah. 因此，请确保它回答了问题并完全回答了问题等等。
Like, I'm really trying to hammer at the LMS so that it does a good job in all three dimensions, and 就像，我真的在努力改进 LMS，以便它在所有三个维度上都做得很好，并且
telling it that it's going to be evaluated is like, uh, you know, hopefully really, really makes 告诉它它将被评估就像，呃，你知道，希望真的，真的让
it focus, pay attention to, to to those objectives. 它聚焦、关注、关注这些目标。
And then we give it some context. 然后我们给它一些背景信息。
With this context, please answer these as question. 在此背景下，请回答这些问题。
Be accurate, relevant and complete. 准确、相关且完整。
There's no harm in being repetitive in system prompts as well as in courses. 在系统提示和课程中重复并没有什么坏处。
Uh uh. 呃呃。
So and you'll notice that this otherwise is very similar format to when we use lang chain. 因此，您会注意到，这与我们使用 lang 链时的格式非常相似。
Same same kind of thing. 一样的东西。
I've got like a little tag in here. 我这里有一个小标签。
That's my system prompt. 这是我的系统提示。
And, and this, this is the, the extent of it. 而且，这，这就是它的范围。
This is the, the thing that's going to turn this into messages. 这就是将其转化为消息的东西。
Um, I take a question, I take the history and I take some chunks. 嗯，我提出一个问题，了解历史，并了解一些内容。
I build my context. 我构建我的背景。
It's just joining together all the different chunks. 它只是将所有不同的块连接在一起。
I call my system prompt format changing, flipping this context field here for the context that's passed 我称我的系统提示格式更改为传递的上下文，在此翻转此上下文字段
in here. 在这里。
And then I return role system for the system prompt. 然后我返回角色系统以获取系统提示。
I add in the history and the user for the user's question. 我添加了用户问题的历史记录和用户。
That makes our messages for our Rag Pipeline. 这就是我们为 Rag Pipeline 提供的信息。
Super easy. 超级简单。
Final rag trick to do is query rewriting. 最后要做的技巧是查询重写。
One more thing to do, which is one where we take the user's question. 还有一件事要做，这就是我们回答用户问题的地方。
We don't just take the question as it is, we try rewriting it first to see whether our LLM can do better. 我们不会直接接受问题本身，我们会先尝试重写它，看看我们的法学硕士是否可以做得更好。
And so here we have query rewriting Onshow. 所以这里我们有查询重写 Onshow。
And now you can see something that you're hopefully super used to. 现在您可以看到一些您希望超级习惯的东西。
Now this is just an example of a function that calls an LLM. 现在这只是调用 LLM 的函数的示例。
There's no structured outputs. 没有结构化的输出。
This time it could not be easier. 这一次再简单不过了。
We describe a message you're in conversation with the user. 我们描述您正在与用户对话的消息。
This is the history of your conversation. 这是你们谈话的历史记录。
This is the user's question. 这是用户的问题。
Respond with a single refined question that will be used to search the knowledge base. 回答一个将用于搜索知识库的精致问题。
Important. 重要的。
Respond only with the query. 仅响应查询。
Nothing else you can imagine. 没有其他你能想象的了。
I had to do this a few times because it kept wanting to tell me more, ask more, follow on questions 我不得不这样做几次，因为它一直想告诉我更多，问更多，跟进问题
and things, which is not the point. 和事情，这不是重点。
So you have to practice and experiment with these prompts and try each one separately. 因此，您必须练习和试验这些提示，并分别尝试每一个提示。
And then the code is so simple I'm using light LM again, which is similar to chat completions. 然后代码非常简单，我再次使用轻型 LM，这类似于聊天完成。
Create model is the model. 创建模型就是模型。
That's the message. 这就是信息。
And then I return response zero dot message content. 然后我返回响应零点消息内容。
That's it. 就是这样。
So that is rewrite query. 这就是重写查询。
It takes a question. 这需要一个问题。
It optionally takes history. 它可以选择获取历史记录。
And then it responds with the new query. 然后它会响应新的查询。
So what happens if we call that with who won the Ieti award. 那么，如果我们将其称为“Ieti 奖”的获得者，会发生什么呢？
We get back something pretty similar. 我们得到了非常相似的东西。
Who was the recipient of the Ieti award? Ieti 奖的获得者是谁？
It's just changed one to recipient. 它只是将一位收件人更改为收件人。
But you know, whatever. 但你知道，无论如何。
Sometimes it can do better. 有时它可以做得更好。
And particularly if there's history, it can do better. 尤其是如果有历史，它可以做得更好。
So now we can turn this into an answer question function. 现在我们可以把它变成一个回答问题的函数。
This is the other key function we had to write. 这是我们必须编写的另一个关键函数。
We had to write answer question. 我们必须写出答案问题。
And we had to write uh fetch content. 我们必须编写呃获取内容。
Those were fetch context. 这些是获取上下文。
Those were the two functions that we implemented before. 这是我们之前实现的两个功能。
So this is the answer question function. 所以这就是回答问题的函数。
It takes a question and it takes the history. 这需要一个问题，也需要历史。
And look at how simple it is. 看看它是多么简单。
We uh we rewrite the query. 我们呃我们重写查询。
We then get the chunks. 然后我们得到块。
We make the rag messages, we call completion for the model and the messages, and we return the response. 我们制作碎布消息，调用模型和消息的完成，然后返回响应。
That's it. 就是这样。
That is rag with no lang chain that nothing more than this. 那是没有郎链的破布，仅此而已。
If you're expecting something that was hundreds of lines of code and lots of rag stuff. 如果您期望的东西是数百行代码和大量破烂的东西。
It's not. 它不是。
It's just a few lines of code. 这只是几行代码。
Rewrite the query, get the chunks. 重写查询，获取块。
Get the messages. 获取消息。
Call OpenAI, return the response. 调用OpenAI，返回响应。
There we go. 我们开始吧。
Let's ask it. 我们来问问吧。
Who won the I o t award? 谁获得了 I o t 奖？
And there we go. 我们就这样吧。
We get Maxine Thompson won the I o t award in 2023. Maxine Thompson 荣获 2023 年物联网奖。
It's a response you can see there that happens to be accurate. 您可以在那里看到一个恰好准确的响应。
It happens to be complete. 它恰好是完整的。
It's got the last name and it happens to be relevant. 它有姓氏并且恰好是相关的。
It hasn't given us any spurious information. 它没有给我们任何虚假信息。
And you can see that there was some nice reranking that went on. 您可以看到正在进行一些不错的重新排名。
Uh, and uh, yeah, these were all the bits of relevant context. 呃，呃，是的，这些都是相关背景的内容。
You can see one other thing that we're going to do when we turn this into code, which is once we've 当我们把它变成代码时，你可以看到我们要做的另一件事，那就是一旦我们
done this reranking, we don't need to pass all of these chunks to the final LM, because now we can 完成此重新排名后，我们不需要将所有这些块传递给最终的 LM，因为现在我们可以
be pretty sure that the bottom ten are going to be less relevant. 可以肯定的是，后十名的相关性会降低。
So once we've done this, we might as well chop it off at some point and be more efficient with our 因此，一旦我们做到了这一点，我们不妨在某个时候将其砍掉，并提高我们的效率。
LM. LM。
That's one of the cool things about using Reranking. 这是使用重新排名的最酷的事情之一。
Okay, let's, uh, we're almost ready for showtime. 好吧，让我们，呃，我们快要准备好表演了。
Let's ask the question. 让我们问这个问题。
Who went to Manchester University? 谁去了曼彻斯特大学？
Uh, and, uh, we get back the answer. 呃，呃，我们得到了答案。
Jessica Liu attended the University of Manchester. 杰西卡刘就读于曼彻斯特大学。
It works. 有用。
It works nicely. 效果很好。
And, uh, you you can check that it doesn't work before. 而且，呃，你可以检查一下它以前是否不起作用。
At least it didn't work for me. 至少对我来说没用。
Now, one thing I'll tell you is that this doesn't always work. 现在，我要告诉你的一件事是，这并不总是有效。
I discovered initially that it. 我最初发现它。
That it didn't work. 那是行不通的。
It did a bad job of working. 它的工作做得很糟糕。
And to my great disappointment, I discovered the reason that it sometimes doesn't work is because of 令我非常失望的是，我发现它有时不起作用的原因是
query rewriting. 查询重写。
Query rewriting sometimes counts against us because when we call query rewriting, it tends to like 查询重写有时对我们不利，因为当我们调用查询重写时，它往往喜欢
to add the word ensure elm in there. 在其中添加“确保榆树”一词。
It tends to like say saying who from ensure elm blah blah blah blah blah blah blah. 它往往喜欢说谁来自确保榆树等等等等。
Even though I added in here, don't mention the company name unless blah blah blah. 即使我在这里添加了，也不要提及公司名称，除非等等。
I had to put that in there and it stopped saying that, but it still does sometimes. 我不得不把它放在那里，它就不再这么说了，但有时它仍然如此。
And when it does that, the rag retrieval is less reliable because it surfaces tons of documents that 当这样做时，抹布检索的可靠性就会降低，因为它会显示大量文档，而这些文档
are generic documents about ensure elm and not and Jessica's document like goes goes all the way down 是关于确保 elm 和 not 的通用文档，杰西卡的文档就像一直向下
and sometimes it doesn't get reranked up. 有时它不会被重新排名。
And so it becomes because of reranking it becomes a bit less reliable. 因此，由于重新排名，它变得不太可靠。
Sorry, not because of Reranking because of rewriting, it becomes a bit less reliable. 抱歉，不是因为Reranking，因为重写，变得不太可靠。
I hope you're following me here. 我希望你能在这里关注我。
Rewriting the query, unfortunately, has added in more words like ensure elm. 不幸的是，重写查询添加了更多单词，例如“ensure elm”。
That ends up diluting the rag retrieval and meaning that the relevant context doesn't get surfaced. 这最终会削弱抹布检索，并意味着相关上下文不会浮出水面。
That's a problem. 这是一个问题。
And that that final problem we will also solve in the last step of this, which is now we're going to 最后一个问题我们也将在最后一步中解决，现在我们要
take all of this and we're going to implement it in Python modules in an implementation directory. 完成所有这些，我们将在实现目录中的 Python 模块中实现它。
And then we're going to take it for a test drive. 然后我们将对其进行试驾。