gsarathkumar commited on
Commit
d7fc27a
1 Parent(s): d01ee7a

initial commit

Browse files
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY='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'
conversational_with_file.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ import os
4
+ from langchain import OpenAI
5
+ from langchain.embeddings import OpenAIEmbeddings
6
+ from langchain.vectorstores import FAISS
7
+ from langchain.chains.question_answering import load_qa_chain
8
+ from langchain.prompts import PromptTemplate
9
+ from dotenv import load_dotenv
10
+ from langchain.document_loaders import TextLoader
11
+ import pickle
12
+
13
+ load_dotenv()
14
+ embeddings = OpenAIEmbeddings(openai_api_key='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')
15
+ new_db = FAISS.load_local("faiss_index", embeddings)
16
+
17
+
18
+
19
+ def get_text_chunks(text):
20
+ text_splitter = RecursiveCharacterTextSplitter(separators=["\n\n", "\n", " "],
21
+ chunk_size = 200,
22
+ chunk_overlap=50, length_function=len)
23
+ chunks = text_splitter.split_documents(text)
24
+ return chunks
25
+
26
+
27
+ # def get_vector_store(text_chunks):
28
+ # embeddings = OpenAIEmbeddings(openai_api_key='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')
29
+ # vectorstore_openai = FAISS.from_documents(text_chunks, embeddings)
30
+ # vectorstore_openai.save_local("faiss_index")
31
+
32
+
33
+ def get_conversational_chain():
34
+ prompt_template = """
35
+ Answer the question as breif as possible from the provided context, make sure to provide all the details, if the answer is not in
36
+ provided context just say, "answer is not available in the context", don't provide the wrong answer.
37
+ Answer the question as canadian citizen as buyproperly customer care \n\n
38
+ Context:\n {context}?\n
39
+ Question: \n{question}\n
40
+
41
+ Answer:
42
+ """
43
+
44
+ model = OpenAI(temperature=0.6, max_tokens=500, model='gpt-3.5-turbo-instruct')
45
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
46
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
47
+
48
+ return chain
49
+
50
+
51
+
52
+ def user_input(user_question):
53
+ docs = new_db.similarity_search(user_question)
54
+ print('loaded from docs')
55
+ chain = get_conversational_chain()
56
+
57
+ response = chain(
58
+ {"input_documents": docs, "question": user_question}
59
+ , return_only_outputs=True)
60
+
61
+ print(response)
62
+ st.write("Reply: ", response["output_text"])
63
+
64
+
65
+ def main():
66
+ st.set_page_config("Chat with BuyProperly AI Assistant")
67
+
68
+ # with st.sidebar:
69
+ # st.title("Menu:")
70
+ # input_file_path = st.sidebar.text_input("Enter the path of the text file:")
71
+ # process_url_clicked = st.sidebar.button("Process URLs")
72
+ # if process_url_clicked:
73
+ # #print('clicked')
74
+ # loader = TextLoader(input_file_path, encoding='UTF-8')
75
+ # raw_text = loader.load()
76
+ # #print(raw_text)
77
+ # text_chunks = get_text_chunks(raw_text)
78
+ # get_vector_store(text_chunks)
79
+ # st.success("Done")
80
+
81
+ user_question = st.text_input("Ask a Question:")
82
+ if st.button("Submit & Process"):
83
+ with st.spinner("Processing..."):
84
+ print('user_question response', user_question)
85
+ if user_question:
86
+ print('entered the user question')
87
+ user_input(user_question)
88
+
89
+
90
+ if __name__ == "__main__":
91
+ main()
faiss_index/index.faiss ADDED
Binary file (227 kB). View file
 
faiss_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:10a1eabe666dfd9ccdea0e307693d0e2a7df577915b0281b636e4e7b5bea8b31
3
+ size 9917
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.0.284
2
+ python-dotenv==1.0.0
3
+ streamlit==1.22.0
4
+ unstructured==0.9.2
5
+ tiktoken==0.4.0
6
+ faiss-cpu==1.7.4
7
+ libmagic==1.0
8
+ python-magic==0.4.27
9
+ python-magic-bin==0.4.14
10
+ OpenAI == 0.28.0