From e20926481b9ef7c4cd07549b5703fcc0e5790cd6 Mon Sep 17 00:00:00 2001 From: Yizhe Wen Date: Mon, 1 Apr 2024 21:32:04 -0400 Subject: [PATCH 1/6] chore: prompt engineering to enhance output stability --- autotx/utils/agent/build_goal.py | 44 ++++++++++--- autotx/utils/agent/define_tasks.py | 102 +++++++++++++++++++++++++++-- 2 files changed, 130 insertions(+), 16 deletions(-) diff --git a/autotx/utils/agent/build_goal.py b/autotx/utils/agent/build_goal.py index 90ea4ca6..9650b4b5 100644 --- a/autotx/utils/agent/build_goal.py +++ b/autotx/utils/agent/build_goal.py @@ -66,35 +66,61 @@ def build_goal(prompt: str, agents_information: str, smart_account_address: ETHA def analyze_user_prompt(chat_history: str, agents_information: str, smart_account_address: ETHAddress) -> DefineGoalResponse: template = dedent( """ - Based on the following chat history between you and the user: - ``` - {chat_history} - ``` - + You are a task assignment expert. You are good at analyzing the user's chat records to complete the task goals and conditions represented in the user's chat records. You must analyze the prompt and define a goal to be executed by the agents. If the prompt is not clear or missing information, you MUST ask for more information. If the prompt is invalid, unsupported or outside the scope of the agents, you MUST ask for a new prompt. Always ensure you have all the information needed to define the goal that can be executed without prior context. + When dealing with Ethereum transactions, assume the following is the user's address: {smart_account_address} The available agents and tools: {agents_information} Respond ONLY in one of three of the following JSON formats: - 1: + {{ "type": "goal", - "goal": "The detailed goal here. No need to mention specific agents or tools. But you MUST mention the user's address." + "goal": "The detailed goal here. No need to mention specific agents or tools." }} - 2: + {{ "type": "missing_info", "message": "The information that is missing here" }} - 3: + {{ "type": "unsupported", "message": "Reason why the prompt is unsupported here" }} + + Please note: You can only output content in json format, and do not output any other content! + + You can refer to the following examples: + Chat History: User: Swap ETH to 0.05 WBTC, then swap WBTC to 1000 USDC, and finally send 50 USDC to vitalik.eth + Output: + {{ + "type": "goal", + "goal": "First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap 0.05 WBTC to 1000 USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'." + }} + + Chat History: User: Send 1 ETH to vitalik.eth + Output: + {{ + "type": "goal", + "goal": "Send 1 ETH from the address 0x5d15311D760511d89cFad67404131cdc155E9FDB to the ENS domain vitalik.eth." + }} + + Chat History: User: Buy 100 USDC with ETH + Output: + {{ + "type": "goal", + "goal": "Execute a swap from ETH to 100 USDC for the user's address 0x5d15311D760511d89cFad67404131cdc155E9FDB" + }} + + OK, start now! + Please note: You can only output content in json format, and do not output any other content! + Chat History: {chat_history} + Output: """ ) diff --git a/autotx/utils/agent/define_tasks.py b/autotx/utils/agent/define_tasks.py index 04754496..116419b9 100644 --- a/autotx/utils/agent/define_tasks.py +++ b/autotx/utils/agent/define_tasks.py @@ -6,23 +6,111 @@ def define_tasks(goal: str, agents_information: str, agents: list[Agent]) -> list[Task]: template = dedent( """ - Based on the following goal: {goal} - - You must convert instructions into specific tasks with the following JSON format: + You are an expert in decomposing and assigning tasks. + You are good at converting instructions into specific sequential tasks in the following JSON format: {{ tasks : [{{ - "task": "The description of task to be done with details needed given by user. You MUST include the user's address if needed." + "task_id": int, // Task number, which represents the task number, starting from 0 + "task": "Concise description of task to be done with details needed given by user" "agent": "The agent that best fits to execute the task" "expected_output":"Description of expected output for the task" - "context": [int] // Index of tasks that will have their output used as context for this task (Always start from 0), if applicable. Eg. [1, 3] or None + "context": [int] // The task_id that task execution depends on indicates which task_id execution result depends on which task execution depends. Eg. [0, 1] or null if no tasks need to be depended on. "extra_information": Any extra information as string with description given by the user needed to execute the task, if applicable. }}] }} + Please note that you are only permitted to output json content. The specific tasks will be created based on the available agents role, goal and available tools: {agents_information} - - IMPORTANT: After all tasks are executed, the prepared transactions will be sent to the Ethereum network. + + Please note: You can only output content in json format, and do not output any other content! + + You can refer to the following examples: + Goal: Send 1 ETH from the address 0x5d15311D760511d89cFad67404131cdc155E9FDB to the ENS domain vitalik.eth. + Output: + {{ + "tasks": [{{ + "task": "Check the balance of 1 ETH for the address 0x5d15311D760511d89cFad67404131cdc155E9FDB", + "agent": "send-tokens", + "expected_output": "A numeric value representing the balance of ETH in the account", + "context": null, + "extra_information": "Make sure the balance is at least 1 ETH to proceed with the transaction." + }}, {{ + "task": "Prepare a transfer transaction of 1 ETH to the ENS domain vitalik.eth", + "agent": "send-tokens", + "expected_output": "A message confirming that the ETH transfer transaction has been prepared", + "context": [0], + "extra_information": "Use the output of the first task to ensure there is enough balance before proceeding." + }}] + }} + + Goal: Execute a swap from ETH to 100 USDC for the user's address 0x5d15311D760511d89cFad67404131cdc155E9FDB + Output: + {{ + "tasks": [ + {{ + "task": "Get current ETH balance of the user", + "agent": "send-tokens", + "expected_output": "Current ETH balance of 0x5d15311D760511d89cFad67404131cdc155E9FDB", + "context": null, + "extra_information": "Use Get ETH balance tool to check current ETH balance of user's address." + }}, + {{ + "task": "Get current USDC balance of the user", + "agent": "send-tokens", + "expected_output": "Current USDC balance of 0x5d15311D760511d89cFad67404131cdc155E9FDB", + "context": null, + "extra_information": "Use Get ERC20 balance tool with USDC token symbol to check current USDC balance of user's address." + }}, + {{ + "task": "Calculate ETH amount needed to swap for 100 USDC", + "agent": "swap-tokens", + "expected_output": "The amount of ETH needed to execute a swap to receive 100 USDC", + "context": null, + "extra_information": "Assuming the swap service or agent has the capability to estimate, based on existing market rates, how much ETH is required." + }}, + {{ + "task": "Build transactions needed to execute the swap from ETH to 100 USDC", + "agent": "swap-tokens", + "expected_output": "Transactions prepared for swapping ETH to exactly 100 USDC", + "context": [2], + "extra_information": "Use the calculated ETH amount from the previous step to prepare the necessary transactions for the swap, ensuring that the exact_input is set to True since we want to receive an exact amount of USDC." + }} + ] + }} + + Goal: First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap 0.05 WBTC to 1000 USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'. + Output: + {{ + "tasks": [ + {{ + "task": "Swap the required amount of Ethereum (ETH) for exactly 0.05 Wrapped Bitcoin (WBTC)", + "agent": "swap-tokens", + "expected_output": "A successful swap transaction from ETH to WBTC where the user ends up with exactly 0.05 WBTC", + "context": null, + "extra_information": "Use the exact_output mode for the swap, ensuring the exact amount of 0.05 WBTC is received. Calculate the required ETH amount based on current market rates." + }}, + {{ + "task": "Swap exactly 0.05 Wrapped Bitcoin (WBTC) for 1000 USD Coin (USDC)", + "agent": "swap-tokens", + "expected_output": "A successful swap transaction from WBTC to USDC where the user ends up with exactly 1000 USDC", + "context": null, + "extra_information": "Use the exact_output mode for the swap, ensuring the exact amount of 1000 USDC is received. The exact amount of 0.05 WBTC is to be swapped." + }}, + {{ + "task": "Transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'", + "agent": "send-tokens", + "expected_output": "A confirmation message that the transaction to transfer 50 USDC to 'vitalik.eth' has been prepared", + "context": null, + "extra_information": "Ensure to use the USDC token symbol in the transfer command and verify the correct ENS domain name before executing the transfer." + }} + ] + }} + + OK, start now! + Please note: You can only output content in json format, and do not output any other content! + Goal: {goal} + Output: """ ) From a31b7828a45cbe4bf00d827618e7e601d9d4bbfb Mon Sep 17 00:00:00 2001 From: Zhou <1359698378@qq.com> Date: Tue, 2 Apr 2024 22:06:03 +0800 Subject: [PATCH 2/6] chore: prompt engineering to enhance output stability --- autotx/utils/agent/build_goal.py | 4 ++-- autotx/utils/agent/define_tasks.py | 7 ------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/autotx/utils/agent/build_goal.py b/autotx/utils/agent/build_goal.py index 9650b4b5..09f7ac7d 100644 --- a/autotx/utils/agent/build_goal.py +++ b/autotx/utils/agent/build_goal.py @@ -100,7 +100,7 @@ def analyze_user_prompt(chat_history: str, agents_information: str, smart_accoun Output: {{ "type": "goal", - "goal": "First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap 0.05 WBTC to 1000 USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'." + "goal": "First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap the entire 0.05 WBTC to USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'." }} Chat History: User: Send 1 ETH to vitalik.eth @@ -125,7 +125,7 @@ def analyze_user_prompt(chat_history: str, agents_information: str, smart_accoun ) formatted_template = template.format( - agents_information=agents_information, chat_history=chat_history + agents_information=agents_information, chat_history=chat_history, smart_account_address=smart_account_address ) response = openai.chat.completions.create( diff --git a/autotx/utils/agent/define_tasks.py b/autotx/utils/agent/define_tasks.py index 116419b9..707e70ec 100644 --- a/autotx/utils/agent/define_tasks.py +++ b/autotx/utils/agent/define_tasks.py @@ -55,13 +55,6 @@ def define_tasks(goal: str, agents_information: str, agents: list[Agent]) -> lis "context": null, "extra_information": "Use Get ETH balance tool to check current ETH balance of user's address." }}, - {{ - "task": "Get current USDC balance of the user", - "agent": "send-tokens", - "expected_output": "Current USDC balance of 0x5d15311D760511d89cFad67404131cdc155E9FDB", - "context": null, - "extra_information": "Use Get ERC20 balance tool with USDC token symbol to check current USDC balance of user's address." - }}, {{ "task": "Calculate ETH amount needed to swap for 100 USDC", "agent": "swap-tokens", From 1a5549ea0d5a0d8daebedefc715e3c36733a00a0 Mon Sep 17 00:00:00 2001 From: Zhou Tuo <45249333+FromCSUZhou@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:53:42 +0800 Subject: [PATCH 3/6] Update autotx/utils/agent/build_goal.py follow cbrzn suggestion Co-authored-by: Cesar Brazon --- autotx/utils/agent/build_goal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autotx/utils/agent/build_goal.py b/autotx/utils/agent/build_goal.py index 09f7ac7d..93071f7d 100644 --- a/autotx/utils/agent/build_goal.py +++ b/autotx/utils/agent/build_goal.py @@ -100,7 +100,7 @@ def analyze_user_prompt(chat_history: str, agents_information: str, smart_accoun Output: {{ "type": "goal", - "goal": "First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap the entire 0.05 WBTC to USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'." + "goal": "First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap WBTC to USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'." }} Chat History: User: Send 1 ETH to vitalik.eth From 5e132af4fe9f24ccb28558a7ee004df1d7fbe9e4 Mon Sep 17 00:00:00 2001 From: Zhou Tuo <45249333+FromCSUZhou@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:53:55 +0800 Subject: [PATCH 4/6] Update autotx/utils/agent/define_tasks.py follow cbrzn suggestion Co-authored-by: Cesar Brazon --- autotx/utils/agent/define_tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autotx/utils/agent/define_tasks.py b/autotx/utils/agent/define_tasks.py index 707e70ec..683c17a3 100644 --- a/autotx/utils/agent/define_tasks.py +++ b/autotx/utils/agent/define_tasks.py @@ -72,7 +72,7 @@ def define_tasks(goal: str, agents_information: str, agents: list[Agent]) -> lis ] }} - Goal: First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap 0.05 WBTC to 1000 USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'. + Goal: First, swap Ethereum (ETH) to 0.05 Wrapped Bitcoin (WBTC), ensuring the right amount of ETH is swapped for exactly 0.05 WBTC. After successfully swapping to WBTC, proceed to swap WBTC to 1000 USD Coin (USDC), making sure to receive exactly 1000 USDC from the swap. Following these swaps, transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'. Output: {{ "tasks": [ From ff60f910c24bc2c5484f9de5d9b99a54dec72abd Mon Sep 17 00:00:00 2001 From: Zhou Tuo <45249333+FromCSUZhou@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:54:01 +0800 Subject: [PATCH 5/6] Update autotx/utils/agent/define_tasks.py follow cbrzn suggestion Co-authored-by: Cesar Brazon --- autotx/utils/agent/define_tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autotx/utils/agent/define_tasks.py b/autotx/utils/agent/define_tasks.py index 683c17a3..414eb318 100644 --- a/autotx/utils/agent/define_tasks.py +++ b/autotx/utils/agent/define_tasks.py @@ -84,7 +84,7 @@ def define_tasks(goal: str, agents_information: str, agents: list[Agent]) -> lis "extra_information": "Use the exact_output mode for the swap, ensuring the exact amount of 0.05 WBTC is received. Calculate the required ETH amount based on current market rates." }}, {{ - "task": "Swap exactly 0.05 Wrapped Bitcoin (WBTC) for 1000 USD Coin (USDC)", + "task": "Swap Wrapped Bitcoin (WBTC) for 1000 USD Coin (USDC)", "agent": "swap-tokens", "expected_output": "A successful swap transaction from WBTC to USDC where the user ends up with exactly 1000 USDC", "context": null, From 0fa56fee6c971674caef88c7d8a0405105e0c951 Mon Sep 17 00:00:00 2001 From: Zhou Tuo <45249333+FromCSUZhou@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:54:13 +0800 Subject: [PATCH 6/6] Update autotx/utils/agent/define_tasks.py follow cbrzn suggestion Co-authored-by: Cesar Brazon --- autotx/utils/agent/define_tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autotx/utils/agent/define_tasks.py b/autotx/utils/agent/define_tasks.py index 414eb318..6cb78835 100644 --- a/autotx/utils/agent/define_tasks.py +++ b/autotx/utils/agent/define_tasks.py @@ -88,7 +88,7 @@ def define_tasks(goal: str, agents_information: str, agents: list[Agent]) -> lis "agent": "swap-tokens", "expected_output": "A successful swap transaction from WBTC to USDC where the user ends up with exactly 1000 USDC", "context": null, - "extra_information": "Use the exact_output mode for the swap, ensuring the exact amount of 1000 USDC is received. The exact amount of 0.05 WBTC is to be swapped." + "extra_information": "Use the exact_output mode for the swap, ensuring the exact amount of 1000 USDC is received." }}, {{ "task": "Transfer 50 USDC to the Ethereum Name Service (ENS) domain 'vitalik.eth'",