Skip to content

Commit 80334e3

Browse files
author
yh 林
committed
format
1 parent 989560a commit 80334e3

File tree

3 files changed

+61
-317
lines changed

3 files changed

+61
-317
lines changed

camel/agents/chat_agent.py

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,6 @@ def __init__(
551551
self._context_summary_agent: Optional["ChatAgent"] = None
552552
self.stream_accumulate = stream_accumulate
553553

554-
# Initialize tool cost calculator
555-
from camel.utils.tool_cost_calculator import ToolCostCalculator
556-
557-
self._tool_cost_calculator = ToolCostCalculator(
558-
token_counter=self.model_backend.token_counter
559-
)
560-
561554
def reset(self):
562555
r"""Resets the :obj:`ChatAgent` to its initial state."""
563556
self.terminated = False
@@ -2168,7 +2161,7 @@ def _step_impl(
21682161
if self.prune_tool_calls_from_memory and tool_call_records:
21692162
self.memory.clean_tool_calls()
21702163

2171-
return self._convert_to_chatagent_response(
2164+
chatagent_response = self._convert_to_chatagent_response(
21722165
response,
21732166
tool_call_records,
21742167
accumulated_context_tokens,
@@ -2177,6 +2170,11 @@ def _step_impl(
21772170
step_token_usage["completion_tokens"],
21782171
step_token_usage["total_tokens"],
21792172
)
2173+
print(
2174+
f"""_STEP_IMPL info (shourld include tool cost):
2175+
{chatagent_response.info}"""
2176+
) # debug
2177+
return chatagent_response
21802178

21812179
@property
21822180
def chat_history(self) -> List[OpenAIMessage]:
@@ -3056,10 +3054,8 @@ def _record_tool_calling(
30563054
)
30573055

30583056
# Calculate tool cost and token usage
3059-
cost_info = self._tool_cost_calculator.estimate_tool_cost(
3060-
func_name, args, result
3061-
)
3062-
3057+
cost_info = self._calculate_tool_cost(assist_msg, func_msg)
3058+
print(f"_CALCULATE_TOOL_COST: {cost_info}") # debug
30633059
# Record information about this tool call with cost tracking
30643060
tool_record = ToolCallingRecord(
30653061
tool_name=func_name,
@@ -3073,8 +3069,61 @@ def _record_tool_calling(
30733069
},
30743070
)
30753071

3072+
print(f"_RECORD_TOOL_CALLING: {tool_record}") # debug
3073+
30763074
return tool_record
30773075

3076+
def _calculate_tool_cost(
3077+
self, assist_msg: OpenAIMessage, func_msg: OpenAIMessage
3078+
) -> Dict[str, int]:
3079+
r"""Calculate the tool cost and token usage for a tool call.
3080+
3081+
Args:
3082+
assist_msg (OpenAIMessage): The assistant message
3083+
as tool call input.
3084+
func_msg (OpenAIMessage): The function message
3085+
as tool call output.
3086+
3087+
Returns:
3088+
Dictionary containing token usage and cost estimates.
3089+
"""
3090+
3091+
if hasattr(self.model_backend, 'token_counter'):
3092+
try:
3093+
input_messages = assist_msg.to_openai_message(
3094+
OpenAIBackendRole.ASSISTANT
3095+
)
3096+
output_messages = func_msg.to_openai_message(
3097+
OpenAIBackendRole.FUNCTION
3098+
)
3099+
input_tokens = \
3100+
self.model_backend.token_counter.count_tokens_from_messages(
3101+
[input_messages]
3102+
)
3103+
output_tokens = \
3104+
self.model_backend.token_counter.count_tokens_from_messages(
3105+
[output_messages]
3106+
)
3107+
except Exception as e:
3108+
logger.error(
3109+
f"Error calculating tool call token usage tokens: {e}"
3110+
)
3111+
input_tokens = len(assist_msg.content.split())
3112+
output_tokens = len(func_msg.content.split())
3113+
else:
3114+
logger.warning(
3115+
"Token counter not available. "
3116+
"Using contexnt words count to estimate token usage."
3117+
)
3118+
input_tokens = len(assist_msg.content.split())
3119+
output_tokens = len(func_msg.content.split())
3120+
3121+
return {
3122+
"prompt_tokens": input_tokens,
3123+
"completion_tokens": output_tokens,
3124+
"total_tokens": input_tokens + output_tokens,
3125+
}
3126+
30783127
def _stream(
30793128
self,
30803129
input_message: Union[BaseMessage, str],

camel/utils/tool_cost_calculator.py

Lines changed: 0 additions & 229 deletions
This file was deleted.

0 commit comments

Comments
 (0)