Skip to content

Commit f49444e

Browse files
committed
feat: Optimize agent prompts and improve CoT visibility
- Make agent prompts more concise and focused - Add real-time step conclusions in chat interface - Show intermediate reasoning steps in Gradio - Improve prompt efficiency for faster responses
1 parent baf2ed2 commit f49444e

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed

agentic_rag/agents/agent_factory.py

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,20 @@ def plan(self, query: str, context: List[Dict[str, Any]] = None) -> str:
4747
logger.info(f"\n🎯 Planning step for query: {query}")
4848

4949
if context:
50-
template = """You are a strategic planning agent. Your role is to break down complex problems into clear, manageable steps.
51-
52-
Given the following context and query, create a step-by-step plan to answer the question.
53-
Each step should be clear and actionable.
54-
55-
Context:
56-
{context}
50+
template = """As a strategic planner, break down this problem into 3-4 clear steps.
5751
52+
Context: {context}
5853
Query: {query}
5954
60-
Plan:"""
55+
Steps:"""
6156
context_str = "\n\n".join([f"Context {i+1}:\n{item['content']}" for i, item in enumerate(context)])
6257
logger.info(f"Using context ({len(context)} items)")
6358
else:
64-
template = """You are a strategic planning agent. Your role is to break down complex problems into clear, manageable steps.
65-
66-
Given the following query, create a step-by-step plan to answer the question.
67-
Each step should be clear and actionable.
59+
template = """As a strategic planner, break down this problem into 3-4 clear steps.
6860
6961
Query: {query}
7062
71-
Plan:"""
63+
Steps:"""
7264
context_str = ""
7365
logger.info("No context available")
7466

@@ -109,15 +101,10 @@ def research(self, query: str, step: str) -> List[Dict[str, Any]]:
109101
logger.warning("No relevant documents found")
110102
return []
111103

112-
# Have LLM analyze and summarize findings
113-
template = """You are a research agent. Your role is to analyze information and extract relevant details.
114-
115-
Given the following research step and context, summarize the key findings that are relevant to this step.
104+
template = """Extract and summarize key information relevant to this step.
116105
117106
Step: {step}
118-
119-
Context:
120-
{context}
107+
Context: {context}
121108
122109
Key Findings:"""
123110

@@ -145,19 +132,13 @@ def __init__(self, llm):
145132
def reason(self, query: str, step: str, context: List[Dict[str, Any]]) -> str:
146133
logger.info(f"\n🤔 Reasoning about step: {step}")
147134

148-
template = """You are a reasoning agent. Your role is to apply logical analysis to information and draw conclusions.
149-
150-
Given the following step, context, and query, apply logical reasoning to reach a conclusion.
151-
Show your reasoning process clearly.
135+
template = """Analyze the information and draw a clear conclusion for this step.
152136
153137
Step: {step}
154-
155-
Context:
156-
{context}
157-
138+
Context: {context}
158139
Query: {query}
159140
160-
Reasoning:"""
141+
Conclusion:"""
161142

162143
context_str = "\n\n".join([f"Context {i+1}:\n{item['content']}" for i, item in enumerate(context)])
163144
prompt = ChatPromptTemplate.from_template(template)
@@ -182,17 +163,12 @@ def __init__(self, llm):
182163
def synthesize(self, query: str, reasoning_steps: List[str]) -> str:
183164
logger.info(f"\n📝 Synthesizing final answer from {len(reasoning_steps)} reasoning steps")
184165

185-
template = """You are a synthesis agent. Your role is to combine multiple pieces of information into a clear, coherent response.
186-
187-
Given the following query and reasoning steps, create a final comprehensive answer.
188-
The answer should be well-structured and incorporate the key points from each step.
166+
template = """Combine the reasoning steps into a clear, comprehensive answer.
189167
190168
Query: {query}
169+
Steps: {steps}
191170
192-
Reasoning Steps:
193-
{steps}
194-
195-
Final Answer:"""
171+
Answer:"""
196172

197173
steps_str = "\n\n".join([f"Step {i+1}:\n{step}" for i, step in enumerate(reasoning_steps)])
198174
prompt = ChatPromptTemplate.from_template(template)

agentic_rag/gradio_app.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,19 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
114114
print("\nChain of Thought Reasoning Steps:")
115115
print("-" * 50)
116116

117-
# Add each reasoning step
117+
# Add each reasoning step with conclusion
118118
for i, step in enumerate(response["reasoning_steps"], 1):
119119
step_text = f"Step {i}:\n{step}\n"
120120
formatted_response += step_text
121121
print(step_text)
122+
123+
# Add intermediate response to chat history to show progress
124+
history.append([None, f"🔄 Step {i} Conclusion:\n{step}"])
122125

123126
# Add final answer
124127
print("\nFinal Answer:")
125128
print("-" * 50)
126-
final_answer = "🎯 Final Answer:\n" + response["answer"]
129+
final_answer = "\n🎯 Final Answer:\n" + response["answer"]
127130
formatted_response += final_answer
128131
print(final_answer)
129132

@@ -145,18 +148,20 @@ def chat(message: str, history: List[List[str]], agent_type: str, use_cot: bool,
145148
source_line = f"- {source} (file: {file_path})\n"
146149
formatted_response += source_line
147150
print(source_line)
151+
152+
# Add final formatted response to history
153+
history.append([message, formatted_response])
148154
else:
149155
formatted_response = response["answer"]
150156
print("\nStandard Response:")
151157
print("-" * 50)
152158
print(formatted_response)
159+
history.append([message, formatted_response])
153160

154161
print("\n" + "="*50)
155162
print("Response complete")
156163
print("="*50 + "\n")
157164

158-
# Return updated history with new message pair
159-
history.append([message, formatted_response])
160165
return history
161166
except Exception as e:
162167
error_msg = f"Error processing query: {str(e)}"

0 commit comments

Comments
 (0)