From 497db8128203f06175ef5f1debef3e66639d4030 Mon Sep 17 00:00:00 2001 From: Wendong Date: Wed, 19 Mar 2025 05:20:04 +0800 Subject: [PATCH] format fix and more safe token info get --- .../virtual_fitting_room/run_gpt4o.py | 66 ++++++++++-------- examples/run_cli.py | 13 ++++ owl/utils/enhanced_role_playing.py | 69 +++++++++++-------- 3 files changed, 91 insertions(+), 57 deletions(-) diff --git a/community_usecase/virtual_fitting_room/run_gpt4o.py b/community_usecase/virtual_fitting_room/run_gpt4o.py index 3a07971..daa176d 100644 --- a/community_usecase/virtual_fitting_room/run_gpt4o.py +++ b/community_usecase/virtual_fitting_room/run_gpt4o.py @@ -13,12 +13,10 @@ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= import os import logging -import functools import json -from typing import Callable, Any, Dict, List from dotenv import load_dotenv -from camel.models import ModelFactory, BaseModelBackend +from camel.models import ModelFactory from camel.toolkits import ( ExcelToolkit, @@ -26,9 +24,8 @@ from camel.toolkits import ( SearchToolkit, BrowserToolkit, FileWriteToolkit, - VirtualTryOnToolkit + VirtualTryOnToolkit, ) -from camel.toolkits.base import BaseToolkit from camel.types import ModelPlatformType from owl.utils import run_society @@ -44,15 +41,16 @@ load_dotenv(dotenv_path=str(env_path)) # set detailed log recording for debug set_log_level(level="DEBUG") logger = get_logger(__name__) -file_handler = logging.FileHandler('tool_calls.log') +file_handler = logging.FileHandler("tool_calls.log") file_handler.setLevel(logging.DEBUG) -formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) logger.addHandler(file_handler) root_logger = logging.getLogger() root_logger.addHandler(file_handler) + def construct_society(question: str) -> RolePlaying: r"""Construct a society of agents based on the given question. @@ -107,7 +105,7 @@ def construct_society(question: str) -> RolePlaying: excel_toolkit = ExcelToolkit() file_toolkit = FileWriteToolkit(output_dir="./") virtual_try_on_toolkit = VirtualTryOnToolkit() - + tools = [ *browser_toolkit.get_tools(), *image_toolkit.get_tools(), @@ -144,7 +142,7 @@ def construct_society(question: str) -> RolePlaying: def main(): r"""Main function to run the OWL system with an example question.""" - question = f"open https://www.uniqlo.com/eu-at/en/women/tops?path=37608%2C84986%2C85018%2C85207 which shows some clothes on sale. First, directly click one image of clothes which should be an big interactive element (don't wrongly click the small like button overlapped on the image!) to go into its specific details page and then get a partial screenshot for this clothes. Second, only after you've get the partial screenshort of the product, using your own virtual try-on toolkit (there is no built-in virtual try-on button on this website, either no third party tool required) to show me the virtual try-on result with the product." + question = "open https://www.uniqlo.com/eu-at/en/women/tops?path=37608%2C84986%2C85018%2C85207 which shows some clothes on sale. First, directly click one image of clothes which should be an big interactive element (don't wrongly click the small like button overlapped on the image!) to go into its specific details page and then get a partial screenshot for this clothes. Second, only after you've get the partial screenshort of the product, using your own virtual try-on toolkit (there is no built-in virtual try-on button on this website, either no third party tool required) to show me the virtual try-on result with the product." # Construct and run the society society = construct_society(question) @@ -159,40 +157,48 @@ def analyze_chat_history(chat_history): r"""分析聊天历史记录,提取工具调用信息。""" print("\n============ 工具调用分析 ============") logger.info("========== 开始分析聊天历史中的工具调用 ==========") - + tool_calls = [] for i, message in enumerate(chat_history): - if message.get('role') == 'assistant' and 'tool_calls' in message: - for tool_call in message.get('tool_calls', []): - if tool_call.get('type') == 'function': - function = tool_call.get('function', {}) + if message.get("role") == "assistant" and "tool_calls" in message: + for tool_call in message.get("tool_calls", []): + if tool_call.get("type") == "function": + function = tool_call.get("function", {}) tool_info = { - 'call_id': tool_call.get('id'), - 'name': function.get('name'), - 'arguments': function.get('arguments'), - 'message_index': i, + "call_id": tool_call.get("id"), + "name": function.get("name"), + "arguments": function.get("arguments"), + "message_index": i, } tool_calls.append(tool_info) - print(f"工具调用: {function.get('name')} 参数: {function.get('arguments')}") - logger.info(f"工具调用: {function.get('name')} 参数: {function.get('arguments')}") - - elif message.get('role') == 'tool' and 'tool_call_id' in message: + print( + f"工具调用: {function.get('name')} 参数: {function.get('arguments')}" + ) + logger.info( + f"工具调用: {function.get('name')} 参数: {function.get('arguments')}" + ) + + elif message.get("role") == "tool" and "tool_call_id" in message: # 找到对应的工具调用 for tool_call in tool_calls: - if tool_call.get('call_id') == message.get('tool_call_id'): - result = message.get('content', '') - result_summary = result[:100] + "..." if len(result) > 100 else result + if tool_call.get("call_id") == message.get("tool_call_id"): + result = message.get("content", "") + result_summary = ( + result[:100] + "..." if len(result) > 100 else result + ) print(f"工具结果: {tool_call.get('name')} 返回: {result_summary}") - logger.info(f"工具结果: {tool_call.get('name')} 返回: {result_summary}") - + logger.info( + f"工具结果: {tool_call.get('name')} 返回: {result_summary}" + ) + print(f"总共发现 {len(tool_calls)} 个工具调用") logger.info(f"总共发现 {len(tool_calls)} 个工具调用") logger.info("========== 结束分析聊天历史中的工具调用 ==========") - + # 将完整聊天历史保存到文件 - with open('chat_history.json', 'w', encoding='utf-8') as f: + with open("chat_history.json", "w", encoding="utf-8") as f: json.dump(chat_history, f, ensure_ascii=False, indent=2) - + print("记录已保存到 chat_history.json") print("============ 分析结束 ============\n") diff --git a/examples/run_cli.py b/examples/run_cli.py index f7040d6..b8e453e 100644 --- a/examples/run_cli.py +++ b/examples/run_cli.py @@ -1,3 +1,16 @@ +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= from dotenv import load_dotenv from camel.models import ModelFactory diff --git a/owl/utils/enhanced_role_playing.py b/owl/utils/enhanced_role_playing.py index fcdec91..80332fd 100644 --- a/owl/utils/enhanced_role_playing.py +++ b/owl/utils/enhanced_role_playing.py @@ -451,30 +451,37 @@ def run_society( input_msg = society.init_chat(init_prompt) for _round in range(round_limit): assistant_response, user_response = society.step(input_msg) - overall_completion_token_count += ( - assistant_response.info["usage"]["completion_tokens"] - + user_response.info["usage"]["completion_tokens"] - ) - overall_prompt_token_count += ( - assistant_response.info["usage"]["prompt_tokens"] - + user_response.info["usage"]["prompt_tokens"] - ) + # Check if usage info is available before accessing it + if assistant_response.info.get("usage") and user_response.info.get("usage"): + overall_completion_token_count += assistant_response.info["usage"].get( + "completion_tokens", 0 + ) + user_response.info["usage"].get("completion_tokens", 0) + overall_prompt_token_count += assistant_response.info["usage"].get( + "prompt_tokens", 0 + ) + user_response.info["usage"].get("prompt_tokens", 0) # convert tool call to dict tool_call_records: List[dict] = [] - for tool_call in assistant_response.info["tool_calls"]: - tool_call_records.append(tool_call.as_dict()) + if assistant_response.info.get("tool_calls"): + for tool_call in assistant_response.info["tool_calls"]: + tool_call_records.append(tool_call.as_dict()) _data = { - "user": user_response.msg.content, - "assistant": assistant_response.msg.content, + "user": user_response.msg.content + if hasattr(user_response, "msg") and user_response.msg + else "", + "assistant": assistant_response.msg.content + if hasattr(assistant_response, "msg") and assistant_response.msg + else "", "tool_calls": tool_call_records, } chat_history.append(_data) - logger.info(f"Round #{_round} user_response:\n {user_response.msgs[0].content}") logger.info( - f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content}" + f"Round #{_round} user_response:\n {user_response.msgs[0].content if user_response.msgs and len(user_response.msgs) > 0 else ''}" + ) + logger.info( + f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content if assistant_response.msgs and len(assistant_response.msgs) > 0 else ''}" ) if ( @@ -509,29 +516,37 @@ async def arun_society( input_msg = society.init_chat(init_prompt) for _round in range(round_limit): assistant_response, user_response = await society.astep(input_msg) - overall_prompt_token_count += assistant_response.info["usage"][ - "completion_tokens" - ] - overall_prompt_token_count += ( - assistant_response.info["usage"]["prompt_tokens"] - + user_response.info["usage"]["prompt_tokens"] - ) + # Check if usage info is available before accessing it + if assistant_response.info.get("usage") and user_response.info.get("usage"): + overall_prompt_token_count += assistant_response.info["usage"].get( + "completion_tokens", 0 + ) + overall_prompt_token_count += assistant_response.info["usage"].get( + "prompt_tokens", 0 + ) + user_response.info["usage"].get("prompt_tokens", 0) # convert tool call to dict tool_call_records: List[dict] = [] - for tool_call in assistant_response.info["tool_calls"]: - tool_call_records.append(tool_call.as_dict()) + if assistant_response.info.get("tool_calls"): + for tool_call in assistant_response.info["tool_calls"]: + tool_call_records.append(tool_call.as_dict()) _data = { - "user": user_response.msg.content, - "assistant": assistant_response.msg.content, + "user": user_response.msg.content + if hasattr(user_response, "msg") and user_response.msg + else "", + "assistant": assistant_response.msg.content + if hasattr(assistant_response, "msg") and assistant_response.msg + else "", "tool_calls": tool_call_records, } chat_history.append(_data) - logger.info(f"Round #{_round} user_response:\n {user_response.msgs[0].content}") logger.info( - f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content}" + f"Round #{_round} user_response:\n {user_response.msgs[0].content if user_response.msgs and len(user_response.msgs) > 0 else ''}" + ) + logger.info( + f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content if assistant_response.msgs and len(assistant_response.msgs) > 0 else ''}" ) # Check other termination conditions