praneeth dodedu commited on
Commit
02ba095
·
1 Parent(s): fc6649d
.chainlit/.langchain.db ADDED
Binary file (61.4 kB). View file
 
.chainlit/config.toml ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ # If true (default), the app will be available to anonymous users.
3
+ # If false, users will need to authenticate and be part of the project to use the app.
4
+ public = true
5
+
6
+ # The project ID (found on https://cloud.chainlit.io).
7
+ # The project ID is required when public is set to false or when using the cloud database.
8
+ id = "You"
9
+
10
+ # Uncomment if you want to persist the chats.
11
+ # local will create a database in your .chainlit directory (requires node.js installed).
12
+ # cloud will use the Chainlit cloud database.
13
+ # custom will load use your custom client.
14
+ # database = "local"
15
+
16
+ # Whether to enable telemetry (default: true). No personal data is collected.
17
+ enable_telemetry = true
18
+
19
+ # List of environment variables to be provided by each user to use the app.
20
+ user_env = []
21
+
22
+ # Duration (in seconds) during which the session is saved when the connection is lost
23
+ session_timeout = 3600
24
+
25
+ [UI]
26
+ # Name of the app and chatbot.
27
+ name = "RyBot"
28
+
29
+ # Description of the app and chatbot. This is used for HTML tags.
30
+ # description = ""
31
+
32
+ # The default value for the expand messages settings.
33
+ default_expand_messages = false
34
+
35
+ # Hide the chain of thought details from the user in the UI.
36
+ hide_cot = false
37
+
38
+ # Link to your github repo. This will add a github button in the UI's header.
39
+ # github = ""
40
+
41
+ # Override default MUI light theme. (Check theme.ts)
42
+ [UI.theme.light]
43
+ #background = "#FAFAFA"
44
+ #paper = "#FFFFFF"
45
+
46
+ [UI.theme.light.primary]
47
+ #main = "#F80061"
48
+ #dark = "#980039"
49
+ #light = "#FFE7EB"
50
+
51
+ # Override default MUI dark theme. (Check theme.ts)
52
+ [UI.theme.dark]
53
+ #background = "#FAFAFA"
54
+ #paper = "#FFFFFF"
55
+
56
+ [UI.theme.dark.primary]
57
+ #main = "#F80061"
58
+ #dark = "#980039"
59
+ #light = "#FFE7EB"
60
+
61
+
62
+ [meta]
63
+ generated_by = "0.6.1"
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 AI Anytime
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
chainlit.md ADDED
@@ -0,0 +1 @@
 
 
1
+ # Welcome to Ryder Bot! 🚀🤖
ingest.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.embeddings import HuggingFaceEmbeddings
2
+ from langchain.vectorstores import FAISS
3
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+
6
+ DATA_PATH = 'data/'
7
+ DB_FAISS_PATH = 'vectorstore/db_faiss'
8
+
9
+
10
+ # Create vector database
11
+ def create_vector_db():
12
+ loader = DirectoryLoader(DATA_PATH,
13
+ glob='*.pdf',
14
+ loader_cls=PyPDFLoader)
15
+
16
+ documents = loader.load()
17
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,
18
+ chunk_overlap=50)
19
+ texts = text_splitter.split_documents(documents)
20
+
21
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
22
+ model_kwargs={'device': 'cpu'})
23
+
24
+ db = FAISS.from_documents(texts, embeddings)
25
+ db.save_local(DB_FAISS_PATH)
26
+
27
+
28
+ if __name__ == "__main__":
29
+ create_vector_db()
model.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import PromptTemplate
2
+ from langchain.embeddings import HuggingFaceEmbeddings
3
+ from langchain.vectorstores import FAISS
4
+ from langchain.llms import CTransformers
5
+ from langchain.chains import RetrievalQA
6
+ import chainlit as cl
7
+
8
+ DB_FAISS_PATH = 'vectorstore/db_faiss'
9
+
10
+ custom_prompt_template = """Use the following pieces of information to answer the user's question.
11
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
12
+
13
+ Context: {context}
14
+ Question: {question}
15
+
16
+ Only return the helpful answer below and nothing else.
17
+ Helpful answer:
18
+ """
19
+
20
+
21
+ def set_custom_prompt():
22
+ """
23
+ Prompt template for QA retrieval for each vectorstore
24
+ """
25
+ prompt = PromptTemplate(template=custom_prompt_template,
26
+ input_variables=['context', 'question'])
27
+ return prompt
28
+
29
+
30
+ # Retrieval QA Chain
31
+ def retrieval_qa_chain(llm, prompt, db):
32
+ qa_chain = RetrievalQA.from_chain_type(llm=llm,
33
+ chain_type='stuff',
34
+ retriever=db.as_retriever(search_kwargs={'k': 2}),
35
+ return_source_documents=True,
36
+ chain_type_kwargs={'prompt': prompt}
37
+ )
38
+ return qa_chain
39
+
40
+
41
+ # Loading the model
42
+ def load_llm():
43
+ # Load the locally downloaded model here
44
+ llm = CTransformers(
45
+ model="llama-2-7b-chat.ggmlv3.q8_0.bin",
46
+ model_type="llama",
47
+ max_new_tokens=512,
48
+ temperature=0.5
49
+ )
50
+ return llm
51
+
52
+
53
+ # QA Model Function
54
+ def qa_bot():
55
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
56
+ model_kwargs={'device': 'cpu'})
57
+ db = FAISS.load_local(DB_FAISS_PATH, embeddings)
58
+ llm = load_llm()
59
+ qa_prompt = set_custom_prompt()
60
+ qa = retrieval_qa_chain(llm, qa_prompt, db)
61
+
62
+ return qa
63
+
64
+
65
+ # output function
66
+ def final_result(query):
67
+ qa_result = qa_bot()
68
+ response = qa_result({'query': query})
69
+ print(response)
70
+ return response
71
+
72
+
73
+ # chain lit code
74
+ @cl.on_chat_start
75
+ async def start():
76
+ chain = qa_bot()
77
+ msg = cl.Message(content="Starting the bot...")
78
+ await msg.send()
79
+ msg.content = "Hi, Welcome to Ryder Bot. What is your query?"
80
+ await msg.update()
81
+ cl.user_session.set("chain", chain)
82
+
83
+
84
+ @cl.on_message
85
+ async def main(message):
86
+ chain = cl.user_session.get("chain")
87
+ cb = cl.AsyncLangchainCallbackHandler(
88
+ stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
89
+ )
90
+ cb.answer_reached = True
91
+ res = await chain.acall(message, callbacks=[cb])
92
+ answer = res["result"]
93
+ sources = res["source_documents"]
94
+
95
+ if sources:
96
+ answer += f"\nSources:" + str(sources)
97
+ else:
98
+ answer += "\nNo sources found"
99
+
100
+ await cl.Message(content=answer).send()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ pypdf
2
+ langchain
3
+ torch
4
+ accelerate
5
+ bitsandbytes
6
+ transformers
7
+ sentence_transformers
8
+ faiss_cpu
vectorstore/db_faiss/index.faiss ADDED
Binary file (333 kB). View file
 
vectorstore/db_faiss/index.pkl ADDED
Binary file (110 kB). View file