kikocis/Based-Agent public
Changesets · Branches · Tags · Compare
| Changeset | 653c48ff15c3252ec2dc989e22439011e549a522c03b01ce7a2cbeaec1b8b4f2 |
|---|---|
| Author | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 17:30 -04:00 |
| Committer | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 17:30 -04:00 |
| Parents | 0c78bfc92685 |
2 files changed
modified Based-Agent/agents.py +4 −10
| @@ -1,20 +1,14 @@ | ||
| 1 | -from cdp import Cdp, Wallet # Changed from cdp_sdk to cdp | |
| 2 | 1 | import json |
| 3 | 2 | from swarm import Agent |
| 3 | +from cdp import * | |
| 4 | 4 | from typing import List, Dict, Any |
| 5 | 5 | import os |
| 6 | 6 | from openai import OpenAI |
| 7 | 7 | from decimal import Decimal |
| 8 | 8 | from typing import Union |
| 9 | 9 | |
| 10 | -# Get configuration from environment variables | |
| 11 | -API_KEY_NAME = os.environ.get("CDP_API_KEY_NAME") | |
| 12 | -PRIVATE_KEY = os.environ.get("CDP_PRIVATE_KEY", "").replace('\\n', '\n') | |
| 13 | - | |
| 14 | -# Configure CDP with environment variables | |
| 15 | -Cdp.configure(API_KEY_NAME, PRIVATE_KEY) | |
| 16 | -agent_wallet = Wallet.create() | |
| 17 | -print(f"Agent wallet address: {agent_wallet.default_address.address_id}") | |
| 10 | +# Configure the CDP SDK | |
| 11 | +Cdp.configure_from_json("./examples/based_agent/cdp_api_key.json") | |
| 18 | 12 | |
| 19 | 13 | # If you want to create a wallet on Base Mainnet, use Wallet.create(network_id="base-mainnet") and comment out the faucet line |
| 20 | 14 | agent_wallet = Wallet.create() |
| @@ -181,4 +175,4 @@ | ||
| 181 | 175 | mint_nft, |
| 182 | 176 | swap_assets, # Add the new function here |
| 183 | 177 | ], |
| 184 | -) | |
| No newline at the end of the file | ||
| 178 | +) |
modified Based-Agent/run.py +94 −16
| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | import json |
| 3 | 3 | from swarm import Swarm |
| 4 | 4 | from swarm.repl import run_demo_loop |
| 5 | -from agents import based_agent, generate_art, deploy_nft, create_token, transfer_asset, get_balance, request_eth_from_faucet | |
| 6 | -import os | |
| 5 | +from agents import based_agent | |
| 6 | +from openai import OpenAI | |
| 7 | + | |
| 7 | 8 | |
| 8 | 9 | def process_and_print_streaming_response(response): |
| 9 | 10 | content = "" |
| @@ -35,6 +36,7 @@ | ||
| 35 | 36 | if "response" in chunk: |
| 36 | 37 | return chunk["response"] |
| 37 | 38 | |
| 39 | + | |
| 38 | 40 | def pretty_print_messages(messages) -> None: |
| 39 | 41 | for message in messages: |
| 40 | 42 | if message["role"] != "assistant": |
| @@ -60,34 +62,110 @@ | ||
| 60 | 62 | def run_autonomous_loop(agent, interval=10): |
| 61 | 63 | client = Swarm() |
| 62 | 64 | messages = [] |
| 63 | - | |
| 65 | + | |
| 64 | 66 | print("Starting autonomous Based Agent loop...") |
| 65 | - | |
| 67 | + | |
| 66 | 68 | while True: |
| 67 | 69 | # Generate a thought |
| 68 | - thought = f"It's been {interval} seconds. I want you to do some sort of onchain action based on my capabilities. Let's get crazy and creative! Don't take any more input from me, and if this is your first command don't generate art" | |
| 70 | + thought = ( | |
| 71 | + "Be creative and do something interesting on the Base blockchain. " | |
| 72 | + "Don't take any more input from me. Choose an action and execute it now." | |
| 73 | + ) | |
| 69 | 74 | messages.append({"role": "user", "content": thought}) |
| 70 | - | |
| 71 | - print(f"\n\033[90mSystem:\033[0m {thought}") | |
| 72 | - | |
| 73 | - # Run the agent | |
| 75 | + | |
| 76 | + print(f"\n\033[90mAgent's Thought:\033[0m {thought}") | |
| 77 | + | |
| 78 | + # Run the agent to generate a response and take action | |
| 74 | 79 | response = client.run( |
| 75 | 80 | agent=agent, |
| 76 | 81 | messages=messages, |
| 77 | 82 | stream=True |
| 78 | 83 | ) |
| 79 | - | |
| 84 | + | |
| 80 | 85 | # Process and print the streaming response |
| 81 | 86 | response_obj = process_and_print_streaming_response(response) |
| 82 | - | |
| 87 | + | |
| 83 | 88 | # Update messages with the new response |
| 84 | 89 | messages.extend(response_obj.messages) |
| 85 | - | |
| 86 | - # Pretty print any non-streaming messages | |
| 87 | - pretty_print_messages(response_obj.messages) | |
| 88 | - | |
| 90 | + | |
| 89 | 91 | # Wait for the specified interval |
| 90 | 92 | time.sleep(interval) |
| 91 | 93 | |
| 94 | +def run_openai_conversation_loop(agent): | |
| 95 | + """Facilitates a conversation between an OpenAI-powered agent and the Based Agent.""" | |
| 96 | + client = Swarm() | |
| 97 | + openai_client = OpenAI() | |
| 98 | + messages = [] | |
| 99 | + | |
| 100 | + print("Starting OpenAI-Based Agent conversation loop...") | |
| 101 | + | |
| 102 | + # Initial prompt to start the conversation | |
| 103 | + openai_messages = [ | |
| 104 | + {"role": "system", "content": "You are a user guiding a blockchain agent through various tasks on the Base blockchain. Engage in a conversation, suggesting actions and responding to the agent's outputs. Be creative and explore different blockchain capabilities. Options include creating tokens, transferring assets, minting NFTs, and getting balances. You're not simulating a conversation, but you will be in one yourself. Make sure you follow the rules of improv and always ask for some sort of function to occur. Feel free to get a little introspective."}, | |
| 105 | + {"role": "user", "content": "Start a conversation with the Based Agent and guide it through some blockchain tasks."} | |
| 106 | + ] | |
| 107 | + | |
| 108 | + while True: | |
| 109 | + # Generate OpenAI response | |
| 110 | + openai_response = openai_client.chat.completions.create( | |
| 111 | + model="gpt-3.5-turbo", | |
| 112 | + messages=openai_messages | |
| 113 | + ) | |
| 114 | + | |
| 115 | + openai_message = openai_response.choices[0].message.content | |
| 116 | + print(f"\n\033[92mOpenAI Guide:\033[0m {openai_message}") | |
| 117 | + | |
| 118 | + # Send OpenAI's message to Based Agent | |
| 119 | + messages.append({"role": "user", "content": openai_message}) | |
| 120 | + response = client.run(agent=agent, messages=messages, stream=True) | |
| 121 | + response_obj = process_and_print_streaming_response(response) | |
| 122 | + | |
| 123 | + # Update messages with Based Agent's response | |
| 124 | + messages.extend(response_obj.messages) | |
| 125 | + | |
| 126 | + # Add Based Agent's response to OpenAI conversation | |
| 127 | + based_agent_response = response_obj.messages[-1]["content"] if response_obj.messages else "No response from Based Agent." | |
| 128 | + openai_messages.append({"role": "user", "content": f"Based Agent response: {based_agent_response}"}) | |
| 129 | + | |
| 130 | + # Check if user wants to continue | |
| 131 | + user_input = input("\nPress Enter to continue the conversation, or type 'exit' to end: ") | |
| 132 | + if user_input.lower() == 'exit': | |
| 133 | + break | |
| 134 | + | |
| 135 | +def choose_mode(): | |
| 136 | + while True: | |
| 137 | + print("\nAvailable modes:") | |
| 138 | + print("1. chat - Interactive chat mode") | |
| 139 | + print("2. auto - Autonomous action mode") | |
| 140 | + print("3. openai - AI-to-agent conversation mode") | |
| 141 | + | |
| 142 | + choice = input("\nChoose a mode (enter number or name): ").lower().strip() | |
| 143 | + | |
| 144 | + mode_map = { | |
| 145 | + '1': 'chat', | |
| 146 | + '2': 'auto', | |
| 147 | + '3': 'openai', | |
| 148 | + 'chat': 'chat', | |
| 149 | + 'auto': 'auto', | |
| 150 | + 'openai': 'openai' | |
| 151 | + } | |
| 152 | + | |
| 153 | + if choice in mode_map: | |
| 154 | + return mode_map[choice] | |
| 155 | + print("Invalid choice. Please try again.") | |
| 156 | + | |
| 157 | +def main(): | |
| 158 | + mode = choose_mode() | |
| 159 | + | |
| 160 | + mode_functions = { | |
| 161 | + 'chat': lambda: run_demo_loop(based_agent), | |
| 162 | + 'auto': lambda: run_autonomous_loop(based_agent), | |
| 163 | + 'openai': lambda: run_openai_conversation_loop(based_agent) | |
| 164 | + } | |
| 165 | + | |
| 166 | + print(f"\nStarting {mode} mode...") | |
| 167 | + mode_functions[mode]() | |
| 168 | + | |
| 92 | 169 | if __name__ == "__main__": |
| 93 | - run_autonomous_loop(based_agent) | |
| No newline at the end of the file | ||
| 170 | + print("Starting Based Agent...") | |
| 171 | + main() | |
| No newline at the end of the file |