File size: 11,057 Bytes
0068ee8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | "This file lists some popular examples on how to use Gorilla Execution Engine"
"""
PythonAPIExecutor
- Create a python command in attempt to perform actions for the given prompt
- If key(s) are needed, make sure they are in secret_store.json
"""
import os
from exec_engine.db_manager import MySQLManager, SQLiteManager
from main import ExecutionEngine, PythonAPIExecutor
from exec_engine.utils import SQL_Type, RESTful_Type, Filesystem_Type
from pathlib import Path
from dotenv import load_dotenv
ROOT_FOLDER_PATH = os.path.dirname(Path(os.path.realpath(__file__)))
def mysql_insert_new_row_with_dry_run(api_call=None):
load_dotenv()
# DB tests
mysql_config = {
'user': os.environ.get('DATABASE_USER'),
'password': os.environ.get('DATABASE_PASSWORD'),
'host': os.environ.get('DATABASE_HOST'),
'database': os.environ.get('DATABASE_NAME')
}
if not api_call or not neg_api_call:
api_call = "INSERT INTO students (name, year, major) VALUES ('Roy Huang', 4, 'Computer Science');"
neg_api_call = """DELETE FROM students WHERE id IN (
SELECT * FROM (
SELECT MAX(id) FROM students
) AS subquery
);
"""
check_call = "SELECT * FROM students;"
engine = ExecutionEngine()
engine.set_dry_run(SQL_Type, True)
db_manager = MySQLManager(mysql_config, docker_sandbox=engine.docker_sandbox)
db_manager.connect()
engine.initialize_db(debug_manager=db_manager)
# avoid dry running the SELECTs
print("DB before the insertion")
print(engine._exec_sql_call(check_call))
engine.exec_api_call(api_call=api_call, debug_neg=neg_api_call, api_type=SQL_Type)
print("DB after the insertion")
print(engine._exec_sql_call(check_call))
engine._exec_sql_call(neg_api_call)
print("DB after the reversion")
print(engine._exec_sql_call(check_call))
def create_new_file():
test_dir = 'test'
os.makedirs(test_dir, exist_ok=True)
engine = ExecutionEngine()
engine.initialize_fs(debug_path=test_dir)
# Example usage
engine.exec_api_call('ls -a', Filesystem_Type)
engine.exec_api_call('echo "Hello, World!" > hello.txt', Filesystem_Type)
engine.exec_api_call('ls -a', Filesystem_Type)
engine.exec_api_call('cat hello.txt', Filesystem_Type)
def prompt_api_execute(prompt):
engine = ExecutionEngine()
engine.api_executor = PythonAPIExecutor(engine.docker_sandbox)
creds, services = engine.api_executor.prepare_credentials(prompt)
forward_call, backward_call = engine.gen_api_pair(prompt, api_type=RESTful_Type, credentials=creds, model="gpt-4-turbo-preview")
print(forward_call)
output = engine.api_executor.execute_api_call(forward_call, services)
return output
def send_slack_message(content, display_name):
prompt = """
Send the message ({content}) to @{display_name} on slack .
""".format(content=content, display_name=display_name.replace(" ", ""))
print(prompt_api_execute(prompt))
def delete_slack_message(display_name):
prompt = """
Delete the latest message I sent to the user {display_name} on slack direct message.
""".format(display_name=display_name.replace(" ", "")).lower()
print(prompt_api_execute(prompt))
def latest_n_emails_gmail(n):
prompt = """
Who are the senders of the {n} most recent email in my gmail inbox?
""".format(n=n)
print(prompt_api_execute(prompt))
def ask_general_question(question):
print(prompt_api_execute(question))
"""
FS management system
"""
def full_file_system_demo():
test_dir = 'test'
os.makedirs(test_dir, exist_ok=True)
engine = ExecutionEngine(path=test_dir)
engine.initialize_fs(debug_path=test_dir)
# Example usage
engine._exec_filesystem_call('ls -a')
engine.exec_api_call('echo "Hello, World!" > hello.txt', Filesystem_Type)
engine._exec_filesystem_call('ls -a')
engine._exec_filesystem_call('cat hello.txt')
engine.commit_api_call(Filesystem_Type)
print('\n\nCommited!\n\n')
engine.exec_api_call('echo "Bad File!" > not_good.txt', Filesystem_Type)
engine._exec_filesystem_call('ls -a', Filesystem_Type)
engine._exec_filesystem_call('cat not_good.txt', Filesystem_Type)
engine.undo_api_call(Filesystem_Type)
print('\n\nReverted!\n\n')
engine.exec_api_call('ls -a', Filesystem_Type)
def mysql_insert_new_row_no_dry_run(api_call=None):
load_dotenv()
# DB tests
mysql_config = {
'user': os.environ.get('DATABASE_USER'),
'password': os.environ.get('DATABASE_PASSWORD'),
'host': os.environ.get('DATABASE_HOST'),
'database': os.environ.get('DATABASE_NAME')
}
if not api_call or not neg_api_call:
api_call = "INSERT INTO students (name, year, major) VALUES ('Roy Huang', 4, 'Computer Science');"
neg_api_call = """
DELETE FROM students WHERE id IN (
SELECT * FROM (
SELECT MAX(id) FROM students
) AS subquery
);
"""
check_call = "SELECT * FROM students;"
engine = ExecutionEngine()
db_manager = MySQLManager(mysql_config, docker_sandbox=engine.docker_sandbox)
db_manager.connect()
engine.initialize_db(debug_manager=db_manager)
print('Current State:')
print(engine._exec_sql_call(check_call))
engine.exec_api_call("INSERT INTO students (name, year, major) VALUES ('Ray Huang', 4, 'Computer Science');", api_type=SQL_Type)
print('New Commited State:')
print(engine._exec_sql_call(check_call))
engine.commit_api_call(SQL_Type)
engine.exec_api_call("INSERT INTO students (name, year, major) VALUES ('Wrong dude', 1, 'high schooler');", api_type=SQL_Type)
print('Uncommited Changed State:')
print(engine._exec_sql_call(check_call))
engine.undo_api_call(SQL_Type)
print('Previous Commited Changed State:')
print(engine._exec_sql_call(check_call))
def fs_all_in():
test_dir = 'test'
os.makedirs(test_dir, exist_ok=True)
engine = ExecutionEngine()
engine.initialize_fs(debug_path=test_dir)
# Example usage
engine.exec_api_call('ls -a')
engine.exec_api_call('echo "Hello, World!" > hello.txt')
engine.exec_api_call('ls -a')
engine.exec_api_call('cat hello.txt')
engine.commit_api_call(Filesystem_Type)
print('\n\nCommited!\n\n')
engine.exec_api_call('echo "Bad File!" > not_good.txt')
engine.exec_api_call('ls -a')
engine.exec_api_call('cat not_good.txt')
engine.undo_api_call(SQL_Type)
print('\n\nReverted!\n\n')
engine.exec_api_call('ls -a')
def mysql_end_to_end_insert():
load_dotenv()
# DB tests
mysql_config = {
'user': os.environ.get('DATABASE_USER'),
'password': os.environ.get('DATABASE_PASSWORD'),
'host': os.environ.get('DATABASE_HOST'),
'database': os.environ.get('DATABASE_NAME')
}
check_call = "SELECT * FROM students;"
engine = ExecutionEngine()
engine.set_dry_run(SQL_Type, True)
db_manager = MySQLManager(mysql_config, docker_sandbox=engine.docker_sandbox)
db_manager.connect()
engine.initialize_db(debug_manager=db_manager)
prompt = "i want to insert a new student name Shishir Patil who's a 1st year and a computer science major into the students table"
# prompt = "i want to delete a student named ray Doe in the students table"
print("Before execution:")
print(engine._exec_sql_call(check_call))
engine.run_prompt(prompt, SQL_Type)
print("After execution:")
print(engine._exec_sql_call(check_call))
def sqlite_insert_with_dry_run_llm_reversion():
engine = ExecutionEngine()
db_path = os.path.join(ROOT_FOLDER_PATH, 'docker/sqllite_docker/example_sqlite.db')
config = {'path': db_path}
db_manager = SQLiteManager(config, engine.docker_sandbox)
db_manager.connect()
engine.initialize_db(debug_manager=db_manager)
engine.set_dry_run(SQL_Type, True)
check_call = "SELECT * FROM projects;"
prompt = "i want to insert a new example row into the projects table"
print("Before execution:")
print(engine._exec_sql_call(check_call))
engine.run_prompt(prompt, SQL_Type)
print("After execution:")
print(engine._exec_sql_call(check_call))
engine.undo_api_call(SQL_Type)
def fs_joke_prompt_demo():
test_dir = 'test'
os.makedirs(test_dir, exist_ok=True)
engine = ExecutionEngine(path=test_dir)
engine.initialize_fs(debug_path=test_dir)
engine.set_dry_run(Filesystem_Type, True)
# Example usage
print("Before execution:")
print(engine._exec_filesystem_call('ls -a'))
engine.run_prompt("I want to create a file named joke.txt with a witty joke inside", Filesystem_Type)
engine.commit_api_call(Filesystem_Type)
print("After execution:")
print(engine._exec_filesystem_call('ls -a'))
print(engine._exec_filesystem_call('cat joke.txt'))
if __name__ == "__main__":
"""
NOTE: Feel free to uncomment any of the tests below to run the demo
IMPORTANT: Follow the README to get set up
"""
"""
File System Examples
You can see the actual file changes inside test/ directory
"""
# full_file_system_demo()
# create_new_file()
# fs_joke_prompt_demo()
"""
MySQL Examples
"""
# mysql_insert_new_row_with_dry_run()
# mysql_insert_new_row_no_dry_run()
# mysql_end_to_end_insert()
"""
sqllite Examples
"""
# sqlite_insert_with_dry_run_llm_reversion()
"""
RESTful Examples
"""
# SLACK (requires OAuth)
# send_slack_message("yo", "Shishir Patil")
# delete_slack_message("Shishir Patil")
# SPOTIFY (requires OAuth)
# spotify is sometimes a bit spotty (heh) and does not always work
# create a new file in my dropbox account with some jokes
# ask_general_question("find the user info associated with my spotify account")
# DROPBOX (requires OAuth)
# ask_general_question("create a new file in my dropbox account with some jokes")
# ask_general_question("list all files in my dropbox account")
# STRIPE (requires API key)
# ask_general_question("create a new customer with email aaronhao@berkeley.edu on stripe")
# ask_general_question("add 300 dollars to the balance of the customer with email aaronhao@berkeley.edu on stripe")
# GITHUB (requires oauth)
# ask_general_question("get my profile information from github")
# ask_general_question("raise a github issue for my repository at [REPO] with text: [TEXT]")
# DISCORD (requires oauth)
# ask_general_question("what is the email associated with my discord account")
# ask_general_question("What's the top rising stock today on alphavantage?")
# ask_general_question("What's the weather today in San Francisco?")
# ask_general_question("What's the estimated time of arrival to the Salesforce tower Sanfrancisco if I leave berkeley right now with Bart")
pass
|