|
from datasets import load_dataset |
|
dataset = load_dataset("Namitg02/Test") |
|
print(dataset) |
|
|
|
from langchain.docstore.document import Document as LangchainDocument |
|
|
|
|
|
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=15,separators=["\n\n", "\n", " ", ""]) |
|
|
|
docs = splitter.create_documents(str(dataset)) |
|
|
|
|
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") |
|
|
|
|
|
|
|
from langchain_community.vectorstores import Chroma |
|
persist_directory = 'docs/chroma/' |
|
|
|
vectordb = Chroma.from_documents( |
|
documents=docs, |
|
embedding=embedding_model, |
|
persist_directory=persist_directory |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain.prompts import PromptTemplate |
|
|
|
template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer. |
|
{You are a helpful dietician} |
|
Question: {question} |
|
Helpful Answer:""" |
|
|
|
|
|
|
|
|
|
from langchain.chains import ConversationalRetrievalChain |
|
from langchain.memory import ConversationBufferMemory |
|
memory = ConversationBufferMemory( |
|
memory_key="chat_history", |
|
return_messages=True |
|
) |
|
|
|
question = "How can I reverse Diabetes?" |
|
|
|
|
|
retriever = vectordb.as_retriever( |
|
search_type="similarity", search_kwargs={"k": 2} |
|
) |
|
|
|
|
|
from transformers import pipeline |
|
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline |
|
from langchain_core.messages import SystemMessage |
|
from langchain_core.prompts import HumanMessagePromptTemplate |
|
from langchain_core.prompts import ChatPromptTemplate |
|
from langchain.prompts import PromptTemplate |
|
|
|
print("check1") |
|
|
|
|
|
template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer. |
|
{context} |
|
Question: {question} |
|
Helpful Answer:""" |
|
|
|
QA_CHAIN_PROMPT = PromptTemplate(input_variables=["context", "question"],template=template) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llm_model = "microsoft/Phi-3-mini-4k-instruct" |
|
from transformers import AutoTokenizer |
|
tokenizer = AutoTokenizer.from_pretrained(llm_model,trust_remote_code=True) |
|
from transformers import AutoModelForCausalLM |
|
model = AutoModelForCausalLM.from_pretrained(llm_model,trust_remote_code=True) |
|
|
|
|
|
|
|
|
|
|
|
question = "How can I reverse diabetes?" |
|
|
|
|
|
docs1 = retriever.get_relevant_documents(question) |
|
print("docs1") |
|
|
|
from langchain.chains.question_answering import load_qa_chain |
|
|
|
|
|
print("check2") |
|
qa = ConversationalRetrievalChain.from_llm( |
|
llm, |
|
retriever=retriever, |
|
memory=memory, |
|
chain_type_kwargs={"prompt": QA_CHAIN_PROMPT} |
|
) |
|
|
|
|
|
result = qa({"question": question}) |
|
print("result") |
|
|
|
|
|
|
|
|
|
print("check3") |
|
chain = pipe(question = "How can I reverse diabetes?",context = "Diabetes remission or reversal is a condition when a person’s HbA1c is less than 6.5% for 3 months or more without diabetes medication. For diabetes remission or reversal, people should follow the advice of their doctors and nutritionist. Weight reduction is the key point for diabetes remission or reversal, as we all know that one of the leading causes of developing diabetes is obesity and more than 82 percent are overweight. But remission does not mean that diabetes has gone away.") |
|
|
|
print("check3A") |
|
print(chain)[0]['generated_text'][-1] |
|
print("check3B") |
|
import gradio as gr |
|
|
|
ragdemo = gr.Interface.from_pipeline(chain) |
|
|
|
print("check4") |
|
ragdemo.launch() |
|
print("check5") |