Capstone Project: Retriever
Learn how to configure the retriever and refine the user query to enhance its clarity.
We'll cover the following...
We continue this capstone project by configuring the retriever to fetch the relevant chunks from the vector store based on the user’s query and refining the user query to enhance its clarity and make it more understandable by the LLM.
Step 8: Setting up the retriever.py
file
We’ll now walk through the step-by-step configuration of the retriever.py
file:
Press + to interact
import osimport requestsimport streamlit as stfrom tempfile import NamedTemporaryFilefrom langchain_huggingface import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import SKLearnVectorStore# Suppress warningsimport warningswarnings.filterwarnings("ignore", category=UserWarning)warnings.filterwarnings("ignore", category=FutureWarning)os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'os.environ["TOKENIZERS_PARALLELISM"] = "false"################################################################################################################################################# Function for retrieving the vector store #################################################################################################################################################def build_vector_store(content):if content:# If the vector store is not already present in the session stateif not st.session_state.vector_store:with st.spinner(text=":red[Please wait while we fetch the information...]"):################################# Fetch the embedding file ##################################embedding = HuggingFaceEmbeddings()embedding_file = 'https://raw.githubusercontent.com/Samuelchazy/Educative.io/8f0e764c1b69e2d61f4e44e3084c0695d85cd6e8/persistence/user_manuel.json'# Download the embedding file from the URL and save it temporarilywith NamedTemporaryFile(delete=False, suffix=".json") as tmp_file:response = requests.get(embedding_file)tmp_file.write(response.content)tmp_file_path = tmp_file.namevector_store = SKLearnVectorStore(embedding=embedding,persist_path=tmp_file_path,serializer='json')######################### Save the vector store to the session state ########################st.session_state.vector_store = vector_storereturn vector_storeelse:# Load the vector store from the cachereturn st.session_state.vector_storeelse:st.error('No content was found...')#################################################################################################################################### Function for retrieving the relevant chunks from the vector store ###################################################################################################################################def retrieve_chunks_from_vector_store(vector_store, re_written_query):########################### Perform a similarity search with relevance scores ############################with st.spinner(text=":red[Please wait while we fetch the relevant information...]"):relevant_documents = vector_store.similarity_search_with_score(query=re_written_query, k=5)return relevant_documents################################################################################################################################################# Function for retrieving the chat history ###############################################################################################################################################def retrieve_history():############################## Go through all the chat messages in the history ###########################for message in st.session_state.messages:with st.container(border=True):with st.chat_message(message['role']):st.markdown(message['content'])
Line 1: We import the
os
module for accessing the environment variables and managing file paths....
Access this course and 1400+ top-rated courses and projects.