From 8537e3b436f296fdb473a054c532a5b5219fb30a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Tue, 6 Feb 2024 17:09:04 +0800 Subject: [PATCH 01/18] feat: add parse arguments for openai tool_calls. --- metagpt/provider/openai_api.py | 34 ++++++++++++++++++++++++++- tests/metagpt/provider/test_openai.py | 8 ++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 63e68c9bd..a20330919 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -8,6 +8,7 @@ """ import json +import re from typing import AsyncIterator, Optional, Union from openai import APIConnectionError, AsyncOpenAI, AsyncStream @@ -194,6 +195,30 @@ class OpenAILLM(BaseLLM): rsp = await self._achat_completion_function(messages, **kwargs) return self.get_choice_function_arguments(rsp) + def _parse_arguments(self, arguments: str) -> dict: + """parse arguments in openai function call""" + if "langugae" not in arguments and "code" not in arguments: + logger.warning(f"Not found `code`, `language`, We assume it is pure code:\n {arguments}\n. ") + return {"language": "python", "code": arguments} + + # 匹配language + language_pattern = re.compile(r'[\"\']?language[\"\']?\s*:\s*["\']([^"\']+?)["\']', re.DOTALL) + language_match = language_pattern.search(arguments) + language_value = language_match.group(1) if language_match else "python" + + # 匹配code + code_pattern = r'(["\'`]{3}|["\'`])([\s\S]*?)\1' + try: + code_value = re.findall(code_pattern, arguments)[-1][-1] + except Exception as e: + logger.error(f"{e}, when re.findall({code_pattern}, {arguments})") + code_value = None + + if code_value is None: + raise ValueError(f"Parse code error for {arguments}") + # arguments只有code的情况 + return {"language": language_value, "code": code_value} + # @handle_exception def get_choice_function_arguments(self, rsp: ChatCompletion) -> dict: """Required to provide the first function arguments of choice. @@ -209,7 +234,14 @@ class OpenAILLM(BaseLLM): and message.tool_calls[0].function.arguments is not None ): # reponse is code - return json.loads(message.tool_calls[0].function.arguments, strict=False) + try: + return json.loads(message.tool_calls[0].function.arguments, strict=False) + except json.decoder.JSONDecodeError as e: + error_msg = ( + f"Got JSONDecodeError for \n{'--'*40} \n{message.tool_calls[0].function.arguments}, {str(e)}" + ) + logger.error(error_msg) + return self._parse_arguments(message.tool_calls[0].function.arguments) elif message.tool_calls is None and message.content is not None: # reponse is code, fix openai tools_call respond bug, # The response content is `code``, but it appears in the content instead of the arguments. diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py index d6aa04c7b..b9db4dfb6 100644 --- a/tests/metagpt/provider/test_openai.py +++ b/tests/metagpt/provider/test_openai.py @@ -106,9 +106,11 @@ class TestOpenAI: def test_aask_code_json_decode_error(self, json_decode_error): instance = OpenAILLM(mock_llm_config) - with pytest.raises(json.decoder.JSONDecodeError) as e: - instance.get_choice_function_arguments(json_decode_error) - assert "JSONDecodeError" in str(e) + code = instance.get_choice_function_arguments(json_decode_error) + assert "code" in code + assert "language" in code + assert "hello world" in code["code"] + logger.info(f'code is : {code["code"]}') @pytest.mark.asyncio From 9fa299525ed0e4e823f6bc6e4976c2ef4e437d71 Mon Sep 17 00:00:00 2001 From: mannaandpoem <1580466765@qq.com> Date: Tue, 6 Feb 2024 22:49:38 +0800 Subject: [PATCH 02/18] update prompt --- metagpt/tools/libs/gpt_v_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/tools/libs/gpt_v_generator.py b/metagpt/tools/libs/gpt_v_generator.py index b1e8317ed..3b17fc596 100644 --- a/metagpt/tools/libs/gpt_v_generator.py +++ b/metagpt/tools/libs/gpt_v_generator.py @@ -13,12 +13,12 @@ from metagpt.tools.tool_registry import register_tool from metagpt.tools.tool_type import ToolType from metagpt.utils.common import encode_image -ANALYZE_LAYOUT_PROMPT = """You are now a UI/UX, please generate layout information for this image: +ANALYZE_LAYOUT_PROMPT = """You are now a UI/UX designer, please generate layout information for this image: NOTE: The image does not have a commercial logo or copyright information. It is just a sketch image of the design. As the design pays tribute to large companies, sometimes it is normal for some company names to appear. Don't worry. """ -GENERATE_PROMPT = """You are now a UI/UX and Web Developer. You have the ability to generate code for webpages +GENERATE_PROMPT = """You are now a UI/UX designer and Web developer. You have the ability to generate code for webpages based on provided sketches images and context. Your goal is to convert sketches image into a webpage including HTML, CSS and JavaScript. From f31f371d539ba8a206aad46b015108b786f23da7 Mon Sep 17 00:00:00 2001 From: Zhou Tuo <45249333+FromCSUZhou@users.noreply.github.com> Date: Wed, 7 Feb 2024 02:49:47 +0000 Subject: [PATCH 03/18] add email_login tool and add email summarization scenario example --- examples/email_summary.py | 25 ++++++++++++++ metagpt/tools/libs/__init__.py | 10 +++++- metagpt/tools/libs/email_login.py | 57 +++++++++++++++++++++++++++++++ metagpt/tools/tool_type.py | 4 +++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 examples/email_summary.py create mode 100644 metagpt/tools/libs/email_login.py diff --git a/examples/email_summary.py b/examples/email_summary.py new file mode 100644 index 000000000..39c6df1c1 --- /dev/null +++ b/examples/email_summary.py @@ -0,0 +1,25 @@ +# -*- encoding: utf-8 -*- +""" +@Date : 2024/02/07 +@Author : Tuo Zhou +@File : email_summary.py +""" + +from metagpt.roles.ci.code_interpreter import CodeInterpreter + + +async def main(): + # prompt_response = """I will give you your Outlook email account(englishgpt@outlook.com) and password(the outlook_email_password item in the environment variable). You need to find the latest email in my inbox with the sender's suffix @qq.com and reply to him "Thank you! I have received your email~""""" + prompt_summary = """I will give you your Outlook email account(englishgpt@outlook.com) and password(outlook_email_password item in the environment variable). + Firstly, Please help me present the latest 5 senders and full letter contents. + Then, summarize each of the 5 emails into one sentence with Chinese(you can do this by yourself, don't need import other models to do this) and output them in a markdown format.""" + # ci_response = CodeInterpreter(goal=prompt_response, use_tools=True) + ci_summary = CodeInterpreter(goal=prompt_summary, use_tools=True) + + await ci_summary.run(prompt_summary) + + +if __name__ == "__main__": + import asyncio + + asyncio.run(main()) diff --git a/metagpt/tools/libs/__init__.py b/metagpt/tools/libs/__init__.py index c9767c1e5..91596fd3d 100644 --- a/metagpt/tools/libs/__init__.py +++ b/metagpt/tools/libs/__init__.py @@ -10,6 +10,14 @@ from metagpt.tools.libs import ( sd_engine, gpt_v_generator, web_scraping, + email_login, ) -_ = data_preprocess, feature_engineering, sd_engine, gpt_v_generator, web_scraping # Avoid pre-commit error +_ = ( + data_preprocess, + feature_engineering, + sd_engine, + gpt_v_generator, + web_scraping, + email_login, +) # Avoid pre-commit error diff --git a/metagpt/tools/libs/email_login.py b/metagpt/tools/libs/email_login.py new file mode 100644 index 000000000..946e294eb --- /dev/null +++ b/metagpt/tools/libs/email_login.py @@ -0,0 +1,57 @@ +from imap_tools import MailBox + +from metagpt.tools.tool_registry import register_tool +from metagpt.tools.tool_type import ToolType + + +@register_tool(tool_type=ToolType.EMAIL_LOGIN.type_name) +def email_login_imap(email_address, email_password): + """ + Use imap_tools package to log in to your email (the email that supports IMAP protocol) to verify and return the account object. + + Args: + email_address (str): Email address that needs to be logged in and linked. + email_password (str): Password for the email address that needs to be logged in and linked. + + Returns: + object: The imap_tools's MailBox object returned after successfully connecting to the mailbox through imap_tools, including various information about this account (email, etc.), or None if login fails. + """ + + # Define a dictionary mapping email domains to their IMAP server addresses + imap_servers = { + "outlook.com": "imap-mail.outlook.com", # Outlook + "163.com": "imap.163.com", # 163 Mail + "qq.com": "imap.qq.com", # QQ Mail + "gmail.com": "imap.gmail.com", # Gmail + "yahoo.com": "imap.mail.yahoo.com", # Yahoo Mail + "icloud.com": "imap.mail.me.com", # iCloud Mail + "hotmail.com": "imap-mail.outlook.com", # Hotmail (同 Outlook) + "live.com": "imap-mail.outlook.com", # Live (同 Outlook) + "sina.com": "imap.sina.com", # Sina Mail + "sohu.com": "imap.sohu.com", # Sohu Mail + "yahoo.co.jp": "imap.mail.yahoo.co.jp", # Yahoo Mail Japan + "yandex.com": "imap.yandex.com", # Yandex Mail + "mail.ru": "imap.mail.ru", # Mail.ru + "aol.com": "imap.aol.com", # AOL Mail + "gmx.com": "imap.gmx.com", # GMX Mail + "zoho.com": "imap.zoho.com", # Zoho Mail + } + + # Extract the domain from the email address + domain = email_address.split("@")[-1] + + # Determine the correct IMAP server + imap_server = imap_servers.get(domain) + + if not imap_server: + print(f"IMAP server for {domain} not found.") + return None + + # Attempt to log in to the email account + try: + mailbox = MailBox(imap_server).login(email_address, email_password) + print("Login successful") + return mailbox + except Exception as e: + print(f"Login failed: {e}") + return None diff --git a/metagpt/tools/tool_type.py b/metagpt/tools/tool_type.py index 6fa971c56..5a0c66a03 100644 --- a/metagpt/tools/tool_type.py +++ b/metagpt/tools/tool_type.py @@ -17,6 +17,10 @@ class ToolType(Enum): desc="Only for changing value inplace.", usage_prompt=DATA_PREPROCESS_PROMPT, ) + EMAIL_LOGIN = ToolTypeDef( + name="email_login", + desc="For logging to an email.", + ) FEATURE_ENGINEERING = ToolTypeDef( name="feature_engineering", desc="Only for creating new columns for input data.", From f51df40ebb4cd5af6263c613e2ffa898a3ae1ccd Mon Sep 17 00:00:00 2001 From: lidanyang Date: Wed, 7 Feb 2024 12:02:46 +0800 Subject: [PATCH 04/18] add eda prompt --- metagpt/prompts/tool_types.py | 7 +++++++ metagpt/tools/tool_type.py | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/metagpt/prompts/tool_types.py b/metagpt/prompts/tool_types.py index f27fbea99..8728d22f3 100644 --- a/metagpt/prompts/tool_types.py +++ b/metagpt/prompts/tool_types.py @@ -1,3 +1,10 @@ +# Prompt for using tools of "eda" type +EDA_PROMPT = """ +The current task is about exploratory data analysis, please note the following: +- Distinguish column types with `select_dtypes` for tailored analysis and visualization, such as correlation. +- Remember to `import numpy as np` before using Numpy functions. +""" + # Prompt for using tools of "data_preprocess" type DATA_PREPROCESS_PROMPT = """ The current task is about data preprocessing, please note the following: diff --git a/metagpt/tools/tool_type.py b/metagpt/tools/tool_type.py index 6fa971c56..7f3f132a6 100644 --- a/metagpt/tools/tool_type.py +++ b/metagpt/tools/tool_type.py @@ -1,6 +1,7 @@ from enum import Enum from metagpt.prompts.tool_types import ( + EDA_PROMPT, DATA_PREPROCESS_PROMPT, FEATURE_ENGINEERING_PROMPT, IMAGE2WEBPAGE_PROMPT, @@ -11,7 +12,11 @@ from metagpt.tools.tool_data_type import ToolTypeDef class ToolType(Enum): - EDA = ToolTypeDef(name="eda", desc="For performing exploratory data analysis") + EDA = ToolTypeDef( + name="eda", + desc="For performing exploratory data analysis", + usage_prompt=EDA_PROMPT, + ) DATA_PREPROCESS = ToolTypeDef( name="data_preprocess", desc="Only for changing value inplace.", From 18a17bede099a1f188c0f1e55b0665bc65aec43c Mon Sep 17 00:00:00 2001 From: yzlin Date: Wed, 7 Feb 2024 13:00:31 +0800 Subject: [PATCH 05/18] create ci example folder, mv existing and create new --- examples/{ => ci}/crawl_webpage.py | 0 examples/ci/data_analysis.py | 14 ++++++++++++++ examples/{ => ci}/imitate_webpage.py | 2 +- examples/ci/rm_image_background.py | 15 +++++++++++++++ examples/{ => ci}/sd_tool_usage.py | 0 examples/ci/solve_math_problems.py | 17 +++++++++++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) rename examples/{ => ci}/crawl_webpage.py (100%) create mode 100644 examples/ci/data_analysis.py rename examples/{ => ci}/imitate_webpage.py (92%) create mode 100644 examples/ci/rm_image_background.py rename examples/{ => ci}/sd_tool_usage.py (100%) create mode 100644 examples/ci/solve_math_problems.py diff --git a/examples/crawl_webpage.py b/examples/ci/crawl_webpage.py similarity index 100% rename from examples/crawl_webpage.py rename to examples/ci/crawl_webpage.py diff --git a/examples/ci/data_analysis.py b/examples/ci/data_analysis.py new file mode 100644 index 000000000..8dc4340d4 --- /dev/null +++ b/examples/ci/data_analysis.py @@ -0,0 +1,14 @@ +import asyncio + +from metagpt.roles.ci.code_interpreter import CodeInterpreter + + +async def main(requirement: str = ""): + code_interpreter = CodeInterpreter(use_tools=False) + await code_interpreter.run(requirement) + + +if __name__ == "__main__": + requirement = "Run data analysis on sklearn Iris dataset, include a plot" + + asyncio.run(main(requirement)) diff --git a/examples/imitate_webpage.py b/examples/ci/imitate_webpage.py similarity index 92% rename from examples/imitate_webpage.py rename to examples/ci/imitate_webpage.py index 5075e1e39..6a83d3a33 100644 --- a/examples/imitate_webpage.py +++ b/examples/ci/imitate_webpage.py @@ -15,7 +15,7 @@ Firstly, utilize Selenium and WebDriver for rendering. Secondly, convert image to a webpage including HTML, CSS and JS in one go. Finally, save webpage in a text file. Note: All required dependencies and environments have been fully installed and configured.""" - ci = CodeInterpreter(goal=prompt, use_tools=True) + ci = CodeInterpreter(use_tools=True) await ci.run(prompt) diff --git a/examples/ci/rm_image_background.py b/examples/ci/rm_image_background.py new file mode 100644 index 000000000..83dbbdc77 --- /dev/null +++ b/examples/ci/rm_image_background.py @@ -0,0 +1,15 @@ +import asyncio + +from metagpt.roles.ci.code_interpreter import CodeInterpreter + + +async def main(requirement: str = ""): + code_interpreter = CodeInterpreter(use_tools=False) + await code_interpreter.run(requirement) + + +if __name__ == "__main__": + image_path = "/your/path/to/the/image.jpeg" + save_path = "/your/intended/save/path/for/image_rm_bg.png" + requirement = f"This is a image, you need to use python toolkit rembg to remove the background of the image and save the result. image path:{image_path}; save path:{save_path}." + asyncio.run(main(requirement)) diff --git a/examples/sd_tool_usage.py b/examples/ci/sd_tool_usage.py similarity index 100% rename from examples/sd_tool_usage.py rename to examples/ci/sd_tool_usage.py diff --git a/examples/ci/solve_math_problems.py b/examples/ci/solve_math_problems.py new file mode 100644 index 000000000..5bf06b9d8 --- /dev/null +++ b/examples/ci/solve_math_problems.py @@ -0,0 +1,17 @@ +import asyncio + +from metagpt.roles.ci.code_interpreter import CodeInterpreter + + +async def main(requirement: str = ""): + code_interpreter = CodeInterpreter(use_tools=False, goal=requirement) + await code_interpreter.run(requirement) + + +if __name__ == "__main__": + problem = "At a school, all 60 students play on at least one of three teams: Basketball, Soccer, and Mathletics. 8 students play all three sports, half the students play basketball, and the ratio of the size of the math team to the size of the basketball team to the size of the soccer team is $4:3:2$. How many students at the school play on exactly two teams?" + requirement = ( + f"This is a math problem:{problem}. You can analyze and solve it step by step or use Python code to solve it." + ) + + asyncio.run(main(requirement)) From 9ed352b2cedf74a44202c1711dadbe05a14e86ae Mon Sep 17 00:00:00 2001 From: lidanyang Date: Wed, 7 Feb 2024 14:56:19 +0800 Subject: [PATCH 06/18] add example for ml_engineer_with_tools --- examples/ml_engineer_with_tools.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 examples/ml_engineer_with_tools.py diff --git a/examples/ml_engineer_with_tools.py b/examples/ml_engineer_with_tools.py new file mode 100644 index 000000000..1c90f2946 --- /dev/null +++ b/examples/ml_engineer_with_tools.py @@ -0,0 +1,16 @@ +import asyncio + +from metagpt.roles.ci.ml_engineer import MLEngineer + + +async def main(requirement: str, auto_run: bool = True, use_tools: bool = True): + role = MLEngineer(goal=requirement, auto_run=auto_run, use_tools=use_tools) + await role.run(requirement) + + +if __name__ == "__main__": + data_path = "your_path_to_icr/icr-identify-age-related-conditions" # 替换 'your_path_to_icr' 为实际数据存放的路径 + train_path = f"{data_path}/your_train_data.csv" # 替换 'your_train_data.csv' 为你的训练数据文件名 + eval_path = f"{data_path}/your_eval_data.csv" # 替换 'your_eval_data.csv' 为你的评估数据文件名 + requirement = f"This is a medical dataset with over fifty anonymized health characteristics linked to three age-related conditions. Your goal is to predict whether a subject has or has not been diagnosed with one of these conditions.The target column is Class. Perform data analysis, data preprocessing, feature engineering, and modeling to predict the target. Report f1 score on the eval data. Train data path: {train_path}, eval data path:{eval_path}." + asyncio.run(main(requirement)) From d29ab799bf7de735a76754e85d662cbbd8e5525e Mon Sep 17 00:00:00 2001 From: yzlin Date: Wed, 7 Feb 2024 15:55:20 +0800 Subject: [PATCH 07/18] change math problems --- examples/ci/solve_math_problems.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/ci/solve_math_problems.py b/examples/ci/solve_math_problems.py index 5bf06b9d8..8c660975b 100644 --- a/examples/ci/solve_math_problems.py +++ b/examples/ci/solve_math_problems.py @@ -4,14 +4,10 @@ from metagpt.roles.ci.code_interpreter import CodeInterpreter async def main(requirement: str = ""): - code_interpreter = CodeInterpreter(use_tools=False, goal=requirement) + code_interpreter = CodeInterpreter(use_tools=False) await code_interpreter.run(requirement) if __name__ == "__main__": - problem = "At a school, all 60 students play on at least one of three teams: Basketball, Soccer, and Mathletics. 8 students play all three sports, half the students play basketball, and the ratio of the size of the math team to the size of the basketball team to the size of the soccer team is $4:3:2$. How many students at the school play on exactly two teams?" - requirement = ( - f"This is a math problem:{problem}. You can analyze and solve it step by step or use Python code to solve it." - ) - + requirement = "Solve this math problem: The greatest common divisor of positive integers m and n is 6. The least common multiple of m and n is 126. What is the least possible value of m + n?" asyncio.run(main(requirement)) From 4a991b4cd4eb0bdfb415b8ffb8bb02c3d33f10e2 Mon Sep 17 00:00:00 2001 From: Zhou Tuo <45249333+FromCSUZhou@users.noreply.github.com> Date: Wed, 7 Feb 2024 07:59:54 +0000 Subject: [PATCH 08/18] add email_login unit test --- examples/email_summary.py | 11 +++-- metagpt/tools/libs/email_login.py | 45 +++++++++--------- tests/metagpt/tools/libs/test_email_login.py | 50 ++++++++++++++++++++ 3 files changed, 79 insertions(+), 27 deletions(-) create mode 100644 tests/metagpt/tools/libs/test_email_login.py diff --git a/examples/email_summary.py b/examples/email_summary.py index 39c6df1c1..0862991da 100644 --- a/examples/email_summary.py +++ b/examples/email_summary.py @@ -9,14 +9,15 @@ from metagpt.roles.ci.code_interpreter import CodeInterpreter async def main(): - # prompt_response = """I will give you your Outlook email account(englishgpt@outlook.com) and password(the outlook_email_password item in the environment variable). You need to find the latest email in my inbox with the sender's suffix @qq.com and reply to him "Thank you! I have received your email~""""" - prompt_summary = """I will give you your Outlook email account(englishgpt@outlook.com) and password(outlook_email_password item in the environment variable). + # For email response prompt + # prompt = """I will give you your Outlook email account(englishgpt@outlook.com) and password(the outlook_email_password item in the environment variable). You need to find the latest email in my inbox with the sender's suffix @qq.com and reply to him "Thank you! I have received your email~""""" + prompt = """I will give you your Outlook email account(englishgpt@outlook.com) and password(outlook_email_password item in the environment variable). Firstly, Please help me present the latest 5 senders and full letter contents. Then, summarize each of the 5 emails into one sentence with Chinese(you can do this by yourself, don't need import other models to do this) and output them in a markdown format.""" - # ci_response = CodeInterpreter(goal=prompt_response, use_tools=True) - ci_summary = CodeInterpreter(goal=prompt_summary, use_tools=True) - await ci_summary.run(prompt_summary) + ci = CodeInterpreter(use_tools=True) + + await ci.run(prompt) if __name__ == "__main__": diff --git a/metagpt/tools/libs/email_login.py b/metagpt/tools/libs/email_login.py index 946e294eb..77772e15a 100644 --- a/metagpt/tools/libs/email_login.py +++ b/metagpt/tools/libs/email_login.py @@ -1,8 +1,29 @@ from imap_tools import MailBox +from metagpt.logs import logger from metagpt.tools.tool_registry import register_tool from metagpt.tools.tool_type import ToolType +# Define a dictionary mapping email domains to their IMAP server addresses +IMAP_SERVERS = { + "outlook.com": "imap-mail.outlook.com", # Outlook + "163.com": "imap.163.com", # 163 Mail + "qq.com": "imap.qq.com", # QQ Mail + "gmail.com": "imap.gmail.com", # Gmail + "yahoo.com": "imap.mail.yahoo.com", # Yahoo Mail + "icloud.com": "imap.mail.me.com", # iCloud Mail + "hotmail.com": "imap-mail.outlook.com", # Hotmail (同 Outlook) + "live.com": "imap-mail.outlook.com", # Live (同 Outlook) + "sina.com": "imap.sina.com", # Sina Mail + "sohu.com": "imap.sohu.com", # Sohu Mail + "yahoo.co.jp": "imap.mail.yahoo.co.jp", # Yahoo Mail Japan + "yandex.com": "imap.yandex.com", # Yandex Mail + "mail.ru": "imap.mail.ru", # Mail.ru + "aol.com": "imap.aol.com", # AOL Mail + "gmx.com": "imap.gmx.com", # GMX Mail + "zoho.com": "imap.zoho.com", # Zoho Mail +} + @register_tool(tool_type=ToolType.EMAIL_LOGIN.type_name) def email_login_imap(email_address, email_password): @@ -17,34 +38,14 @@ def email_login_imap(email_address, email_password): object: The imap_tools's MailBox object returned after successfully connecting to the mailbox through imap_tools, including various information about this account (email, etc.), or None if login fails. """ - # Define a dictionary mapping email domains to their IMAP server addresses - imap_servers = { - "outlook.com": "imap-mail.outlook.com", # Outlook - "163.com": "imap.163.com", # 163 Mail - "qq.com": "imap.qq.com", # QQ Mail - "gmail.com": "imap.gmail.com", # Gmail - "yahoo.com": "imap.mail.yahoo.com", # Yahoo Mail - "icloud.com": "imap.mail.me.com", # iCloud Mail - "hotmail.com": "imap-mail.outlook.com", # Hotmail (同 Outlook) - "live.com": "imap-mail.outlook.com", # Live (同 Outlook) - "sina.com": "imap.sina.com", # Sina Mail - "sohu.com": "imap.sohu.com", # Sohu Mail - "yahoo.co.jp": "imap.mail.yahoo.co.jp", # Yahoo Mail Japan - "yandex.com": "imap.yandex.com", # Yandex Mail - "mail.ru": "imap.mail.ru", # Mail.ru - "aol.com": "imap.aol.com", # AOL Mail - "gmx.com": "imap.gmx.com", # GMX Mail - "zoho.com": "imap.zoho.com", # Zoho Mail - } - # Extract the domain from the email address domain = email_address.split("@")[-1] # Determine the correct IMAP server - imap_server = imap_servers.get(domain) + imap_server = IMAP_SERVERS.get(domain) if not imap_server: - print(f"IMAP server for {domain} not found.") + logger.error(f"IMAP server for {domain} not found.") return None # Attempt to log in to the email account diff --git a/tests/metagpt/tools/libs/test_email_login.py b/tests/metagpt/tools/libs/test_email_login.py new file mode 100644 index 000000000..fd8d41506 --- /dev/null +++ b/tests/metagpt/tools/libs/test_email_login.py @@ -0,0 +1,50 @@ +import os +from unittest.mock import Mock, patch + +import pytest + +from metagpt.tools.libs.email_login import email_login_imap + +# Configuration for the test IMAP servers +TEST_IMAP_SERVERS = {"outlook.com": "imap-mail.outlook.com"} + +# Setup correct and incorrect email information +correct_email_address = "englishgpt@outlook.com" +correct_email_password = os.environ.get("outlook_email_password") +incorrect_email_address = "test@unknown.com" +incorrect_email_password = "incorrect_password" + + +@pytest.fixture +def imap_server_setup(): + # Use patch to mock the behavior of MailBox from the correct module path + with patch("metagpt.tools.libs.email_login.MailBox") as mock_mailbox: + # Setup for successful login + mock_mail_instance = Mock() + mock_mail_instance.login.return_value = mock_mail_instance + mock_mailbox.return_value = mock_mail_instance + yield mock_mail_instance + + +def test_email_login_imap_success(imap_server_setup): + # Mock successful login + mailbox = email_login_imap(correct_email_address, correct_email_password) + assert mailbox is not None + # Correctly assert that the login method of the MailBox mock was called with the correct arguments + imap_server_setup.login.assert_called_with(correct_email_address, correct_email_password) + + +def test_email_login_imap_failure_due_to_incorrect_server(imap_server_setup): + # Attempt to login with an incorrect server + mailbox = email_login_imap(incorrect_email_address, incorrect_email_password) + assert mailbox is None + + +def test_email_login_imap_failure_due_to_wrong_credentials(imap_server_setup): + # Configure mock to throw an exception to simulate login failure due to incorrect credentials + imap_server_setup.login.side_effect = Exception("Login failed") + # Attempt to login which should simulate a failure + mailbox = email_login_imap(correct_email_address, incorrect_email_password) + assert mailbox is None + # Verify that the login method was called with the expected arguments + imap_server_setup.login.assert_called_with(correct_email_address, incorrect_email_password) From 3369c9e53671313d73ae57a17c010e3de786fb41 Mon Sep 17 00:00:00 2001 From: Zhou Tuo <45249333+FromCSUZhou@users.noreply.github.com> Date: Wed, 7 Feb 2024 08:59:32 +0000 Subject: [PATCH 09/18] modify by comment --- examples/email_summary.py | 9 +++++---- metagpt/tools/libs/email_login.py | 4 ++-- tests/metagpt/tools/libs/test_email_login.py | 16 +++++++--------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/examples/email_summary.py b/examples/email_summary.py index 0862991da..dd8dd8c8e 100644 --- a/examples/email_summary.py +++ b/examples/email_summary.py @@ -10,10 +10,11 @@ from metagpt.roles.ci.code_interpreter import CodeInterpreter async def main(): # For email response prompt - # prompt = """I will give you your Outlook email account(englishgpt@outlook.com) and password(the outlook_email_password item in the environment variable). You need to find the latest email in my inbox with the sender's suffix @qq.com and reply to him "Thank you! I have received your email~""""" - prompt = """I will give you your Outlook email account(englishgpt@outlook.com) and password(outlook_email_password item in the environment variable). - Firstly, Please help me present the latest 5 senders and full letter contents. - Then, summarize each of the 5 emails into one sentence with Chinese(you can do this by yourself, don't need import other models to do this) and output them in a markdown format.""" + email_account = "your_email_account" + # prompt = f"""I will give you your Outlook email account({email_account}) and password(email_password item in the environment variable). You need to find the latest email in my inbox with the sender's suffix @qq.com and reply to him "Thank you! I have received your email~""""" + prompt = f"""I will give you your Outlook email account({email_account}) and password(email_password item in the environment variable). + Firstly, Please help me fetch the latest 5 senders and full letter contents. + Then, summarize each of the 5 emails into one sentence(you can do this by yourself, no need import other models to do this) and output them in a markdown format.""" ci = CodeInterpreter(use_tools=True) diff --git a/metagpt/tools/libs/email_login.py b/metagpt/tools/libs/email_login.py index 77772e15a..8fd77274c 100644 --- a/metagpt/tools/libs/email_login.py +++ b/metagpt/tools/libs/email_login.py @@ -51,8 +51,8 @@ def email_login_imap(email_address, email_password): # Attempt to log in to the email account try: mailbox = MailBox(imap_server).login(email_address, email_password) - print("Login successful") + logger.info("Login successful") return mailbox except Exception as e: - print(f"Login failed: {e}") + logger.error(f"Login failed: {e}") return None diff --git a/tests/metagpt/tools/libs/test_email_login.py b/tests/metagpt/tools/libs/test_email_login.py index fd8d41506..c18d15c7d 100644 --- a/tests/metagpt/tools/libs/test_email_login.py +++ b/tests/metagpt/tools/libs/test_email_login.py @@ -1,5 +1,4 @@ import os -from unittest.mock import Mock, patch import pytest @@ -16,14 +15,13 @@ incorrect_email_password = "incorrect_password" @pytest.fixture -def imap_server_setup(): - # Use patch to mock the behavior of MailBox from the correct module path - with patch("metagpt.tools.libs.email_login.MailBox") as mock_mailbox: - # Setup for successful login - mock_mail_instance = Mock() - mock_mail_instance.login.return_value = mock_mail_instance - mock_mailbox.return_value = mock_mail_instance - yield mock_mail_instance +def imap_server_setup(mocker): + # Use the mocker fixture to mock the MailBox class + mock_mailbox = mocker.patch("metagpt.tools.libs.email_login.MailBox") + mock_mail_instance = mocker.Mock() + mock_mail_instance.login.return_value = mock_mail_instance + mock_mailbox.return_value = mock_mail_instance + return mock_mail_instance def test_email_login_imap_success(imap_server_setup): From eb1e1b9ef22fe58720dfa71f01b812eecaf32b87 Mon Sep 17 00:00:00 2001 From: yzlin Date: Wed, 7 Feb 2024 21:54:40 +0800 Subject: [PATCH 10/18] mv examples --- examples/ci/{data_analysis.py => data_visualization.py} | 0 examples/{ => ci}/email_summary.py | 0 examples/ci/machine_learning.py | 0 examples/{ => ci}/ml_engineer_with_tools.py | 6 +++--- 4 files changed, 3 insertions(+), 3 deletions(-) rename examples/ci/{data_analysis.py => data_visualization.py} (100%) rename examples/{ => ci}/email_summary.py (100%) create mode 100644 examples/ci/machine_learning.py rename examples/{ => ci}/ml_engineer_with_tools.py (74%) diff --git a/examples/ci/data_analysis.py b/examples/ci/data_visualization.py similarity index 100% rename from examples/ci/data_analysis.py rename to examples/ci/data_visualization.py diff --git a/examples/email_summary.py b/examples/ci/email_summary.py similarity index 100% rename from examples/email_summary.py rename to examples/ci/email_summary.py diff --git a/examples/ci/machine_learning.py b/examples/ci/machine_learning.py new file mode 100644 index 000000000..e69de29bb diff --git a/examples/ml_engineer_with_tools.py b/examples/ci/ml_engineer_with_tools.py similarity index 74% rename from examples/ml_engineer_with_tools.py rename to examples/ci/ml_engineer_with_tools.py index 1c90f2946..1c73a1dd0 100644 --- a/examples/ml_engineer_with_tools.py +++ b/examples/ci/ml_engineer_with_tools.py @@ -9,8 +9,8 @@ async def main(requirement: str, auto_run: bool = True, use_tools: bool = True): if __name__ == "__main__": - data_path = "your_path_to_icr/icr-identify-age-related-conditions" # 替换 'your_path_to_icr' 为实际数据存放的路径 - train_path = f"{data_path}/your_train_data.csv" # 替换 'your_train_data.csv' 为你的训练数据文件名 - eval_path = f"{data_path}/your_eval_data.csv" # 替换 'your_eval_data.csv' 为你的评估数据文件名 + data_path = "your_path_to_icr/icr-identify-age-related-conditions" + train_path = f"{data_path}/your_train_data.csv" + eval_path = f"{data_path}/your_eval_data.csv" requirement = f"This is a medical dataset with over fifty anonymized health characteristics linked to three age-related conditions. Your goal is to predict whether a subject has or has not been diagnosed with one of these conditions.The target column is Class. Perform data analysis, data preprocessing, feature engineering, and modeling to predict the target. Report f1 score on the eval data. Train data path: {train_path}, eval data path:{eval_path}." asyncio.run(main(requirement)) From d2f10ea90ca1ffa8e5b571017f2ab548db8d1897 Mon Sep 17 00:00:00 2001 From: yzlin Date: Thu, 8 Feb 2024 00:05:00 +0800 Subject: [PATCH 11/18] add ocr example, modify email example --- examples/ci/email_summary.py | 14 ++++-- examples/ci/ocr_receipt.py | 19 ++++++++ metagpt/tools/libs/email_login.py | 14 ++---- requirements.txt | 1 + tests/metagpt/tools/libs/test_email_login.py | 49 ++------------------ 5 files changed, 37 insertions(+), 60 deletions(-) create mode 100644 examples/ci/ocr_receipt.py diff --git a/examples/ci/email_summary.py b/examples/ci/email_summary.py index dd8dd8c8e..d6fa283ca 100644 --- a/examples/ci/email_summary.py +++ b/examples/ci/email_summary.py @@ -4,17 +4,23 @@ @Author : Tuo Zhou @File : email_summary.py """ +import os from metagpt.roles.ci.code_interpreter import CodeInterpreter async def main(): - # For email response prompt email_account = "your_email_account" - # prompt = f"""I will give you your Outlook email account({email_account}) and password(email_password item in the environment variable). You need to find the latest email in my inbox with the sender's suffix @qq.com and reply to him "Thank you! I have received your email~""""" - prompt = f"""I will give you your Outlook email account({email_account}) and password(email_password item in the environment variable). + # your password will stay only on your device and not go to LLM api + os.environ["email_password"] = "your_email_password" + + ### Prompt for automatic email reply, uncomment to try this too ### + # prompt = f"""I will give you your Outlook email account ({email_account}) and password (email_password item in the environment variable). You need to find the latest email in my inbox with the sender's suffix @gmail.com and reply "Thank you! I have received your email~""""" + + ### Prompt for automatic email summary ### + prompt = f"""I will give you your Outlook email account ({email_account}) and password (email_password item in the environment variable). Firstly, Please help me fetch the latest 5 senders and full letter contents. - Then, summarize each of the 5 emails into one sentence(you can do this by yourself, no need import other models to do this) and output them in a markdown format.""" + Then, summarize each of the 5 emails into one sentence (you can do this by yourself, no need to import other models to do this) and output them in a markdown format.""" ci = CodeInterpreter(use_tools=True) diff --git a/examples/ci/ocr_receipt.py b/examples/ci/ocr_receipt.py new file mode 100644 index 000000000..dc2db2d01 --- /dev/null +++ b/examples/ci/ocr_receipt.py @@ -0,0 +1,19 @@ +from metagpt.roles.ci.code_interpreter import CodeInterpreter + + +async def main(): + # Notice: pip install metagpt[ocr] before using this example + image_path = "image.jpg" + language = "English" + requirement = f"""This is a {language} invoice image. + Your goal is to perform OCR on images using PaddleOCR, then extract the total amount from ocr text results, and finally save as table. Image path: {image_path}. + NOTE: The environments for Paddle and PaddleOCR are all ready and has been fully installed.""" + ci = CodeInterpreter() + + await ci.run(requirement) + + +if __name__ == "__main__": + import asyncio + + asyncio.run(main()) diff --git a/metagpt/tools/libs/email_login.py b/metagpt/tools/libs/email_login.py index 8fd77274c..79734665e 100644 --- a/metagpt/tools/libs/email_login.py +++ b/metagpt/tools/libs/email_login.py @@ -1,6 +1,5 @@ from imap_tools import MailBox -from metagpt.logs import logger from metagpt.tools.tool_registry import register_tool from metagpt.tools.tool_type import ToolType @@ -44,15 +43,8 @@ def email_login_imap(email_address, email_password): # Determine the correct IMAP server imap_server = IMAP_SERVERS.get(domain) - if not imap_server: - logger.error(f"IMAP server for {domain} not found.") - return None + assert imap_server, f"IMAP server for {domain} not found." # Attempt to log in to the email account - try: - mailbox = MailBox(imap_server).login(email_address, email_password) - logger.info("Login successful") - return mailbox - except Exception as e: - logger.error(f"Login failed: {e}") - return None + mailbox = MailBox(imap_server).login(email_address, email_password) + return mailbox diff --git a/requirements.txt b/requirements.txt index 6cb25d52b..1357f2e5d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -67,3 +67,4 @@ google-generativeai==0.3.2 anytree ipywidgets==8.1.1 Pillow +imap_tools==1.5.0 # Used by metagpt/tools/libs/email_login.py diff --git a/tests/metagpt/tools/libs/test_email_login.py b/tests/metagpt/tools/libs/test_email_login.py index c18d15c7d..e98820f70 100644 --- a/tests/metagpt/tools/libs/test_email_login.py +++ b/tests/metagpt/tools/libs/test_email_login.py @@ -1,48 +1,7 @@ -import os - -import pytest - from metagpt.tools.libs.email_login import email_login_imap -# Configuration for the test IMAP servers -TEST_IMAP_SERVERS = {"outlook.com": "imap-mail.outlook.com"} -# Setup correct and incorrect email information -correct_email_address = "englishgpt@outlook.com" -correct_email_password = os.environ.get("outlook_email_password") -incorrect_email_address = "test@unknown.com" -incorrect_email_password = "incorrect_password" - - -@pytest.fixture -def imap_server_setup(mocker): - # Use the mocker fixture to mock the MailBox class - mock_mailbox = mocker.patch("metagpt.tools.libs.email_login.MailBox") - mock_mail_instance = mocker.Mock() - mock_mail_instance.login.return_value = mock_mail_instance - mock_mailbox.return_value = mock_mail_instance - return mock_mail_instance - - -def test_email_login_imap_success(imap_server_setup): - # Mock successful login - mailbox = email_login_imap(correct_email_address, correct_email_password) - assert mailbox is not None - # Correctly assert that the login method of the MailBox mock was called with the correct arguments - imap_server_setup.login.assert_called_with(correct_email_address, correct_email_password) - - -def test_email_login_imap_failure_due_to_incorrect_server(imap_server_setup): - # Attempt to login with an incorrect server - mailbox = email_login_imap(incorrect_email_address, incorrect_email_password) - assert mailbox is None - - -def test_email_login_imap_failure_due_to_wrong_credentials(imap_server_setup): - # Configure mock to throw an exception to simulate login failure due to incorrect credentials - imap_server_setup.login.side_effect = Exception("Login failed") - # Attempt to login which should simulate a failure - mailbox = email_login_imap(correct_email_address, incorrect_email_password) - assert mailbox is None - # Verify that the login method was called with the expected arguments - imap_server_setup.login.assert_called_with(correct_email_address, incorrect_email_password) +def test_email_login(mocker): + mock_mailbox = mocker.patch("metagpt.tools.libs.email_login.MailBox.login") + mock_mailbox.login.return_value = mocker.Mock() + email_login_imap("test@outlook.com", "test_password") From 8e6d722b8fe42e8e6504b24c053a55fefbe77c55 Mon Sep 17 00:00:00 2001 From: yzlin Date: Thu, 8 Feb 2024 00:17:31 +0800 Subject: [PATCH 12/18] format --- tests/metagpt/provider/test_openai.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py index b9db4dfb6..96c08a867 100644 --- a/tests/metagpt/provider/test_openai.py +++ b/tests/metagpt/provider/test_openai.py @@ -1,5 +1,3 @@ -import json - import pytest from openai.types.chat import ( ChatCompletion, From 0949f166ae7a2fb7fe145360f187c800bfb86c1c Mon Sep 17 00:00:00 2001 From: yzlin Date: Thu, 8 Feb 2024 00:26:59 +0800 Subject: [PATCH 13/18] format --- metagpt/tools/tool_type.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/tools/tool_type.py b/metagpt/tools/tool_type.py index bee9a98eb..e9c9a3482 100644 --- a/metagpt/tools/tool_type.py +++ b/metagpt/tools/tool_type.py @@ -1,8 +1,8 @@ from enum import Enum from metagpt.prompts.tool_types import ( - EDA_PROMPT, DATA_PREPROCESS_PROMPT, + EDA_PROMPT, FEATURE_ENGINEERING_PROMPT, IMAGE2WEBPAGE_PROMPT, MODEL_EVALUATE_PROMPT, From b0b5c3d1c523295f10ad06e2edfe86f9cdd4c68a Mon Sep 17 00:00:00 2001 From: yzlin Date: Thu, 8 Feb 2024 11:19:36 +0800 Subject: [PATCH 14/18] update cache --- tests/data/rsp_cache.json | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/tests/data/rsp_cache.json b/tests/data/rsp_cache.json index 75fc9ceb2..26a3bd494 100644 --- a/tests/data/rsp_cache.json +++ b/tests/data/rsp_cache.json @@ -355,7 +355,7 @@ "code": "print(data.head())" }, "\n # Context:\n user: run analysis on sklearn iris dataset\n # Task:\n Based on the context, write a plan or modify an existing plan of what you should do to achieve the goal. A plan consists of one to 5 tasks.\n If you are modifying an existing plan, carefully follow the instruction, don't make unnecessary changes. Give the whole plan unless instructed to modify only one task of the plan.\n If you encounter errors on the current task, revise and output the current single task only.\n Output a list of jsons following the format:\n ```json\n [\n {\n \"task_id\": str = \"unique identifier for a task in plan, can be an ordinal\",\n \"dependent_task_ids\": list[str] = \"ids of tasks prerequisite to this task\",\n \"instruction\": \"what you should do in this task, one short phrase or sentence\",\n },\n ...\n ]\n ```\n ": "```json\n[\n {\n \"task_id\": \"1\",\n \"dependent_task_ids\": [],\n \"instruction\": \"Import the Iris dataset from sklearn.datasets\"\n },\n {\n \"task_id\": \"2\",\n \"dependent_task_ids\": [\"1\"],\n \"instruction\": \"Perform exploratory data analysis to understand the dataset\"\n },\n {\n \"task_id\": \"3\",\n \"dependent_task_ids\": [\"2\"],\n \"instruction\": \"Preprocess the data to prepare it for modeling\"\n },\n {\n \"task_id\": \"4\",\n \"dependent_task_ids\": [\"3\"],\n \"instruction\": \"Split the dataset into training and testing sets\"\n },\n {\n \"task_id\": \"5\",\n \"dependent_task_ids\": [\"4\"],\n \"instruction\": \"Train a classifier using the training set and evaluate it using the test set\"\n }\n]\n```", - "[{\"role\": \"user\", \"content\": \"\\nPlease assign a task type to each task in the list below from the given categories:\\nTask 1: Import the Iris dataset from sklearn.datasets\\nTask 2: Perform exploratory data analysis to understand the dataset\\nTask 3: Preprocess the data to prepare it for modeling\\nTask 4: Split the dataset into training and testing sets\\nTask 5: Train a classifier using the training set and evaluate it using the test set\\n\\n## All Task Type:\\n- **eda**: For performing exploratory data analysis\\n- **data_preprocess**: Only for changing value inplace.\\n- **feature_engineering**: Only for creating new columns for input data.\\n- **model_train**: Only for training model.\\n- **model_evaluate**: Only for evaluating model.\\n- **stable_diffusion**: Related to text2image, image2image using stable diffusion model.\\n- **image2webpage**: For converting image into webpage code.\\n- **web_scraping**: For scraping data from web pages.\\n- **other**: Any tools not in the defined categories\\n\"}]": { + "[{\"role\": \"user\", \"content\": \"\\nPlease assign a task type to each task in the list below from the given categories:\\nTask 1: Import the Iris dataset from sklearn.datasets\\nTask 2: Perform exploratory data analysis to understand the dataset\\nTask 3: Preprocess the data to prepare it for modeling\\nTask 4: Split the dataset into training and testing sets\\nTask 5: Train a classifier using the training set and evaluate it using the test set\\n\\n## All Task Type:\\n- **eda**: For performing exploratory data analysis\\n- **data_preprocess**: Only for changing value inplace.\\n- **email_login**: For logging to an email.\\n- **feature_engineering**: Only for creating new columns for input data.\\n- **model_train**: Only for training model.\\n- **model_evaluate**: Only for evaluating model.\\n- **stable_diffusion**: Related to text2image, image2image using stable diffusion model.\\n- **image2webpage**: For converting image into webpage code.\\n- **web_scraping**: For scraping data from web pages.\\n- **other**: Any tools not in the defined categories\\n\"}]": { "task_type": [ "other", "eda", @@ -366,34 +366,30 @@ ] }, "\n # Context:\n user: \n## User Requirement\nRun data analysis on sklearn Iris dataset, include a plot\n## Context\n\n## Current Plan\n[]\n## Current Task\n{}\n\n # Task:\n Based on the context, write a plan or modify an existing plan of what you should do to achieve the goal. A plan consists of one to 3 tasks.\n If you are modifying an existing plan, carefully follow the instruction, don't make unnecessary changes. Give the whole plan unless instructed to modify only one task of the plan.\n If you encounter errors on the current task, revise and output the current single task only.\n Output a list of jsons following the format:\n ```json\n [\n {\n \"task_id\": str = \"unique identifier for a task in plan, can be an ordinal\",\n \"dependent_task_ids\": list[str] = \"ids of tasks prerequisite to this task\",\n \"instruction\": \"what you should do in this task, one short phrase or sentence\",\n },\n ...\n ]\n ```\n ": "```json\n[\n {\n \"task_id\": \"1\",\n \"dependent_task_ids\": [],\n \"instruction\": \"Load the sklearn Iris dataset.\"\n },\n {\n \"task_id\": \"2\",\n \"dependent_task_ids\": [\"1\"],\n \"instruction\": \"Perform exploratory data analysis on the Iris dataset.\"\n },\n {\n \"task_id\": \"3\",\n \"dependent_task_ids\": [\"2\"],\n \"instruction\": \"Create a plot visualizing the Iris dataset.\"\n }\n]\n```", - "[{\"role\": \"user\", \"content\": \"\\nPlease assign a task type to each task in the list below from the given categories:\\nTask 1: Load the sklearn Iris dataset.\\nTask 2: Perform exploratory data analysis on the Iris dataset.\\nTask 3: Create a plot visualizing the Iris dataset.\\n\\n## All Task Type:\\n- **eda**: For performing exploratory data analysis\\n- **data_preprocess**: Only for changing value inplace.\\n- **feature_engineering**: Only for creating new columns for input data.\\n- **model_train**: Only for training model.\\n- **model_evaluate**: Only for evaluating model.\\n- **stable_diffusion**: Related to text2image, image2image using stable diffusion model.\\n- **image2webpage**: For converting image into webpage code.\\n- **web_scraping**: For scraping data from web pages.\\n- **other**: Any tools not in the defined categories\\n\"}]": { + "[{\"role\": \"user\", \"content\": \"\\nPlease assign a task type to each task in the list below from the given categories:\\nTask 1: Load the sklearn Iris dataset.\\nTask 2: Perform exploratory data analysis on the Iris dataset.\\nTask 3: Create a plot visualizing the Iris dataset.\\n\\n## All Task Type:\\n- **eda**: For performing exploratory data analysis\\n- **data_preprocess**: Only for changing value inplace.\\n- **email_login**: For logging to an email.\\n- **feature_engineering**: Only for creating new columns for input data.\\n- **model_train**: Only for training model.\\n- **model_evaluate**: Only for evaluating model.\\n- **stable_diffusion**: Related to text2image, image2image using stable diffusion model.\\n- **image2webpage**: For converting image into webpage code.\\n- **web_scraping**: For scraping data from web pages.\\n- **other**: Any tools not in the defined categories\\n\"}]": { "task_type": [ - "other", + "data_preprocess", "eda", "other" ] }, - "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"1\\\",\\\"dependent_task_ids\\\":[],\\\"instruction\\\":\\\"Load the sklearn Iris dataset.\\\",\\\"task_type\\\":\\\"other\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { - "code": "from sklearn.datasets import load_iris\niris_data = load_iris()" + "[{\"role\": \"user\", \"content\": \"\\n## User Requirement:\\nLoad the sklearn Iris dataset.\\n\\n## Task\\nRecommend up to five tools from 'Available Tools' that can help solve the 'User Requirement'. \\n\\n## Available Tools:\\n{'FillMissingValue': 'Completing missing values with simple strategies.', 'MinMaxScale': 'Transform features by scaling each feature to a range, which is (0, 1).', 'StandardScale': 'Standardize features by removing the mean and scaling to unit variance.', 'MaxAbsScale': 'Scale each feature by its maximum absolute value.', 'RobustScale': 'Apply the RobustScaler to scale features using statistics that are robust to outliers.', 'OrdinalEncode': 'Encode categorical features as ordinal integers.', 'OneHotEncode': 'Apply one-hot encoding to specified categorical columns, the original columns will be dropped.', 'LabelEncode': 'Apply label encoding to specified categorical columns in-place.'}\\n\\n## Tool Selection and Instructions:\\n- Select tools most relevant to completing the 'User Requirement'.\\n- If you believe that no tools are suitable, indicate with an empty list.\\n- Only list the names of the tools, not the full schema of each tool.\\n- Ensure selected tools are listed in 'Available Tools'.\\n\"}]": { + "recommend_tools": [] }, - "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"2\\\",\\\"dependent_task_ids\\\":[\\\"1\\\"],\\\"instruction\\\":\\\"Perform exploratory data analysis on the Iris dataset.\\\",\\\"task_type\\\":\\\"eda\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { - "code": "from sklearn import datasets\nimport pandas as pd\n\n# Load Iris dataset\niris = datasets.load_iris()\ndf_iris = pd.DataFrame(data=iris.data, columns=iris.feature_names)\ndf_iris['target'] = iris.target\n\n# Display basic information about the dataset\nprint(df_iris.info())\n\n# Display statistical summary of the dataset\nprint(df_iris.describe())\n\n# Display the first few rows of the dataset\nprint(df_iris.head())\n\n# Display the distribution of the target variable\ntarget_counts = df_iris['target'].value_counts()\nprint(target_counts)" + "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"data_preprocess\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"1\\\",\\\"dependent_task_ids\\\":[],\\\"instruction\\\":\\\"Load the sklearn Iris dataset.\\\",\\\"task_type\\\":\\\"data_preprocess\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\nThe current task is about data preprocessing, please note the following:\\n- Monitor data types per column, applying appropriate methods.\\n- Ensure operations are on existing dataset columns.\\n- Avoid writing processed data to files.\\n- Avoid any change to label column, such as standardization, etc.\\n- Prefer alternatives to one-hot encoding for categorical data.\\n- Only encode or scale necessary columns to allow for potential feature-specific engineering tasks (like time_extract, binning, extraction, etc.) later.\\n- Each step do data preprocessing to train, must do same for test separately at the same time.\\n\\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { + "code": "from sklearn.datasets import load_iris\niris_data = load_iris()\nX, y = iris_data.data, iris_data.target" }, - "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"3\\\",\\\"dependent_task_ids\\\":[\\\"2\\\"],\\\"instruction\\\":\\\"Create a plot visualizing the Iris dataset.\\\",\\\"task_type\\\":\\\"other\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { - "code": "from sklearn import datasets\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\n# Load Iris dataset\niris = datasets.load_iris()\niris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)\niris_df['target'] = iris.target\niris_df['target_name'] = iris_df['target'].apply(lambda x: iris.target_names[x])\n\n# Plotting\nfig, ax = plt.subplots(figsize=(12, 8))\nfor target, target_name in zip(iris.target_names, iris.target_names):\n subset = iris_df[iris_df['target_name'] == target_name]\n ax.scatter(subset[iris.feature_names[0]], subset[iris.feature_names[1]], label=target_name)\n\nax.set_xlabel(iris.feature_names[0])\nax.set_ylabel(iris.feature_names[1])\nax.legend()\nplt.show()" + "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"data_preprocess\\\",\\n \\\"code\\\": \\\"from sklearn.datasets import load_iris\\\\niris_data = load_iris()\\\\nX, y = iris_data.data, iris_data.target\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"2\\\",\\\"dependent_task_ids\\\":[\\\"1\\\"],\\\"instruction\\\":\\\"Perform exploratory data analysis on the Iris dataset.\\\",\\\"task_type\\\":\\\"eda\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\nThe current task is about exploratory data analysis, please note the following:\\n- Distinguish column types with `select_dtypes` for tailored analysis and visualization, such as correlation.\\n- Remember to `import numpy as np` before using Numpy functions.\\n\\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { + "code": "import numpy as np\nimport pandas as pd\n\n# Convert the sklearn dataset to a pandas DataFrame for easier manipulation\nfeature_names = iris_data.feature_names\niris_df = pd.DataFrame(X, columns=feature_names)\n\n# Add target column to the DataFrame\niris_df['species'] = pd.Categorical.from_codes(iris_data.target, iris_data.target_names)\n\n# Display basic information about the dataset\niris_info = iris_df.info()\n\n# Display basic statistics about the dataset\niris_description = iris_df.describe()\n\n# Check for missing values\nmissing_values = iris_df.isnull().sum()\n\n# Distinguish column types\nnumerical_cols = iris_df.select_dtypes(include=[np.number]).columns.tolist()\ncategorical_cols = iris_df.select_dtypes(include=['category']).columns.tolist()\n\n# Display the first few rows of the DataFrame\nhead = iris_df.head()\n\n# Output the results\n(iris_info, iris_description, missing_values, numerical_cols, categorical_cols, head)" }, - "[{\"role\": \"user\", \"content\": \"\\n# Background\\nAs a data scientist, you need to help user to achieve their goal [This is a titanic passenger survival dataset, your goal is to predict passenger survival outcome. The target column is Survived. Perform data analysis, data preprocessing, feature engineering, and modeling to predict the target. Report accuracy on the eval data. Train data path: 'tests/data/ml_datasets/titanic/split_train.csv', eval data path: 'tests/data/ml_datasets/titanic/split_eval.csv'.] step-by-step in an continuous Jupyter notebook.\\n\\n## Done Tasks\\n```python\\n\\n```end\\n\\n## Current Task\\nPerform exploratory data analysis on the train dataset to understand the features and target variable.\\n\\n# Latest Data Info\\nLatest data info after previous tasks:\\n\\n\\n# Task\\nWrite complete code for 'Current Task'. And avoid duplicating code from 'Done Tasks', such as repeated import of packages, reading data, etc.\\nSpecifically, \\n\\n# Output Example:\\nwhen current task is \\\"train a lightgbm model on training data\\\", the code can be like:\\n```python\\n# Step 1: check data type and convert to numeric\\nobj_cols = train.select_dtypes(include='object').columns.tolist()\\n\\nfor col in obj_cols:\\n encoder = LabelEncoder()\\n train[col] = encoder.fit_transform(train[col].unique().tolist() + ['unknown'])\\n test[col] = test[col].apply(lambda x: x if x in encoder.classes_ else 'unknown')\\n test[col] = encoder.transform(test[col])\\n\\n# Step 2: train lightgbm model\\nmodel = LGBMClassifier()\\nmodel.fit(train, y_train)\\n```end\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n\"}]": { - "code": "# Perform exploratory data analysis on the train dataset\ndf_train = pd.read_csv('tests/data/ml_datasets/titanic/split_train.csv')\n\n# Display the first few rows of the dataset\ndisplay(df_train.head())\n\n# Summary statistics for numerical features\ndisplay(df_train.describe())\n\n# Summary information about the dataset including the data types and number of non-null values\ndisplay(df_train.info())\n\n# Distribution of the target variable 'Survived'\nsurvival_counts = df_train['Survived'].value_counts()\nprint(\"Survival counts:\\n\", survival_counts)\n\n# Visualizations\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Distribution of the target variable\nsns.countplot(x='Survived', data=df_train)\nplt.title('Distribution of Survival')\nplt.show()\n\n# Correlation matrix heatmap to understand the relationship between features\nplt.figure(figsize=(10, 8))\nsns.heatmap(df_train.corr(), annot=True, fmt='.2f')\nplt.title('Correlation Matrix')\nplt.show()\n\n# Pairplot to visualize the pairwise relationships between features\nsns.pairplot(df_train, hue='Survived')\nplt.title('Pairplot of Features')\nplt.show()" + "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"data_preprocess\\\",\\n \\\"code\\\": \\\"from sklearn.datasets import load_iris\\\\niris_data = load_iris()\\\\nX, y = iris_data.data, iris_data.target\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"import numpy as np\\\\nimport pandas as pd\\\\n\\\\n# Convert the sklearn dataset to a pandas DataFrame for easier manipulation\\\\nfeature_names = iris_data.feature_names\\\\niris_df = pd.DataFrame(X, columns=feature_names)\\\\n\\\\n# Add target column to the DataFrame\\\\niris_df['species'] = pd.Categorical.from_codes(iris_data.target, iris_data.target_names)\\\\n\\\\n# Display basic information about the dataset\\\\niris_info = iris_df.info()\\\\n\\\\n# Display basic statistics about the dataset\\\\niris_description = iris_df.describe()\\\\n\\\\n# Check for missing values\\\\nmissing_values = iris_df.isnull().sum()\\\\n\\\\n# Distinguish column types\\\\nnumerical_cols = iris_df.select_dtypes(include=[np.number]).columns.tolist()\\\\ncategorical_cols = iris_df.select_dtypes(include=['category']).columns.tolist()\\\\n\\\\n# Display the first few rows of the DataFrame\\\\nhead = iris_df.head()\\\\n\\\\n# Output the results\\\\n(iris_info, iris_description, missing_values, numerical_cols, categorical_cols, head)\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"3\\\",\\\"dependent_task_ids\\\":[\\\"2\\\"],\\\"instruction\\\":\\\"Create a plot visualizing the Iris dataset.\\\",\\\"task_type\\\":\\\"other\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { + "code": "import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Set the style of seaborn\nsns.set(style='whitegrid')\n\n# Create a pairplot to visualize the relationships between the features\npairplot = sns.pairplot(iris_df, hue='species')\nplt.show()" + }, + "[{\"role\": \"user\", \"content\": \"\\n# Background\\nAs a data scientist, you need to help user to achieve their goal [This is a titanic passenger survival dataset, your goal is to predict passenger survival outcome. The target column is Survived. Perform data analysis, data preprocessing, feature engineering, and modeling to predict the target. Report accuracy on the eval data. Train data path: 'tests/data/ml_datasets/titanic/split_train.csv', eval data path: 'tests/data/ml_datasets/titanic/split_eval.csv'.] step-by-step in an continuous Jupyter notebook.\\n\\n## Done Tasks\\n```python\\n\\n```end\\n\\n## Current Task\\nPerform exploratory data analysis on the train dataset to understand the features and target variable.\\n\\n# Latest Data Info\\nLatest data info after previous tasks:\\n\\n\\n# Task\\nWrite complete code for 'Current Task'. And avoid duplicating code from 'Done Tasks', such as repeated import of packages, reading data, etc.\\nSpecifically, \\nThe current task is about exploratory data analysis, please note the following:\\n- Distinguish column types with `select_dtypes` for tailored analysis and visualization, such as correlation.\\n- Remember to `import numpy as np` before using Numpy functions.\\n\\n\\n# Output Example:\\nwhen current task is \\\"train a lightgbm model on training data\\\", the code can be like:\\n```python\\n# Step 1: check data type and convert to numeric\\nobj_cols = train.select_dtypes(include='object').columns.tolist()\\n\\nfor col in obj_cols:\\n encoder = LabelEncoder()\\n train[col] = encoder.fit_transform(train[col].unique().tolist() + ['unknown'])\\n test[col] = test[col].apply(lambda x: x if x in encoder.classes_ else 'unknown')\\n test[col] = encoder.transform(test[col])\\n\\n# Step 2: train lightgbm model\\nmodel = LGBMClassifier()\\nmodel.fit(train, y_train)\\n```end\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n\"}]": { + "code": "# Perform exploratory data analysis on the train dataset\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Read the train dataset\ntrain_data = pd.read_csv('tests/data/ml_datasets/titanic/split_train.csv')\n\n# Display the first few rows of the dataset\ndisplay(train_data.head())\n\n# Summary statistics for numerical features\nprint(train_data.describe())\n\n# Summary statistics for categorical features\nprint(train_data.describe(include=['O']))\n\n# Check for missing values\nprint(train_data.isnull().sum())\n\n# Distribution of the target variable\nsns.countplot(x='Survived', data=train_data)\nplt.title('Distribution of Survival on the Titanic')\nplt.show()\n\n# Correlation matrix for numerical features\nnumerical_features = train_data.select_dtypes(include=[np.number])\ncorrelation_matrix = numerical_features.corr()\nplt.figure(figsize=(10, 8))\nsns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', center=0)\nplt.title('Correlation Matrix for Numerical Features')\nplt.show()\n\n# Pairplot for selected features\nselected_features = ['Survived', 'Pclass', 'Age', 'SibSp', 'Parch', 'Fare']\nsns.pairplot(train_data[selected_features], hue='Survived')\nplt.show()\n\n# Boxplot for categorical features vs Survived\nfor column in train_data.select_dtypes(include=['O']).columns:\n if column != 'Survived':\n plt.figure(figsize=(10, 5))\n sns.boxplot(x='Survived', y=column, data=train_data)\n plt.title(f'Survived vs {column}')\n plt.show()\n" }, "[{\"role\": \"system\", \"content\": \"You are an AI Python assistant. You will be given your previous implementation code of a task, runtime error results, and a hint to change the implementation appropriately. Write your full implementation \"}, {\"role\": \"user\", \"content\": \"\\nHere is an example for you.\\n\\nExample 1:\\n[previous impl]:\\n```python\\ndef add(a: int, b: int) -> int:\\n \\\"\\\"\\\"\\n Given integers a and b, return the total value of a and b.\\n \\\"\\\"\\\"\\n return a - b\\n```\\n\\n[runtime Error]:\\nTested passed:\\n\\nTests failed:\\nassert add(1, 2) == 3 # output: -1\\nassert add(1, 2) == 4 # output: -1\\n\\n[reflection on previous impl]:\\nThe implementation failed the test cases where the input integers are 1 and 2. The issue arises because the code does not add the two integers together, but instead subtracts the second integer from the first. To fix this issue, we should change the operator from `-` to `+` in the return statement. This will ensure that the function returns the correct output for the given input.\\n\\n[improved impl]:\\n```python\\ndef add(a: int, b: int) -> int:\\n \\\"\\\"\\\"\\n Given integers a and b, return the total value of a and b.\\n \\\"\\\"\\\"\\n return a + b\\n```\\n\\n[context]\\nSolve the problem in Python:\\ndef sort_array(arr):\\n \\\"\\\"\\\"\\n In this Kata, you have to sort an array of non-negative integers according to\\n number of ones in their binary representation in ascending order.\\n For similar number of ones, sort based on decimal value.\\n\\n It must be implemented like this:\\n >>> sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5]\\n >>> sort_array([-2, -3, -4, -5, -6]) == [-6, -5, -4, -3, -2]\\n >>> sort_array([1, 0, 2, 3, 4]) [0, 1, 2, 3, 4]\\n \\\"\\\"\\\"\\n\\n\\n[previous impl]\\n\\ndef sort_array(arr):\\n # Helper function to count the number of ones in the binary representation\\n def count_ones(n):\\n return bin(n).count('1')\\n \\n # Sort the array using a custom key function\\n # The key function returns a tuple (number of ones, value) for each element\\n # This ensures that if two elements have the same number of ones, they are sorted by their value\\n sorted_arr = sorted(arr, key=lambda x: (count_ones(x), x))\\n \\n return sorted_arr\\n```\\n\\n[runtime Error]\\n[user: Tested passed:\\n\\nTests failed:\\nassert sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5] # output: [1, 2, 4, 3, 5]\\n]\\n\\nAnalysis the error step by step, provide me improve method and code. Remember to follow [context] requirement. Don't forget write code for steps behind the error step.\\n[reflection on previous impl]:\\nxxx\\n\"}]": { "reflection": "The implementation failed the test case where the input array is [1, 5, 2, 3, 4]. The issue arises because the code does not handle the sorting of elements with the same number of ones in their binary representation correctly. According to the context, for similar number of ones, the elements should be sorted based on their decimal value in ascending order. However, the output [1, 2, 4, 3, 5] shows that 3 and 4 are not in the correct order, indicating that the sorting by decimal value is not functioning as intended. To fix this issue, we should ensure that the sorting is stable, so that when two elements have the same number of ones, they maintain their relative order if their decimal values are already in ascending order.", "improved_impl": "def sort_array(arr):\n # Helper function to count the number of ones in the binary representation\n def count_ones(n):\n return bin(n).count('1')\n \n # Sort the array using a custom key function\n # The key function returns a tuple (number of ones, value) for each element\n # This ensures that if two elements have the same number of ones, they are sorted by their value\n # We use a stable sorting algorithm to maintain the relative order of elements with the same number of ones\n sorted_arr = sorted(arr, key=lambda x: (count_ones(x), x), reverse=True)\n sorted_arr = sorted(sorted_arr, key=count_ones)\n \n return sorted_arr\n" - }, - "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"from sklearn.datasets import load_iris\\\\niris_data = load_iris()\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"2\\\",\\\"dependent_task_ids\\\":[\\\"1\\\"],\\\"instruction\\\":\\\"Perform exploratory data analysis on the Iris dataset.\\\",\\\"task_type\\\":\\\"eda\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { - "code": "import pandas as pd\n\n# Convert the Iris dataset to a DataFrame for easier manipulation\niris_df = pd.DataFrame(data=iris_data.data, columns=iris_data.feature_names)\niris_df['target'] = iris_data.target\niris_df['target_names'] = iris_df['target'].apply(lambda x: iris_data.target_names[x])\n\n# Display basic information about the dataset\niris_df.info()\n\n# Display statistical summary of the dataset\niris_df.describe()\n\n# Display the first few rows of the dataset\ndisplay(iris_df.head())" - }, - "[{\"role\": \"system\", \"content\": \"You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.Usually the libraries you need are already installed.Dont check if packages already imported.**\"}, {\"role\": \"user\", \"content\": \"\\n## User Requirement\\nRun data analysis on sklearn Iris dataset, include a plot\\n## Context\\n\\n## Current Plan\\n[\\n {\\n \\\"task_id\\\": \\\"1\\\",\\n \\\"dependent_task_ids\\\": [],\\n \\\"instruction\\\": \\\"Load the sklearn Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"from sklearn.datasets import load_iris\\\\niris_data = load_iris()\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"2\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"1\\\"\\n ],\\n \\\"instruction\\\": \\\"Perform exploratory data analysis on the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"eda\\\",\\n \\\"code\\\": \\\"import pandas as pd\\\\n\\\\n# Convert the Iris dataset to a DataFrame for easier manipulation\\\\niris_df = pd.DataFrame(data=iris_data.data, columns=iris_data.feature_names)\\\\niris_df['target'] = iris_data.target\\\\niris_df['target_names'] = iris_df['target'].apply(lambda x: iris_data.target_names[x])\\\\n\\\\n# Display basic information about the dataset\\\\niris_df.info()\\\\n\\\\n# Display statistical summary of the dataset\\\\niris_df.describe()\\\\n\\\\n# Display the first few rows of the dataset\\\\ndisplay(iris_df.head())\\\",\\n \\\"result\\\": \\\"a successful run\\\",\\n \\\"is_success\\\": true,\\n \\\"is_finished\\\": true\\n },\\n {\\n \\\"task_id\\\": \\\"3\\\",\\n \\\"dependent_task_ids\\\": [\\n \\\"2\\\"\\n ],\\n \\\"instruction\\\": \\\"Create a plot visualizing the Iris dataset.\\\",\\n \\\"task_type\\\": \\\"other\\\",\\n \\\"code\\\": \\\"\\\",\\n \\\"result\\\": \\\"\\\",\\n \\\"is_success\\\": false,\\n \\\"is_finished\\\": false\\n }\\n]\\n## Current Task\\n{\\\"task_id\\\":\\\"3\\\",\\\"dependent_task_ids\\\":[\\\"2\\\"],\\\"instruction\\\":\\\"Create a plot visualizing the Iris dataset.\\\",\\\"task_type\\\":\\\"other\\\",\\\"code\\\":\\\"\\\",\\\"result\\\":\\\"\\\",\\\"is_success\\\":false,\\\"is_finished\\\":false}\\n\"}, {\"role\": \"user\", \"content\": \"\\n# Instruction\\nWrite complete code for 'Current Task'. And avoid duplicating code from finished tasks, such as repeated import of packages, reading data, etc.\\nSpecifically, \\n\\n# Capabilities\\n- You can utilize pre-defined tools in any code lines from 'Available Tools' in the form of Python Class.\\n- You can freely combine the use of any other public packages, like sklearn, numpy, pandas, etc..\\n\\n# Available Tools (can be empty):\\nEach Class tool is described in JSON format. When you call a tool, import the tool first.\\n{}\\n\\n# Constraints:\\n- Ensure the output new code is executable in the same Jupyter notebook with previous tasks code have been executed.\\n- Always prioritize using pre-defined tools for the same functionality.\\n\"}]": { - "code": "import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Set the style of seaborn\nsns.set_style('whitegrid')\n\n# Pairplot to visualize the relationships between features\nsns.pairplot(iris_df, hue='target_names', markers='+')\nplt.show()" - }, - "\n## context\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"invoice\": \"False\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- invoice: # if it's a invoice file, return True else False\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"invoice\": \"True\"\n}\n[/CONTENT]" + } } \ No newline at end of file From 27c82c17e1230f50dcd6f126ebbaa882c0a15d56 Mon Sep 17 00:00:00 2001 From: yzlin Date: Thu, 8 Feb 2024 11:50:10 +0800 Subject: [PATCH 15/18] recover a cache for other tests --- tests/data/rsp_cache.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/data/rsp_cache.json b/tests/data/rsp_cache.json index 26a3bd494..d3058fbd0 100644 --- a/tests/data/rsp_cache.json +++ b/tests/data/rsp_cache.json @@ -391,5 +391,6 @@ "[{\"role\": \"system\", \"content\": \"You are an AI Python assistant. You will be given your previous implementation code of a task, runtime error results, and a hint to change the implementation appropriately. Write your full implementation \"}, {\"role\": \"user\", \"content\": \"\\nHere is an example for you.\\n\\nExample 1:\\n[previous impl]:\\n```python\\ndef add(a: int, b: int) -> int:\\n \\\"\\\"\\\"\\n Given integers a and b, return the total value of a and b.\\n \\\"\\\"\\\"\\n return a - b\\n```\\n\\n[runtime Error]:\\nTested passed:\\n\\nTests failed:\\nassert add(1, 2) == 3 # output: -1\\nassert add(1, 2) == 4 # output: -1\\n\\n[reflection on previous impl]:\\nThe implementation failed the test cases where the input integers are 1 and 2. The issue arises because the code does not add the two integers together, but instead subtracts the second integer from the first. To fix this issue, we should change the operator from `-` to `+` in the return statement. This will ensure that the function returns the correct output for the given input.\\n\\n[improved impl]:\\n```python\\ndef add(a: int, b: int) -> int:\\n \\\"\\\"\\\"\\n Given integers a and b, return the total value of a and b.\\n \\\"\\\"\\\"\\n return a + b\\n```\\n\\n[context]\\nSolve the problem in Python:\\ndef sort_array(arr):\\n \\\"\\\"\\\"\\n In this Kata, you have to sort an array of non-negative integers according to\\n number of ones in their binary representation in ascending order.\\n For similar number of ones, sort based on decimal value.\\n\\n It must be implemented like this:\\n >>> sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5]\\n >>> sort_array([-2, -3, -4, -5, -6]) == [-6, -5, -4, -3, -2]\\n >>> sort_array([1, 0, 2, 3, 4]) [0, 1, 2, 3, 4]\\n \\\"\\\"\\\"\\n\\n\\n[previous impl]\\n\\ndef sort_array(arr):\\n # Helper function to count the number of ones in the binary representation\\n def count_ones(n):\\n return bin(n).count('1')\\n \\n # Sort the array using a custom key function\\n # The key function returns a tuple (number of ones, value) for each element\\n # This ensures that if two elements have the same number of ones, they are sorted by their value\\n sorted_arr = sorted(arr, key=lambda x: (count_ones(x), x))\\n \\n return sorted_arr\\n```\\n\\n[runtime Error]\\n[user: Tested passed:\\n\\nTests failed:\\nassert sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5] # output: [1, 2, 4, 3, 5]\\n]\\n\\nAnalysis the error step by step, provide me improve method and code. Remember to follow [context] requirement. Don't forget write code for steps behind the error step.\\n[reflection on previous impl]:\\nxxx\\n\"}]": { "reflection": "The implementation failed the test case where the input array is [1, 5, 2, 3, 4]. The issue arises because the code does not handle the sorting of elements with the same number of ones in their binary representation correctly. According to the context, for similar number of ones, the elements should be sorted based on their decimal value in ascending order. However, the output [1, 2, 4, 3, 5] shows that 3 and 4 are not in the correct order, indicating that the sorting by decimal value is not functioning as intended. To fix this issue, we should ensure that the sorting is stable, so that when two elements have the same number of ones, they maintain their relative order if their decimal values are already in ascending order.", "improved_impl": "def sort_array(arr):\n # Helper function to count the number of ones in the binary representation\n def count_ones(n):\n return bin(n).count('1')\n \n # Sort the array using a custom key function\n # The key function returns a tuple (number of ones, value) for each element\n # This ensures that if two elements have the same number of ones, they are sorted by their value\n # We use a stable sorting algorithm to maintain the relative order of elements with the same number of ones\n sorted_arr = sorted(arr, key=lambda x: (count_ones(x), x), reverse=True)\n sorted_arr = sorted(sorted_arr, key=count_ones)\n \n return sorted_arr\n" - } + }, + "\n## context\n\n\n-----\n\n## format example\n[CONTENT]\n{\n \"invoice\": \"False\"\n}\n[/CONTENT]\n\n## nodes: \": # \"\n- invoice: # if it's a invoice file, return True else False\n\n\n## constraint\nLanguage: Please use the same language as Human INPUT.\nFormat: output wrapped inside [CONTENT][/CONTENT] like format example, nothing else.\n\n## action\nFollow instructions of nodes, generate output and make sure it follows the format example.\n": "[CONTENT]\n{\n \"invoice\": \"True\"\n}\n[/CONTENT]" } \ No newline at end of file From f781021d5ad8b5d75eb4e93f6f4e8a7ded2f8c2f Mon Sep 17 00:00:00 2001 From: mannaandpoem <1580466765@qq.com> Date: Thu, 8 Feb 2024 14:21:39 +0800 Subject: [PATCH 16/18] remove debug --- metagpt/actions/ci/execute_nb_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/actions/ci/execute_nb_code.py b/metagpt/actions/ci/execute_nb_code.py index 0ff00de8f..a8c9c3085 100644 --- a/metagpt/actions/ci/execute_nb_code.py +++ b/metagpt/actions/ci/execute_nb_code.py @@ -99,7 +99,7 @@ class ExecuteNbCode(Action): for i, output in enumerate(outputs): if output["output_type"] == "stream" and not any( tag in output["text"] - for tag in ["| INFO | metagpt", "| ERROR | metagpt", "| WARNING | metagpt"] + for tag in ["| INFO | metagpt", "| ERROR | metagpt", "| WARNING | metagpt", "DEBUG"] ): parsed_output += output["text"] elif output["output_type"] == "display_data": From 74c5807d0111397424e2a946f3001c731f9ae134 Mon Sep 17 00:00:00 2001 From: yzlin Date: Thu, 8 Feb 2024 17:09:07 +0800 Subject: [PATCH 17/18] add ml example --- examples/ci/machine_learning.py | 13 +++++++++++++ examples/ci/ml_engineer_with_tools.py | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/ci/machine_learning.py b/examples/ci/machine_learning.py index e69de29bb..9eda981ac 100644 --- a/examples/ci/machine_learning.py +++ b/examples/ci/machine_learning.py @@ -0,0 +1,13 @@ +import asyncio + +from metagpt.roles.ci.code_interpreter import CodeInterpreter + + +async def main(requirement: str): + role = CodeInterpreter(auto_run=True, use_tools=False) + await role.run(requirement) + + +if __name__ == "__main__": + requirement = "Run data analysis on sklearn Wine recognition dataset, include a plot, and train a model to predict wine class (20% as validation), and show validation accuracy." + asyncio.run(main(requirement)) diff --git a/examples/ci/ml_engineer_with_tools.py b/examples/ci/ml_engineer_with_tools.py index 1c73a1dd0..66d37316b 100644 --- a/examples/ci/ml_engineer_with_tools.py +++ b/examples/ci/ml_engineer_with_tools.py @@ -3,8 +3,8 @@ import asyncio from metagpt.roles.ci.ml_engineer import MLEngineer -async def main(requirement: str, auto_run: bool = True, use_tools: bool = True): - role = MLEngineer(goal=requirement, auto_run=auto_run, use_tools=use_tools) +async def main(requirement: str): + role = MLEngineer(auto_run=True, use_tools=True) await role.run(requirement) From 66d891fe81cbca40dad61d00ddda3003c3d45f12 Mon Sep 17 00:00:00 2001 From: yzlin Date: Thu, 8 Feb 2024 18:30:20 +0800 Subject: [PATCH 18/18] add example readme --- examples/ci/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 examples/ci/README.md diff --git a/examples/ci/README.md b/examples/ci/README.md new file mode 100644 index 000000000..d526b94c7 --- /dev/null +++ b/examples/ci/README.md @@ -0,0 +1,18 @@ +# Code Interpreter (CI) + +## What is CodeInterpreter +CodeInterpreter is an agent who solves problems through codes. It understands user requirements, makes plans, writes codes for execution, and uses tools if necessary. These capabilities enable it to tackle a wide range of scenarios, please check out the examples below. + +## Example List +- Data visualization +- Machine learning modeling +- Image background removal +- Solve math problems +- Receipt OCR +- Tool usage: web page imitation +- Tool usage: web crawling +- Tool usage: text2image +- Tool usage: email summarization and response +- More on the way! + +Please see [here](https://docs.deepwisdom.ai/main/en/guide/use_cases/agent/code_interpreter/ci_intro.html) for detailed explanation. \ No newline at end of file