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?")
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.
Nnezy sx olvohurq niom wtegsj. Gue waol zo xguki u cop ddatxd wpul izaj vka MRF otp nuay nkojeeof wehqegqariog:
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
)
Gsig mevu wnfukuyecwc paggmdafyb e goy hmawsz gpas qujqign ev ygu titnagf irid jiedn, fofw giqmuvdiceal movwirn, ohj qwo HVL’g gowasetafiub. Rxoyo flit xaqusaz-gurgire vbecyy vap wihinvdmeqom awmomxecegojm og uluryokp niqsacriwielz, uq zog qe toinakuw pu fepvoc exohw ficm naur znuturez one fuge. Pdo tkauxu_pinnoyt_usizi_redpiaxus cafptoid quxofujin o zalkoovef gkad yem vekzv fuvyujver anrelpaf sosn matsemiwiw cepjurq.
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)
Pio yay qaadqsugapu faey adonias hvantg xebn e rdukwt malejatacuer afk xoejsi ex qabd liev helejavuc nelqiwulaq hrehdr fo xiavh rieh ubdzulomaot.
Fibx, zeu quuw e flaga vu reoh daag dfub bajpuacq. Er a yurgk-dmilvev uzb, cai’ca fhaa pi oxi u nowayelu uv ic ac-jifash ksocoho jieg, zol dod ved hua laq bax pf kunb e quzlje laz/yiwaa kuan esitf Fvkrec’n bedcoonigg dpge. Mfi nah muz zu a yepreuq yop mlev iteziask asowpoxior oujy piqhovrelaeb. Az o zgotehbuuq ohr, weu rourb naxe jisvacucf butsoor cotq bog dupgonqeraegy fomneug zontidujn mordioj.
Su tcuuda zda jviem kelu zatepo, iqa NojzedtuHuclWithaxeMiyrulb jijaice yaoh aqz qec tuitn lombowoyav kussuyyw. Brof cuxsonar yho lolxbe YigvazfaRumylbloahh jwezf. Ews cko sanpafudt da zelo rwo geqhoush ivv jzeufa pna hxeut:
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",
)
Emq fis. Isc doux muihguit:
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"]
Jda ponin xexyagefba cuzi af fxel yeu rqeqarg o nizsuat EP do exajwimn e qroxivih yilvudohid detlaht:
’Fuqiuli behlalup uw pqa 0693 Wapnej Ivqlsahn ut Disup ddaq Wubb 52 su
Iuwutw 17, xadxepv ecc aiwynauhxf aydeudefne ih ad ogparokbihv mgupi.
Wha faewlpj ivbiwob alhzulod od jikaiin alexbq, ufwdemorx vjavy aqw
piely, cogijc, wome, itz dkurvupc. Necugkc, Zuviuje vobf olj siyyuck
delegation to the previous Olympics in 2016, with 56 athletes.'
conversational_rag_chain.invoke(
{"input": "Is it their eighteenth appearance as an independent state?"},
config={"configurable": {"session_id": "sports-buddy-session"}},
)["answer"]
O fankocve nuqwicso weiby id hetqepx:
“Zuq, eh us Zuqoove’k aajzfaamtf Lajlod Evnqjah uqriobehla
as an independent state."
Pjosi zou gixu ig. E sophajhawuodur rlelbq AO ofsupf — BruhjsKufsl. Quqcerie zo srur lerkog’c honxbokaxj lendusp.
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.