| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 
 | import bs4from langchain.chains import create_history_aware_retriever, create_retrieval_chain
 from langchain.chains.combine_documents import create_stuff_documents_chain
 from langchain_community.chat_message_histories import ChatMessageHistory
 from langchain_community.document_loaders import WebBaseLoader
 from langchain_community.vectorstores import Chroma
 from langchain_core.chat_history import BaseChatMessageHistory
 from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
 from langchain_core.runnables.history import RunnableWithMessageHistory
 from langchain_text_splitters import RecursiveCharacterTextSplitter
 
 
 
 
 loader = WebBaseLoader(
 web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
 bs_kwargs=dict(
 parse_only=bs4.SoupStrainer(
 class_=("post-content", "post-title", "post-header")
 )
 ),
 )
 docs = loader.load()
 
 text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
 splits = text_splitter.split_documents(docs)
 vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
 retriever = vectorstore.as_retriever()
 
 
 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(
 chat, retriever, contextualize_q_prompt
 )
 
 
 qa_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, just say that you don't know. \
 Use three sentences maximum and keep the answer concise.\
 
 {context}"""
 qa_prompt = ChatPromptTemplate.from_messages(
 [
 ("system", qa_system_prompt),
 MessagesPlaceholder("chat_history"),
 ("human", "{input}"),
 ]
 )
 question_answer_chain = create_stuff_documents_chain(chat, qa_prompt)
 
 rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
 
 
 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",
 )
 
 conversational_rag_chain.invoke(
 {"input": "What is Task Decomposition?"},
 config={
 "configurable": {"session_id": "abc123"}
 },
 )["answer"]
 
 conversational_rag_chain.invoke(
 {"input": "What are common ways of doing it?"},
 config={"configurable": {"session_id": "abc123"}},
 )["answer"]
 
 |