...
/مشروع التخرج: المسترد
مشروع التخرج: المسترد
تعرف على كيفية تكوين المسترد وتحسين استعلام المستخدم لتحسين وضوحه.
سنغطي ما يلي...
سنغطي ما يلي...
نواصل هذا المشروع النهائي من خلال تكوين المسترد لجلب الأجزاء ذات الصلة من مخزن المتجهات استنادًا إلى استعلام المستخدم وتحسين استعلام المستخدم لتعزيز وضوحه وجعله أكثر قابلية للفهم من قبل LLM.
Step 8: Setting up the retriever.py
file
سنقوم الآن بالمرور على تكوين ملف retriever.py
خطوة بخطوة:
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'])
السطر 1: نقوم باستيراد وحدة
os
للوصول إلى متغيرات البيئة وإدارة مسارات الملفات.السطر 2: نقوم باستيراد مكتبة
requests
للتعامل مع طلباتHTTP
لتنزيل ملف PDF.السطر 3: نقوم باستيراد Streamlit للتعامل مع واجهة المستخدم وإدارة الجلسة.
السطر 4: نقوم باستيراد أداة لإنشاء ملفات مؤقتة لتخزين محتوى ملف
JSON
مؤقتًا.السطر 5: نقوم باستيراد وحدة تضمينات Hugging Face . ...