AIMon Rerank¶
如果您在 Colab 上打开此 Notebook,您可能需要安装 LlamaIndex 🦙
In [ ]
已复制!
%%capture
!pip install llama-index
!pip install llama-index-postprocessor-aimon-rerank
%%capture !pip install llama-index !pip install llama-index-postprocessor-aimon-rerank
In [ ]
已复制!
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.response.pprint_utils import pprint_response
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.core.response.pprint_utils import pprint_response
此 Notebook 需要 OpenAI 和 AIMon API 密钥。从 Colab Secrets 导入 AIMon 和 OpenAI API 密钥。
In [ ]
已复制!
import os
# Import Colab Secrets userdata module.
from google.colab import userdata
os.environ["AIMON_API_KEY"] = userdata.get("AIMON_API_KEY")
os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_API_KEY")
import os # Import Colab Secrets userdata module. from google.colab import userdata os.environ["AIMON_API_KEY"] = userdata.get("AIMON_API_KEY") os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_API_KEY")
下载数据
In [ ]
已复制!
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
!mkdir -p 'data/paul_graham/' !wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
--2025-03-10 18:01:07-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.111.133, 185.199.110.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 75042 (73K) [text/plain] Saving to: ‘data/paul_graham/paul_graham_essay.txt’ data/paul_graham/pa 100%[===================>] 73.28K --.-KB/s in 0.03s 2025-03-10 18:01:08 (2.59 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]
生成文档并构建索引
In [ ]
已复制!
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
# build index
index = VectorStoreIndex.from_documents(documents=documents)
# load documents documents = SimpleDirectoryReader("./data/paul_graham/").load_data() # build index index = VectorStoreIndex.from_documents(documents=documents)
定义 AIMon 重排序器的任务定义,并实例化 AIMonRerank 类。 任务定义 作为对系统的明确指令,定义了重排序评估应该关注的内容。
In [ ]
已复制!
import os
from llama_index.postprocessor.aimon_rerank import AIMonRerank
task_definition = "Your task is to assess the actions of an individual specified in the user query against the context documents supplied."
aimon_rerank = AIMonRerank(
top_n=2,
api_key=userdata.get("AIMON_API_KEY"),
task_definition=task_definition,
)
import os from llama_index.postprocessor.aimon_rerank import AIMonRerank task_definition = "Your task is to assess the actions of an individual specified in the user query against the context documents supplied." aimon_rerank = AIMonRerank( top_n=2, api_key=userdata.get("AIMON_API_KEY"), task_definition=task_definition, )
直接检索最相似的前 2 个节点(即不使用重排序器)¶
In [ ]
已复制!
query_engine = index.as_query_engine(similarity_top_k=2)
response = query_engine.query("What did Sam Altman do in this essay?")
query_engine = index.as_query_engine(similarity_top_k=2) response = query_engine.query("What did Sam Altman do in this essay?")
In [ ]
已复制!
pprint_response(response, show_source=True)
pprint_response(response, show_source=True)
Final Response: Sam Altman was asked to become the president of Y Combinator, initially declined the offer to pursue starting a startup focused on nuclear reactors, but eventually agreed to take over starting with the winter 2014 batch. ______________________________________________________________________ Source Node 1/2 Node ID: 2940ea4a-69ec-4fc4-9dd4-8ed54a9d4f1b Similarity: 0.8305926707169754 Text: When I was dealing with some urgent problem during YC, there was about a 60% chance it had to do with HN, and a 40% chance it had do with everything else combined. [17] As well as HN, I wrote all of YC's internal software in Arc. But while I continued to work a good deal in Arc, I gradually stopped working on Arc, partly because I didn't have t... ______________________________________________________________________ Source Node 2/2 Node ID: 2f043635-e4ce-4054-92f3-b624fd90ae04 Similarity: 0.8239262662012308 Text: Up till that point YC had been controlled by the original LLC we four had started. But we wanted YC to last for a long time, and to do that it couldn't be controlled by the founders. So if Sam said yes, we'd let him reorganize YC. Robert and I would retire, and Jessica and Trevor would become ordinary partners. When we asked Sam if he wanted to...
检索最相关的 Top 10 节点,然后使用 AIMon 重排序器进行重排序¶
重排序过程说明
该图说明了重排序器如何优化文档检索以获得更准确的响应。
初始检索 (向量数据库):
- 查询被发送到向量数据库。
- 系统根据相似性得分检索最相关的 Top 10 记录 (
top_k = 10
)。
使用 AIMon 进行重排序:
- 这些 Top 10 记录不直接使用得分最高的记录,而是使用 AIMon 重排序器进行重排序。
- 重排序器根据文档与查询的实际相关性,而不是仅仅根据原始相似性得分来评估文档。
- 在此步骤中,应用了任务定义,它作为明确的指令,定义了重排序评估应关注的内容。
- 这确保了选定的记录不仅在统计上相似,而且与预期任务在上下文上相关。
最终选择 (
top_n = 2
):- 重排序后,系统选择在上下文中最相关的 Top 2 记录用于生成响应。
- 任务定义确保这些记录与查询意图一致,从而产生更精确和信息丰富的响应。
In [ ]
已复制!
query_engine = index.as_query_engine(
similarity_top_k=10, node_postprocessors=[aimon_rerank]
)
response = query_engine.query("What did Sam Altman do in this essay?")
query_engine = index.as_query_engine( similarity_top_k=10, node_postprocessors=[aimon_rerank] ) response = query_engine.query("What did Sam Altman do in this essay?")
Total number of batches formed: 1 Processing batch 1/1 with 10 context documents. Finished processing. Total batches sent to AIMon reranker: 1 Top 2 nodes selected after reranking.
In [ ]
已复制!
pprint_response(response, show_source=True)
pprint_response(response, show_source=True)
Final Response: Sam Altman was asked to become the president of Y Combinator, initially declined the offer to pursue starting a startup focused on nuclear reactors, but eventually agreed to take over as president starting with the winter 2014 batch. ______________________________________________________________________ Source Node 1/2 Node ID: 2940ea4a-69ec-4fc4-9dd4-8ed54a9d4f1b Similarity: 0.48260445005911023 Text: When I was dealing with some urgent problem during YC, there was about a 60% chance it had to do with HN, and a 40% chance it had do with everything else combined. [17] As well as HN, I wrote all of YC's internal software in Arc. But while I continued to work a good deal in Arc, I gradually stopped working on Arc, partly because I didn't have t... ______________________________________________________________________ Source Node 2/2 Node ID: 0baaf5af-6e6b-4889-8407-e49d1753980c Similarity: 0.48151918284717965 Text: As Jessica and I were walking home from dinner on March 11, at the corner of Garden and Walker streets, these three threads converged. Screw the VCs who were taking so long to make up their minds. We'd start our own investment firm and actually implement the ideas we'd been talking about. I'd fund it, and Jessica could quit her job and work for ...
结论¶
AIMon 重排序器通过使用任务定义,将检索重点从一般的 YC 领导层变动转移到 Sam Altman 的具体行动上。最初,高相似度文档缺乏其决策细节。重排序后,相似度较低但上下文相关的文档突显了他的不情愿和时间线,与纯粹基于相似性的检索相比,确保了更准确、与任务对齐的响应。