
تم إجراء تطبيق RAG القائم على Langchain والذي يعمل باستخدام ضمانات Vector ونموذج Google Gemini Pro LLM.
export GOOGLE_API_KEY= " your api key "تم استخدام UnstructuredExcelloader من Langchain.document-loaders التي تستخدم لتحميل بيانات جدول بيانات Excel في مستندات.
تم استخدام نموذج Google Gemini Pro للحصول على إكمال الدردشة السياقية.
llm = ChatGoogleGenerativeAI ( model = "gemini-pro" , google_api_key = os . environ [ "GOOGLE_API_KEY" ])
embeddings = GoogleGenerativeAIEmbeddings ( model = "models/embedding-001" )تم استخدام Store Vector Faiss (Facebook AI التشابه) لإنشاء وتخزين التضمينات الدلالية للوثائق المحملة. يمكن بعد ذلك الاستعلام عن متجر Vector Store مع بحث عن التشابه للحصول على معظم المعلومات ذات الصلة.
vectordb = FAISS . from_documents ( documents = docs , embedding = embeddings )
# save db as pickle file
with open ( "vectorstore.pkl" , "wb" ) as f :
pickle . dump ( vectordb , f )
#load db from pickle file
with open ( "vectorstore.pkl" , "rb" ) as f :
my_vector_database = pickle . load ( f )
# get 5 most relevant similar results
retriever = my_vector_database . as_retriever ( search_kwargs = { "k" : 5 })تم استخدام kordtemplate من Langchain لصياغة مطالبات فعالة والتي سيتم نقلها لاحقًا إلى النموذج. يحتوي المطالبة أيضًا على متغيرات الإدخال التي تشير إلى النموذج أنه سيتم تمرير بعض المعلومات من قبل المستخدم.
template = """
You are a very helpful AI assistant.
You answer every question and apologize polietly if you dont know the answer.
The context contains information about a person,
title of their thesis,
the abstract of their thesis
and a link to their thesis.
Your task is to answer based on that information.
context: {context}
input: {input}
answer:
"""
prompt = PromptTemplate . from_template ( template )تم استخدام سلسلة Retreival لتمرير المستندات/التضمينات إلى النموذج كسياق للجيل المعزز Retreival.
combine_docs_chain = create_stuff_documents_chain ( llm , prompt )
retrieval_chain = create_retrieval_chain ( retriever , combine_docs_chain )