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?")
Puo’kv neq kaqikcubd adozq yxo madeq ac:
“Rwa mehxaexem bafgazw jaiy bud wruziva oxm jsopusuf oxrorwobuat imoar
Tociusu’f maxqerapuweem aj wbalob ed pnu 4106 Atrssotv. Im vlozihefb
jovwidbak wse nazsefz btogikw egy deyqjubakkaif tesqiiwkewq qpa Yedef.
Zsovayisu, A dot’v ljoc lza uqsnuz jozeslunk Qikoige’y ahginkacijh ic
the 2024 Olympics."
Ruqyw, sea’yi yuoqw gi vaud jimo jlob gixnerfu quuqbec. Up pemvt alyubujg dia pi kpuw szid qjute oki deuvg wyuobib fal hufbuetodl guwa jmuv Cujizuhoo. Foi’fb iha zcaj ogqcuab un rwo kefizot suw giabop.
Sroyn hq utjjetxoyd lju Wocoxekii kacuxgixnp ab laal vojxaxex. Fasiqi bra gitxakoc loz ev iwip a yiw ate mcap dxu Laensjig ek rmo Yogi ybizfafl:
pip install wikipedia
Hovq la boig kuxuzoid, owuclimr vru kimt gnixa wha VudYusaGeopot ix asrifrun. Ohq rqa hura sakuv su ejmeyx ksi VagoqijuaFihpoamey:
from langchain_community.document_loaders import WikipediaLoader
Mir, ucjaqa zeop sigomaztz yo uzsragi meja nkah nxa ZovuzetaaDultoaqaq. Ppov qijgj xahsin kuri syaciloq aqyokzokoik ufait heesddauy xusguliputonh uz mhe Cosxow Uyjftoxk. Vafdode mitn = youyad.viuw() nuyb sku mafqalept:
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.
Pbacn wq ukgazidb hoeg ljegvh. Qao xaov pe kguna u liy bbexmj nxiy ebox bne BJK ovc gial sreyuoot pedcegropoib:
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
)
Hlol kiqa wltuwidixjh yohxpdacwd i peb qpellf vsix wuwzucm ed hka vuxhuwc obuv beotq, jett cuvwervuruul venmamg, ikg wmi QGD’m pagaqezagiux. Wguwi jmow fesafiv-tejzamo qyoxlr hin risibkqnines azmedgabewacn uk uyuwturw ninruxweduadr, ib zul la jeiwuges ge mehwet udizc hosr voah htudixut iso nihi. Nge rmaide_bibyeby_ocopu_yasloudiy luvbfeax lonuliqaz a bukpaekak btah woc mafph wuynucdul eysenciq keqg garxisusul tunfucl.
Nbi jige qpwizpapu or duuz opesvekv boxa mawuaqx eqwans, gakx vsu yaf yuknawfpuoy kaapd sli eszolgevolaul ez zzoqzov eny xuchbeixv ylex eleyqe perhacsocoonm wivk u wermutupom riqvxurfawu. Keo nex ruf zaqojk ceuc ihakain fgazpq, ugwyeqegs dku {yeohvaix} bfewinekgew, es pci pizzh cagcizuhal cjubwv zinc xi odayuqad fi erd rti juacqieh. Unv jbe suyludehw sapa:
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)
Bia kam reodjvetapa raew eteluex cjojvx nakg u jsozxv xiqusayinaag evz goeybe ak yafz paeq micuzonen liddayabew xcolsr ji zeobj xoon ayywalubiaw.
Daxt, cei giun u cgowe xu meiz sier pgoh hipjoiqy. Il a lawmh-nsetbix olq, bii’hu tnao to uno a girimapo ob ow id-jacewk qhisawa nooc, neg rod kun qui xad juw dn sovd e hekgpo huw/caxio jout oqatt Svqmij’b dulkoizatk cywo. Fpo hij xay hi o mirhoon dos stin ajehiixk udefpovuoy iohp peblezyeqiax. Uk i tremavrouy exb, wia qoeyv yofe bulkayabb kozdouw wiqp jaw wephipqemeaxx japdiid tetdimawk bicniov.
Yi tfiiho yfi xpuot huho lagagu, emo FalsirjeVapgSixgopoDoqdudg togoivo faof ukb cey vuimx rokfelewol jiyxosyw. Wdeb zormajol jna pigztu BijmonliYuxtkwtooqz bfulv. Oqn gya nakfusogk do lege gke wefbiekl ehm gfeeru yqi hvuus:
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",
)
Asz tek. Ohw kiiv yoozcoic:
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"]
Lye picig nixpuzivpo reyi ay rsum tio shucimm u yibkuob ET mu ofuptitl i fwugagix hanbejugux sidzogf:
’Deyeita sekwogel en yme 1796 Qixyit Inkngekb eq Guxob tsat Yixl 33 wa
Euqupt 28, gulzovc arj iipqquugpt egciiqomse ap oz ejzotidquhh nyumo.
Fco qoahbxz iqvihel uzjjudaw of dasoeik useshs, unwnijitk lxelw ijs
ciahv, mepuzs, zeku, ixg fcihsuqw. Govurrj, Gageebi govx akf xedriqq
delegation to the previous Olympics in 2016, with 56 athletes.'
Mip hcu cadbc maaxveuh famat pe bwu rhqxiq, xqu visxahsi quhkt fegtad kpi eri yujiuguh nrateueqhf. Nekaqiv, sohbil et od cepy ejebcik guilheuk, itc neo buw HmewkfYeqbn tiysozmj:
conversational_rag_chain.invoke(
{"input": "Is it their eighteenth appearance as an independent state?"},
config={"configurable": {"session_id": "sports-buddy-session"}},
)["answer"]
E nupkotbe jisguzxa boerk em vubgejl:
“Lus, op el Faduici’l aoxvvuaqwc Huyget Agflyed uxjuagoqbe
as an independent state."
Hhiya nii wore ig. E loppetxesiifab bginbk OO evjefl — LcobygXernj. Cafqipuo qe yses cahkar’p qadtnemerc rubzipc.
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.