Namitg02 commited on
Commit
f40cccc
·
verified ·
1 Parent(s): 7d07b9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -14
app.py CHANGED
@@ -38,19 +38,70 @@ retriever = vectordb.as_retriever()
38
  # retriever=vectordb.as_retriever()
39
  #)
40
 
41
- from langchain_community.output_parsers.rail_parser import GuardrailsOutputParser
 
 
 
 
 
 
 
 
42
  from langchain.prompts import PromptTemplate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- 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.
45
- {You are a helpful dietician}
46
- Question: {question}
47
- Helpful Answer:"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
50
 
51
- from langchain.chains import ConversationalRetrievalChain
52
- from langchain.memory import ConversationBufferMemory
53
- memory = ConversationBufferMemory(
 
 
 
 
 
 
 
 
 
 
 
54
  memory_key="chat_history",
55
  return_messages=True
56
  )
@@ -58,13 +109,10 @@ memory = ConversationBufferMemory(
58
  question = "Can I reverse Diabetes?"
59
  print("template")
60
 
61
-
62
- retriever=vectordb.as_retriever()
63
- READER_MODEL = "HuggingFaceH4/zephyr-7b-beta"
64
- qa = ConversationalRetrievalChain.from_llm(llm=READER_MODEL,retriever=retriever,memory=memory)
65
 
66
  import gradio as gr
67
  gr.load("READER_MODEL").launch()
68
 
69
  #result = ({"query": question})
70
- print("qa")
 
38
  # retriever=vectordb.as_retriever()
39
  #)
40
 
41
+ from transformers import pipeline
42
+ from transformers import AutoTokenizer, BitsAndBytesConfig
43
+
44
+ READER_MODEL = "HuggingFaceH4/zephyr-7b-beta"
45
+ bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16)
46
+ model = AutoModelForCausalLM.from_pretrained(READER_MODEL, quantization_config=bnb_config)
47
+ tokenizer = AutoTokenizer.from_pretrained(READER_MODEL)
48
+
49
+ from langchain.llms import HuggingFacePipeline
50
  from langchain.prompts import PromptTemplate
51
+ from transformers import pipeline
52
+ from langchain_core.output_parsers import StrOutputParser
53
+
54
+
55
+ text_generation_pipeline = pipeline(
56
+ model=model,
57
+ tokenizer=tokenizer,
58
+ task="text-generation",
59
+ temperature=0.2,
60
+ do_sample=True,
61
+ repetition_penalty=1.1,
62
+ return_full_text=True,
63
+ max_new_tokens=100,
64
+ )
65
+
66
+ llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
67
+
68
 
69
+ #from langchain_community.output_parsers.rail_parser import GuardrailsOutputParser
70
+
71
+ prompt_template = """
72
+ <|system|>
73
+ Answer the question based on your knowledge. Use the following context to help:
74
+
75
+ {context}
76
+
77
+ </s>
78
+ <|user|>
79
+ {question}
80
+ </s>
81
+ <|assistant|>
82
+
83
+ """
84
+
85
+ QA_CHAIN_PROMPT = PromptTemplate(
86
+ input_variables=["context", "question"],
87
+ template=prompt_template,
88
+ )
89
 
 
90
 
91
+ llm_chain = QA_CHAIN_PROMPT | llm | StrOutputParser()
92
+
93
+
94
+ from langchain_core.runnables import RunnablePassthrough
95
+
96
+
97
+ retriever=vectordb.as_retriever()
98
+
99
+ rag_chain = {"context": retriever, "question": RunnablePassthrough()} | llm_chain
100
+
101
+
102
+ #from langchain.chains import ConversationalRetrievalChain
103
+ #from langchain.memory import ConversationBufferMemory
104
+ #memory = ConversationBufferMemory(
105
  memory_key="chat_history",
106
  return_messages=True
107
  )
 
109
  question = "Can I reverse Diabetes?"
110
  print("template")
111
 
112
+ #qa = ConversationalRetrievalChain.from_llm(llm=READER_MODEL,retriever=retriever,memory=memory)
 
 
 
113
 
114
  import gradio as gr
115
  gr.load("READER_MODEL").launch()
116
 
117
  #result = ({"query": question})
118
+ #print("qa")