🐫 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
enhance: More robut time setting and skip empty message for ChatAgent (#2582)
Wendong-Fan09d671a1a4ed
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.pyindex f25dc622a..7f4f39356 100644--- a/camel/agents/chat_agent.py+++ b/camel/agents/chat_agent.py@@ -19,7 +19,6 @@ import textwrapimport threadingimport uuidfrom collections import defaultdict-from datetime import datetimefrom pathlib import Pathfrom typing import (TYPE_CHECKING,@@ -509,7 +508,7 @@ class ChatAgent(BaseAgent):memory record. If None, current timestamp will be used.(default: :obj:`None`)"""- from datetime import timezone+ import timeself.memory.write_record(MemoryRecord(@@ -517,7 +516,7 @@ class ChatAgent(BaseAgent):role_at_backend=role,timestamp=timestampif timestamp is not None- else datetime.now(timezone.utc).timestamp(),+ else time.time_ns() / 1_000_000_000, # Nanosecond precisionagent_id=self.agent_id,))@@ -1331,6 +1330,13 @@ class ChatAgent(BaseAgent):"""output_messages: List[BaseMessage] = []for choice in response.choices:+ # Skip messages with no meaningful content+ if (+ choice.message.content is None+ or choice.message.content.strip() == ""+ ) and not choice.message.tool_calls:+ continue+meta_dict = {}if logprobs_info := handle_logprobs(choice):meta_dict["logprobs_info"] = logprobs_info@@ -1623,17 +1629,24 @@ class ChatAgent(BaseAgent):tool_call_id=tool_call_id,)- # Use slightly different timestamps to ensure correct ordering+ # Use precise timestamps to ensure correct ordering# This ensures the assistant message (tool call) always appears before# the function message (tool result) in the conversation context- current_time = datetime.now().timestamp()+ # Use time.time_ns() for nanosecond precision to avoid collisions+ import time++ current_time_ns = time.time_ns()+ base_timestamp = current_time_ns / 1_000_000_000 # Convert to seconds+self.update_memory(- assist_msg, OpenAIBackendRole.ASSISTANT, timestamp=current_time+ assist_msg, OpenAIBackendRole.ASSISTANT, timestamp=base_timestamp)++ # Add minimal increment to ensure function message comes afterself.update_memory(func_msg,OpenAIBackendRole.FUNCTION,- timestamp=current_time + 0.001,+ timestamp=base_timestamp + 1e-9,)# Record information about this tool calldiff --git a/camel/memories/records.py b/camel/memories/records.pyindex 861fcfa1e..3b722efba 100644--- a/camel/memories/records.py+++ b/camel/memories/records.py@@ -15,8 +15,8 @@# Enables postponed evaluation of annotations (for string-based type hints)from __future__ import annotations+import timefrom dataclasses import asdict-from datetime import datetime, timezonefrom typing import Any, ClassVar, Dictfrom uuid import UUID, uuid4@@ -53,7 +53,8 @@ class MemoryRecord(BaseModel):uuid: UUID = Field(default_factory=uuid4)extra_info: Dict[str, str] = Field(default_factory=dict)timestamp: float = Field(- default_factory=lambda: datetime.now(timezone.utc).timestamp()+ default_factory=lambda: time.time_ns()+ / 1_000_000_000 # Nanosecond precision)agent_id: str = Field(default="")@@ -109,5 +110,6 @@ class ContextRecord(BaseModel):memory_record: MemoryRecordscore: floattimestamp: float = Field(- default_factory=lambda: datetime.now(timezone.utc).timestamp()+ default_factory=lambda: time.time_ns()+ / 1_000_000_000 # Nanosecond precision)