Skip to content

Commit

Permalink
Add medical chabot
Browse files Browse the repository at this point in the history
  • Loading branch information
mwitiderrick committed Nov 14, 2023
1 parent bece201 commit f65a156
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 0 deletions.
21 changes: 21 additions & 0 deletions demos/medical-chatbot/LICENSE
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.
16 changes: 16 additions & 0 deletions demos/medical-chatbot/README.md
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">
12 changes: 12 additions & 0 deletions demos/medical-chatbot/chainlit.md
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 not shown.
28 changes: 28 additions & 0 deletions demos/medical-chatbot/ingest.py
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()

109 changes: 109 additions & 0 deletions demos/medical-chatbot/model.py
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()
9 changes: 9 additions & 0 deletions demos/medical-chatbot/requirements.txt
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.

0 comments on commit f65a156

Please sign in to comment.