Skip to content

Commit 0bc3a56

Browse files
authored
Merge pull request #40 from Sathvika-891/main
updated reasoning
2 parents 773f05c + b3140cb commit 0bc3a56

File tree

4 files changed

+18
-41
lines changed

4 files changed

+18
-41
lines changed

maslibpy/agent/agent.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from maslibpy.prompts.cot.cot_prompts import CoT
77
from maslibpy.agent.baseagent import BaseAgent
88

9-
109
class Agent(BaseAgent):
1110
def __init__(self, **kwargs):
1211
super().__init__(**kwargs)
@@ -26,12 +25,12 @@ def select_prompt(self,kwargs):
2625
self.prompt_pattern=kwargs.get("prompt_pattern","cot")
2726
cot_instance=CoT(**{self.prompt_pattern: True})
2827
self.system_prompt = cot_instance.fetch_prompt()
29-
print("cot instane",cot_instance)
28+
3029
else:
3130
self.prompt_pattern=kwargs.get("prompt_pattern","react")
3231
react_instance = ReAct(**{self.prompt_pattern: True})
3332
self.system_prompt = react_instance.fetch_prompt()
34-
print("react instane",react_instance)
33+
3534

3635
def invoke(self, query: Union[str, List[Dict[str, str]]]):
3736
"""Default Scoring function is prompt based"""
@@ -40,19 +39,4 @@ def invoke(self, query: Union[str, List[Dict[str, str]]]):
4039
generated_response=Scorer().mathematical(agent=self,query=query)
4140
else:
4241
generated_response=Scorer().prompt_based(agent=self,query=query)
43-
return generated_response
44-
if __name__=="__main__":
45-
agent = Agent(
46-
name="TestAgent-1",
47-
role="AI Assistant",
48-
goal="Assist users effectively",
49-
backstory="An advanced AI designed to provide helpful insights.",
50-
prompt_type="react",
51-
prompt_pattern="react",
52-
max_iterations=3,
53-
critique_llm=LLM(provider="together", model_name="together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"),
54-
generator_llm=LLM(provider="together", model_name="together_ai/mistralai/Mistral-7B-Instruct-v0.1"),
55-
# score_type="mathematical"
56-
)
57-
58-
print(agent.invoke([{"role":"user","content":"what are AI Agents"}]))
42+
return generated_response

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "maslibpy"
3-
version = "0.1.1"
3+
version = "0.0.2"
44
description = "Multiagent Framework"
55
authors = ["Bayes Labs", "contact@bayeslabs.co"]
66
readme = "README.md"

0 commit comments

Comments
 (0)