🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Single very long message processing (#2689)
Tao Sun781e15cba29a
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex 65d219e1d..7b884ddee 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -54,7 +54,11 @@ from camel.memories import (MemoryRecord,ScoreBasedContextCreator,)-from camel.messages import BaseMessage, FunctionCallingMessage, OpenAIMessage+from camel.messages import (+ BaseMessage,+ FunctionCallingMessage,+ OpenAIMessage,+)from camel.models import (BaseModelBackend,ModelFactory,@@ -512,27 +516,172 @@ class ChatAgent(BaseAgent):) -> None:r"""Updates the agent memory with a new message.+ If the single *message* exceeds the model's context window, it will+ be **automatically split into multiple smaller chunks** before being+ written into memory. This prevents later failures in+ `ScoreBasedContextCreator` where an over-sized message cannot fit+ into the available token budget at all.++ This slicing logic handles both regular text messages (in the+ `content` field) and long tool call results (in the `result` field of+ a `FunctionCallingMessage`).+Args:message (BaseMessage): The new message to add to the storedmessages.role (OpenAIBackendRole): The backend role type.timestamp (Optional[float], optional): Custom timestamp for the- memory record. If None, current timestamp will be used.+ memory record. If `None`, the current time will be used.(default: :obj:`None`)+ (default: obj:`None`)"""+ import mathimport time+ import uuid as _uuid++ # 1. Helper to write a record to memory+ def _write_single_record(+ message: BaseMessage, role: OpenAIBackendRole, timestamp: float+ ):+ self.memory.write_record(+ MemoryRecord(+ message=message,+ role_at_backend=role,+ timestamp=timestamp,+ agent_id=self.agent_id,+ )+ )++ base_ts = (+ timestamp+ if timestamp is not None+ else time.time_ns() / 1_000_000_000+ )++ # 2. Get token handling utilities, fallback if unavailable+ try:+ context_creator = self.memory.get_context_creator()+ token_counter = context_creator.token_counter+ token_limit = context_creator.token_limit+ except AttributeError:+ _write_single_record(message, role, base_ts)+ return- self.memory.write_record(- MemoryRecord(- message=message,- role_at_backend=role,- timestamp=timestamp- if timestamp is not None- else time.time_ns() / 1_000_000_000, # Nanosecond precision- agent_id=self.agent_id,+ # 3. Check if slicing is necessary+ try:+ current_tokens = token_counter.count_tokens_from_messages(+ [message.to_openai_message(role)]+ )+ _, ctx_tokens = self.memory.get_context()+ remaining_budget = max(0, token_limit - ctx_tokens)++ if current_tokens <= remaining_budget:+ _write_single_record(message, role, base_ts)+ return+ except Exception as e:+ logger.warning(+ f"Token calculation failed before chunking, "+ f"writing message as-is. Error: {e}")+ _write_single_record(message, role, base_ts)+ return++ # 4. Perform slicing+ logger.warning(+ f"Message with {current_tokens} tokens exceeds remaining budget "+ f"of {remaining_budget}. Slicing into smaller chunks.")+ text_to_chunk: Optional[str] = None+ is_function_result = False++ if isinstance(message, FunctionCallingMessage) and isinstance(+ message.result, str+ ):+ text_to_chunk = message.result+ is_function_result = True+ elif isinstance(message.content, str):+ text_to_chunk = message.content++ if not text_to_chunk or not text_to_chunk.strip():+ _write_single_record(message, role, base_ts)+ return+ # Encode the entire text to get a list of all token IDs+ try:+ all_token_ids = token_counter.encode(text_to_chunk)+ except Exception as e:+ logger.error(f"Failed to encode text for chunking: {e}")+ _write_single_record(message, role, base_ts) # Fallback+ return++ if not all_token_ids:+ _write_single_record(message, role, base_ts) # Nothing to chunk+ return++ # 1. Base chunk size: one-tenth of the smaller of (a) total token+ # limit and (b) current remaining budget. This prevents us from+ # creating chunks that are guaranteed to overflow the+ # immediate context window.+ base_chunk_size = max(1, remaining_budget) // 10++ # 2. Each chunk gets a textual prefix such as:+ # "[chunk 3/12 of a long message]\n"+ # The prefix itself consumes tokens, so if we do not subtract its+ # length the *total* tokens of the outgoing message (prefix + body)+ # can exceed the intended bound. We estimate the prefix length+ # with a representative example that is safely long enough for the+ # vast majority of cases (three-digit indices).+ sample_prefix = "[chunk 1/1000 of a long message]\n"+ prefix_token_len = len(token_counter.encode(sample_prefix))++ # 3. The real capacity for the message body is therefore the base+ # chunk size minus the prefix length. Fallback to at least one+ # token to avoid zero or negative sizes.+ chunk_body_limit = max(1, base_chunk_size - prefix_token_len)++ # 4. Calculate how many chunks we will need with this body size.+ num_chunks = math.ceil(len(all_token_ids) / chunk_body_limit)+ group_id = str(_uuid.uuid4())++ for i in range(num_chunks):+ start_idx = i * chunk_body_limit+ end_idx = start_idx + chunk_body_limit+ chunk_token_ids = all_token_ids[start_idx:end_idx]++ chunk_body = token_counter.decode(chunk_token_ids)++ prefix = f"[chunk {i + 1}/{num_chunks} of a long message]\n"+ new_body = prefix + chunk_body++ if is_function_result and isinstance(+ message, FunctionCallingMessage+ ):+ new_msg: BaseMessage = FunctionCallingMessage(+ role_name=message.role_name,+ role_type=message.role_type,+ meta_dict=message.meta_dict,+ content=message.content,+ func_name=message.func_name,+ args=message.args,+ result=new_body,+ tool_call_id=message.tool_call_id,+ )+ else:+ new_msg = message.create_new_instance(new_body)++ meta = (new_msg.meta_dict or {}).copy()+ meta.update(+ {+ "chunk_idx": i + 1,+ "chunk_total": num_chunks,+ "chunk_group_id": group_id,+ }+ )+ new_msg.meta_dict = meta++ # Increment timestamp slightly to maintain order+ _write_single_record(new_msg, role, base_ts + i * 1e-6)+def load_memory(self, memory: AgentMemory) -> None:r"""Load the provided memory into the agent.@@ -652,9 +801,19 @@ class ChatAgent(BaseAgent):r"""Initializes the stored messages list with the current systemmessage."""+ import time+self.memory.clear()+ # avoid UserWarning: The `ChatHistoryMemory` is empty.if self.system_message is not None:- self.update_memory(self.system_message, OpenAIBackendRole.SYSTEM)+ self.memory.write_record(+ MemoryRecord(+ message=self.system_message,+ role_at_backend=OpenAIBackendRole.SYSTEM,+ timestamp=time.time_ns() / 1_000_000_000,+ agent_id=self.agent_id,+ )+ )def record_message(self, message: BaseMessage) -> None:r"""Records the externally provided message into the agent memory as ifdiff --git a/camel/memories/context_creators/score_based.py b/camel/memories/context_creators/score_based.pyindex 4defa6ec4..fa6aeb042 100644--- a/camel/memories/context_creators/score_based.py+++ b/camel/memories/context_creators/score_based.py@@ -11,6 +11,7 @@# See the License for the specific language governing permissions and# limitations under the License.# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+from collections import defaultdictfrom typing import Dict, List, Optional, Tuplefrom pydantic import BaseModel@@ -18,7 +19,7 @@ from pydantic import BaseModelfrom camel.logger import get_loggerfrom camel.memories.base import BaseContextCreatorfrom camel.memories.records import ContextRecord-from camel.messages import OpenAIMessage+from camel.messages import FunctionCallingMessage, OpenAIMessagefrom camel.types.enums import OpenAIBackendRolefrom camel.utils import BaseTokenCounter@@ -112,6 +113,11 @@ class ScoreBasedContextCreator(BaseContextCreator):# Process non-system messages with deduplicationfor idx, record in enumerate(records):+ if (+ record.memory_record.role_at_backend+ == OpenAIBackendRole.SYSTEM+ ):+ continueif record.memory_record.uuid in seen_uuids:continueseen_uuids.add(record.memory_record.uuid)@@ -178,47 +184,32 @@ class ScoreBasedContextCreator(BaseContextCreator):def _group_tool_calls_and_responses(self, units: List[_ContextUnit]) -> Dict[str, List[_ContextUnit]]:- r"""Groups tool calls with their corresponding responses.+ r"""Groups tool calls with their corresponding responses based on+ `tool_call_id`.++ This improved logic robustly gathers all messages (assistant requests+ and tool responses, including chunks) that share a `tool_call_id`.Args:- units (List[_ContextUnit]): List of context units to analyze+ units (List[_ContextUnit]): List of context units to analyze.Returns:- Dict[str, List[_ContextUnit]]: Mapping from tool_call_id to list of- related units (tool call + responses)+ Dict[str, List[_ContextUnit]]: Mapping from `tool_call_id` to a+ list of related units."""- tool_call_groups: Dict[str, List[_ContextUnit]] = {}+ tool_call_groups: Dict[str, List[_ContextUnit]] = defaultdict(list)for unit in units:+ # FunctionCallingMessage stores tool_call_id.message = unit.record.memory_record.message- backend_role = unit.record.memory_record.role_at_backend+ tool_call_id = getattr(message, 'tool_call_id', None)- # Check if this is a tool call message- if hasattr(message, 'func_name') and hasattr(- message, 'tool_call_id'- ):- tool_call_id = getattr(message, 'tool_call_id', None)- if tool_call_id:- if tool_call_id not in tool_call_groups:- tool_call_groups[tool_call_id] = []- tool_call_groups[tool_call_id].append(unit)-- # Check if this is a tool response message- elif backend_role == OpenAIBackendRole.FUNCTION:- tool_call_id = None- if hasattr(message, 'tool_call_id'):- tool_call_id = getattr(message, 'tool_call_id', None)- elif hasattr(message, 'result') and hasattr(- message, 'tool_call_id'- ):- tool_call_id = getattr(message, 'tool_call_id', None)-- if tool_call_id:- if tool_call_id not in tool_call_groups:- tool_call_groups[tool_call_id] = []- tool_call_groups[tool_call_id].append(unit)+ if tool_call_id:+ tool_call_groups[tool_call_id].append(unit)- return tool_call_groups+ # Filter out empty or incomplete groups if necessary,+ # though defaultdict and getattr handle this gracefully.+ return dict(tool_call_groups)def _truncate_with_tool_call_awareness(self,@@ -227,60 +218,130 @@ class ScoreBasedContextCreator(BaseContextCreator):system_tokens: int,) -> List[_ContextUnit]:r"""Truncates messages while preserving tool call-response pairs.+ This method implements a more sophisticated truncation strategy:+ 1. It treats tool call groups (request + responses) and standalone+ messages as individual items to be included.+ 2. It sorts all items by score and greedily adds them to the context.+ 3. **Partial Truncation**: If a complete tool group is too large to+ fit,it attempts to add the request message and as many of the most+ recent response chunks as the token budget allows.Args:- regular_units (List[_ContextUnit]): All regular message units+ regular_units (List[_ContextUnit]): All regular message units.tool_call_groups (Dict[str, List[_ContextUnit]]): Grouped tool- calls- system_tokens (int): Tokens used by system message+ calls.+ system_tokens (int): Tokens used by the system message.Returns:- List[_ContextUnit]: Units that fit within token limit+ List[_ContextUnit]: A list of units that fit within the token+ limit."""- # Create sets for quick lookup of tool call related units- tool_call_unit_ids = set()- for group in tool_call_groups.values():- for unit in group:- tool_call_unit_ids.add(unit.record.memory_record.uuid)- # Separate tool call groups and standalone units+ # Create a set for quick lookup of units belonging to any tool call+ tool_call_unit_ids = {+ unit.record.memory_record.uuid+ for group in tool_call_groups.values()+ for unit in group+ }++ # Separate standalone units from tool call groupsstandalone_units = [ufor u in regular_unitsif u.record.memory_record.uuid not in tool_call_unit_ids]- # Sort standalone units for truncation (high scores first)- standalone_units.sort(key=self._truncation_sort_key)-- # Sort tool call groups by their best (highest) score- sorted_tool_groups = []- for _tool_call_id, group in tool_call_groups.items():- # Use the highest score in the group as the group's score- best_score = max(unit.record.score for unit in group)- latest_timestamp = max(unit.record.timestamp for unit in group)- group_tokens = sum(unit.num_tokens for unit in group)- sorted_tool_groups.append(- ((-best_score, -latest_timestamp), group, group_tokens)+ # Prepare all items (standalone units and groups) for sorting+ all_potential_items: List[Dict] = []+ for unit in standalone_units:+ all_potential_items.append(+ {+ "type": "standalone",+ "score": unit.record.score,+ "timestamp": unit.record.timestamp,+ "tokens": unit.num_tokens,+ "item": unit,+ }+ )+ for group in tool_call_groups.values():+ all_potential_items.append(+ {+ "type": "group",+ "score": max(u.record.score for u in group),+ "timestamp": max(u.record.timestamp for u in group),+ "tokens": sum(u.num_tokens for u in group),+ "item": group,+ })- sorted_tool_groups.sort(key=lambda x: x[0])+ # Sort all potential items by score (high to low), then timestamp+ all_potential_items.sort(key=lambda x: (-x["score"], -x["timestamp"]))- # Greedy selection to fit within token limit- remaining_units = []+ remaining_units: List[_ContextUnit] = []current_tokens = system_tokens- # First, try to include complete tool call groups- for _, group, group_tokens in sorted_tool_groups:- if current_tokens + group_tokens <= self.token_limit:- remaining_units.extend(group)- current_tokens += group_tokens-- # Then, include standalone units- for unit in standalone_units:- if current_tokens + unit.num_tokens <= self.token_limit:- remaining_units.append(unit)- current_tokens += unit.num_tokens+ for item_dict in all_potential_items:+ item_type = item_dict["type"]+ item = item_dict["item"]+ item_tokens = item_dict["tokens"]++ if current_tokens + item_tokens <= self.token_limit:+ # The whole item (standalone or group) fits, so add it+ if item_type == "standalone":+ remaining_units.append(item)+ else: # item_type == "group"+ remaining_units.extend(item)+ current_tokens += item_tokens++ elif item_type == "group":+ # The group does not fit completely; try partial inclusion.+ request_unit: Optional[_ContextUnit] = None+ response_units: List[_ContextUnit] = []++ for unit in item:+ # Assistant msg with `args` is the request+ if (+ isinstance(+ unit.record.memory_record.message,+ FunctionCallingMessage,+ )+ and unit.record.memory_record.message.args is not None+ ):+ request_unit = unit+ else:+ response_units.append(unit)++ # A group must have a request to be considered for inclusion.+ if request_unit is None:+ continue++ # Check if we can at least fit the request.+ if (+ current_tokens + request_unit.num_tokens+ <= self.token_limit+ ):+ units_to_add = [request_unit]+ tokens_to_add = request_unit.num_tokens++ # Sort responses by timestamp to add newest chunks first+ response_units.sort(+ key=lambda u: u.record.timestamp, reverse=True+ )++ for resp_unit in response_units:+ if (+ current_tokens+ + tokens_to_add+ + resp_unit.num_tokens+ <= self.token_limit+ ):+ units_to_add.append(resp_unit)+ tokens_to_add += resp_unit.num_tokens++ # A request must be followed by at least one response+ if len(units_to_add) > 1:+ remaining_units.extend(units_to_add)+ current_tokens += tokens_to_addreturn remaining_units@@ -319,25 +380,6 @@ class ScoreBasedContextCreator(BaseContextCreator):)return system_message_unit, []- def _truncation_sort_key(self, unit: _ContextUnit) -> Tuple[float, float]:- r"""Defines the sorting key for the truncation phase.-- Sorting priority:- - Primary: Sort by score in descending order (higher scores first).- - Secondary: Sort by timestamp in ascending order (older messages- first when scores are equal).-- Args:- unit (_ContextUnit): A `_ContextUnit` representing a conversation- record.-- Returns:- Tuple[float, float]:- - Negative score for descending order sorting.- - Timestamp for ascending order sorting.- """- return (-unit.record.score, unit.record.timestamp)-def _conversation_sort_key(self, unit: _ContextUnit) -> Tuple[float, float]:diff --git a/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py b/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.pyindex 3d08170f8..8a64dcad0 100644--- a/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py+++ b/camel/toolkits/non_visual_browser_toolkit/nv_browser_session.py@@ -16,12 +16,6 @@ from __future__ import annotationsfrom pathlib import Pathfrom typing import TYPE_CHECKING, Any, Optional-from playwright.async_api import (- Browser,- BrowserContext,- Page,-)-from .actions import ActionExecutorfrom .snapshot import PageSnapshot