AIMon 重排序¶
如果您在 Colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙
In [ ]:
Copied!
%%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 [ ]:
Copied!
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
本笔记本需要 OpenAI 和 AIMon 的 API 密钥。请从 Colab Secrets 导入 AIMon 和 OpenAI 的 API 密钥
In [ ]:
Copied!
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 [ ]:
Copied!
!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 [ ]:
Copied!
# 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 [ ]:
Copied!
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 [ ]:
Copied!
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 [ ]:
Copied!
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...
检索前10个最相关节点,但随后使用AIMon重新排序器进行重排序¶
重排序流程说明:
该图表展示了重排序器如何优化文档检索以获得更精准的响应。
初始检索(向量数据库):
- 向向量数据库发送查询请求
- 系统根据相似度分数检索前10条最相关记录(
top_k = 10
)
AIMon重排序:
- 并非直接使用最高分记录,而是通过AIMon重排序器对这10条记录重新排序
- 重排序器根据文档与查询的实际相关性(而非原始相似度分数)进行评估
- 此阶段会应用任务定义作为显式指令,明确重排序评估的关注重点
- 确保所选记录不仅统计相似,还与目标任务保持上下文关联
最终筛选(
top_n = 2
):- 重排序后,系统选择前2条最具上下文相关性的记录用于生成响应
- 任务定义确保这些记录符合查询意图,从而产生更精确且信息丰富的响应
In [ ]:
Copied!
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 [ ]:
Copied!
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的具体行动。最初,高相似度文档缺乏其决策细节。经过重排序后,相似度较低但上下文相关的文档突出了他的犹豫态度和时间线,确保生成比纯相似度检索更准确、更符合任务需求的响应。