-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
271bcb5
commit 457eed7
Showing
9 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 AI Anytime | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# MPT-Medical-Chatbot | ||
This is a medical bot built using MPT and Sentence Transformers. The bot is powered by DeepSparse, Langchain, and Chainlit. The bot runs on a decent CPU machine with a minimum of 16GB of RAM. | ||
|
||
## Installation | ||
|
||
```bash | ||
pip install -r requrements.txt | ||
``` | ||
|
||
## Start with Chainlit | ||
|
||
```bash | ||
MODEL_PATH="hf:neuralmagic/mpt-7b-chat-pruned50-quant" chainlit run model.py -w | ||
``` | ||
|
||
<img width="1350" alt="Screenshot 2023-08-13 at 7 12 54 PM" src="https://github.com/mgoin/MPT-Medical-Chatbot/assets/3195154/6841439d-0f27-42d6-af15-65a36e2f0a87"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Welcome to MPT Med-Bot! 🚀🤖 | ||
|
||
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. | ||
|
||
## Useful Links 🔗 | ||
|
||
- **DeepSparse:** This is the [inference engine](https://github.com/neuralmagic/deepsparse) powering the sparse LLM in this demo! | ||
- **Data:** This is the data which has been used as a knowledge base. [Knowledge Base](https://docs.chainlit.io) 📚 | ||
- **Join AI Anytime Community:** Join our friendly [WhatsApp Group](https://discord.gg/ZThrUxbAYw) to ask questions, share your projects, and connect with other developers! 💬 | ||
|
||
Happy chatting! 💻😊 | ||
|
Binary file added
BIN
+15.4 MB
demos/medical-chatbot/data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from langchain.embeddings import HuggingFaceEmbeddings | ||
from langchain.vectorstores import FAISS | ||
from langchain.document_loaders import PyPDFLoader, DirectoryLoader | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
|
||
DATA_PATH = 'data/' | ||
DB_FAISS_PATH = 'vectorstore/db_faiss' | ||
|
||
# Create vector database | ||
def create_vector_db(): | ||
loader = DirectoryLoader(DATA_PATH, | ||
glob='*.pdf', | ||
loader_cls=PyPDFLoader) | ||
|
||
documents = loader.load() | ||
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, | ||
chunk_overlap=50) | ||
texts = text_splitter.split_documents(documents) | ||
|
||
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2', | ||
model_kwargs={'device': 'cpu'}) | ||
|
||
db = FAISS.from_documents(texts, embeddings) | ||
db.save_local(DB_FAISS_PATH) | ||
|
||
if __name__ == "__main__": | ||
create_vector_db() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from langchain.document_loaders import PyPDFLoader, DirectoryLoader | ||
from langchain.prompts import PromptTemplate | ||
from langchain.embeddings import HuggingFaceEmbeddings | ||
from langchain.vectorstores import FAISS | ||
from langchain.chains import RetrievalQA | ||
import chainlit as cl | ||
from langchain.llms import DeepSparse | ||
import os | ||
|
||
MODEL_PATH = os.environ.get("MODEL_PATH") or "hf:neuralmagic/mpt-7b-chat-pruned50-quant" | ||
|
||
DB_FAISS_PATH = "vectorstore/db_faiss" | ||
|
||
custom_prompt_template = """Use the following pieces of information to answer the user's question. | ||
If you don't know the answer, just say that you don't know, don't try to make up an answer. | ||
Context: {context} | ||
Question: {question} | ||
Only return the helpful answer below and nothing else. | ||
Helpful answer: | ||
""" | ||
|
||
|
||
# Loading the model | ||
def load_llm(): | ||
# Load the locally downloaded model here | ||
llm = DeepSparse( | ||
model=MODEL_PATH, | ||
model_config={"sequence_length": 2048, "trust_remote_code": True}, | ||
generation_config={"max_new_tokens": 300}, | ||
) | ||
return llm | ||
|
||
|
||
llm = load_llm() | ||
|
||
|
||
def set_custom_prompt(): | ||
""" | ||
Prompt template for QA retrieval for each vectorstore | ||
""" | ||
prompt = PromptTemplate( | ||
template=custom_prompt_template, input_variables=["context", "question"] | ||
) | ||
return prompt | ||
|
||
|
||
# Retrieval QA Chain | ||
def retrieval_qa_chain(llm, prompt, db): | ||
qa_chain = RetrievalQA.from_chain_type( | ||
llm=llm, | ||
chain_type="stuff", | ||
retriever=db.as_retriever(search_kwargs={"k": 2}), | ||
return_source_documents=True, | ||
chain_type_kwargs={"prompt": prompt}, | ||
) | ||
return qa_chain | ||
|
||
|
||
# QA Model Function | ||
def qa_bot(): | ||
embeddings = HuggingFaceEmbeddings( | ||
model_name="sentence-transformers/all-MiniLM-L6-v2", | ||
model_kwargs={"device": "cpu"}, | ||
) | ||
db = FAISS.load_local(DB_FAISS_PATH, embeddings) | ||
qa_prompt = set_custom_prompt() | ||
qa = retrieval_qa_chain(llm, qa_prompt, db) | ||
|
||
return qa | ||
|
||
|
||
# output function | ||
def final_result(query): | ||
qa_result = qa_bot() | ||
response = qa_result({"query": query}) | ||
return response | ||
|
||
|
||
# chainlit code | ||
@cl.on_chat_start | ||
async def start(): | ||
chain = qa_bot() | ||
msg = cl.Message(content="Starting the bot...") | ||
await msg.send() | ||
msg.content = "Hi, Welcome to Medical Bot. What is your query?" | ||
await msg.update() | ||
|
||
cl.user_session.set("chain", chain) | ||
|
||
|
||
@cl.on_message | ||
async def main(message: cl.Message): | ||
chain = cl.user_session.get("chain") | ||
cb = cl.AsyncLangchainCallbackHandler( | ||
stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"] | ||
) | ||
cb.answer_reached = True | ||
res = await chain.acall(message.content, callbacks=[cb]) | ||
answer = res["result"] | ||
sources = res["source_documents"] | ||
|
||
if sources: | ||
answer += f"\nSources:" + str(sources) | ||
else: | ||
answer += "\nNo sources found" | ||
|
||
await cl.Message(content=answer).send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pypdf | ||
langchain | ||
torch | ||
transformers | ||
datasets | ||
sentence_transformers | ||
faiss_cpu | ||
chainlit | ||
deepsparse-nightly[llm] |
Binary file not shown.
Binary file not shown.