add community usecase: virtual fitting room (#393)
Browse files
community_usecase/virtual_fitting_room/run_gpt4o.py
CHANGED
@@ -13,10 +13,12 @@
|
|
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,8 +26,9 @@ from camel.toolkits import (
|
|
24 |
SearchToolkit,
|
25 |
BrowserToolkit,
|
26 |
FileWriteToolkit,
|
27 |
-
VirtualTryOnToolkit
|
28 |
)
|
|
|
29 |
from camel.types import ModelPlatformType
|
30 |
|
31 |
from owl.utils import run_society
|
@@ -41,16 +44,15 @@ load_dotenv(dotenv_path=str(env_path))
|
|
41 |
# set detailed log recording for debug
|
42 |
set_log_level(level="DEBUG")
|
43 |
logger = get_logger(__name__)
|
44 |
-
file_handler = logging.FileHandler(
|
45 |
file_handler.setLevel(logging.DEBUG)
|
46 |
-
formatter = logging.Formatter(
|
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,7 +107,7 @@ def construct_society(question: str) -> RolePlaying:
|
|
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,66 +144,13 @@ def construct_society(question: str) -> RolePlaying:
|
|
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)
|
149 |
answer, chat_history, token_count = run_society(society)
|
150 |
-
|
151 |
-
# record tool using history (for debug)
|
152 |
-
analyze_chat_history(chat_history)
|
153 |
print(f"\033[94mAnswer: {answer}\033[0m")
|
154 |
|
155 |
-
|
156 |
-
def analyze_chat_history(chat_history):
|
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 |
-
|
205 |
-
|
206 |
if __name__ == "__main__":
|
207 |
main()
|
|
|
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 |
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 |
# 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 |
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 |
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)
|
151 |
answer, chat_history, token_count = run_society(society)
|
152 |
+
# output the result
|
|
|
|
|
153 |
print(f"\033[94mAnswer: {answer}\033[0m")
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
if __name__ == "__main__":
|
156 |
main()
|