From da8bc983b202168273310298d2fc6ca82a6dd55b Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 3 May 2024 17:15:27 +0300 Subject: [PATCH 01/11] model name in 'select * from models' are not unique --- mindsdb_sdk/models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mindsdb_sdk/models.py b/mindsdb_sdk/models.py index 8b7b1ce..0d24c48 100644 --- a/mindsdb_sdk/models.py +++ b/mindsdb_sdk/models.py @@ -618,8 +618,11 @@ def list(self, with_versions: bool = False, filters = { } if name is not None: filters['NAME'] = name + if version is not None: filters['VERSION'] = version + else: + filters['ACTIVE'] = True ast_query = Select( targets=[Star()], From 8201c4602b28588339a7a0e6952bb8f727d2a23d Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 31 May 2024 16:31:04 +0300 Subject: [PATCH 02/11] select version from 'models' --- mindsdb_sdk/models.py | 14 ++++++-------- tests/test_sdk.py | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/mindsdb_sdk/models.py b/mindsdb_sdk/models.py index 0d24c48..1c26823 100644 --- a/mindsdb_sdk/models.py +++ b/mindsdb_sdk/models.py @@ -609,24 +609,22 @@ def list(self, with_versions: bool = False, :return: list of Model or ModelVersion objects """ - table = 'models' model_class = Model - if with_versions: - table = 'models_versions' - model_class = ModelVersion - filters = { } + filters = {} if name is not None: filters['NAME'] = name - if version is not None: filters['VERSION'] = version + + if with_versions: + model_class = ModelVersion else: - filters['ACTIVE'] = True + filters['ACTIVE'] = '1' ast_query = Select( targets=[Star()], - from_table=Identifier(table), + from_table=Identifier('models'), where=dict_to_binary_op(filters) ) df = self.project.query(ast_query.to_string()).fetch() diff --git a/tests/test_sdk.py b/tests/test_sdk.py index 2d1bba8..a41fb25 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -183,7 +183,7 @@ def check_model(self, model, database, mock_post): # list all versions models = model.list_versions() - check_sql_call(mock_post, f"SELECT * FROM models_versions WHERE NAME = '{model.name}'", + check_sql_call(mock_post, f"SELECT * FROM models WHERE NAME = '{model.name}'", database=model.project.name) model2 = models[0] # Model object From f3814946a2df5bd2164c83b47df8aad576689329 Mon Sep 17 00:00:00 2001 From: dusvyat Date: Thu, 20 Jun 2024 10:04:31 +0300 Subject: [PATCH 03/11] add example for text2sql using mindsdb agents --- examples/using_agents_with_text2sql.py | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/using_agents_with_text2sql.py diff --git a/examples/using_agents_with_text2sql.py b/examples/using_agents_with_text2sql.py new file mode 100644 index 0000000..370c8c9 --- /dev/null +++ b/examples/using_agents_with_text2sql.py @@ -0,0 +1,37 @@ +import mindsdb_sdk +from uuid import uuid4 +import os + +con = mindsdb_sdk.connect() + +open_ai_key = os.environ['OPENAI_API_KEY'] + +# Now create an agent that will use the model we just created. +agent = con.agents.create(name=f'mindsdb_sql_agent_{uuid4().hex}', + params={'openai_api_key': open_ai_key}) + +# Set up a Postgres data source with our new agent. +data_source = 'postgres' +connection_args = { + "user": "demo_user", + "password": "demo_password", + "host": "samples.mindsdb.com", + "port": "5432", + "database": "demo", + "schema": "demo_data" +} +description = 'mindsdb demo database' +database = con.databases.create( + f'mindsdb_sql_agent_datasource_{uuid4().hex}', + data_source, + connection_args +) + +# Actually connect the agent to the datasource. +agent.add_database(database.name, [], description) + + +print('Ask a question: ') +question = 'What is the average rental price of homes?' +answer = agent.completion([{'question': question, 'answer': None}]) +print(answer.content) From 8021ce8b8f8cedc82f78db213c85dadc2de20ffd Mon Sep 17 00:00:00 2001 From: dusvyat Date: Thu, 20 Jun 2024 12:30:03 +0300 Subject: [PATCH 04/11] Add 'ENGINE' attribute to database in SDK The 'ENGINE' attribute has been added to databases in the software development kit for improved functionality and clarity. The Database class is now initialised with this attribute and queries are updated to select 'ENGINE' from information_schema.databases. This affects multiple test scripts, making them more comprehensive and specific. --- mindsdb_sdk/databases.py | 19 ++++++++++--------- tests/test_sdk.py | 14 +++++++------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/mindsdb_sdk/databases.py b/mindsdb_sdk/databases.py index f5e0257..9ab9821 100644 --- a/mindsdb_sdk/databases.py +++ b/mindsdb_sdk/databases.py @@ -27,9 +27,10 @@ class Database: """ - def __init__(self, server, name): + def __init__(self, server, name, engine=None): self.server = server self.name = name + self.engine = engine self.api = server.api self.tables = Tables(self, self.api) @@ -82,9 +83,9 @@ def __init__(self, api): def _list_databases(self): data = self.api.sql_query( - "select NAME from information_schema.databases where TYPE='data'" + "select NAME, ENGINE from information_schema.databases where TYPE='data'" ) - return list(data.NAME) + return dict(zip(data.NAME, data.ENGINE)) def list(self) -> List[Database]: """ @@ -92,7 +93,8 @@ def list(self) -> List[Database]: :return: list of Database objects """ - return [Database(self, name) for name in self._list_databases()] + databases = self._list_databases() + return [Database(self, name, engine=engine) for name, engine in databases.items()] def create(self, name: str, engine: Union[str, Handler], connection_args: dict) -> Database: """ @@ -112,7 +114,7 @@ def create(self, name: str, engine: Union[str, Handler], connection_args: dict) parameters=connection_args, ) self.api.sql_query(ast_query.to_string()) - return Database(self, name) + return Database(self, name, engine=engine) def drop(self, name: str): """ @@ -130,8 +132,7 @@ def get(self, name: str) -> Database: :param name: name of integration :return: Database object """ - if name not in self._list_databases(): + databases = self._list_databases() + if name not in databases: raise AttributeError("Database doesn't exist") - return Database(self, name) - - + return Database(self, name, engine=databases[name]) diff --git a/tests/test_sdk.py b/tests/test_sdk.py index 2d1bba8..c80406d 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -238,11 +238,11 @@ def test_flow(self, mock_post, mock_put, mock_get): assert call_args[0][0] == 'https://cloud.mindsdb.com/api/status' # --------- databases ------------- - response_mock(mock_post, pd.DataFrame([{'NAME': 'db1'}])) + response_mock(mock_post, pd.DataFrame([{'NAME': 'db1','ENGINE': 'postgres'}])) databases = server.list_databases() - check_sql_call(mock_post, "select NAME from information_schema.databases where TYPE='data'") + check_sql_call(mock_post, "select NAME, ENGINE from information_schema.databases where TYPE='data'") database = databases[0] str(database) @@ -284,7 +284,7 @@ def test_flow(self, mock_post, mock_put, mock_get): check_sql_call(mock_post, 'DROP DATABASE `proj1-1`') # test upload file - response_mock(mock_post, pd.DataFrame([{'NAME': 'files'}])) + response_mock(mock_post, pd.DataFrame([{'NAME': 'files', 'ENGINE': 'file'}])) database = server.get_database('files') # create file df = pd.DataFrame([{'s': '1'}, {'s': 'a'}]) @@ -609,11 +609,11 @@ def test_flow(self, mock_post, mock_put): assert call_args[1]['json']['email'] == 'a@b.com' # --------- databases ------------- - response_mock(mock_post, pd.DataFrame([{'NAME': 'db1'}])) + response_mock(mock_post, pd.DataFrame([{'NAME': 'db1', 'ENGINE': 'postgres'}])) databases = con.databases.list() - check_sql_call(mock_post, "select NAME from information_schema.databases where TYPE='data'") + check_sql_call(mock_post, "select NAME, ENGINE from information_schema.databases where TYPE='data'") database = databases[0] assert database.name == 'db1' @@ -660,7 +660,7 @@ def test_flow(self, mock_post, mock_put): check_sql_call(mock_post, 'DROP DATABASE `proj1-1`') # test upload file - response_mock(mock_post, pd.DataFrame([{'NAME': 'files'}])) + response_mock(mock_post, pd.DataFrame([{'NAME': 'files', 'ENGINE': 'file'}])) database = con.databases.files # create file df = pd.DataFrame([{'s': '1'}, {'s': 'a'}]) @@ -1614,7 +1614,7 @@ def test_add_database(self, mock_post, mock_put, mock_get): responses_mock(mock_post, [ # DB get (POST /sql). pd.DataFrame([ - {'NAME': 'existing_db'} + {'NAME': 'existing_db', 'ENGINE': 'postgres'} ]), # DB tables get (POST /sql). pd.DataFrame([ From 3942ca0151af7aaf3a63adf95ec721ec1bb694a8 Mon Sep 17 00:00:00 2001 From: dusvyat Date: Thu, 20 Jun 2024 13:31:51 +0300 Subject: [PATCH 05/11] Update examples and refactor code for text2sql functionality This commit primarily focuses on refactoring and updating the examples related to text2sql functionality in the MindsDB SDK. Two examples were removed, adjustments to environment variable handling and naming were made in one, and a new example was added, demonstrating the use of the MindsDB API key from an environment variable. In addition, a minor change was made in the 'agents' module to rename a function for clarity. --- examples/using_agents_with_text2sql.py | 10 +- examples/using_database_mind_text2sql.py | 51 ++++++++++ ..._mindsdb_inference_with_text2sql_prompt.py | 95 ------------------- ...sdb_inference_with_text2sql_using_tools.py | 71 -------------- mindsdb_sdk/agents.py | 2 +- 5 files changed, 59 insertions(+), 170 deletions(-) create mode 100644 examples/using_database_mind_text2sql.py delete mode 100644 examples/using_mindsdb_inference_with_text2sql_prompt.py delete mode 100644 examples/using_mindsdb_inference_with_text2sql_using_tools.py diff --git a/examples/using_agents_with_text2sql.py b/examples/using_agents_with_text2sql.py index 370c8c9..1b0e993 100644 --- a/examples/using_agents_with_text2sql.py +++ b/examples/using_agents_with_text2sql.py @@ -4,10 +4,12 @@ con = mindsdb_sdk.connect() -open_ai_key = os.environ['OPENAI_API_KEY'] +open_ai_key = os.getenv('OPENAI_API_KEY') +model_name = 'gpt-4' # Now create an agent that will use the model we just created. -agent = con.agents.create(name=f'mindsdb_sql_agent_{uuid4().hex}', +agent = con.agents.create(name=f'mindsdb_sql_agent_{model_name}_{uuid4().hex}', + model={'model_name': model_name,'openai_api_key': open_ai_key}, params={'openai_api_key': open_ai_key}) # Set up a Postgres data source with our new agent. @@ -31,7 +33,9 @@ agent.add_database(database.name, [], description) -print('Ask a question: ') question = 'What is the average rental price of homes?' answer = agent.completion([{'question': question, 'answer': None}]) print(answer.content) + +con.databases.drop(database.name) +con.agents.drop(agent.name) diff --git a/examples/using_database_mind_text2sql.py b/examples/using_database_mind_text2sql.py new file mode 100644 index 0000000..5e86830 --- /dev/null +++ b/examples/using_database_mind_text2sql.py @@ -0,0 +1,51 @@ +from uuid import uuid4 + +from openai import OpenAI +from mindsdb_sdk.utils.mind import create_mind +import os + + +# Load MindsDB API key from environment variable. or set it here. +MINDSDB_API_KEY = os.getenv('MINDSDB_API_KEY') + +# Set the model name for mind to use +model_name = 'gpt-4' + +# Set the base URL for the MindsDB LiteLLM proxy. +base_url = 'https://ai.dev.mindsdb.com' + + +# Connect to MindsDB LiteLLM proxy. +client = OpenAI( + api_key=MINDSDB_API_KEY, + base_url=base_url +) + +# create a database mind +mind = create_mind( + name = f'my_house_data_mind_{uuid4().hex}', + description= 'House Sales', + base_url= base_url, + api_key= MINDSDB_API_KEY, + model= model_name, + data_source_type='postgres', + data_source_connection_args={ + 'user': 'demo_user', + 'password': 'demo_password', + 'host': 'samples.mindsdb.com', + 'port': '5432', + 'database': 'demo', + 'schema': 'demo_data' + } +) + +# Actually pass in our tool to get a SQL completion. +completion = client.chat.completions.create( + model=mind.name, + messages=[ + {'role': 'user', 'content': 'How many 2 bedroom houses sold in 2008?'} + ], + stream=False +) + +print(completion.choices[0].message.content) diff --git a/examples/using_mindsdb_inference_with_text2sql_prompt.py b/examples/using_mindsdb_inference_with_text2sql_prompt.py deleted file mode 100644 index 8f05550..0000000 --- a/examples/using_mindsdb_inference_with_text2sql_prompt.py +++ /dev/null @@ -1,95 +0,0 @@ -from openai import OpenAI -from mindsdb_sdk.utils.openai import extract_sql_query, query_database, chat_completion_request, \ - pretty_print_conversation - -import mindsdb_sdk -import os - -from mindsdb_sdk.utils.table_schema import get_table_schemas - -# generate the key at https://llm.mdb.ai -MINDSDB_API_KEY = os.environ.get("MINDSDB_API_KEY") -OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") - -MODEL = "gpt-3.5-turbo" - -# the prompt should be a question that can be answered by the database -SYSTEM_PROMPT = """You are a SQL expert. Given an input question, first create a syntactically correct SQL query to run, then look at the results of the query and return the answer to the input question. -Unless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the LIMIT clause as per SQL standards. You can order the results to return the most informative data in the database. -Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in backticks (`) to denote them as identifiers. -Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. -Pay attention to use CURRENT_DATE function to get the current date, if the question involves "today". - -Use the following format: - -Question: -SQLQuery: -SQLResult: -Answer: - -Only use the following tables: - -{schema} -""" -PROMPT = "what was the average delay on arrivals?" - - -def generate_system_prompt(system_prompt: str, schema: dict) -> dict: - prompt = { - "role":"system", - "content":system_prompt.format(schema=schema) - } - return prompt - - -def generate_user_prompt(query: str) -> dict: - prompt = { - "role":"user", - "content":query - } - return prompt - - -con = mindsdb_sdk.connect() - -# given database name, returns schema and database object -# using example_db from mindsdb - -database = con.databases.get("example_db") -schema = get_table_schemas(database, included_tables=["airline_passenger_satisfaction"]) - -# client_mindsdb_serve = OpenAI( -# api_key=MINDSDB_API_KEY, -# base_url="https://llm.mdb.ai" -# ) - -client_mindsdb_serve = OpenAI( - api_key=OPENAI_API_KEY -) - -messages = [ - generate_system_prompt(SYSTEM_PROMPT, schema), - generate_user_prompt(PROMPT) -] - -chat_response = chat_completion_request(client=client_mindsdb_serve, model=MODEL, messages=messages, tools=None, - tool_choice=None) - -# extract the SQL query from the response -query = extract_sql_query(chat_response.choices[0].message.content) - -result = query_database(database, query) - -# generate the user prompt with the query result, this will be used to generate the final response -query = generate_user_prompt(f"Given this SQLResult: {str(result)} provide Answer: ") - -# add the query to the messages list -messages.append(query) - -# generate the final response -chat_completion_gpt = chat_completion_request(client=client_mindsdb_serve, model=MODEL, messages=messages, tools=None, - tool_choice=None) -response = chat_completion_gpt.choices[0].message.content - -pretty_print_conversation(messages) - diff --git a/examples/using_mindsdb_inference_with_text2sql_using_tools.py b/examples/using_mindsdb_inference_with_text2sql_using_tools.py deleted file mode 100644 index 02c8a23..0000000 --- a/examples/using_mindsdb_inference_with_text2sql_using_tools.py +++ /dev/null @@ -1,71 +0,0 @@ -from openai import OpenAI - - -from mindsdb_sdk.utils.openai import ( - make_query_tool, - execute_function_call, - chat_completion_request, - pretty_print_conversation) - -import mindsdb_sdk -import os - -from mindsdb_sdk.utils.table_schema import get_table_schemas - -# generate the key at https://llm.mdb.ai -MINDSDB_API_KEY = os.environ.get("MINDSDB_API_KEY") -OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY") - -MODEL = "gpt-3.5-turbo" - - -con = mindsdb_sdk.connect() - -# given database name, returns schema and database object -# using example_db from mindsdb - -# client_mindsdb_serve = OpenAI( -# api_key=MINDSDB_API_KEY, -# base_url="https://llm.mdb.ai" -# ) - -client_mindsdb_serve = OpenAI( - api_key=OPENAI_API_KEY -) - -database = con.databases.get("mindsdb_demo_db") -schema = get_table_schemas(database, included_tables=["airline_passenger_satisfaction"]) - -tools = [make_query_tool(schema)] - -SYSTEM_PROMPT = """You are a SQL expert. Given an input question, Answer user questions by generating SQL queries -against the database schema provided in tools -Unless the user specifies in the question a specific number of examples to obtain, query for at most 5 results using the -LIMIT clause as per SQL standards. You can order the results to return the most informative data in the database. -Never query for all columns from a table. You must query only the columns that are needed to answer the question. -Wrap each column name in backticks (`) to denote them as identifiers. -Pay attention to use only the column names you can see in the tables below. -Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table. -Pay attention to use CURRENT_DATE function to get the current date, if the question involves "today".""" - -messages = [{ - "role":"system", "content":SYSTEM_PROMPT - }, {"role":"user", "content":"what was the average delay on arrivals?"}] - -chat_response = chat_completion_request(client=client_mindsdb_serve, model=MODEL, messages=messages, tools=tools, tool_choice=None) - -assistant_message = chat_response.choices[0].message - -assistant_message.content = str(assistant_message.tool_calls[0].function) - -messages.append({"role": assistant_message.role, "content": assistant_message.content}) - -if assistant_message.tool_calls: - results = execute_function_call(message=assistant_message, database=database) - messages.append({ - "role": "function", "tool_call_id": assistant_message.tool_calls[0].id, - "name": assistant_message.tool_calls[0].function.name, - "content": results - }) - -pretty_print_conversation(messages) diff --git a/mindsdb_sdk/agents.py b/mindsdb_sdk/agents.py index 444a7d4..7811324 100644 --- a/mindsdb_sdk/agents.py +++ b/mindsdb_sdk/agents.py @@ -58,7 +58,7 @@ class Agent: Delete an agent by name: - >>> agents.delete('my_agent') + >>> agents.drop('my_agent') """ def __init__( self, From 25d28a832be86543dfe7b1671db1fc0950a50243 Mon Sep 17 00:00:00 2001 From: dusvyat Date: Tue, 25 Jun 2024 12:50:51 +0300 Subject: [PATCH 06/11] Update agent parameters and query in text2sql example Adjusted the formatting in the model parameters within the agent creation process for better readability. Also updated the question variable to reflect a different query, demonstrating flexibility in possible queries. --- examples/using_agents_with_text2sql.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/using_agents_with_text2sql.py b/examples/using_agents_with_text2sql.py index 1b0e993..fde3184 100644 --- a/examples/using_agents_with_text2sql.py +++ b/examples/using_agents_with_text2sql.py @@ -9,7 +9,7 @@ # Now create an agent that will use the model we just created. agent = con.agents.create(name=f'mindsdb_sql_agent_{model_name}_{uuid4().hex}', - model={'model_name': model_name,'openai_api_key': open_ai_key}, + model={'model_name': model_name, 'openai_api_key': open_ai_key}, params={'openai_api_key': open_ai_key}) # Set up a Postgres data source with our new agent. @@ -33,7 +33,7 @@ agent.add_database(database.name, [], description) -question = 'What is the average rental price of homes?' +question = 'How many three-bedroom houses were sold in 2008?' answer = agent.completion([{'question': question, 'answer': None}]) print(answer.content) From 2d09aa9a59ce152137ab6e02452bff89e4cee654 Mon Sep 17 00:00:00 2001 From: Max Stepanov Date: Tue, 25 Jun 2024 14:26:17 +0300 Subject: [PATCH 07/11] better exception processing --- mindsdb_sdk/utils/mind.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mindsdb_sdk/utils/mind.py b/mindsdb_sdk/utils/mind.py index c7c298f..e66e3b0 100644 --- a/mindsdb_sdk/utils/mind.py +++ b/mindsdb_sdk/utils/mind.py @@ -54,7 +54,14 @@ def create_mind( response = requests.post(url, json=payload, headers=headers) response.raise_for_status() except requests.exceptions.HTTPError as e: - logger.error(f"Failed to create mind: {e.response.json()}") + try: + error_message = e.response.json() + except Exception: + error_message = str(e) + logger.error(f"Failed to create mind: {error_message}") + raise e + except Exception as e: + logger.error(f"Failed to create mind: {e}") raise e name = response.json()['name'] From 8ed4a64456f2deec17dc70a1fe2b51388a54c1c4 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 28 Jun 2024 17:40:03 +0300 Subject: [PATCH 08/11] set active --- mindsdb_sdk/models.py | 14 ++++---------- tests/test_sdk.py | 3 +-- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/mindsdb_sdk/models.py b/mindsdb_sdk/models.py index 1c26823..16a38c2 100644 --- a/mindsdb_sdk/models.py +++ b/mindsdb_sdk/models.py @@ -7,7 +7,7 @@ from mindsdb_sql.parser.dialects.mindsdb import CreatePredictor, DropPredictor from mindsdb_sql.parser.dialects.mindsdb import RetrainPredictor, FinetunePredictor -from mindsdb_sql.parser.ast import Identifier, Select, Star, Join, Update, Describe, Constant +from mindsdb_sql.parser.ast import Identifier, Select, Star, Join, Update, Describe, Constant, Set from mindsdb_sql import parse_sql from mindsdb_sql.exceptions import ParsingException @@ -388,15 +388,9 @@ def set_active(self, version: int): :param version: version to set active """ - ast_query = Update( - table=Identifier(parts=[self.project.name, 'models_versions']), - update_columns={ - 'active': Constant(1) - }, - where=dict_to_binary_op({ - 'name': self.name, - 'version': version - }) + ast_query = Set( + category='active', + value=Identifier(parts=[self.project.name, self.name, str(version)]) ) sql = ast_query.to_string() if is_saving(): diff --git a/tests/test_sdk.py b/tests/test_sdk.py index a41fb25..940039a 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -194,8 +194,7 @@ def check_model(self, model, database, mock_post): # get call before last call mock_call = mock_post.call_args_list[-2] - assert mock_call[1]['json'][ - 'query'] == f"update {model2.project.name}.models_versions set active=1 where name = '{model2.name}' AND version = 3" + assert mock_call[1]['json']['query'] == f"SET active {model2.project.name}.{model2.name}.`3`" @patch('requests.Session.post') def check_table(self, table, mock_post): From 7558f6aea47048e0270e98564e0606d9577820a0 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 28 Jun 2024 17:48:05 +0300 Subject: [PATCH 09/11] drop version --- mindsdb_sdk/models.py | 2 +- mindsdb_sdk/projects.py | 16 ++++------------ tests/test_sdk.py | 4 ++-- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/mindsdb_sdk/models.py b/mindsdb_sdk/models.py index 16a38c2..6d44565 100644 --- a/mindsdb_sdk/models.py +++ b/mindsdb_sdk/models.py @@ -7,7 +7,7 @@ from mindsdb_sql.parser.dialects.mindsdb import CreatePredictor, DropPredictor from mindsdb_sql.parser.dialects.mindsdb import RetrainPredictor, FinetunePredictor -from mindsdb_sql.parser.ast import Identifier, Select, Star, Join, Update, Describe, Constant, Set +from mindsdb_sql.parser.ast import Identifier, Select, Star, Join, Describe, Set from mindsdb_sql import parse_sql from mindsdb_sql.exceptions import ParsingException diff --git a/mindsdb_sdk/projects.py b/mindsdb_sdk/projects.py index e734ab4..b8bda07 100644 --- a/mindsdb_sdk/projects.py +++ b/mindsdb_sdk/projects.py @@ -1,10 +1,8 @@ from typing import List -from mindsdb_sql.parser.dialects.mindsdb import CreateDatabase +from mindsdb_sql.parser.dialects.mindsdb import CreateDatabase, DropPredictor from mindsdb_sql.parser.ast import DropDatabase -from mindsdb_sql.parser.ast import Identifier, Delete - -from mindsdb_sdk.utils.sql import dict_to_binary_op +from mindsdb_sql.parser.ast import Identifier from mindsdb_sdk.agents import Agents from mindsdb_sdk.databases import Databases @@ -96,7 +94,6 @@ def query(self, sql: str) -> Query: """ return Query(self.api, sql, database=self.name) - def drop_model_version(self, name: str, version: int): """ Drop version of the model @@ -104,13 +101,8 @@ def drop_model_version(self, name: str, version: int): :param name: name of the model :param version: version to drop """ - ast_query = Delete( - table=Identifier('models_versions'), - where=dict_to_binary_op({ - 'name': name, - 'version': version - }) - ) + ast_query = DropPredictor(Identifier(parts=[name, str(version)])) + self.query(ast_query.to_string()).fetch() diff --git a/tests/test_sdk.py b/tests/test_sdk.py index 940039a..eedc013 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -493,7 +493,7 @@ def check_project_models_versions(self, project, database, mock_post): self.check_model(model, database) project.drop_model_version('m1', 1) - check_sql_call(mock_post, f"delete from models_versions where name='m1' and version=1") + check_sql_call(mock_post, f"DROP PREDICTOR m1.`1`") @patch('requests.Session.post') @@ -960,7 +960,7 @@ def check_project_models_versions(self, project, database, mock_post): self.check_model(model, database) project.models.m1.drop_version(1) - check_sql_call(mock_post, f"delete from models_versions where name='m1' and version=1") + check_sql_call(mock_post, f"DROP PREDICTOR m1.`1`") @patch('requests.Session.post') def check_database(self, database, mock_post): From ec3fc18af1b4272fbf1041c5efd7f4b556877d48 Mon Sep 17 00:00:00 2001 From: Max Stepanov Date: Fri, 5 Jul 2024 11:54:29 +0300 Subject: [PATCH 10/11] make `model` arg optional for `cretae_mind` --- mindsdb_sdk/utils/mind.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mindsdb_sdk/utils/mind.py b/mindsdb_sdk/utils/mind.py index e66e3b0..e7de32f 100644 --- a/mindsdb_sdk/utils/mind.py +++ b/mindsdb_sdk/utils/mind.py @@ -1,3 +1,5 @@ +from typing import Optional + import requests from logging import getLogger @@ -18,12 +20,11 @@ def __init__(self, name): def create_mind( base_url: str, api_key: str, - name: str, description: str, - model: str, data_source_type: str, data_source_connection_args: dict, + model: Optional[str] = None, ) -> Mind: """ Create a mind entity in LiteLLM proxy. From 17592701d71ccca4db0cd9c683fc5eaff6157096 Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 5 Jul 2024 12:02:14 +0300 Subject: [PATCH 11/11] bump version --- mindsdb_sdk/__about__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mindsdb_sdk/__about__.py b/mindsdb_sdk/__about__.py index b5a66c1..6a1198a 100755 --- a/mindsdb_sdk/__about__.py +++ b/mindsdb_sdk/__about__.py @@ -1,6 +1,6 @@ __title__ = 'mindsdb_sdk' __package_name__ = 'mindsdb_sdk' -__version__ = '2.4.3' +__version__ = '2.4.4' __description__ = "MindsDB Python SDK, provides an SDK to use a remote mindsdb instance" __email__ = "jorge@mindsdb.com" __author__ = 'MindsDB Inc'