|
|
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
from langchain_google_genai import GoogleGenerativeAIEmbeddings |
|
from langchain.prompts import PromptTemplate |
|
from langchain_community.document_loaders import PyPDFLoader |
|
from langchain_text_splitters import CharacterTextSplitter |
|
from langchain.chains.combine_documents import create_stuff_documents_chain |
|
from langchain.chains import create_retrieval_chain |
|
from langchain.vectorstores import Chroma |
|
|
|
|
|
GOOGLE_API_KEY = "AIzaSyCHLS-wFvSYxSTJjkRQQ-FiC5064112Eq8" |
|
|
|
|
|
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=GOOGLE_API_KEY) |
|
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=GOOGLE_API_KEY) |
|
|
|
|
|
loader = PyPDFLoader("handbook.pdf") |
|
text_splitter = CharacterTextSplitter( |
|
separator=".", |
|
chunk_size=500, |
|
chunk_overlap=50, |
|
length_function=len, |
|
is_separator_regex=False, |
|
) |
|
pages = loader.load_and_split(text_splitter) |
|
|
|
|
|
vectordb = Chroma.from_documents(pages, embeddings) |
|
|
|
|
|
retriever = vectordb.as_retriever(search_kwargs={"k": 5}) |
|
|
|
|
|
template = """ |
|
You are a helpful AI assistant. |
|
Answer based on the context provided. |
|
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) |
|
|
|
|
|
response = retrieval_chain.invoke({"input": "How do I apply for personal leave?"}) |
|
|
|
|
|
print(response["answer"]) |
|
|