Skip to content

Commit 0b713f7

Browse files
committed
feat: removed context printing in console
1 parent 1c7dd2e commit 0b713f7

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

agentic_rag/agents/agent_factory.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,30 @@ class Agent(BaseModel):
2727

2828
def log_prompt(self, prompt: str, prefix: str = ""):
2929
"""Log a prompt being sent to the LLM"""
30-
logger.info(f"\n{'='*80}\n{prefix} Prompt:\n{'-'*40}\n{prompt}\n{'='*80}")
30+
# Check if the prompt contains context
31+
if "Context:" in prompt:
32+
# Split the prompt at "Context:" and keep only the first part
33+
parts = prompt.split("Context:")
34+
# Keep the first part and add a note that context is omitted
35+
truncated_prompt = parts[0] + "Context: [Context omitted for brevity]"
36+
if len(parts) > 2 and "Key Findings:" in parts[1]:
37+
# For researcher prompts, keep the "Key Findings:" part
38+
key_findings_part = parts[1].split("Key Findings:")
39+
if len(key_findings_part) > 1:
40+
truncated_prompt += "\nKey Findings:" + key_findings_part[1]
41+
logger.info(f"\n{'='*80}\n{prefix} Prompt:\n{'-'*40}\n{truncated_prompt}\n{'='*80}")
42+
else:
43+
# If no context, log the full prompt
44+
logger.info(f"\n{'='*80}\n{prefix} Prompt:\n{'-'*40}\n{prompt}\n{'='*80}")
3145

3246
def log_response(self, response: str, prefix: str = ""):
3347
"""Log a response received from the LLM"""
34-
logger.info(f"\n{'='*80}\n{prefix} Response:\n{'-'*40}\n{response}\n{'='*80}")
48+
# Log the response but truncate if it's too long
49+
if len(response) > 500:
50+
truncated_response = response[:500] + "... [response truncated]"
51+
logger.info(f"\n{'='*80}\n{prefix} Response:\n{'-'*40}\n{truncated_response}\n{'='*80}")
52+
else:
53+
logger.info(f"\n{'='*80}\n{prefix} Response:\n{'-'*40}\n{response}\n{'='*80}")
3554

3655
class PlannerAgent(Agent):
3756
"""Agent responsible for breaking down problems and planning steps"""
@@ -108,6 +127,7 @@ def research(self, query: str, step: str) -> List[Dict[str, Any]]:
108127
109128
Key Findings:"""
110129

130+
# Create context string but don't log it
111131
context_str = "\n\n".join([f"Source {i+1}:\n{item['content']}" for i, item in enumerate(all_results)])
112132
prompt = ChatPromptTemplate.from_template(template)
113133
messages = prompt.format_messages(step=step, context=context_str)
@@ -140,6 +160,7 @@ def reason(self, query: str, step: str, context: List[Dict[str, Any]]) -> str:
140160
141161
Conclusion:"""
142162

163+
# Create context string but don't log it
143164
context_str = "\n\n".join([f"Context {i+1}:\n{item['content']}" for i, item in enumerate(context)])
144165
prompt = ChatPromptTemplate.from_template(template)
145166
messages = prompt.format_messages(step=step, query=query, context=context_str)

agentic_rag/local_rag_agent.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -262,27 +262,13 @@ def _process_query_with_cot(self, query: str) -> Dict[str, Any]:
262262
pdf_context = self.vector_store.query_pdf_collection(query)
263263
initial_context.extend(pdf_context)
264264
logger.info(f"Retrieved {len(pdf_context)} chunks from PDF Collection")
265-
# Log each chunk with citation number but not full content
266-
for i, chunk in enumerate(pdf_context):
267-
source = chunk["metadata"].get("source", "Unknown")
268-
pages = chunk["metadata"].get("page_numbers", [])
269-
logger.info(f"Source [{i+1}]: {source} (pages: {pages})")
270-
# Only log content preview at debug level
271-
content_preview = chunk["content"][:150] + "..." if len(chunk["content"]) > 150 else chunk["content"]
272-
logger.debug(f"Content preview for source [{i+1}]: {content_preview}")
265+
# Don't log individual sources to keep console clean
273266
elif self.collection == "Repository Collection":
274267
logger.info(f"Retrieving context from Repository Collection for query: '{query}'")
275268
repo_context = self.vector_store.query_repo_collection(query)
276269
initial_context.extend(repo_context)
277270
logger.info(f"Retrieved {len(repo_context)} chunks from Repository Collection")
278-
# Log each chunk with citation number but not full content
279-
for i, chunk in enumerate(repo_context):
280-
source = chunk["metadata"].get("source", "Unknown")
281-
file_path = chunk["metadata"].get("file_path", "Unknown")
282-
logger.info(f"Source [{i+1}]: {source} (file: {file_path})")
283-
# Only log content preview at debug level
284-
content_preview = chunk["content"][:150] + "..." if len(chunk["content"]) > 150 else chunk["content"]
285-
logger.debug(f"Content preview for source [{i+1}]: {content_preview}")
271+
# Don't log individual sources to keep console clean
286272
# For General Knowledge, no context is needed
287273
else:
288274
logger.info("Using General Knowledge collection, no context retrieval needed")
@@ -306,9 +292,8 @@ def _process_query_with_cot(self, query: str) -> Dict[str, Any]:
306292
continue
307293
step_research = self.agents["researcher"].research(query, step)
308294
research_results.append({"step": step, "findings": step_research})
309-
# Log which sources were used for this step
310-
source_indices = [initial_context.index(finding) + 1 for finding in step_research if finding in initial_context]
311-
logger.info(f"Research for step: {step}\nUsing sources: {source_indices}")
295+
# Don't log source indices to keep console clean
296+
logger.info(f"Research for step: {step}")
312297
else:
313298
# If no researcher or no context, use the steps directly
314299
research_results = [{"step": step, "findings": []} for step in plan.split("\n") if step.strip()]
@@ -328,7 +313,8 @@ def _process_query_with_cot(self, query: str) -> Dict[str, Any]:
328313
result["findings"] if result["findings"] else [{"content": "Using general knowledge", "metadata": {"source": "General Knowledge"}}]
329314
)
330315
reasoning_steps.append(step_reasoning)
331-
logger.info(f"Reasoning for step: {result['step']}\n{step_reasoning}")
316+
# Log just the step, not the full reasoning
317+
logger.info(f"Reasoning for step: {result['step']}")
332318

333319
# Step 4: Synthesize final answer
334320
logger.info("Step 4: Synthesis")
@@ -337,7 +323,7 @@ def _process_query_with_cot(self, query: str) -> Dict[str, Any]:
337323
return self._generate_general_response(query)
338324

339325
final_answer = self.agents["synthesizer"].synthesize(query, reasoning_steps)
340-
logger.info(f"Final synthesized answer:\n{final_answer}")
326+
logger.info("Final answer synthesized successfully")
341327

342328
return {
343329
"answer": final_answer,
@@ -360,26 +346,12 @@ def _process_query_standard(self, query: str) -> Dict[str, Any]:
360346
logger.info(f"Retrieving context from PDF Collection for query: '{query}'")
361347
pdf_context = self.vector_store.query_pdf_collection(query)
362348
logger.info(f"Retrieved {len(pdf_context)} chunks from PDF Collection")
363-
# Log each chunk with citation number but not full content
364-
for i, chunk in enumerate(pdf_context):
365-
source = chunk["metadata"].get("source", "Unknown")
366-
pages = chunk["metadata"].get("page_numbers", [])
367-
logger.info(f"Source [{i+1}]: {source} (pages: {pages})")
368-
# Only log content preview at debug level
369-
content_preview = chunk["content"][:150] + "..." if len(chunk["content"]) > 150 else chunk["content"]
370-
logger.debug(f"Content preview for source [{i+1}]: {content_preview}")
349+
# Don't log individual sources to keep console clean
371350
elif self.collection == "Repository Collection":
372351
logger.info(f"Retrieving context from Repository Collection for query: '{query}'")
373352
repo_context = self.vector_store.query_repo_collection(query)
374353
logger.info(f"Retrieved {len(repo_context)} chunks from Repository Collection")
375-
# Log each chunk with citation number but not full content
376-
for i, chunk in enumerate(repo_context):
377-
source = chunk["metadata"].get("source", "Unknown")
378-
file_path = chunk["metadata"].get("file_path", "Unknown")
379-
logger.info(f"Source [{i+1}]: {source} (file: {file_path})")
380-
# Only log content preview at debug level
381-
content_preview = chunk["content"][:150] + "..." if len(chunk["content"]) > 150 else chunk["content"]
382-
logger.debug(f"Content preview for source [{i+1}]: {content_preview}")
354+
# Don't log individual sources to keep console clean
383355

384356
# Combine all context
385357
all_context = pdf_context + repo_context
@@ -451,12 +423,9 @@ def _generate_response(self, query: str, context: List[Dict[str, Any]]) -> Dict[
451423

452424
# Print concise source information
453425
print("\nSources detected:")
454-
for source, details in sources.items():
455-
if isinstance(details, set): # PDF with pages
456-
pages = ", ".join(sorted(details))
457-
print(f"Document: {source} (pages: {pages})")
458-
else: # Code with file path
459-
print(f"Code file: {source}")
426+
# Print a single line for each source without additional details
427+
for source in sources:
428+
print(f"- {source}")
460429

461430
return {
462431
"answer": response_text,

0 commit comments

Comments
 (0)