Wendong-Fan commited on
Commit
99d585a
·
1 Parent(s): 479e8d1

format fix and more safe token info get

Browse files
community_usecase/virtual_fitting_room/run_gpt4o.py CHANGED
@@ -13,12 +13,10 @@
13
  # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
  import os
15
  import logging
16
- import functools
17
  import json
18
- from typing import Callable, Any, Dict, List
19
 
20
  from dotenv import load_dotenv
21
- from camel.models import ModelFactory, BaseModelBackend
22
 
23
  from camel.toolkits import (
24
  ExcelToolkit,
@@ -26,9 +24,8 @@ from camel.toolkits import (
26
  SearchToolkit,
27
  BrowserToolkit,
28
  FileWriteToolkit,
29
- VirtualTryOnToolkit
30
  )
31
- from camel.toolkits.base import BaseToolkit
32
  from camel.types import ModelPlatformType
33
 
34
  from owl.utils import run_society
@@ -44,15 +41,16 @@ load_dotenv(dotenv_path=str(env_path))
44
  # set detailed log recording for debug
45
  set_log_level(level="DEBUG")
46
  logger = get_logger(__name__)
47
- file_handler = logging.FileHandler('tool_calls.log')
48
  file_handler.setLevel(logging.DEBUG)
49
- formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
50
  file_handler.setFormatter(formatter)
51
  logger.addHandler(file_handler)
52
 
53
  root_logger = logging.getLogger()
54
  root_logger.addHandler(file_handler)
55
 
 
56
  def construct_society(question: str) -> RolePlaying:
57
  r"""Construct a society of agents based on the given question.
58
 
@@ -107,7 +105,7 @@ def construct_society(question: str) -> RolePlaying:
107
  excel_toolkit = ExcelToolkit()
108
  file_toolkit = FileWriteToolkit(output_dir="./")
109
  virtual_try_on_toolkit = VirtualTryOnToolkit()
110
-
111
  tools = [
112
  *browser_toolkit.get_tools(),
113
  *image_toolkit.get_tools(),
@@ -144,7 +142,7 @@ def construct_society(question: str) -> RolePlaying:
144
  def main():
145
  r"""Main function to run the OWL system with an example question."""
146
 
147
- 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."
148
 
149
  # Construct and run the society
150
  society = construct_society(question)
@@ -159,40 +157,48 @@ def analyze_chat_history(chat_history):
159
  r"""分析聊天历史记录,提取工具调用信息。"""
160
  print("\n============ 工具调用分析 ============")
161
  logger.info("========== 开始分析聊天历史中的工具调用 ==========")
162
-
163
  tool_calls = []
164
  for i, message in enumerate(chat_history):
165
- if message.get('role') == 'assistant' and 'tool_calls' in message:
166
- for tool_call in message.get('tool_calls', []):
167
- if tool_call.get('type') == 'function':
168
- function = tool_call.get('function', {})
169
  tool_info = {
170
- 'call_id': tool_call.get('id'),
171
- 'name': function.get('name'),
172
- 'arguments': function.get('arguments'),
173
- 'message_index': i,
174
  }
175
  tool_calls.append(tool_info)
176
- print(f"工具调用: {function.get('name')} 参数: {function.get('arguments')}")
177
- logger.info(f"工具调用: {function.get('name')} 参数: {function.get('arguments')}")
178
-
179
- elif message.get('role') == 'tool' and 'tool_call_id' in message:
 
 
 
 
180
  # 找到对应的工具调用
181
  for tool_call in tool_calls:
182
- if tool_call.get('call_id') == message.get('tool_call_id'):
183
- result = message.get('content', '')
184
- result_summary = result[:100] + "..." if len(result) > 100 else result
 
 
185
  print(f"工具结果: {tool_call.get('name')} 返回: {result_summary}")
186
- logger.info(f"工具结果: {tool_call.get('name')} 返回: {result_summary}")
187
-
 
 
188
  print(f"总共发现 {len(tool_calls)} 个工具调用")
189
  logger.info(f"总共发现 {len(tool_calls)} 个工具调用")
190
  logger.info("========== 结束分析聊天历史中的工具调用 ==========")
191
-
192
  # 将完整聊天历史保存到文件
193
- with open('chat_history.json', 'w', encoding='utf-8') as f:
194
  json.dump(chat_history, f, ensure_ascii=False, indent=2)
195
-
196
  print("记录已保存到 chat_history.json")
197
  print("============ 分析结束 ============\n")
198
 
 
13
  # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
  import os
15
  import logging
 
16
  import json
 
17
 
18
  from dotenv import load_dotenv
19
+ from camel.models import ModelFactory
20
 
21
  from camel.toolkits import (
22
  ExcelToolkit,
 
24
  SearchToolkit,
25
  BrowserToolkit,
26
  FileWriteToolkit,
27
+ VirtualTryOnToolkit,
28
  )
 
29
  from camel.types import ModelPlatformType
30
 
31
  from owl.utils import run_society
 
41
  # set detailed log recording for debug
42
  set_log_level(level="DEBUG")
43
  logger = get_logger(__name__)
44
+ file_handler = logging.FileHandler("tool_calls.log")
45
  file_handler.setLevel(logging.DEBUG)
46
+ formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
47
  file_handler.setFormatter(formatter)
48
  logger.addHandler(file_handler)
49
 
50
  root_logger = logging.getLogger()
51
  root_logger.addHandler(file_handler)
52
 
53
+
54
  def construct_society(question: str) -> RolePlaying:
55
  r"""Construct a society of agents based on the given question.
56
 
 
105
  excel_toolkit = ExcelToolkit()
106
  file_toolkit = FileWriteToolkit(output_dir="./")
107
  virtual_try_on_toolkit = VirtualTryOnToolkit()
108
+
109
  tools = [
110
  *browser_toolkit.get_tools(),
111
  *image_toolkit.get_tools(),
 
142
  def main():
143
  r"""Main function to run the OWL system with an example question."""
144
 
145
+ 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."
146
 
147
  # Construct and run the society
148
  society = construct_society(question)
 
157
  r"""分析聊天历史记录,提取工具调用信息。"""
158
  print("\n============ 工具调用分析 ============")
159
  logger.info("========== 开始分析聊天历史中的工具调用 ==========")
160
+
161
  tool_calls = []
162
  for i, message in enumerate(chat_history):
163
+ if message.get("role") == "assistant" and "tool_calls" in message:
164
+ for tool_call in message.get("tool_calls", []):
165
+ if tool_call.get("type") == "function":
166
+ function = tool_call.get("function", {})
167
  tool_info = {
168
+ "call_id": tool_call.get("id"),
169
+ "name": function.get("name"),
170
+ "arguments": function.get("arguments"),
171
+ "message_index": i,
172
  }
173
  tool_calls.append(tool_info)
174
+ print(
175
+ f"工具调用: {function.get('name')} 参数: {function.get('arguments')}"
176
+ )
177
+ logger.info(
178
+ f"工具调用: {function.get('name')} 参数: {function.get('arguments')}"
179
+ )
180
+
181
+ elif message.get("role") == "tool" and "tool_call_id" in message:
182
  # 找到对应的工具调用
183
  for tool_call in tool_calls:
184
+ if tool_call.get("call_id") == message.get("tool_call_id"):
185
+ result = message.get("content", "")
186
+ result_summary = (
187
+ result[:100] + "..." if len(result) > 100 else result
188
+ )
189
  print(f"工具结果: {tool_call.get('name')} 返回: {result_summary}")
190
+ logger.info(
191
+ f"工具结果: {tool_call.get('name')} 返回: {result_summary}"
192
+ )
193
+
194
  print(f"总共发现 {len(tool_calls)} 个工具调用")
195
  logger.info(f"总共发现 {len(tool_calls)} 个工具调用")
196
  logger.info("========== 结束分析聊天历史中的工具调用 ==========")
197
+
198
  # 将完整聊天历史保存到文件
199
+ with open("chat_history.json", "w", encoding="utf-8") as f:
200
  json.dump(chat_history, f, ensure_ascii=False, indent=2)
201
+
202
  print("记录已保存到 chat_history.json")
203
  print("============ 分析结束 ============\n")
204
 
examples/run_cli.py CHANGED
@@ -1,3 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from dotenv import load_dotenv
2
 
3
  from camel.models import ModelFactory
 
1
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
14
  from dotenv import load_dotenv
15
 
16
  from camel.models import ModelFactory
owl/utils/enhanced_role_playing.py CHANGED
@@ -451,30 +451,37 @@ def run_society(
451
  input_msg = society.init_chat(init_prompt)
452
  for _round in range(round_limit):
453
  assistant_response, user_response = society.step(input_msg)
454
- overall_completion_token_count += (
455
- assistant_response.info["usage"]["completion_tokens"]
456
- + user_response.info["usage"]["completion_tokens"]
457
- )
458
- overall_prompt_token_count += (
459
- assistant_response.info["usage"]["prompt_tokens"]
460
- + user_response.info["usage"]["prompt_tokens"]
461
- )
462
 
463
  # convert tool call to dict
464
  tool_call_records: List[dict] = []
465
- for tool_call in assistant_response.info["tool_calls"]:
466
- tool_call_records.append(tool_call.as_dict())
 
467
 
468
  _data = {
469
- "user": user_response.msg.content,
470
- "assistant": assistant_response.msg.content,
 
 
 
 
471
  "tool_calls": tool_call_records,
472
  }
473
 
474
  chat_history.append(_data)
475
- logger.info(f"Round #{_round} user_response:\n {user_response.msgs[0].content}")
476
  logger.info(
477
- f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content}"
 
 
 
478
  )
479
 
480
  if (
@@ -509,29 +516,37 @@ async def arun_society(
509
  input_msg = society.init_chat(init_prompt)
510
  for _round in range(round_limit):
511
  assistant_response, user_response = await society.astep(input_msg)
512
- overall_prompt_token_count += assistant_response.info["usage"][
513
- "completion_tokens"
514
- ]
515
- overall_prompt_token_count += (
516
- assistant_response.info["usage"]["prompt_tokens"]
517
- + user_response.info["usage"]["prompt_tokens"]
518
- )
 
519
 
520
  # convert tool call to dict
521
  tool_call_records: List[dict] = []
522
- for tool_call in assistant_response.info["tool_calls"]:
523
- tool_call_records.append(tool_call.as_dict())
 
524
 
525
  _data = {
526
- "user": user_response.msg.content,
527
- "assistant": assistant_response.msg.content,
 
 
 
 
528
  "tool_calls": tool_call_records,
529
  }
530
 
531
  chat_history.append(_data)
532
- logger.info(f"Round #{_round} user_response:\n {user_response.msgs[0].content}")
533
  logger.info(
534
- f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content}"
 
 
 
535
  )
536
 
537
  # Check other termination conditions
 
451
  input_msg = society.init_chat(init_prompt)
452
  for _round in range(round_limit):
453
  assistant_response, user_response = society.step(input_msg)
454
+ # Check if usage info is available before accessing it
455
+ if assistant_response.info.get("usage") and user_response.info.get("usage"):
456
+ overall_completion_token_count += assistant_response.info["usage"].get(
457
+ "completion_tokens", 0
458
+ ) + user_response.info["usage"].get("completion_tokens", 0)
459
+ overall_prompt_token_count += assistant_response.info["usage"].get(
460
+ "prompt_tokens", 0
461
+ ) + user_response.info["usage"].get("prompt_tokens", 0)
462
 
463
  # convert tool call to dict
464
  tool_call_records: List[dict] = []
465
+ if assistant_response.info.get("tool_calls"):
466
+ for tool_call in assistant_response.info["tool_calls"]:
467
+ tool_call_records.append(tool_call.as_dict())
468
 
469
  _data = {
470
+ "user": user_response.msg.content
471
+ if hasattr(user_response, "msg") and user_response.msg
472
+ else "",
473
+ "assistant": assistant_response.msg.content
474
+ if hasattr(assistant_response, "msg") and assistant_response.msg
475
+ else "",
476
  "tool_calls": tool_call_records,
477
  }
478
 
479
  chat_history.append(_data)
 
480
  logger.info(
481
+ f"Round #{_round} user_response:\n {user_response.msgs[0].content if user_response.msgs and len(user_response.msgs) > 0 else ''}"
482
+ )
483
+ logger.info(
484
+ f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content if assistant_response.msgs and len(assistant_response.msgs) > 0 else ''}"
485
  )
486
 
487
  if (
 
516
  input_msg = society.init_chat(init_prompt)
517
  for _round in range(round_limit):
518
  assistant_response, user_response = await society.astep(input_msg)
519
+ # Check if usage info is available before accessing it
520
+ if assistant_response.info.get("usage") and user_response.info.get("usage"):
521
+ overall_prompt_token_count += assistant_response.info["usage"].get(
522
+ "completion_tokens", 0
523
+ )
524
+ overall_prompt_token_count += assistant_response.info["usage"].get(
525
+ "prompt_tokens", 0
526
+ ) + user_response.info["usage"].get("prompt_tokens", 0)
527
 
528
  # convert tool call to dict
529
  tool_call_records: List[dict] = []
530
+ if assistant_response.info.get("tool_calls"):
531
+ for tool_call in assistant_response.info["tool_calls"]:
532
+ tool_call_records.append(tool_call.as_dict())
533
 
534
  _data = {
535
+ "user": user_response.msg.content
536
+ if hasattr(user_response, "msg") and user_response.msg
537
+ else "",
538
+ "assistant": assistant_response.msg.content
539
+ if hasattr(assistant_response, "msg") and assistant_response.msg
540
+ else "",
541
  "tool_calls": tool_call_records,
542
  }
543
 
544
  chat_history.append(_data)
 
545
  logger.info(
546
+ f"Round #{_round} user_response:\n {user_response.msgs[0].content if user_response.msgs and len(user_response.msgs) > 0 else ''}"
547
+ )
548
+ logger.info(
549
+ f"Round #{_round} assistant_response:\n {assistant_response.msgs[0].content if assistant_response.msgs and len(assistant_response.msgs) > 0 else ''}"
550
  )
551
 
552
  # Check other termination conditions