From 22ff372b895d979f0ca6b35ceef7ce42166638a7 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 22 Sep 2023 14:54:39 +0300 Subject: [PATCH 1/3] test for openai key --- tests/test_sdk.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_sdk.py b/tests/test_sdk.py index f1013f1..4c37efd 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -670,9 +670,9 @@ def test_flow(self, mock_post, mock_put): con.ml_engines.create( 'openai1', openai_handler, - connection_data={'api_key': '111'} + connection_data={'api_key': 'sk-11'} ) - check_sql_call(mock_post, 'CREATE ML_ENGINE openai1 FROM openai USING api_key = "111"') + check_sql_call(mock_post, 'CREATE ML_ENGINE openai1 FROM openai USING api_key = \'sk-11\'') con.ml_engines.create( 'openai1', From 266e4efd044b831372eba083f6c267adde35b817 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 22 Sep 2023 15:30:49 +0300 Subject: [PATCH 2/3] updated 'tables' example --- examples/working_with_tables.py | 45 +++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/examples/working_with_tables.py b/examples/working_with_tables.py index 73ca881..ee2f2fa 100644 --- a/examples/working_with_tables.py +++ b/examples/working_with_tables.py @@ -3,21 +3,44 @@ con = mindsdb_sdk.connect() -# get user's database (connected to mindsdb as rental_db) -db = con.databases.rental_db +# connect to mindsdb example database +example_db = con.databases.create( + 'example_db', + engine='postgres', + connection_args={ + "user": "demo_user", + "password": "demo_password", + "host": "3.220.66.106", + "port": "5432", + "database": "demo" + } +) -# get table -table1 = db.tables.house_sales +# connect to the empty user database +my_db = con.databases.create( + 'my_db', + engine='postgres', + connection_args={ + "user": "postgres", + "host": "localhost", + "port": "5432", + "database": "my_database" + } +) +# get home_rentals table +table1 = example_db.tables.get('demo_data.home_rentals') # ---- create new table ---- -# copy create table house_sales and fill it with rows with type=house -table2 = db.tables.create('house_sales2', table1.filter(type='house')) +# create table home_rentals in user db and fill it with rows with location=great +table2 = my_db.tables.create('home_rentals', table1.filter(location='great')) + # create table from csv file + df = pd.read_csv('my_data.csv') -table3 = db.tables.create('my_table', df) +table3 = my_db.tables.create('my_table', df) # ---- insert into table ---- @@ -28,16 +51,16 @@ # ---- update data in table ---- -# get all rows with type=house from table1 and update values in table2 using key ('saledate', 'type', 'bedrooms') +# get all rows with number_of_rooms=1 from table1 and update values in table2 using key ('location', 'neighborhood') table2.update( - table1.filter(type='house'), - on=['saledate', 'type', 'bedrooms'] + table1.filter(number_of_rooms=1), + on=['location', 'neighborhood'] ) # ---- delete rows from table ---- # delete all rows where bedrooms=2 -table2.delete(bedrooms=2) +table2.delete(number_of_rooms=1) From d39d056f5e0e8623e71ab864a23f5bcd98e8047d Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 22 Sep 2023 15:31:17 +0300 Subject: [PATCH 3/3] using handler in create database --- mindsdb_sdk/databases.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mindsdb_sdk/databases.py b/mindsdb_sdk/databases.py index 2e69b7b..f5e0257 100644 --- a/mindsdb_sdk/databases.py +++ b/mindsdb_sdk/databases.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Union from mindsdb_sql.parser.dialects.mindsdb import CreateDatabase from mindsdb_sql.parser.ast import DropDatabase, Identifier @@ -7,6 +7,7 @@ from .query import Query from .tables import Tables +from .handlers import Handler class Database: @@ -93,7 +94,7 @@ def list(self) -> List[Database]: """ return [Database(self, name) for name in self._list_databases()] - def create(self, name: str, engine: str, connection_args: dict) -> Database: + def create(self, name: str, engine: Union[str, Handler], connection_args: dict) -> Database: """ Create new integration and return it @@ -102,6 +103,9 @@ def create(self, name: str, engine: str, connection_args: dict) -> Database: :param connection_args: {"key": "value"} object with the connection parameters specific for each engine :return: created Database object """ + if isinstance(engine, Handler): + engine = engine.name + ast_query = CreateDatabase( name=Identifier(name), engine=engine,