Jatin Mehra commited on
Commit
9492bcd
·
1 Parent(s): ff4330b

Refactor agentic_rag function to include memory parameter and enhance prompt clarity with detailed instructions for context usage and search behavior.

Browse files
Files changed (1) hide show
  1. preprocessing.py +32 -18
preprocessing.py CHANGED
@@ -72,7 +72,7 @@ def retrieve_similar_chunks(query, index, chunks, model, k=10, max_chunk_length=
72
  distances, indices = index.search(query_embedding, k)
73
  return [(chunks[i]["text"][:max_chunk_length], distances[0][j], chunks[i]["metadata"]) for j, i in enumerate(indices[0])]
74
 
75
- def agentic_rag(llm, tools, query, context_chunks, Use_Tavily=False):
76
  # Sort chunks by relevance (lower distance = more relevant)
77
  context_chunks = sorted(context_chunks, key=lambda x: x[1]) # Sort by distance
78
  context = ""
@@ -87,23 +87,37 @@ def agentic_rag(llm, tools, query, context_chunks, Use_Tavily=False):
87
  total_tokens += chunk_tokens
88
  else:
89
  break
 
 
 
 
 
 
 
90
 
91
  # Define prompt template
92
- search_instructions = (
93
- "Use the search tool if the context is insufficient to answer the question or you are unsure. Give source links if you use the search tool."
94
- if Use_Tavily
95
- else "Use the context provided to answer the question."
96
- )
97
 
98
  prompt = ChatPromptTemplate.from_messages([
99
  ("system", """
100
- You are a helpful assistant. {search_instructions}
101
- Instructions:
102
- 1. Use the provided context to answer the user's question.
103
- 2. Provide a clear answer, if you don't know the answer, say 'I don't know'.
104
- 3. Prioritize information from the most relevant context chunks.
105
- 4. Don't use based on provided context instead use Based on the Document.
106
- """),
 
 
 
 
 
 
 
 
 
 
 
 
107
  ("human", "Context: {context}\n\nQuestion: {input}"),
108
  MessagesPlaceholder(variable_name="chat_history"),
109
  MessagesPlaceholder(variable_name="agent_scratchpad"),
@@ -116,7 +130,7 @@ def agentic_rag(llm, tools, query, context_chunks, Use_Tavily=False):
116
  return agent_executor.invoke({
117
  "input": query,
118
  "context": context,
119
- "search_instructions": search_instructions
120
  })
121
  except Exception as e:
122
  print(f"Error during agent execution: {str(e)}")
@@ -127,7 +141,7 @@ def agentic_rag(llm, tools, query, context_chunks, Use_Tavily=False):
127
  response = llm.invoke(fallback_prompt.format(context=context, input=query))
128
  return {"output": response.content}
129
 
130
- if __name__ == "__main__":
131
  # Process PDF and prepare index
132
  dotenv.load_dotenv()
133
  pdf_file = "JatinCV.pdf"
@@ -147,8 +161,8 @@ if __name__ == "__main__":
147
 
148
  # Retrieve similar chunks
149
  similar_chunks = retrieve_similar_chunks(query, index, chunks, model, k=3)
150
- context = "\n".join([chunk for chunk, _ in similar_chunks])
151
 
152
  # Generate response
153
- response = agentic_rag(llm, tools, query=query, context=context, Use_Tavily=True)
154
- print("Assistant:", response["output"])
 
72
  distances, indices = index.search(query_embedding, k)
73
  return [(chunks[i]["text"][:max_chunk_length], distances[0][j], chunks[i]["metadata"]) for j, i in enumerate(indices[0])]
74
 
75
+ def agentic_rag(llm, tools, query, context_chunks, memory, Use_Tavily=False):
76
  # Sort chunks by relevance (lower distance = more relevant)
77
  context_chunks = sorted(context_chunks, key=lambda x: x[1]) # Sort by distance
78
  context = ""
 
87
  total_tokens += chunk_tokens
88
  else:
89
  break
90
+
91
+ # Set up the search behavior
92
+ search_behavior = (
93
+ "If the context is insufficient, *then* use the 'search' tool to find the answer."
94
+ if Use_Tavily
95
+ else "If the context is insufficient, you *must* state that you don't know."
96
+ )
97
 
98
  # Define prompt template
 
 
 
 
 
99
 
100
  prompt = ChatPromptTemplate.from_messages([
101
  ("system", """
102
+ You are an expert Q&A system. Your primary function is to answer questions using a given set of documents (Context).
103
+
104
+ **Your Process:**
105
+
106
+ 1. **Analyze the Question:** Understand exactly what the user is asking.
107
+ 2. **Scan the Context:** Thoroughly review the 'Context' provided to find relevant information.
108
+ 3. **Formulate the Answer:**
109
+ * If the context contains a clear answer, synthesize it into a concise response.
110
+ * **Always** start your answer with "Based on the Document, ...".
111
+ * {search_behavior}
112
+ * If, after all steps, you cannot find an answer, respond with: "Based on the Document, I don't know the answer."
113
+ 4. **Clarity:** Ensure your final answer is clear, direct, and avoids jargon if possible.
114
+
115
+ **Important Rules:**
116
+
117
+ * **Stick to the Context:** Unless you use the search tool, do *not* use any information outside of the provided 'Context'.
118
+ * **No Speculation:** Do not make assumptions or infer information not explicitly present.
119
+ * **Cite Sources (If Searching):** If you use the search tool, you MUST include the source links in your response.
120
+ """),
121
  ("human", "Context: {context}\n\nQuestion: {input}"),
122
  MessagesPlaceholder(variable_name="chat_history"),
123
  MessagesPlaceholder(variable_name="agent_scratchpad"),
 
130
  return agent_executor.invoke({
131
  "input": query,
132
  "context": context,
133
+ "search_behavior": search_behavior
134
  })
135
  except Exception as e:
136
  print(f"Error during agent execution: {str(e)}")
 
141
  response = llm.invoke(fallback_prompt.format(context=context, input=query))
142
  return {"output": response.content}
143
 
144
+ """if __name__ == "__main__":
145
  # Process PDF and prepare index
146
  dotenv.load_dotenv()
147
  pdf_file = "JatinCV.pdf"
 
161
 
162
  # Retrieve similar chunks
163
  similar_chunks = retrieve_similar_chunks(query, index, chunks, model, k=3)
164
+ # context = "\n".join([chunk for chunk, _ in similar_chunks])
165
 
166
  # Generate response
167
+ response = agentic_rag(llm, tools, query=query, context=similar_chunks, Use_Tavily=True, memory=memory)
168
+ print("Assistant:", response["output"])"""