nikesh66 commited on
Commit
767c95c
Β·
1 Parent(s): 460f3d9

Upload 11 files

Browse files
.chainlit/config.toml ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ # Whether to enable telemetry (default: true). No personal data is collected.
3
+ enable_telemetry = true
4
+
5
+ # List of environment variables to be provided by each user to use the app.
6
+ user_env = []
7
+
8
+ # Duration (in seconds) during which the session is saved when the connection is lost
9
+ session_timeout = 3600
10
+
11
+ # Enable third parties caching (e.g LangChain cache)
12
+ cache = false
13
+
14
+ # Follow symlink for asset mount (see https://github.com/Chainlit/chainlit/issues/317)
15
+ # follow_symlink = false
16
+
17
+ [features]
18
+ # Show the prompt playground
19
+ prompt_playground = true
20
+
21
+ [UI]
22
+ # Name of the app and chatbot.
23
+ name = "Chatbot"
24
+
25
+ # Description of the app and chatbot. This is used for HTML tags.
26
+ # description = ""
27
+
28
+ # Large size content are by default collapsed for a cleaner ui
29
+ default_collapse_content = true
30
+
31
+ # The default value for the expand messages settings.
32
+ default_expand_messages = false
33
+
34
+ # Hide the chain of thought details from the user in the UI.
35
+ hide_cot = false
36
+
37
+ # Link to your github repo. This will add a github button in the UI's header.
38
+ # github = ""
39
+
40
+ # Override default MUI light theme. (Check theme.ts)
41
+ [UI.theme.light]
42
+ #background = "#FAFAFA"
43
+ #paper = "#FFFFFF"
44
+
45
+ [UI.theme.light.primary]
46
+ #main = "#F80061"
47
+ #dark = "#980039"
48
+ #light = "#FFE7EB"
49
+
50
+ # Override default MUI dark theme. (Check theme.ts)
51
+ [UI.theme.dark]
52
+ #background = "#FAFAFA"
53
+ #paper = "#FFFFFF"
54
+
55
+ [UI.theme.dark.primary]
56
+ #main = "#F80061"
57
+ #dark = "#980039"
58
+ #light = "#FFE7EB"
59
+
60
+
61
+ [meta]
62
+ generated_by = "0.7.0"
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf filter=lfs diff=lfs merge=lfs -text
37
+ vectorstore/db_faiss/index.faiss filter=lfs diff=lfs merge=lfs -text
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.
README.md ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # Llama2-Medical-Chatbot
2
+ This is a medical bot built using Llama2 and Sentence Transformers. The bot is powered by Langchain and Chainlit. The bot runs on a decent CPU machine with a minimum of 16GB of RAM.
__pycache__/model.cpython-310.pyc ADDED
Binary file (3.12 kB). View file
 
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader
2
+ from langchain import PromptTemplate
3
+ from langchain.embeddings import HuggingFaceEmbeddings
4
+ from langchain.vectorstores import FAISS
5
+ from langchain.llms import CTransformers
6
+ from langchain.chains import RetrievalQA
7
+ import chainlit as cl
8
+
9
+ DB_FAISS_PATH = 'vectorstore/db_faiss'
10
+
11
+ custom_prompt_template = """Use the following pieces of information to answer the user's question.
12
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
13
+
14
+ Context: {context}
15
+ Question: {question}
16
+
17
+ Only return the helpful answer below and nothing else.
18
+ Helpful answer:
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
+ #Retrieval QA Chain
30
+ def retrieval_qa_chain(llm, prompt, db):
31
+ qa_chain = RetrievalQA.from_chain_type(llm=llm,
32
+ chain_type='stuff',
33
+ retriever=db.as_retriever(search_kwargs={'k': 2}),
34
+ return_source_documents=True,
35
+ chain_type_kwargs={'prompt': prompt}
36
+ )
37
+ return qa_chain
38
+
39
+ #Loading the model
40
+ def load_llm():
41
+ # Load the locally downloaded model here
42
+ llm = CTransformers(
43
+ model = "TheBloke/Llama-2-7B-Chat-GGML",
44
+ model_type="llama",
45
+ max_new_tokens = 512,
46
+ temperature = 0.5
47
+ )
48
+ return llm
49
+
50
+ #QA Model Function
51
+ def qa_bot():
52
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
53
+ model_kwargs={'device': 'cpu'})
54
+ db = FAISS.load_local(DB_FAISS_PATH, embeddings)
55
+ llm = load_llm()
56
+ qa_prompt = set_custom_prompt()
57
+ qa = retrieval_qa_chain(llm, qa_prompt, db)
58
+
59
+ return qa
60
+
61
+ #output function
62
+ def final_result(query):
63
+ qa_result = qa_bot()
64
+ response = qa_result({'query': query})
65
+ return response
66
+
67
+ #chainlit code
68
+ @cl.on_chat_start
69
+ async def start():
70
+ chain = qa_bot()
71
+ msg = cl.Message(content="Starting the bot...")
72
+ await msg.send()
73
+ msg.content = "Hi, Welcome to Medical Bot. What is your query?"
74
+ await msg.update()
75
+
76
+ cl.user_session.set("chain", chain)
77
+
78
+ @cl.on_message
79
+ async def main(message):
80
+ chain = cl.user_session.get("chain")
81
+ cb = cl.AsyncLangchainCallbackHandler(
82
+ stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
83
+ )
84
+ cb.answer_reached = True
85
+ res = await chain.acall(message, callbacks=[cb])
86
+ answer = res["result"]
87
+ sources = res["source_documents"]
88
+
89
+ if sources:
90
+ answer += f"\nSources:" + str(sources)
91
+ else:
92
+ answer += "\nNo sources found"
93
+
94
+ await cl.Message(content=answer).send()
95
+
chainlit.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Welcome to Llama2 Med-Bot! πŸš€πŸ€–
2
+
3
+ Hi there, πŸ‘‹ We're excited to have you on board. This is a powerful bot designed to help you ask queries related to your data/knowledge.
4
+
5
+ ## Useful Links πŸ”—
6
+
7
+ - **Data:** This is the data which has been used as a knowledge base. [Knowledge Base](https://docs.chainlit.io) πŸ“š
8
+ - **Join AI Anytime Community:** Join our friendly [WhatsApp Group](https://discord.gg/ZThrUxbAYw) to ask questions, share your projects, and connect with other developers! πŸ’¬
9
+
10
+ Happy chatting! πŸ’»πŸ˜Š
11
+
data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:753cd53b7a3020bbd91f05629b0e3ddcfb6a114d7bbedb22c2298b66f5dd00cc
3
+ size 16127037
ingest.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Create vector database
10
+ def create_vector_db():
11
+ loader = DirectoryLoader(DATA_PATH,
12
+ glob='*.pdf',
13
+ loader_cls=PyPDFLoader)
14
+
15
+ documents = loader.load()
16
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,
17
+ chunk_overlap=50)
18
+ texts = text_splitter.split_documents(documents)
19
+
20
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
21
+ model_kwargs={'device': 'cpu'})
22
+
23
+ db = FAISS.from_documents(texts, embeddings)
24
+ db.save_local(DB_FAISS_PATH)
25
+
26
+ if __name__ == "__main__":
27
+ create_vector_db()
28
+
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ pypdf
2
+ langchain
3
+ torch
4
+ accelerate
5
+ bitsandbytes
6
+ transformers
7
+ sentence_transformers
8
+ faiss_cpu
9
+ chainlit
10
+ ctransformer
vectorstore/db_faiss/index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:71cb9d7859158ba922504166d439220bda5f1b03d89c0425b0705a31621b6539
3
+ size 10983981
vectorstore/db_faiss/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4f2fdf338f626ea3f40025e5ac735e0dee5350128d97079fa3be3600ef876bcc
3
+ size 3567746