sidcww commited on
Commit
e46888d
·
verified ·
1 Parent(s): 82b4514

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import Python modules
2
+ from langchain_google_genai import ChatGoogleGenerativeAI
3
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
4
+ from langchain.prompts import PromptTemplate
5
+ from langchain_community.document_loaders import PyPDFLoader
6
+ from langchain_text_splitters import CharacterTextSplitter
7
+ from langchain.chains.combine_documents import create_stuff_documents_chain
8
+ from langchain.chains import create_retrieval_chain
9
+ from langchain.vectorstores import Chroma
10
+
11
+ # Set your API key
12
+ GOOGLE_API_KEY = "AIzaSyCHLS-wFvSYxSTJjkRQQ-FiC5064112Eq8"
13
+
14
+ # Load the models with the API key
15
+ llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=GOOGLE_API_KEY)
16
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=GOOGLE_API_KEY)
17
+
18
+ # Load the PDF and create chunks
19
+ loader = PyPDFLoader("handbook.pdf")
20
+ text_splitter = CharacterTextSplitter(
21
+ separator=".",
22
+ chunk_size=500,
23
+ chunk_overlap=50,
24
+ length_function=len,
25
+ is_separator_regex=False,
26
+ )
27
+ pages = loader.load_and_split(text_splitter)
28
+
29
+ # Turn the chunks into embeddings and store them in Chroma
30
+ vectordb = Chroma.from_documents(pages, embeddings)
31
+
32
+ # Configure Chroma as a retriever with top_k=5
33
+ retriever = vectordb.as_retriever(search_kwargs={"k": 5})
34
+
35
+ # Create the retrieval chain
36
+ template = """
37
+ You are a helpful AI assistant.
38
+ Answer based on the context provided.
39
+ context: {context}
40
+ input: {input}
41
+ answer:
42
+ """
43
+ prompt = PromptTemplate.from_template(template)
44
+ combine_docs_chain = create_stuff_documents_chain(llm, prompt)
45
+ retrieval_chain = create_retrieval_chain(retriever, combine_docs_chain)
46
+
47
+ # Invoke the retrieval chain
48
+ response = retrieval_chain.invoke({"input": "How do I apply for personal leave?"})
49
+
50
+ # Print the answer to the question
51
+ print(response["answer"])