Rajat.bans commited on
Commit
2ac13d6
1 Parent(s): 1a563f1

Updated the prompt for reformulation and randomly giving 100 urls in the bottom

Browse files
Files changed (1) hide show
  1. rag.py +23 -7
rag.py CHANGED
@@ -25,11 +25,20 @@ CHUNK_OVERLAP = 128
25
  embedding_model_hf = "BAAI/bge-m3"
26
  # embedding_model_hf = "sentence-transformers/all-mpnet-base-v2"
27
  qa_model_name = "gpt-3.5-turbo"
28
- bestReformulationPrompt = "Given a chat history and the latest user question, which may reference context from the chat history, you must formulate a standalone question that can be understood without the chat history. You are strictly forbidden from using any outside knowledge. Do not, under any circumstances, answer the question. Reformulate it if necessary; otherwise, return it as is."
29
- bestSystemPrompt = "You're an assistant for question-answering tasks. Under absolutely no circumstances should you use external knowledge or go beyond the provided preknowledge. Your approach must be systematic and meticulous. First, identify CLUES such as keywords, phrases, contextual information, semantic relations, tones, and references that aid in determining the context of the input. Second, construct a concise diagnostic REASONING process (limiting to 130 words) based on premises supporting the INPUT relevance within the provided context. Third, utilizing the identified clues, reasoning, and input, furnish the pertinent answer for the question. Remember, you are required to use ONLY the provided context to answer the questions. If the question does not align with the preknowledge or if the preknowledge is absent, state that you don't know the answer. External knowledge is strictly prohibited. Failure to adhere will result in incorrect answers. The preknowledge is as follows:"
 
 
 
 
 
 
 
 
 
30
 
31
  # embeddings_oa = OpenAIEmbeddings(model=embedding_model_oa)
32
- embeddings_hf = HuggingFaceEmbeddings(model_name=embedding_model_hf, show_progress=True)
33
 
34
 
35
  def setupDb(data_path):
@@ -98,8 +107,8 @@ def chatWithRag(reformulationPrompt, QAPrompt, question, chat_history):
98
  if QAPrompt != None or len(QAPrompt):
99
  curr_question_prompt = QAPrompt
100
 
101
- # reformulated_query = reformulate_question(chat_history, question, reformulationPrompt)
102
- reformulated_query = question
103
  retreived_documents = [
104
  doc
105
  for doc in db.similarity_search_with_score(reformulated_query)
@@ -144,7 +153,6 @@ with gr.Blocks() as demo:
144
  )
145
  output = gr.Textbox(label="Output")
146
  submit_btn = gr.Button("Submit")
147
- selected_urls = random.sample(relevant_content, min(100, len(relevant_content)))
148
 
149
  chat_history = gr.State([])
150
  submit_btn.click(
@@ -158,7 +166,15 @@ with gr.Blocks() as demo:
158
  outputs=[output, chat_history],
159
  )
160
  with gr.Accordion("Urls", open=False):
161
- gr.Markdown(", ".join(selected_urls))
 
 
 
 
 
 
 
 
162
 
163
  gr.close_all()
164
  demo.launch()
 
25
  embedding_model_hf = "BAAI/bge-m3"
26
  # embedding_model_hf = "sentence-transformers/all-mpnet-base-v2"
27
  qa_model_name = "gpt-3.5-turbo"
28
+ bestReformulationPrompt = """Given a chat history and the latest user question, which may reference context from the chat history, you must formulate a standalone question that can be understood without the chat history. You are strictly forbidden from using any outside knowledge. You are strictly forbidden from adding extra things in the question if not required. Do not, under any circumstances, answer the question. Reformulate ONLY if it is necessary; otherwise, return it as is.
29
+
30
+ Example: I am getting fat, how can I loose weight?
31
+ Reformulated question: What are some effective strategies for losing weight in a healthy manner?
32
+ This reformulation is BAD since it added extra thing "in a health manner". The question was understandable even without chat history.
33
+
34
+ Example: When was he born
35
+ Chat History: Who is brack obama
36
+ Reformulated question: When was Barack obama born?
37
+ This reformulation is good since I am able to understand the question which I earlier was not able to understand without chat history."""
38
+ bestSystemPrompt = "You're an assistant for question-answering tasks. Under absolutely no circumstances should you use external knowledge or go beyond the provided preknowledge. Your approach must be systematic and meticulous. First, identify CLUES such as keywords, phrases, contextual information, semantic relations, tones, and references that aid in determining the context of the input. Second, construct a concise diagnostic REASONING process (limiting to 130 words) based on premises supporting the INPUT relevance within the provided preknowledge. Third, utilizing the identified clues, reasoning, and input, furnish the pertinent answer for the question. Remember, you are required to use ONLY the provided preknowledge to answer the questions. If the question does not align with the preknowledge or if the preknowledge is absent, state that you don't know the answer. External knowledge is strictly prohibited. Failure to adhere will result in incorrect answers. The preknowledge is as follows:"
39
 
40
  # embeddings_oa = OpenAIEmbeddings(model=embedding_model_oa)
41
+ embeddings_hf = HuggingFaceEmbeddings(model_name=embedding_model_hf)
42
 
43
 
44
  def setupDb(data_path):
 
107
  if QAPrompt != None or len(QAPrompt):
108
  curr_question_prompt = QAPrompt
109
 
110
+ reformulated_query = reformulate_question(chat_history, question, reformulationPrompt)
111
+ # reformulated_query = question
112
  retreived_documents = [
113
  doc
114
  for doc in db.similarity_search_with_score(reformulated_query)
 
153
  )
154
  output = gr.Textbox(label="Output")
155
  submit_btn = gr.Button("Submit")
 
156
 
157
  chat_history = gr.State([])
158
  submit_btn.click(
 
166
  outputs=[output, chat_history],
167
  )
168
  with gr.Accordion("Urls", open=False):
169
+ urls = gr.Markdown()
170
+
171
+ demo.load(
172
+ lambda: ", ".join(
173
+ random.sample(relevant_content, min(100, len(relevant_content)))
174
+ ),
175
+ None,
176
+ urls,
177
+ )
178
 
179
  gr.close_all()
180
  demo.launch()