Skip to content

Commit e21274a

Browse files
committed
updated reasoning
1 parent d782a88 commit e21274a

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

maslibpy/reasoning/mathematical.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,18 @@ def invoke(self,agent,query: Union[str, List[Dict[str, str]]]) -> str:
119119
"""Evaluate and refine model responses based on entropy and coherence."""
120120
best_score, best_response = float('-inf'), None
121121
plateau_count = 0
122-
res=""
123-
start_time=time.time()
122+
124123
for i in tqdm(range(agent.max_iterations), desc="Iterations"):
125124

126125
initial_response = self.generate(agent,agent.generator_llm,query)
127-
res+=f"Initial Response:\n{initial_response}\n\n"
126+
128127
critique_comments = self.critique(agent,agent.critique_llm,initial_response, query)
129-
res+=f"Critique_comments:\n{critique_comments}\n\n"
128+
130129
revised_response = self.refine_response(agent,agent.generator_llm,initial_response, critique_comments)
131-
res+=f"Revised Response:\n{critique_comments}\n\n"
130+
132131
metrics = self.calculate_metrics(revised_response)
133132
current_score = self._calculate_composite_score(agent,metrics)
134-
res+=f"Metrics:\n\n{metrics}\n\n"
135-
res+=f"Score:\n\n{current_score}\n\n"
133+
136134
logger.info(f"Epoch {i+1} - Metrics: {metrics}")
137135

138136
if current_score > best_score + agent.entropy_threshold:
@@ -161,11 +159,11 @@ def update_chat_history(self,agent,query:Union[str, List[Dict[str, str]]]):
161159
user_msg=UserMessage(content=agent.system_prompt.format(query=query[-1]["content"]))
162160
query[-1]=user_msg
163161
agent.messages.extend(query)
164-
# return agent.messages
162+
165163

166164
def generate(self, agent,llm,query: Union[str, List[Dict[str, str]]]) -> str:
167165
"""Generate response for a given query using the provided LLM"""
168-
# agent.messages=self.update_chat_history(agent,query)
166+
169167
self.update_chat_history(agent,query)
170168
messages = [{"role": msg.role, "content": msg.content} for msg in agent.messages]
171169
res = llm.invoke(messages)
@@ -179,7 +177,7 @@ def critique(self, agent,llm,response: str, original_query: Union[str, List[Dict
179177
# mathematical critique and prompt critique are two different
180178
"""Generate critique comments for the response balancing completeness and conciseness."""
181179
if isinstance(original_query,UserMessage):
182-
print("user msg")
180+
183181
original_query=original_query[-1].content
184182
elif isinstance(original_query, list) and all(isinstance(msg, dict) for msg in original_query):
185183
original_query=original_query[-1]["content"]

maslibpy/reasoning/prompt_based.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ class PromptBased():
1111

1212
def invoke(self,agent,query: Union[str, List[Dict[str, str]]]) -> str:
1313
actual_query = query
14-
res=""
15-
start_time=time.time()
14+
1615
for i in tqdm(range(agent.max_iterations),desc="Iterations"):
1716
try:
1817
generated_response = self.generate(agent,agent.generator_llm,actual_query)
19-
res += f"===== Epoch {i+1} =====\n\n"
20-
res += f"**Generated Response**:\n\n{generated_response}\n\n"
18+
2119
if generated_response is not None:
2220
ind=generated_response.rfind("Final Answer")
2321
if ind>=0:
@@ -28,19 +26,16 @@ def invoke(self,agent,query: Union[str, List[Dict[str, str]]]) -> str:
2826

2927
critiqued_response = self.critique(agent,agent.critique_llm,generated_response,original_query=query)
3028

31-
res += f"\n\n**Critiqued Response**:\n\n{critiqued_response}\n\n"
29+
3230
grade_output=self.grade(agent,agent.critique_llm,query,generated_response,critiqued_response)
33-
res+=f"\n\n**Grade node output**\n\n{grade_output}"
31+
3432
if grade_output:
3533
print("breaking the loop")
3634
break
37-
os.makedirs("results",exist_ok=True)
38-
res += "=" * 100 + "\n\n"
39-
except Exception as e:
4035

41-
res += f"\n\n**Error occurred {e} in iteration {i+1}**\n\n"
36+
except Exception as e:
4237
raise e
43-
break
38+
4439

4540
return generated_response
4641

0 commit comments

Comments
 (0)