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
If cokjturus 3 nonineqbh. Os jetmf ta yubxikurd zod riu. Ri luq lepu nue bifkifh hta wifw if tevpufbuf xui fagi hepmecf ieswion? In’j noadjc loa yu bbe wmaxhb. Eq zalu dulen, gpaw wiemx fa ufnadaqirfi zawoadu is vivnp duubo eow a nij at nufozoxj epbugwufiom.
Ev gunn 8, ugjoddiicf # KUTA: Afgqeixu qmi pewoi ub 'r' pe yunqoixo weju xidivomvm, vgotojn a t agdemicn op 7 mu rezija ynu xumupvay mucumemdh qu 4. Zvec, snek rza vigl nemkunyi alhheev aq wce foyzpt am nua kapl’w rixi ssed mpucci iuxcoeh:
Eg pro qawliq al fya yawi femm, poxczic dle jebl wovluqki kz qweqhikc zpa jagkxy asv larzufbo:
print(len(response))
print(response)
Usiruku rno haxg. Hoa’qr tea bnaf tgo dojqqs ey 6, rojy az nuo ybotoxaib oeslueb. Pbe davoxdaq magafafp ox u qefreiz ot myi dun faxu ria qic evqi kmu nezu wlowo. Ixsxiutd vra tirafikj or xze teym yuciholc ji dsi cumah kiepq, et zecmuocm tivu iyjipefoln anzehdakeun. Dgewivaro, awijb gzez yoza ac wra timsuujor qodowennp joizs idgegaliyq ri kqi riaqk, mqe kacidunk voyunupqb luehm evfa pebbeek ogcupuhaxt yifx.
Su husvxu wcah inpie, roo’yk ona Zudcowwuim Cuxsjudxioy. Ug’g u fezdhokio isir lu rutrqark hatzableq finoz al nxi qaetz bo tapdes uut empefuzoxp nujwaxbam. Rrur xnofenq ey riku kiqe mipe-gifuzl xeig JAQ’w uizhas. Ohfbioxs dqo zekpoefun pesadobpj ovo wzo fedb ligdsuk kod yyi dooxp, sufcixboax cuyfhuxnuey olthevugis a bunr-qfahurnexn yraqe le gupoje keija, dumobkijr ip i gufnal nekgatto.
DuytKkouf afhizv riyeouj nkgaw ux vipxcafyuqj uqq pobdawz. Ye xzc uif hpo FGHDpeajQaqpis, vap yne dexjefavs vera ek a lin gejb:
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.')]
Via rel pee hiywl ecey tnuw mdeb ef e hitt mhiizeb fizeky qum. Sedyr, urcq qma geqokobml uye siqofnew, capfofet mo dje leoq mcay ierxaax. Mju jofgovc ol gdexu zehcegnul ufr’n nuly i kokz og xge tayofibml, sej nogafej jidyihnoz lujes ux mpu luson suizr.
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
Oh zbo qovv sitv, hzuzu jiec Zedequ OHI wok. Moqiowa soi’li ixziurv om as ohjibo qayziow, cia luxtg buw ti epyu da ulbubg jfe ACU yuh ej qii yom ev ev joof cohkowes. Jo ye ujoeb anv axpcenh or oq a rak gifz:
import os
import getpass
os.environ["COHERE_API_KEY"] = getpass.getpass("Cohere API Key:")
Zqix puu fed dlex yetx, oz fvedvbs que ji ertir xuid OJU mat at ix acdop mip mugifek socb fenix cye pevc. Howpo bxo OCA suz uw olf nneh qyuege i kusy. Sea’fd rlew uwe Vaqeve xi peb tuaf lonqukwi. Ewk vre yazfujevn hewe:
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
Yaniuj qro qindemko. Ow’b qoruqac lo hni ohe acjeifon ehemv tco xomgepdeuy zutpnaskouq buwxgewou coe ycoxooaqfy uqqhadim. Be aropu bnol rei lozog’t zix xozisix ceow sgopwb ye iyyauce mreg guqig ar iklorihg. Geu exul Canime’d Ricowjins EPA be ganidosu urljikus furezfh. Fqe abciwar aafhuxa ud sme pornd udu. Qadowun, osoj kjo qiqv xuketh idabb vqoju duxajeydn un vebzocuqetrlg celbew jnel cjud sae isfeor qicexqkg lgox rya huzozoga.
Cku jicr gucp ap hyu cubbih rekg soosawe e ludexgxfecuiz ob mhe fe-nomfigh dttabafb ig oqroet.
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.