Skip to content

Commit

Permalink
Making all tests good
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomdmoura committed Sep 13, 2024
1 parent 7120a98 commit 91ddab2
Show file tree
Hide file tree
Showing 88 changed files with 25,899 additions and 24,702 deletions.
19 changes: 0 additions & 19 deletions src/crewai/agents/crew_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def __init__(
self.name_to_tool_map = {tool.name: tool for tool in self.tools}

def invoke(self, inputs: Dict[str, str]) -> Dict[str, Any]:
print("starting invoke")
if "system" in self.prompt:
system_prompt = self._format_prompt(self.prompt["system"], inputs)
user_prompt = self._format_prompt(self.prompt["user"], inputs)
Expand All @@ -73,13 +72,9 @@ def invoke(self, inputs: Dict[str, str]) -> Dict[str, Any]:
else:
user_prompt = self._format_prompt(self.prompt["prompt"], inputs)
self.messages.append(self._format_msg(user_prompt))
print("after messages")
print(self.messages)
self.ask_for_human_input = inputs.get("ask_for_human_input", False)

formatted_answer = self._invoke_loop()
print("111after formatted_answer")
print(formatted_answer)

if self.ask_for_human_input:
human_feedback = self._ask_human_input(formatted_answer.output)
Expand All @@ -93,36 +88,25 @@ def invoke(self, inputs: Dict[str, str]) -> Dict[str, Any]:
return {"output": formatted_answer.output}

def _invoke_loop(self, formatted_answer=None):
print("starting _invoke_loop")
try:
while not isinstance(formatted_answer, AgentFinish):
if not self.request_within_rpm_limit or self.request_within_rpm_limit():
print("******* messages")
print(self.messages)
answer = LLM(
self.llm, stop=self.stop, callbacks=self.callbacks
).call(self.messages)
print("after answer")
print(answer)

self.iterations += 1
formatted_answer = self._format_answer(answer)
print("222after formatted_answer")
print(formatted_answer)

if isinstance(formatted_answer, AgentAction):
action_result = self._use_tool(formatted_answer)
formatted_answer.text += f"\nObservation: {action_result}"
print("after formatted_answer.text")
print(formatted_answer.text)

if self.step_callback:
formatted_answer.result = action_result
self.step_callback(formatted_answer)
if self._should_force_answer():
print("starting _should_force_answer")
if self.have_forced_answer:
print("forcing answer")
return {
"output": self._i18n.errors(
"force_final_answer_error"
Expand All @@ -139,13 +123,10 @@ def _invoke_loop(self, formatted_answer=None):
)

except OutputParserException as e:
print("********* ERROR1")
self.messages.append({"role": "assistant", "content": e.error})
self._invoke_loop(formatted_answer)

except Exception as e:
print("********* ERRORw")
print(e)
if LLMContextLengthExceededException(str(e))._is_context_limit_error(
str(e)
):
Expand Down
1 change: 0 additions & 1 deletion src/crewai/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def __init__(self, model: str, stop: List[str] = [], callbacks: List[Any] = []):
self.stop = stop
self.model = model
litellm.callbacks = callbacks
litellm.set_verbose = True

def call(self, messages: List[Dict[str, str]]) -> Dict[str, Any]:
response = completion(
Expand Down
7 changes: 1 addition & 6 deletions src/crewai/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,13 @@ def _execute_core(
tools = tools or self.tools or []

self.processed_by_agents.add(agent.role)
print("====================================================")
print("context", self.prompt_context)
print("context", agent.role)
print("context", context)

result = agent.execute_task(
task=self,
context=context,
tools=tools,
)
print("result", result)
print("====================================================")

pydantic_output, json_output = self._export_output(result)

task_output = TaskOutput(
Expand Down
11 changes: 10 additions & 1 deletion src/crewai/utilities/rpm_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,26 @@ def reset_counter(self):
return self

def check_or_wait(self):
print("check_or_waitcheck_or_waitcheck_or_waitcheck_or_wait")
if self.max_rpm is None:
return True

def _check_and_increment():
print(
"_check_and_increment_check_and_increment_check_and_increment_check_and_increment"
)
if self.max_rpm is not None and self._current_rpm < self.max_rpm:
self._current_rpm += 1
print("111111111111111")
print("self._current_rpm", self._current_rpm)
print("self.max_rpm", self.max_rpm)
return True
elif self.max_rpm is not None:
print("22222222222222")
self.logger.log(
"info", "Max RPM reached, waiting for next minute to start."
)
print("CARALHO")
self._wait_for_next_minute()
self._current_rpm = 1
return True
Expand All @@ -52,7 +61,7 @@ def stop_rpm_counter(self):
self._timer = None

def _wait_for_next_minute(self):
time.sleep(60)
time.sleep(1)
self._current_rpm = 0

def _reset_request_count(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_agent_execution():
)

output = agent.execute_task(task)
assert output == "The result of the math operation is 2."
assert output == "The result of the math operation 1 + 1 is 2."


@pytest.mark.vcr(filter_headers=["authorization"])
Expand All @@ -91,7 +91,7 @@ def multiplier(first_number: int, second_number: int) -> float:
expected_output="The result of the multiplication.",
)
output = agent.execute_task(task)
assert output == "12"
assert output == "The result of 3 times 4 is 12."


@pytest.mark.vcr(filter_headers=["authorization"])
Expand Down Expand Up @@ -122,7 +122,7 @@ def multiplier(first_number: int, second_number: int) -> float:
tool_usage = InstructorToolCalling(
tool_name=multiplier.name, arguments={"first_number": 3, "second_number": 4}
)
assert output == "12"
assert output == "The result of multiplying 3 times 4 is 12."
assert agent.tools_handler.last_used_tool.tool_name == tool_usage.tool_name
assert agent.tools_handler.last_used_tool.arguments == tool_usage.arguments

Expand Down Expand Up @@ -276,7 +276,7 @@ def multiplier(first_number: int, second_number: int) -> float:
expected_output="The result of the multiplication.",
)
output = agent.execute_task(task=task, tools=[multiplier])
assert output == "12"
assert output == "The result of 3 times 4 is 12."


@pytest.mark.vcr(filter_headers=["authorization"])
Expand Down Expand Up @@ -312,7 +312,7 @@ def get_final_answer() -> float:
@pytest.mark.vcr(filter_headers=["authorization"])
def test_agent_repeated_tool_usage(capsys):
@tool
def get_final_answer(anything: str) -> float:
def get_final_answer() -> float:
"""Get the final answer but don't give it yet, just re-use this
tool non-stop."""
return 42
Expand Down
Loading

0 comments on commit 91ddab2

Please sign in to comment.