Skip to content

Commit e353789

Browse files
authored
feat: Replace ChatAgent single_iteration with max_iteration_ (#2612)
1 parent 0a8a2ea commit e353789

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

camel/agents/chat_agent.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ class ChatAgent(BaseAgent):
160160
(default: :obj:`None`)
161161
scheduling_strategy (str): name of function that defines how to select
162162
the next model in ModelManager. (default: :str:`round_robin`)
163-
single_iteration (bool): Whether to let the agent perform only one
164-
model calling at each step. (default: :obj:`False`)
163+
max_iteration (Optional[int], optional): Maximum number of model
164+
calling iterations allowed per step. If `None` (default), there's
165+
no explicit limit. If `1`, it performs a single model call. If `N
166+
> 1`, it allows up to N model calls. (default: :obj:`None`)
165167
agent_id (str, optional): The ID of the agent. If not provided, a
166168
random UUID will be generated. (default: :obj:`None`)
167169
stop_event (Optional[threading.Event], optional): Event to signal
@@ -197,7 +199,7 @@ def __init__(
197199
] = None,
198200
response_terminators: Optional[List[ResponseTerminator]] = None,
199201
scheduling_strategy: str = "round_robin",
200-
single_iteration: bool = False,
202+
max_iteration: Optional[int] = None,
201203
agent_id: Optional[str] = None,
202204
stop_event: Optional[threading.Event] = None,
203205
) -> None:
@@ -272,7 +274,7 @@ def __init__(
272274
# Set up other properties
273275
self.terminated = False
274276
self.response_terminators = response_terminators or []
275-
self.single_iteration = single_iteration
277+
self.max_iteration = max_iteration
276278
self.stop_event = stop_event
277279

278280
def reset(self):
@@ -789,6 +791,7 @@ def step(
789791

790792
# Initialize token usage tracker
791793
step_token_usage = self._create_token_usage_tracker()
794+
iteration_count = 0
792795

793796
while True:
794797
try:
@@ -805,6 +808,7 @@ def step(
805808
response_format,
806809
self._get_full_tool_schemas(),
807810
)
811+
iteration_count += 1
808812

809813
# Accumulate API token usage
810814
self._update_token_usage_tracker(
@@ -839,7 +843,10 @@ def step(
839843
if external_tool_call_requests:
840844
break
841845

842-
if self.single_iteration:
846+
if (
847+
self.max_iteration is not None
848+
and iteration_count >= self.max_iteration
849+
):
843850
break
844851

845852
# If we're still here, continue the loop
@@ -914,6 +921,7 @@ async def astep(
914921

915922
# Initialize token usage tracker
916923
step_token_usage = self._create_token_usage_tracker()
924+
iteration_count = 0
917925
while True:
918926
try:
919927
openai_messages, num_tokens = self.memory.get_context()
@@ -929,6 +937,7 @@ async def astep(
929937
response_format,
930938
self._get_full_tool_schemas(),
931939
)
940+
iteration_count += 1
932941

933942
# Terminate Agent if stop_event is set
934943
if self.stop_event and self.stop_event.is_set():
@@ -959,7 +968,10 @@ async def astep(
959968
if external_tool_call_requests:
960969
break
961970

962-
if self.single_iteration:
971+
if (
972+
self.max_iteration is not None
973+
and iteration_count >= self.max_iteration
974+
):
963975
break
964976

965977
# If we're still here, continue the loop
@@ -1754,7 +1766,7 @@ def clone(self, with_memory: bool = False) -> ChatAgent:
17541766
],
17551767
response_terminators=self.response_terminators,
17561768
scheduling_strategy=self.model_backend.scheduling_strategy.__name__,
1757-
single_iteration=self.single_iteration,
1769+
max_iteration=self.max_iteration,
17581770
stop_event=self.stop_event,
17591771
)
17601772

0 commit comments

Comments
 (0)