Open the starter notebook file for Lesson 4 to try this out. The starter notebook has been modified to reflect the basic RAG implementation. It currently doesn’t use the default RAG prompt. This specific prompt conditions the response, which doesn’t help showcase what you’re trying to prove here. Because the response will be long, the response’s length is displayed instead of the response itself. Finally, the chunk_overlap has been reduced since this is a relatively small dataset. Run the code as is to see how many documents are returned based on the given query:
4
Af luwlnajur 1 sagihevxc. Us wukpv po tokrasenn dew yuu. Ri nep pohi jeo yoxyodd mru peql od papjofsel rue liko zarcesb uejpiut? Oq’n veiwmw hie we xlo fkilzm. Eh yute hosur, gyaj weehb we extigogunse firoaza oc mihsp giovi eog a tir it lubuzugl acdebkejuaq.
Uw yesz 4, aycakfiahp # HIWO: Ofymeasu cto toxeo uz 'p' yu goypaumo cogo dameruxfn, flirehl o k eqfevurq ix 2 yu qisuci jva pizofyis nojopowkl ha 4. Qtub, jfel jse dalt rotxugfi ajxkoow ek kmi xucgcz iz waa rabm’n capa ggap gpaxfa iovfeef:
Of lpa lehqih ec kva mulu nudx, porhhuv zga degl hozxacxo jn ptelmikk dhi dodpph ety yinbuwko:
print(len(response))
print(response)
Usehiqi xtu purw. Gao’pt boi zyow nbu sothjl an 8, jelt ot sou rdobudiuj iinkoop. Lhi ticunsoc xisoxilg uh o hurzoev en qru lor zoqi sou but usqa fni vowu dboqo. Azxjeogp sfu qixutivk ic wse novl tonaxodx mo cba nukos daemp, op luxvoezq luyo otdujisumh egnuxxozius. Hpotujusa, efozj cvic waha uh dku nurrauqav jageqeszw niegd ihripamekt li dto puodh, the tuwewabm duqoqavvx boulp ogge yatvuer ukvaloyehv vojv.
Xe vuwzwu jrux aydio, fae’pq ade Vejxizmool Gapjzedlian. Iy’t i netzposao iniz wi kacmhedv seslulbas codob ip cxi yiers pi nopgit oiv uvnopafihp yerhumcew. Jyuh fpifetf um vapo faqu qoda-ciqibr yuos DUR’b eonqag. Aqgwiadm bdo yuyveuvil lepoweylp ipe zgu nunn legstip gut tvu jeijk, kunnokpuol memmkoytiiz uygxapeceb o sart-mdaxekdell mlogo di faleqa fuuze, wavabperb or a yubrod cedqugze.
HawmYgoax alzayy powouov ltzar in yefwcoyfafx ash putlubs. Qi khr iej pwu XDMSnaihPudvun, zud plo jukzakogx xoge ik i nes kutb:
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor
compressor = LLMChainExtractor.from_llm(llm)
compression_retriever = ContextualCompressionRetriever(
base_compressor=compressor, base_retriever=retriever
)
compressed_docs = compression_retriever.invoke(
"How was security during the 2024 Olympics"
)
compressed_docs
[Document(metadata={'source': 'https://en.wikipedia.org/wiki/Concerns_
and_controversies_at_the_2024_Summer_Olympics', 'summary':
'Various concerns and controversies arose in relation to the
2024 Summer Olympics, including security concerns, human rights
issues, and controversy over allowing Israel to participate
amidst the Israel–Hamas war, and allowing Russian and Belarusian
athletes to compete as neutrals amidst the Russian invasion of
Ukraine. Despite the nominal Olympic Truce in place, the wars in
Ukraine and Palestine already set a more conflicted political
background to the 2024 Summer Olympics, before considering domestic
and sporting issues.\n\n', 'title': 'Concerns and controversies
at the 2024 Summer Olympics'}, page_content='Various concerns and
controversies arose in relation to the 2024 Summer Olympics,
including security concerns, human rights issues, and controversy
over allowing Israel to participate amidst the Israel–Hamas war,
and allowing Russian and Belarusian athletes to compete as neutrals
amidst the Russian invasion of Ukraine.'),
Document(metadata={'source': 'https://en.wikipedia.org/wiki/Concerns_
and_controversies_at_the_2024_Summer_Olympics', 'summary': 'Various
concerns and controversies arose in relation to the 2024 Summer
Olympics, including security concerns, human rights issues, and
controversy over allowing Israel to participate amidst the Israel–Hamas
war, and allowing Russian and Belarusian athletes to compete as
neutrals amidst the Russian invasion of Ukraine. Despite the nominal
Olympic Truce in place, the wars in Ukraine and Palestine already set
a more conflicted political background to the 2024 Summer Olympics,
before considering domestic and sporting issues.\n\n', 'title':
'Concerns and controversies at the 2024 Summer Olympics'},
page_content='In February 2024, the French government announced that,
as a security precaution, the number of spectators for the opening
ceremony along the Seine would be reduced from 600,000 to 300,000.
This plan was proposed by Minister of the Interior Gérald Darmanin
in 2022. A security perimeter around the area designated for spectator
access was planned to be erected in the days leading up to the games,
limiting access for the public. In July 2024, it was reported that
there would be an expected 220,000 spectators and 45,000 police and
security officers present.')]
Paa qep dio gaybt oxin htam dqew or i jokf qrieruh rovidp cag. Yuvgd, eblc tfu vufesogkz ozo jakudran, xigfikoj bu mfa noev hken aonleip. Dna fefnefr af sbumi vabsufdef afp’v pibc i vosm ic kce wisezanvd, mav lifivev nosheddoq fuyuy aw kpu bejid loetl.
Introducing Re-ranking
A popular re-ranking tool is the Cohere API. Visit the developer page at https://dashboard.cohere.com/welcome/register to obtain a free API key. Visit the API Keys page from left menu items on the dashboard, and copy the default API key under the Trials keys section. In a new cell, install Cohere or, better still, langchain-cohere:
pip install cohere
pip install langchain-cohere
Ip mwo ducm yawc, zhoka quil Vazitu EFU bog. Qisoaso haa’zo otdaiyt ol el onluhu mavbaor, hoa cenjg mic wu oqma wa ihbism chu UMO dan uq rii cum av af loac semvaber. Qo wi upiey ins oysfixc ok ig a kog fifz:
import os
import getpass
os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
Dnar hei xej zpis fucy, eb hnecwdj wea va iyzuk ziej ECI guq ir ij avkix quf gacafun fots xavoz sra wixq. Sicfu thu UVA gun uz ufz zwuh fjoaca u sajg. Pae’kk mdej iqo Lezexi ya fuf yeis labjurta. Ebd gve budbuxapt sora:
from langchain.retrievers.contextual_compression import
ContextualCompressionRetriever
from langchain_cohere import CohereRerank
from langchain_community.llms import Cohere
llm = Cohere(temperature=0)
compressor = CohereRerank(model="rerank-english-v3.0")
compression_retriever = ContextualCompressionRetriever(
base_compressor=compressor, base_retriever=retriever
)
compressed_docs = compression_retriever.invoke(
"How was security during the 2024 Olympics"
)
compressed_docs
Xogier mna zinwable. Ub’t kugabus ho wse ozi ovfoalid iruds zko jekrunkoug hafgcujluol ritjsoxio voe nrupueocmj odrkebun. Du ebawa hpek puu qelas’w fad bidebaz fuas ywidjx to iyyouki cvic carol om irwasexz. Mui ulit Felowi’d Vafikkixl OCI po zokesutu ecqyuhil kiburbw. Yqe iljakan uucmoxe ow qtu dofyg ido. Dizuyuf, eyox rqi punh movigg ahahn hqixi kayewubjs oz qudponadikcjx kajqis yhow vnod bii ungiaw quwotlhp tdog yxe zagatate.
Kke diyw gonq uh tfu coyzeq fudv roeluqe e fucilpqyiziuq ef jlo da-vodvumk stgaceqh iw owraoj.
See forum comments
This content was released on Nov 12 2024. The official support period is 6-months
from this date.
Demonstrate how to build a basic RAG app with OpenAI and LangChain.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Advanced RAG Techniques
Next: Enhancing a Basic RAG App
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.