Upload 11 files
Browse filesFiles changed (12)
- .chainlit/config.tomlAdded+62
- .gitattributes+2
- LICENSEAdded+21
- README.mdAdded+2
- __pycache__/model.cpython-310.pycAdded
- app.pyAdded+95
- chainlit.mdAdded+11
- data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdfAdded+3
- ingest.pyAdded+28
- requirements.txtAdded+10
- vectorstore/db_faiss/index.faissAdded+3
- vectorstore/db_faiss/index.pklAdded+3
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162[project]# Whether to enable telemetry (default: true). No personal data is collected.enable_telemetry = true
# List of environment variables to be provided by each user to use the app.user_env = []
# Duration (in seconds) during which the session is saved when the connection is lostsession_timeout = 3600
# Enable third parties caching (e.g LangChain cache)cache = false
# Follow symlink for asset mount (see https://github.com/Chainlit/chainlit/issues/317)# follow_symlink = false
[features]# Show the prompt playgroundprompt_playground = true
[UI]# Name of the app and chatbot.name = "Chatbot"
# Description of the app and chatbot. This is used for HTML tags.# description = ""
# Large size content are by default collapsed for a cleaner uidefault_collapse_content = true
# The default value for the expand messages settings.default_expand_messages = false
# Hide the chain of thought details from the user in the UI.hide_cot = false
# Link to your github repo. This will add a github button in the UI's header.# github = ""
# Override default MUI light theme. (Check theme.ts)[UI.theme.light] #background = "#FAFAFA" #paper = "#FFFFFF"
[UI.theme.light.primary] #main = "#F80061" #dark = "#980039" #light = "#FFE7EB"
# Override default MUI dark theme. (Check theme.ts)[UI.theme.dark] #background = "#FAFAFA" #paper = "#FFFFFF"
[UI.theme.dark.primary] #main = "#F80061" #dark = "#980039" #light = "#FFE7EB"
[meta]generated_by = "0.7.0"32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines333435363732 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -textdata/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf filter=lfs diff=lfs merge=lfs -textvectorstore/db_faiss/index.faiss filter=lfs diff=lfs merge=lfs -text
123456789101112131415161718192021MIT License
Copyright (c) 2023 AI Anytime
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.12# Llama2-Medical-ChatbotThis 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.1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495from langchain.document_loaders import PyPDFLoader, DirectoryLoaderfrom langchain import PromptTemplatefrom langchain.embeddings import HuggingFaceEmbeddingsfrom langchain.vectorstores import FAISSfrom langchain.llms import CTransformersfrom langchain.chains import RetrievalQAimport chainlit as cl
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:"""
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 Chaindef 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
#Loading the modeldef load_llm(): # Load the locally downloaded model here llm = CTransformers( model = "TheBloke/Llama-2-7B-Chat-GGML", model_type="llama", max_new_tokens = 512, temperature = 0.5 ) return llm
#QA Model Functiondef qa_bot(): embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': 'cpu'}) db = FAISS.load_local(DB_FAISS_PATH, embeddings) llm = load_llm() qa_prompt = set_custom_prompt() qa = retrieval_qa_chain(llm, qa_prompt, db)
return qa
#output functiondef final_result(query): qa_result = qa_bot() response = qa_result({'query': query}) return response
#chainlit code@cl.on_chat_startasync 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_messageasync def main(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, 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()
1234567891011# Welcome to Llama2 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 π
- **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! π»π
123version https://git-lfs.github.com/spec/v1oid sha256:753cd53b7a3020bbd91f05629b0e3ddcfb6a114d7bbedb22c2298b66f5dd00ccsize 1612703712345678910111213141516171819202122232425262728from langchain.embeddings import HuggingFaceEmbeddingsfrom langchain.vectorstores import FAISSfrom langchain.document_loaders import PyPDFLoader, DirectoryLoaderfrom langchain.text_splitter import RecursiveCharacterTextSplitter
DATA_PATH = 'data/'DB_FAISS_PATH = 'vectorstore/db_faiss'
# Create vector databasedef 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()
12345678910pypdflangchaintorchacceleratebitsandbytestransformerssentence_transformersfaiss_cpuchainlitctransformer123version https://git-lfs.github.com/spec/v1oid sha256:71cb9d7859158ba922504166d439220bda5f1b03d89c0425b0705a31621b6539size 10983981123version https://git-lfs.github.com/spec/v1oid sha256:4f2fdf338f626ea3f40025e5ac735e0dee5350128d97079fa3be3600ef876bccsize 3567746.chainlit/config.tomlAdded+62
.gitattributes+2
LICENSEAdded+21
README.mdAdded+2
__pycache__/model.cpython-310.pycAdded
app.pyAdded+95
chainlit.mdAdded+11
data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdfAdded+3
ingest.pyAdded+28
requirements.txtAdded+10
vectorstore/db_faiss/index.faissAdded+3
vectorstore/db_faiss/index.pklAdded+3