LLM Reranker Demonstration (Great Gatsby)ï
This tutorial showcases how to do a two-stage pass for retrieval. Use embedding-based retrieval with a high top-k value in order to maximize recall and get a large set of candidate items. Then, use LLM-based retrieval to dynamically select the nodes that are actually relevant to the query.
import nest_asyncio
nest_asyncio.apply()
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
from llama_index import (
VectorStoreIndex,
SimpleDirectoryReader,
ServiceContext,
LLMPredictor,
)
from llama_index.postprocessor import LLMRerank
from llama_index.llms import OpenAI
from IPython.display import Markdown, display
Load Data, Build Indexï
# LLM Predictor (gpt-3.5-turbo) + service context
llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
service_context = ServiceContext.from_defaults(llm=llm, chunk_size=512)
WARNING:llama_index.llm_predictor.base:Unknown max input size for gpt-3.5-turbo, using defaults.
Unknown max input size for gpt-3.5-turbo, using defaults.
# load documents
documents = SimpleDirectoryReader("../../../examples/gatsby/data").load_data()
documents
index = VectorStoreIndex.from_documents(
documents, service_context=service_context
)
INFO:llama_index.token_counter.token_counter:> [build_index_from_nodes] Total LLM token usage: 0 tokens
> [build_index_from_nodes] Total LLM token usage: 0 tokens
INFO:llama_index.token_counter.token_counter:> [build_index_from_nodes] Total embedding token usage: 49266 tokens
> [build_index_from_nodes] Total embedding token usage: 49266 tokens
Retrievalï
from llama_index.retrievers import VectorIndexRetriever
from llama_index.schema import QueryBundle
import pandas as pd
from IPython.display import display, HTML
pd.set_option("display.max_colwidth", -1)
def get_retrieved_nodes(
query_str, vector_top_k=10, reranker_top_n=3, with_reranker=False
):
query_bundle = QueryBundle(query_str)
# configure retriever
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=vector_top_k,
)
retrieved_nodes = retriever.retrieve(query_bundle)
if with_reranker:
# configure reranker
reranker = LLMRerank(
choice_batch_size=5,
top_n=reranker_top_n,
service_context=service_context,
)
retrieved_nodes = reranker.postprocess_nodes(
retrieved_nodes, query_bundle
)
return retrieved_nodes
def pretty_print(df):
return display(HTML(df.to_html().replace("\\n", "<br>")))
def visualize_retrieved_nodes(nodes) -> None:
result_dicts = []
for node in nodes:
result_dict = {"Score": node.score, "Text": node.node.get_text()}
result_dicts.append(result_dict)
pretty_print(pd.DataFrame(result_dicts))
/var/folders/1r/c3h91d9s49xblwfvz79s78_c0000gn/T/ipykernel_44297/3519340226.py:7: FutureWarning: Passing a negative integer is deprecated in version 1.0 and will not be supported in future version. Instead, use None to not limit the column width.
pd.set_option('display.max_colwidth', -1)
new_nodes = get_retrieved_nodes(
"Who was driving the car that hit Myrtle?",
vector_top_k=3,
with_reranker=False,
)
INFO:llama_index.token_counter.token_counter:> [retrieve] Total LLM token usage: 0 tokens
> [retrieve] Total LLM token usage: 0 tokens
INFO:llama_index.token_counter.token_counter:> [retrieve] Total embedding token usage: 10 tokens
> [retrieve] Total embedding token usage: 10 tokens
visualize_retrieved_nodes(new_nodes)
Score | Text | |
---|---|---|
0 | 0.828844 | and some garrulous man telling over and over what had happened, until it became less and less real even to him and he could tell it no longer, and Myrtle Wilsonâs tragic achievement was forgotten. Now I want to go back a little and tell what happened at the garage after we left there the night before. They had difficulty in locating the sister, Catherine. She must have broken her rule against drinking that night, for when she arrived she was stupid with liquor and unable to understand that the ambulance had already gone to Flushing. When they convinced her of this, she immediately fainted, as if that was the intolerable part of the affair. Someone, kind or curious, took her in his car and drove her in the wake of her sisterâs body. Until long after midnight a changing crowd lapped up against the front of the garage, while George Wilson rocked himself back and forth on the couch inside. For a while the door of the office was open, and everyone who came into the garage glanced irresistibly through it. Finally someone said it was a shame, and closed the door. Michaelis and several other men were with him; first, four or five men, later two or three men. Still later Michaelis had to ask the last stranger to wait there fifteen minutes longer, while he went back to his own place and made a pot of coffee. After that, he stayed there alone with Wilson until dawn. About three oâclock the quality of Wilsonâs incoherent muttering changedâhe grew quieter and began to talk about the yellow car. He announced that he had a way of finding out whom the yellow car belonged to, and then he blurted out that a couple of months ago his wife had come from the city with her face bruised and her nose swollen. But when he heard himself say this, he flinched and began to cry âOh, my God!â again in his groaning voice. Michaelis made a clumsy |
1 | 0.827754 | she rushed out into the dusk, waving her hands and shoutingâbefore he could move from his door the business was over. The âdeath carâ as the newspapers called it, didnât stop; it came out of the gathering darkness, wavered tragically for a moment, and then disappeared around the next bend. Mavro Michaelis wasnât even sure of its colourâhe told the first policeman that it was light green. The other car, the one going toward New York, came to rest a hundred yards beyond, and its driver hurried back to where Myrtle Wilson, her life violently extinguished, knelt in the road and mingled her thick dark blood with the dust. Michaelis and this man reached her first, but when they had torn open her shirtwaist, still damp with perspiration, they saw that her left breast was swinging loose like a flap, and there was no need to listen for the heart beneath. The mouth was wide open and ripped a little at the corners, as though she had choked a little in giving up the tremendous vitality she had stored so long. ------------------------------------------------------------------------ We saw the three or four automobiles and the crowd when we were still some distance away. âWreck!â said Tom. âThatâs good. Wilsonâll have a little business at last.â He slowed down, but still without any intention of stopping, until, as we came nearer, the hushed, intent faces of the people at the garage door made him automatically put on the brakes. âWeâll take a look,â he said doubtfully, âjust a look.â I became aware now of a hollow, wailing sound which issued incessantly from the garage, a sound which as we got out of the coupĂ© and walked toward the door resolved itself into the words âOh, my God!â uttered over and over in a gasping |
2 | 0.826390 | went on, âand left the car in my garage. I donât think anybody saw us, but of course I canât be sure.â I disliked him so much by this time that I didnât find it necessary to tell him he was wrong. âWho was the woman?â he inquired. âHer name was Wilson. Her husband owns the garage. How the devil did it happen?â âWell, I tried to swing the wheelââ He broke off, and suddenly I guessed at the truth. âWas Daisy driving?â âYes,â he said after a moment, âbut of course Iâll say I was. You see, when we left New York she was very nervous and she thought it would steady her to driveâand this woman rushed out at us just as we were passing a car coming the other way. It all happened in a minute, but it seemed to me that she wanted to speak to us, thought we were somebody she knew. Well, first Daisy turned away from the woman toward the other car, and then she lost her nerve and turned back. The second my hand reached the wheel I felt the shockâit must have killed her instantly.â âIt ripped her openââ âDonât tell me, old sport.â He winced. âAnyhowâDaisy stepped on it. I tried to make her stop, but she couldnât, so I pulled on the emergency brake. Then she fell over into my lap and I drove on. âSheâll be all right tomorrow,â he said presently. âIâm just going to wait here and see if he tries to bother her about that unpleasantness this afternoon. Sheâs locked herself into her room, and if he tries any brutality sheâs going to turn the light out and on again.â âHe wonât touch |
new_nodes = get_retrieved_nodes(
"Who was driving the car that hit Myrtle?",
vector_top_k=10,
reranker_top_n=3,
with_reranker=True,
)
visualize_retrieved_nodes(new_nodes)
Score | Text | |
---|---|---|
0 | 10.0 | went on, âand left the car in my garage. I donât think anybody saw us, but of course I canât be sure.â I disliked him so much by this time that I didnât find it necessary to tell him he was wrong. âWho was the woman?â he inquired. âHer name was Wilson. Her husband owns the garage. How the devil did it happen?â âWell, I tried to swing the wheelââ He broke off, and suddenly I guessed at the truth. âWas Daisy driving?â âYes,â he said after a moment, âbut of course Iâll say I was. You see, when we left New York she was very nervous and she thought it would steady her to driveâand this woman rushed out at us just as we were passing a car coming the other way. It all happened in a minute, but it seemed to me that she wanted to speak to us, thought we were somebody she knew. Well, first Daisy turned away from the woman toward the other car, and then she lost her nerve and turned back. The second my hand reached the wheel I felt the shockâit must have killed her instantly.â âIt ripped her openââ âDonât tell me, old sport.â He winced. âAnyhowâDaisy stepped on it. I tried to make her stop, but she couldnât, so I pulled on the emergency brake. Then she fell over into my lap and I drove on. âSheâll be all right tomorrow,â he said presently. âIâm just going to wait here and see if he tries to bother her about that unpleasantness this afternoon. Sheâs locked herself into her room, and if he tries any brutality sheâs going to turn the light out and on again.â âHe wonât touch |
new_nodes = get_retrieved_nodes(
"What did Gatsby want Daisy to do in front of Tom?",
vector_top_k=3,
with_reranker=False,
)
INFO:llama_index.token_counter.token_counter:> [retrieve] Total LLM token usage: 0 tokens
> [retrieve] Total LLM token usage: 0 tokens
INFO:llama_index.token_counter.token_counter:> [retrieve] Total embedding token usage: 14 tokens
> [retrieve] Total embedding token usage: 14 tokens
visualize_retrieved_nodes(new_nodes)
****Score****: 0.8647796939111776
****Node text****
: got to make your house into a pigsty in order to have any
friendsâin the modern world.â
Angry as I was, as we all were, I was tempted to laugh whenever he
opened his mouth. The transition from libertine to prig was so
complete.
âIâve got something to tell you, old sportââ began Gatsby. But Daisy
guessed at his intention.
âPlease donât!â she interrupted helplessly. âPlease letâs all go
home. Why donât we all go home?â
âThatâs a good idea,â I got up. âCome on, Tom. Nobody wants a drink.â
âI want to know what Mr. Gatsby has to tell me.â
âYour wife doesnât love you,â said Gatsby. âSheâs never loved you.
She loves me.â
âYou must be crazy!â exclaimed Tom automatically.
Gatsby sprang to his feet, vivid with excitement.
âShe never loved you, do you hear?â he cried. âShe only married you
because I was poor and she was tired of waiting for me. It was a
terrible mistake, but in her heart she never loved anyone except me!â
At this point Jordan and I tried to go, but Tom and Gatsby insisted
with competitive firmness that we remainâas though neither of them had
anything to conceal and it would be a privilege to partake vicariously
of their emotions.
âSit down, Daisy,â Tomâs voice groped unsuccessfully for the paternal
note. âWhatâs been going on? I want to hear all about it.â
âI told you whatâs been going on,â said Gatsby. âGoing on for five
yearsâand you didnât know.â
Tom turned to Daisy
****Score****: 0.8609230717744326
****Node text****
: to keep your
shoes dry?â There was a husky tenderness in his tone ⊠âDaisy?â
âPlease donât.â Her voice was cold, but the rancour was gone from it.
She looked at Gatsby. âThere, Jay,â she saidâbut her hand as she tried
to light a cigarette was trembling. Suddenly she threw the cigarette
and the burning match on the carpet.
âOh, you want too much!â she cried to Gatsby. âI love you nowâisnât
that enough? I canât help whatâs past.â She began to sob
helplessly. âI did love him onceâbut I loved you too.â
Gatsbyâs eyes opened and closed.
âYou loved me too?â he repeated.
âEven thatâs a lie,â said Tom savagely. âShe didnât know you were
alive. Whyâthereâs things between Daisy and me that youâll never know,
things that neither of us can ever forget.â
The words seemed to bite physically into Gatsby.
âI want to speak to Daisy alone,â he insisted. âSheâs all excited
nowââ
âEven alone I canât say I never loved Tom,â she admitted in a pitiful
voice. âIt wouldnât be true.â
âOf course it wouldnât,â agreed Tom.
She turned to her husband.
âAs if it mattered to you,â she said.
âOf course it matters. Iâm going to take better care of you from now
on.â
âYou donât understand,â said Gatsby, with a touch of panic. âYouâre
not going to take care of her any more.â
âIâm not?â Tom opened his eyes wide and
****Score****: 0.8555028907426916
****Node text****
: shadowed well with awnings, was dark and cool. Daisy and
Jordan lay upon an enormous couch, like silver idols weighing down
their own white dresses against the singing breeze of the fans.
âWe canât move,â they said together.
Jordanâs fingers, powdered white over their tan, rested for a moment
in mine.
âAnd Mr. Thomas Buchanan, the athlete?â I inquired.
Simultaneously I heard his voice, gruff, muffled, husky, at the hall
telephone.
Gatsby stood in the centre of the crimson carpet and gazed around with
fascinated eyes. Daisy watched him and laughed, her sweet, exciting
laugh; a tiny gust of powder rose from her bosom into the air.
âThe rumour is,â whispered Jordan, âthat thatâs Tomâs girl on the
telephone.â
We were silent. The voice in the hall rose high with annoyance: âVery
well, then, I wonât sell you the car at all ⊠Iâm under no obligations
to you at all ⊠and as for your bothering me about it at lunch time, I
wonât stand that at all!â
âHolding down the receiver,â said Daisy cynically.
âNo, heâs not,â I assured her. âItâs a bona-fide deal. I happen to
know about it.â
Tom flung open the door, blocked out its space for a moment with his
thick body, and hurried into the room.
âMr. Gatsby!â He put out his broad, flat hand with well-concealed
dislike. âIâm glad to see you, sir ⊠Nick âŠâ
âMake us a cold drink,â cried Daisy.
As he left the room again she got up and went over to Gatsby and
pulled his face
new_nodes = get_retrieved_nodes(
"What did Gatsby want Daisy to do in front of Tom?",
vector_top_k=10,
reranker_top_n=3,
with_reranker=True,
)
INFO:llama_index.token_counter.token_counter:> [retrieve] Total LLM token usage: 0 tokens
> [retrieve] Total LLM token usage: 0 tokens
INFO:llama_index.token_counter.token_counter:> [retrieve] Total embedding token usage: 14 tokens
> [retrieve] Total embedding token usage: 14 tokens
Doc: 2, Relevance: 10
No relevant documents found. Please provide a different question.
visualize_retrieved_nodes(new_nodes)
****Score****: 10.0
****Node text****
: to keep your
shoes dry?â There was a husky tenderness in his tone ⊠âDaisy?â
âPlease donât.â Her voice was cold, but the rancour was gone from it.
She looked at Gatsby. âThere, Jay,â she saidâbut her hand as she tried
to light a cigarette was trembling. Suddenly she threw the cigarette
and the burning match on the carpet.
âOh, you want too much!â she cried to Gatsby. âI love you nowâisnât
that enough? I canât help whatâs past.â She began to sob
helplessly. âI did love him onceâbut I loved you too.â
Gatsbyâs eyes opened and closed.
âYou loved me too?â he repeated.
âEven thatâs a lie,â said Tom savagely. âShe didnât know you were
alive. Whyâthereâs things between Daisy and me that youâll never know,
things that neither of us can ever forget.â
The words seemed to bite physically into Gatsby.
âI want to speak to Daisy alone,â he insisted. âSheâs all excited
nowââ
âEven alone I canât say I never loved Tom,â she admitted in a pitiful
voice. âIt wouldnât be true.â
âOf course it wouldnât,â agreed Tom.
She turned to her husband.
âAs if it mattered to you,â she said.
âOf course it matters. Iâm going to take better care of you from now
on.â
âYou donât understand,â said Gatsby, with a touch of panic. âYouâre
not going to take care of her any more.â
âIâm not?â Tom opened his eyes wide and
Query Engineï
query_engine = index.as_query_engine(
similarity_top_k=10,
node_postprocessors=[reranker],
response_mode="tree_summarize",
)
response = query_engine.query(
"What did the author do during his time at Y Combinator?",
)
query_engine = index.as_query_engine(
similarity_top_k=3, response_mode="tree_summarize"
)
response = query_engine.query(
"What did the author do during his time at Y Combinator?",
)
retrieval =