Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

refactor: allow use of str for step function if batch_size = 1 (#2084)

hallerite5a0ffc756f48
diff --git a/camel/environments/models.py b/camel/environments/models.py
index 8039dfa0d..a5bcfa987 100644
--- a/camel/environments/models.py
+++ b/camel/environments/models.py
@@ -33,7 +33,7 @@ class Action(BaseModel):
generated (UTC).
"""
- index: Optional[int] = Field(default=None, description="...")
+ index: int = Field(default=0, description="...")
llm_response: str = Field(description="Generated response from the LLM")
metadata: Dict[str, Any] = Field(
diff --git a/camel/environments/single_step.py b/camel/environments/single_step.py
index 81ba6f91c..a57648e61 100644
--- a/camel/environments/single_step.py
+++ b/camel/environments/single_step.py
@@ -224,29 +224,49 @@ class SingleStepEnv:
raise TypeError(f"Unsupported dataset type: {type(self.dataset)}")
async def step(
- self, action: Union[Action, List[Action]]
+ self, action: Union[Action, List[Action], str]
) -> Union[
Tuple[Observation, float, bool, Dict[str, Any]],
List[Tuple[Observation, float, bool, Dict[str, Any]]],
]:
- r"""Process actions for a subset of states and update their finished
- status.
+ r"""Execute one interaction step in the environment using the
+ proposed solution.
+
+ This method processes the agent's response(s) to the current
+ observation(s), verifies the correctness of the responses using
+ the verifier, computes rewards, and returns the resulting
+ state transition(s).
+
+ The environment is strictly single-step. Once an action is
+ submitted for a state, that state is marked as done, and
+ the observation will not change.
Args:
- action: Single action (for batch_size=1 or micro-batch of size 1)
- or list of actions (for batch_size>=2 with multiple actions).
- Each action must have an index for batch_size>=2, indicating
- which state it corresponds to.
+ action (Union[Action, List[Action], str]):
+ The action(s) taken by the agent,
+ which should contain the response(s)
+ to the observation(s). Can be:
+ - A single `Action` object (for batch size 1),
+ - A list of `Action` objects (for batched evaluation),
+ - A raw string (only allowed when batch size is 1).
Returns:
- Union[StepResult, List[StepResult]]: StepResult or list of
- StepResults for the processed states.
+ Union[Tuple[Observation, float, bool, Dict[str, Any]], List[...]]:
+ A tuple or list of tuples containing:
+ - `Observation`: Placeholder indicating episode end.
+ - `float`: The reward for the response.
+ - `bool`: Whether the episode is done
+ (always `True` in this case).
+ - `dict`: Additional info including the proposed solution,
+ verification result, and original data point.
Raises:
- RuntimeError: If environment isn't set up or episode has ended.
- ValueError: If indices are invalid, duplicate, or correspond to
- finished states.
+ RuntimeError: If the environment has not been set up,
+ or if `reset()` has not been called.
+ ValueError: If invalid action format, duplicate indices,
+ or out-of-bounds indices are detected.
"""
+
if not self._is_setup:
raise RuntimeError("Environment not set up. Call setup() first.")
if self._batch_done():
@@ -256,65 +276,10 @@ class SingleStepEnv:
if not self._states:
raise RuntimeError("No current observation. Call reset() first.")
- # Normalize actions into a list for uniform processing
- if self.current_batch_size == 1:
- if isinstance(action, list):
- if len(action) != 1 or not isinstance(action[0], Action):
- raise ValueError(
- "For batch_size=1, expect a single Action or a "
- "list containing exactly one Action"
- )
- actions = action
- elif isinstance(action, Action):
- actions = [action]
- else:
- raise ValueError(
- "For batch_size=1, expect a single Action or a "
- "list containing exactly one Action"
- )
-
- # For batch_size=1, set index to 0 if not provided
- if actions[0].index is None:
- actions[0].index = 0
- elif actions[0].index != 0:
- raise ValueError("For batch_size=1, index must be None or 0")
+ actions = self._normalize_actions(action)
- else: # batch_size >= 2
- if isinstance(action, Action):
- if action.index is None:
- raise ValueError(
- "For batch_size>=2, each Action must have an index"
- )
- if not isinstance(action.index, int):
- raise ValueError("Index must be an integer")
- actions = [action]
- elif isinstance(action, list):
- if not action: # Empty list
- raise ValueError("Action list cannot be empty")
- actions = action
- for act in actions:
- if not isinstance(act, Action):
- raise ValueError(
- "All elements in list must be Action objects"
- )
- if act.index is None:
- raise ValueError(
- "For batch_size>=2, each Action must have an index"
- )
- if not isinstance(act.index, int):
- raise ValueError("Index must be an integer")
- else:
- raise ValueError(
- "For batch_size>=2, expect an Action or list of Actions"
- )
+ indices = [a.index for a in actions]
- # Validate indices
- indices: List[int] = []
- for act in actions:
- assert act.index is not None
- indices.append(act.index)
- if len(set(indices)) != len(indices):
- raise ValueError("Duplicate state indices in actions.")
for idx in indices:
if idx < 0 or idx >= len(self._states):
raise ValueError(f"Invalid state index {idx}.")
@@ -354,7 +319,6 @@ class SingleStepEnv:
total_rewards, rewards_dicts = await self._compute_reward_batch(
proposed_solutions, verification_results
)
-
# Create and return step results in batch
step_results = [
StepResult(
@@ -375,6 +339,86 @@ class SingleStepEnv:
return step_results[0] if len(step_results) == 1 else step_results
+ def _normalize_actions(
+ self, action: Union[Action, List[Action], str]
+ ) -> List[Action]:
+ r"""Normalize the user-provided action(s) into a validated list
+ of `Action` objects.
+
+ This method handles flexibility in input format by converting
+ raw strings (only allowed when batch size is 1) and ensuring
+ all necessary structure and integrity checks on actions
+ (e.g., index bounds, duplicates).
+
+ Args:
+ action (Union[Action, List[Action], str]):
+ The raw input action(s) provided by the agent. Can be:
+ - A single `Action` object.
+ - A list of `Action` objects.
+ - A raw string (if `batch_size == 1`), auto-wrapped
+ in an `Action`.
+
+ Returns:
+ List[Action]: A list of validated `Action` instances
+ ready for evaluation.
+
+ Raises:
+ ValueError: If:
+ - Action indices are invalid or duplicated,
+ - Action list is empty,
+ - Index mismatches expected values
+ (e.g., 0 for batch size 1),
+ - Wrong structure is used
+ (e.g., string used with batch size > 1).
+ TypeError: If the action is of an unsupported type.
+ """
+
+ if isinstance(action, str):
+ if self.current_batch_size != 1:
+ raise ValueError(
+ "String input for action is only allowed"
+ " when batch_size == 1"
+ )
+ logger.warning("Auto-converting from str to Action", stacklevel=2)
+ action = Action(index=0, llm_response=action)
+
+ if isinstance(action, Action):
+ actions = [action]
+ elif isinstance(action, list):
+ if not action:
+ raise ValueError("Action list cannot be empty")
+ if not all(isinstance(a, Action) for a in action):
+ raise ValueError(
+ "All elements in the list must be Action objects"
+ )
+ actions = action
+ else:
+ raise TypeError("Action must be a str, Action, or list of Actions")
+
+ if self.current_batch_size == 1 and len(actions) != 1:
+ raise ValueError(
+ "For batch_size=1, expect a single Action or a "
+ "list containing exactly one Action"
+ )
+
+ # Validate indices
+ for a in actions:
+ if not isinstance(a.index, int):
+ raise ValueError(
+ f"Action index must be an integer, got {a.index}"
+ )
+ if self.current_batch_size == 1:
+ if a.index != 0:
+ raise ValueError(
+ "For batch_size=1, Action index must be 0"
+ )
+
+ indices = [a.index for a in actions]
+ if len(set(indices)) != len(indices):
+ raise ValueError("Duplicate state indices in actions.")
+
+ return actions
+
async def _compute_reward_batch(
self,
proposed_solutions: List[str],
diff --git a/test/environments/test_single_stage_env.py b/test/environments/test_single_step_env.py
similarity index 99%
rename from test/environments/test_single_stage_env.py
rename to test/environments/test_single_step_env.py
index 15a5230ea..1882d0fda 100644
--- a/test/environments/test_single_stage_env.py
+++ b/test/environments/test_single_step_env.py
@@ -121,7 +121,7 @@ async def test_single_step_env_lifecycle_single_generator():
assert (
observation2.question == "What is the capital of France?"
) # Second datapoint
- action2 = Action(index=None, llm_response="Paris")
+ action2 = Action(llm_response="Paris")
result2 = await env.step(action2)
next_obs2, reward2, done2, info2 = result2
assert next_obs2 == env.PLACEHOLDER_OBS
@@ -205,12 +205,10 @@ async def test_single_step_env_lifecycle_single():
):
await env.step(action)
- # Test reset and step again with index=None (should default to 0)
+ # Test reset and step again with no index (should default to 0)
observation2 = await env.reset(batch_size=1)
assert isinstance(observation2, Observation)
- action2 = Action(
- index=None, llm_response="Paris"
- ) # Assuming second question
+ action2 = Action(llm_response="Paris") # Assuming second question
result2 = await env.step(action2)
next_obs2, reward2, done2, info2 = result2
assert next_obs2 == env.PLACEHOLDER_OBS
@@ -581,7 +579,7 @@ async def test_single_step_env_error_handling_single():
# a) Action with invalid index
with pytest.raises(
- ValueError, match="For batch_size=1, index must be None or 0"
+ ValueError, match="For batch_size=1, Action index must be 0"
):
await env_invalid_actions.step(Action(index=1, llm_response="4"))