
แอพ RAG ที่ใช้ Langchain ได้รับการสร้างขึ้นซึ่งใช้งานได้โดยใช้แบบจำลองเวกเตอร์และรุ่น 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" )FAISS (การค้นหาความคล้ายคลึงกันของ Facebook AI) ร้านค้าเวกเตอร์ถูกนำมาใช้เพื่อสร้างและจัดเก็บการฝังความหมายสำหรับเอกสารที่โหลด จากนั้นร้านค้าเวกเตอร์สามารถสอบถามได้ในภายหลังด้วยการค้นหาความคล้ายคลึงกันเพื่อรับข้อมูลที่เกี่ยวข้องมากที่สุด
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 })PromptTemplate ถูกนำมาใช้ตั้งแต่ 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 chain ถูกนำมาใช้เพื่อส่งผ่านเอกสาร/การฝังตัวไปยังโมเดลเป็นบริบทสำหรับการสร้างเพิ่มเติม retreival
combine_docs_chain = create_stuff_documents_chain ( llm , prompt )
retrieval_chain = create_retrieval_chain ( retriever , combine_docs_chain )