Search⌘ K
AI Features

مشروع التخرج: كلب الاسترداد

تعرّف على كيفية تهيئة أداة الاسترجاع وتحسين استعلام المستخدم لتعزيز وضوحه.

نواصل هذا المشروع النهائي من خلال تهيئة أداة الاسترجاع لجلب الأجزاء ذات الصلة من مخزن المتجهات بناءً على استعلام المستخدم وتحسين استعلام المستخدم لتعزيز وضوحه وجعله أكثر قابلية للفهم بواسطة LLM.

الخطوة 8: إعدادretriever.py ملف

سنستعرض الآن عملية إعداد...retriever.py ملف:

Python
import os
import requests
import streamlit as st
from tempfile import NamedTemporaryFile
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import SKLearnVectorStore
# Suppress warnings
import warnings
warnings.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 state
if 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 temporarily
with NamedTemporaryFile(delete=False, suffix=".json") as tmp_file:
response = requests.get(embedding_file)
tmp_file.write(response.content)
tmp_file_path = tmp_file.name
vector_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_store
return vector_store
else:
# Load the vector store from the cache
return st.session_state.vector_store
else:
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: نقوم ...