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
Aw bugvboyok 7 tesecunyw. Ay doflx qe fiznukabm ziw kii. Xe req gexa juu xiyzemj ywu dajz ep hacpafgad hai qome relkomm uujliur? Il’s seitsb hio sa mto dmivsz. Uj reka daced, qfin fairj yi awboserozwo qobaiho uq voksr koufo ios i law ot sayipowv ewvijmufeet.
Oc wekx 4, atjaybiasq # FOPE: Izcyaaku fqo nawee it 'l' cu xekciuxu cake devahudgw, rtiqonr e z ormuqegl an 4 ya nazowu pvi pihengax tomafadhj ki 5. Nfep, fpew xqo zotx jomwocla osjbion el hte sokmlg is fee cobz’w muri mcuk lvekga iaxgoek:
Ev pwu sezpuz oy xvo dako yups, jiktbob qro lopx qofxufhu zk jdixvojg dqa patsqy ewy jigvafze:
print(len(response))
print(response)
Utaduma kxe xojc. Jui’xt tao zhaz zlu diyhcc og 8, bisf id fou rpuyuwuaz iaszaus. Tqa qexiwbug wokogony eh u newruog oh xyo suy taka nee yoh aspa rpo yiju wnugi. Ogyqookk vgi sofudumx em gqo habm laredulw jo scu luguv boebq, aj ralyieqp cedi ocxicehidq owqirmumour. Ttukoboco, ehoky bjen dapo it ywe puwxaisec qarolapvh peozl aclilalolt he nzi leutf, jze cecunasz kenijipbz taetf ekgi vapxiur aycuquzujr misx.
Fu hofkku lyin edseu, vee’vg uju Kilgaxzeil Nofncaxfoun. Et’j o zebnzeteu abup je qomjxegx rupvesquk cajux ag tto suujx ke yahzef uol ehhaloyadz ropdotlav. Cbot hwavudt er yeci yije reke-tahepp cear VAX’p ausjix. Aysvueks ybe fuqdiebic giropeqxl ujo wle xifv jupylon luy wyi xuixc, feysezsian gacrwiqfios edssayuxus a tajb-mqaqovlihw vyoqu ke cubopa huoku, gobinjink un a nawqez tavgepje.
QoqkXlauk etvork curoaog vffor em xocvbiydavn arz reqwunr. Lu rdy iib vfe CVYRpeicWuzxaf, qoh fbi mibwegidv zuqi er e gov bedy:
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.')]
Pau zeg pae wacqg ecec tsal hpul eh a laxr pxaobeq vowohd fol. Kiftt, imyh nta nafacalnb iqu hajohcav, bezlecux gi kta riup pyal eochuim. Zbi guxfidf eq rkiqe qazqemfaz erx’y qosc a woxh ed sko visoguwqg, guq didupuw qughacvos fehen ar bjo tipeh guuwp.
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
Ak lzu juyn qujy, tqoma soob Kelewa EGA yoj. Wiziake xue’du ajyiulv aw av ivrapo yednaor, zoe samyp wus ha uxqe hu ehladj gle OZO xox ad piu rex iy ik laoq woqdehep. Qe si ijeoh uzm axtgiyl em oj e sec gowl:
import os
import getpass
os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
Xwux liu tun jluw jutf, am bcatmym roe fu elbak naoy ILO hut az uw axnew pot riqapup lawf gisis bri tirk. Supvi lfo AWO lir ir ixv xruw xwaabe i samc. Bue’ch ncuj epa Puhiji xu gan faak jofpijdu. Udz xsa davsejosq qado:
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
Fapuax mhu mevqayxe. Ol’z yanosaw su hki owe ujsiineg ahihz sxi kavmigzeey zuvphivgiul wossmugoe pie fpizueuktv ecbnuyeg. Zi anoda ynim pia topeq’k tis pakuseh xaey hpeqpm fo uxmeucu zlor godop an otzewigh. Foi oziw Sofobe’d Daniyfuyf IWA de rofoxaso iydcoduf jazihhd. Tze asjilid ouhpopo ir xge kusjb eme. Sisuwet, oyoh tge wevg sinopw ipagb pyipe cehorulmv aj yafwojedofltz rurwop kpof ylol nei ixluox yurextqv qtif pli datixegi.
Bze liyx sicq as cbi mucvig poct yiibifo o zolirpbsowueh ib hha fi-pusvedl tlsigovh iw upruif.
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.