1414from langchain_core .runnables import RunnablePassthrough
1515from langchain_core .output_parsers import StrOutputParser
1616
17- from transformers import AutoTokenizer , AutoModelForQuestionAnswering
18- from transformers import AutoTokenizer , pipeline
1917from langchain .callbacks .streaming_stdout import StreamingStdOutCallbackHandler
2018
2119# for streaming in Streamlit without LECL
@@ -61,9 +59,9 @@ def on_llm_new_token(self, token: str, **kwargs) -> None:
6159def format_docs (docs ):
6260 return "\n \n " .join ([doc .page_content for doc in docs ])
6361
64- ####################### RAG #################################
65-
62+ ############################################## RAG ########################################################
6663
64+ ########## Creating prompt ##########
6765prompt_template = """Use the following pieces of context regarding titanic ship to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
6866
6967{context}
@@ -74,7 +72,7 @@ def format_docs(docs):
7472
7573prompt = PromptTemplate (template = prompt_template , input_variables = ['context' , 'question' ])
7674
77- #VectorDB creation and saving to disk
75+ ########## VectorDB creation and saving to disk ##########
7876client = chromadb .Client ()
7977
8078persist_directory = "/Users/raunakanand/Documents/Work_R/llm0/vector_stores"
@@ -86,7 +84,7 @@ def format_docs(docs):
8684)
8785vectordb .persist ()
8886
89- #VectorDB -loading from disk
87+ ########## VectorDB -loading from disk ##########
9088vectordb = Chroma (persist_directory = persist_directory , embedding_function = embeddings , collection_name = 'chroma1' )
9189retriever = vectordb .as_retriever (search_kwargs = {"k" : 3 })
9290
@@ -107,12 +105,14 @@ def format_docs(docs):
107105 # callbacks=[StreamingStdOutCallbackHandler()]
108106)
109107
108+ ########## When using RetrievalQA chain from llm's chain ##########
110109qa = RetrievalQA .from_chain_type (llm = llm , chain_type = 'stuff' ,
111110 retriever = retriever ,
112111 # return_source_documents=True,
113112 chain_type_kwargs = {'prompt' : prompt },
114113 verbose = False )
115114
115+ ########## RAG's chain in langchain's LECL format ##########
116116rag_chain = ({"context" : retriever | format_docs , "question" : RunnablePassthrough ()} |
117117 prompt | llm | StrOutputParser ())
118118
@@ -121,7 +121,6 @@ def inference(query: str):
121121 # return qa.run(query)
122122 return rag_chain .stream (query )
123123
124- print ('final' )
125124
126125
127126
0 commit comments