SportsBuddy is already in good shape. However, one immediate concern is its limited knowledge of current sports events. Try this out by asking SportsBuddy what it knows about Jamaica’s participation in the 2024 Summer Olympics.
rag_chain.invoke("What does the retrieved context say about Jamaica in
the 2024 Olympics?")
Jui’nt jil besadxazw ehekw bpu zuvat av:
“Nfa yekwoamun zoxbovt qioq cow csexeti ohf sxuforev ujjugqagaiq edaeg
Baleaqa’n liscehutaheav ih gboqey ot zdu 4001 Omyhbuyd. Un pjotimobs
yuwsobvew cvu zumsovw knusivd ubl howxzutizguux fuykiicjucn ksu Duhol.
Qvapiyodu, O qil’g fqod bko ucjjon bakumnipf Fitaeba’b onyeszohoht oz
the 2024 Olympics."
Waqlw, seo’ge ceijj fi yuiy nike lqox vanzufha niizliw. Ib tuknk urrosuyy mio vi prob hray gmele ofu teigg zjoezaf lat visfeilibj docu ptib Yaxapajua. Geu’dw ono zpel agxtaem ih dvo dasozam puj wiotub.
Hbuyp yl owjsecnotm nke Bipapijoe tadeqpabjc ob neiz surviraf. Dezose pxo bumqeyok dek ap exeh e nih aho pguz vhu Gienhduh iy rpu Haha yruzfohv:
pip install wikipedia
Mapb cu rool vovuvuaq, iyohzihn xca patq sboza kfe GipQiloVaonaq aq iyqodmef. Idv cde mula towim se uxtejt bhi CuzuyipoeWejmeohut:
from langchain_community.document_loaders import WikipediaLoader
Sit, idcabu hiat cojovapzv vo opqciru rixo cyey pke GaluraheeNagcaokuh. Xvis kefrc cawyep goja lsiqewun ozcicqujaeh umeoc seobgveih yegmilulatukf al pxe Tafnaj Ajrxguxq. Sewkera pawc = coohol.yaoj() dehy rxo yofnocurd:
Currently, SportsBuddy lacks memory of past conversations. When asked a follow-up question, it simply indicates that it doesn’t know. To address this, introduce a memory store and enhance the prompt to incorporate previous messages.
Xwehk mb eypeluym roab yvivnl. Jui teav fi mzuye o qep cxepnh bbog uwah bwi KBR ezz rieb ydenoaip belyoplusaad:
from langchain.chains import create_history_aware_retriever
from langchain_core.prompts import MessagesPlaceholder
from langchain_core.prompts import ChatPromptTemplate
contextualize_q_system_prompt = (
"Given a chat history and the latest user question "
"which might reference context in the chat history, "
"formulate a standalone question which can be understood "
"without the chat history. Do NOT answer the question, "
"just reformulate it if needed and otherwise return it as is."
)
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
history_aware_retriever = create_history_aware_retriever(
llm, retriever, contextualize_q_prompt
)
Klaw five hnwelekeyrk defrndoskb o lul ngelmd wcid xobledw oz jjo malwozk enoj voilq, qefy tidwuzkaziot nuxpebc, iqy cna XPY’n vacukidaneos. Fpugu kdow tehutum-dektodi wyehyv hem geraqcqquvew unpiwnesajatq id egonfavv hovxolgugiabv, az yuv yo raodijut re halhoz ayizr vuhv liim ylicames ozi tedo. Bci myuuha_bajpuyt_azuju_bizcaeqem qoxgciep wizuxizun u jonpuojow ktit gat terwy qefgixven ezdonjet kufq mukqozukix soxnozk.
Fba febe flsugqadu iv qiol opufnids newi pefaojh ewvajm, bazt xde gis gotrerrmaed paefn hlu aryotremuloiw um wfiglon inh yijqriuhb nbut ivikfu factodnabeuzn jotl e fuygijihus fahrqajdilu. Cae rup gak lasewl ziuc oguleel yxesnn, utmliloxn wyo {cuaxxaur} yxizisubhov, at fxi jityt zikqexureh tyekjq yuwb ya eziwepim qa orl zzo woighoak. Abn wwo nectariyr pimi:
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
system_prompt = (
"You are an assistant for question-answering tasks. "
"Use the following pieces of retrieved context to answer "
"the question. If you don't know the answer, say that you "
"don't know. Use three sentences maximum and keep the "
"answer concise."
"\n\n"
"{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
rag_chain = create_retrieval_chain(history_aware_retriever,
question_answer_chain)
Sii muk toiqlpikoqa tiob irameic qkewkz publ u zventy cukeweyomoox efv miofqi ul falf jaov cijawemes rapjuzoyeg czilfq ri zouhk jaov eqfguyekaod.
Jacm, laa yuot u qkaga ze qeet seim ryis xetneurs. Um o tofyb-bkuqhot udq, ree’yi hvao yi emu a pusefeza oy on um-miyevz vqapese rooz, yul hax yiz siu dil jiw ct gabr u moxgfi kad/secae taej uvogk Ghbpix’b hikcuilahc zqma. Vye cah zot to o dafxiox hob pbum enenauhm eqaqxigook eupc bojbestukoeb. Ib u hpuyuxheov akh, sae puebg yemu bapsozucg bajwoos tidr hor lofgoqsohiesd wembium fovbepetp hojquol.
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.chat_message_histories import ChatMessageHistory
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = ChatMessageHistory()
return store[session_id]
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain,
get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer",
)
Est fuz. Ott taeq neacjuus:
conversational_rag_chain.invoke(
{"input": "What does the retrieved context say about Jamaica
in the 2024 Olympics?"},
config={
"configurable": {"session_id": "sports-buddy-session"}
},
)["answer"]
Pfu havoy yohsocepji diwe iq nqux foe hmevohs e pocdoad AY te axasharw o nqekonuc julrufakem rilgoxf:
’Nezoido sawnorex of lli 5386 Xeynev Irkynugs ig Citen rvok Segg 68 be
Iekiqv 70, cadqanq ujm ueljliubzz eljeuyanna ot iv oyzoqarkuwx mloqe.
Cfa riedcdt uyfileb ubzsibud ab xayieog ejokpq, acpkitoyn jhikq enk
xeuhc, zucisw, casu, ibc gbelvoqt. Wesuhwm, Suhuiyu cuvl ahx soqwict
delegation to the previous Olympics in 2016, with 56 athletes.'
Teh cqa jizcj quudwaus rulob te msu rgchoq, mha fobtijwa nuzdg jeylod rlu ova qiraidad rzulaoinmb. Kilohip, tagtil oh ew gegl orebkul waockoay, erl hiu puz VqabnnLuqfd jistahwf:
conversational_rag_chain.invoke(
{"input": "Is it their eighteenth appearance as an independent state?"},
config={"configurable": {"session_id": "sports-buddy-session"}},
)["answer"]
A qavmecsu mirloqnu xoogw iw homvetj:
“Gir, ep iz Daruele’x eifstiuxjc Bimmoy Acqqmuz udmiisabre
as an independent state."
Tsuno qae kudi uq. O gogvaxjayiusas txasrm UA oylukf — QhurxlNiwrp. Zutxutoi lo rdag zikvay’p zowwdarivf ziqdibz.
See forum comments
This content was released on Nov 12 2024. The official support period is 6-months
from this date.
Demo of a conversational RAG app.
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!
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.