-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
129 lines (97 loc) · 3.6 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import boto3
import streamlit as st
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_aws import BedrockEmbeddings
from langchain_aws import BedrockLLM
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_community.vectorstores import FAISS
bedrock = boto3.client(service_name="bedrock-runtime")
bedrock_embeddings = BedrockEmbeddings(model_id="amazon.titan-embed-text-v1", client=bedrock)
def data_ingestion():
loader = PyPDFDirectoryLoader("data")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000,
chunk_overlap=1000)
docs = text_splitter.split_documents(documents)
return docs
def get_vector_store(docs):
vectorstore_faiss = FAISS.from_documents(
docs,
bedrock_embeddings
)
vectorstore_faiss.save_local("faiss_index")
def get_claude_llm():
llm = BedrockLLM(
model_id="anthropic.claude-v2:1",
client=bedrock,
model_kwargs={
"max_tokens": 512,
"temperature": 0.7,
"anthropic_version": "bedrock-2023-05-31"
}
)
return llm
def get_llama_llm():
llm = BedrockLLM(
model_id="us.meta.llama3-1-70b-instruct-v1:0",
client=bedrock,
model_kwargs={
"max_tokens": 512,
"temperature": 0.7
}
)
return llm
prompt_template = """
H: Use the following pieces of context to provide a
concise answer to the question at the end but use at least summarize with
250 words with detailed explanations. If you don't know the answer,
just say that you don't know, don't try to make up an answer.
<context>
{context}
</context>
Question: {question}
A:"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
def get_response_llm(llm, vectorstore_faiss, query):
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore_faiss.as_retriever(
search_type="similarity", search_kwargs={"k": 3}
),
return_source_documents=True,
chain_type_kwargs={"prompt": PROMPT}
)
answer = qa({"query": query})
return answer['result']
def main():
st.set_page_config("Chat PDF")
st.header("Chat with PDF using AWS Bedrock💁")
user_question = st.text_input("Ask a Question from the PDF Files")
with st.sidebar:
st.title("Update Or Create Vector Store:")
if st.button("Vectors Update"):
with st.spinner("Processing..."):
docs = data_ingestion()
get_vector_store(docs)
st.success("Done")
if user_question and st.button("Claude Output"):
with st.spinner("Processing..."):
faiss_index = FAISS.load_local("faiss_index", bedrock_embeddings, allow_dangerous_deserialization=True)
llm = get_claude_llm()
# faiss_index = get_vector_store(docs)
st.write(get_response_llm(llm, faiss_index, user_question))
st.success("Done")
if user_question and st.button("Llama Output"):
with st.spinner("Processing..."):
faiss_index = FAISS.load_local("faiss_index", bedrock_embeddings, allow_dangerous_deserialization=True)
llm = get_llama_llm()
# faiss_index = get_vector_store(docs)
st.write(get_response_llm(llm, faiss_index, user_question))
st.success("Done")
if __name__ == "__main__":
main()