Skip to content

Commit a71b340

Browse files
authored
add community usecase: virtual fitting room (#393)
2 parents 3140c05 + ea0142a commit a71b340

File tree

1 file changed

+10
-61
lines changed

1 file changed

+10
-61
lines changed

community_usecase/virtual_fitting_room/run_gpt4o.py

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@
1313
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414
import os
1515
import logging
16+
import functools
1617
import json
18+
from typing import Callable, Any, Dict, List
1719

1820
from dotenv import load_dotenv
19-
from camel.models import ModelFactory
21+
from camel.models import ModelFactory, BaseModelBackend
2022

2123
from camel.toolkits import (
2224
ExcelToolkit,
2325
ImageAnalysisToolkit,
2426
SearchToolkit,
2527
BrowserToolkit,
2628
FileWriteToolkit,
27-
VirtualTryOnToolkit,
29+
VirtualTryOnToolkit
2830
)
31+
from camel.toolkits.base import BaseToolkit
2932
from camel.types import ModelPlatformType
3033

3134
from owl.utils import run_society
@@ -41,16 +44,15 @@
4144
# set detailed log recording for debug
4245
set_log_level(level="DEBUG")
4346
logger = get_logger(__name__)
44-
file_handler = logging.FileHandler("tool_calls.log")
47+
file_handler = logging.FileHandler('tool_calls.log')
4548
file_handler.setLevel(logging.DEBUG)
46-
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
49+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
4750
file_handler.setFormatter(formatter)
4851
logger.addHandler(file_handler)
4952

5053
root_logger = logging.getLogger()
5154
root_logger.addHandler(file_handler)
5255

53-
5456
def construct_society(question: str) -> RolePlaying:
5557
r"""Construct a society of agents based on the given question.
5658
@@ -105,7 +107,7 @@ def construct_society(question: str) -> RolePlaying:
105107
excel_toolkit = ExcelToolkit()
106108
file_toolkit = FileWriteToolkit(output_dir="./")
107109
virtual_try_on_toolkit = VirtualTryOnToolkit()
108-
110+
109111
tools = [
110112
*browser_toolkit.get_tools(),
111113
*image_toolkit.get_tools(),
@@ -142,66 +144,13 @@ def construct_society(question: str) -> RolePlaying:
142144
def main():
143145
r"""Main function to run the OWL system with an example question."""
144146

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."
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."
146148

147149
# Construct and run the society
148150
society = construct_society(question)
149151
answer, chat_history, token_count = run_society(society)
150-
151-
# record tool using history (for debug)
152-
analyze_chat_history(chat_history)
152+
# output the result
153153
print(f"\033[94mAnswer: {answer}\033[0m")
154154

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-
206155
if __name__ == "__main__":
207156
main()

0 commit comments

Comments
 (0)