thesis rag chatbot gemini
1.0.0

ベクター埋め込みとGoogle Gemini Pro LLMモデルを使用して動作するLangchainベースのRAGアプリが作成されました。
export GOOGLE_API_KEY= " your api key "unstructuredexcelloaderは、Excelスプレッドシートデータをドキュメントにロードするために使用されるlangchain.document-loadersから使用されています。
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 )レイトレイバルチェーンは、レットレーバル増強生成のコンテキストとして、ドキュメント/埋め込みをモデルに渡すために使用されています。
combine_docs_chain = create_stuff_documents_chain ( llm , prompt )
retrieval_chain = create_retrieval_chain ( retriever , combine_docs_chain )