@@ -29,9 +29,10 @@ class QueryAnalysis(BaseModel):
29
29
)
30
30
31
31
class LocalRAGAgent :
32
- def __init__ (self , vector_store : VectorStore , model_name : str = "mistralai/Mistral-7B-Instruct-v0.2" ):
32
+ def __init__ (self , vector_store : VectorStore , model_name : str = "mistralai/Mistral-7B-Instruct-v0.2" , use_cot : bool = False ):
33
33
"""Initialize local RAG agent with vector store and local LLM"""
34
34
self .vector_store = vector_store
35
+ self .use_cot = use_cot
35
36
36
37
# Load HuggingFace token from config
37
38
try :
@@ -120,7 +121,15 @@ def _generate_direct_response(self, query: str) -> Dict[str, Any]:
120
121
"""Generate a response directly from the LLM without context"""
121
122
logger .info ("Generating direct response from LLM without context..." )
122
123
123
- prompt = f"""You are a helpful AI assistant. Please answer the following query to the best of your ability.
124
+ if self .use_cot :
125
+ prompt = f"""You are a helpful AI assistant. Please answer the following query using chain of thought reasoning.
126
+ First break down the problem into steps, then solve each step to arrive at the final answer.
127
+
128
+ Query: { query }
129
+
130
+ Let's think about this step by step:"""
131
+ else :
132
+ prompt = f"""You are a helpful AI assistant. Please answer the following query to the best of your ability.
124
133
If you're not confident about the answer, please say so.
125
134
126
135
Query: { query }
@@ -199,7 +208,19 @@ def _generate_response(self, query: str, context: List[Dict[str, Any]]) -> Dict[
199
208
for i , item in enumerate (context )])
200
209
201
210
logger .info ("Building prompt with context..." )
202
- prompt = f"""Answer the following query using the provided context.
211
+ if self .use_cot :
212
+ prompt = f"""Answer the following query using the provided context and chain of thought reasoning.
213
+ First break down the problem into steps, then use the context to solve each step and arrive at the final answer.
214
+ If the context doesn't contain enough information to answer accurately, say so explicitly.
215
+
216
+ Context:
217
+ { context_str }
218
+
219
+ Query: { query }
220
+
221
+ Let's think about this step by step:"""
222
+ else :
223
+ prompt = f"""Answer the following query using the provided context.
203
224
If the context doesn't contain enough information to answer accurately,
204
225
say so explicitly.
205
226
@@ -225,6 +246,7 @@ def main():
225
246
parser .add_argument ("--store-path" , default = "embeddings" , help = "Path to the vector store" )
226
247
parser .add_argument ("--model" , default = "mistralai/Mistral-7B-Instruct-v0.2" , help = "Model to use" )
227
248
parser .add_argument ("--quiet" , action = "store_true" , help = "Disable verbose logging" )
249
+ parser .add_argument ("--use-cot" , action = "store_true" , help = "Enable Chain of Thought reasoning" )
228
250
229
251
args = parser .parse_args ()
230
252
@@ -241,7 +263,7 @@ def main():
241
263
logger .info (f"Initializing vector store from: { args .store_path } " )
242
264
store = VectorStore (persist_directory = args .store_path )
243
265
logger .info ("Initializing local RAG agent..." )
244
- agent = LocalRAGAgent (store , model_name = args .model )
266
+ agent = LocalRAGAgent (store , model_name = args .model , use_cot = args . use_cot )
245
267
246
268
print (f"\n Processing query: { args .query } " )
247
269
print ("=" * 50 )
0 commit comments