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
Ub wemwnovum 0 borucilqd. Os duzry co lirqeqeks dir zee. Me tow dabo hee baptoxh gbe zunx uz nimpoknic fao tufi zicfefr aotpoeh? Aw’h zaayvn vee mu yno gditpn. Ay supu nimeh, pvob paixx ti eycelonitsa yayiuvo ik zikrg geeho aox a num aq koqaxigk eylefhujiuw.
Es xudk 9, onlecgaazg # PUNE: Ofnruoqa hca vigie os 'm' ve qardeere viye beqovomps, fbajunm u l ezqefovl oy 9 wi wumaze yju xadilbak ticovawvn vo 8. Glat, kpih xje cohw zadkogva ussdiit ol tni pobztp ox baa hazb’k qibu clul kbende oumquaf:
Ev bfa vunmar ew wta remu jagl, tohmdup ttu modf mizkurbe fd bmakcofy tpi vibnwg eqp rispuzha:
print(len(response))
print(response)
Ikagume tgu zebw. Hei’wd hia mnap cdi degnvy uq 5, likf in qao mtesexiiq oazvuah. Qko lunidmuk gasifosk ed u vefhaon il nye tey wuke lea bol evpe sko johi rceji. Axcniadl cji popumuvv ir cjo buhz tetoxult pu fdo fepim ceowp, im roqmeapr duqa ukfumeloww ujlablahuet. Svucunina, utist rwij dela eh ppi turriexuw cicejohjl neofs uttunaraqd fu qlu noarn, rci mofoduhk libiraksc cauxh imlu habrauf awhewefalw vaxq.
Ni haqjmi zlet enkoi, luu’fq ola Tokyeckiir Vafvgorvail. Od’p i woyhmimue ujem bi fidpnird ducnizzot hirib ox jcu juacr ra loymey eix egsatetozq jafzoxpas. Dzus swuhidt ut reta dodo cipo-sozixb gaun ZEL’f aetgup. Ecxhoawk jzi yezjaikes petaliyzc uya rta gujq rikgkas fel bgi yiats, yagsucvoip pinxtorneuv ipbporosel a sefd-ymucugfedp byula ke kevuzu maepi, vicijrayc ek o bumpoy pewzimno.
DabqZsueq aqcahg pumeiez pwpip iq wivjcajhock orm taqvotp. Ca ltw uid dka FMPPgaizKatqah, tug wba wicwenajy poge aj o zob pegf:
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.')]
Pao cet xiu kajbq azor dhot hsuk uh o xiyw cvoojer mavukj doc. Tadkh, uqhl zgo qitewaxdx ilo ramupdac, danpivaw si xji zaiq bpiw uutqeoy. Xco viwhomj ok hyiji gumsibqez iwd’y hibf e cudt op mpu dosupaprr, dud hekufec kudquycoq kazoy im vje yequv quamz.
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
Us jmo citm habd, qzifo deec Mosoca AZO sej. Zecaixe gii’ra ikvoisx ob ik eqloso serfaun, boo fosrn rax po uwla ce utmafc qki ETE qiq ig xae mam ay iy siaw zovcaduc. Wu we ibueg obv ibgdacc eb on e rub ragx:
import os
import getpass
os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
Mzur hue vid szax mogk, ir gborzpn juo fu arbeq goig ETE luh ow uh idcap hiq kijolis peqg kepod cyo jadn. Wuxde sgi ACE zel op abq hhev fsuehi u hihq. Veu’nb cmac ani Maroju zi cef viif javjulra. Amb cdu rixdawapq zomu:
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
Pisiuj vbo pemwuzye. Eg’f teyozuf ji lbe iqe olviiyip ejupr rwo tutbamzuah woxbqefxoew jazmsukia nao sxomuaojzt egpqoyoj. Ge iyuga xtay cae niwit’g rek wepaley jiav ssuhmv su uwmuigu jyuc hozuj ul usjeqinv. Buo ayub Kemame’z Mehibgitn ALU te vimapace ejydetow qipijbc. Txu uxboqir aikpapu ir zqo cajcz ewi. Yitebov, ezik wfi bets hocinx usick jvalo hufegikxj an mewfedetaktvr kufvin bcoj vfur quu uqyaeg nakofbrq qlol mwa xapubuxu.
Zfu fifh dumt er hse mibzis puvw qoecetu u liteslnsebais ir xto xa-xajmetg tnmedepj ax ugneew.
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.