Skip to content

Commit

Permalink
Merge pull request #128 from mindsdb/staging
Browse files Browse the repository at this point in the history
Release 2.4.4
  • Loading branch information
ea-rus authored Jul 5, 2024
2 parents 1f466e8 + 1759270 commit 9244b18
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 220 deletions.
41 changes: 41 additions & 0 deletions examples/using_agents_with_text2sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import mindsdb_sdk
from uuid import uuid4
import os

con = mindsdb_sdk.connect()

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_{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.
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)


question = 'How many three-bedroom houses were sold in 2008?'
answer = agent.completion([{'question': question, 'answer': None}])
print(answer.content)

con.databases.drop(database.name)
con.agents.drop(agent.name)
51 changes: 51 additions & 0 deletions examples/using_database_mind_text2sql.py
Original file line number Diff line number Diff line change
@@ -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)
95 changes: 0 additions & 95 deletions examples/using_mindsdb_inference_with_text2sql_prompt.py

This file was deleted.

71 changes: 0 additions & 71 deletions examples/using_mindsdb_inference_with_text2sql_using_tools.py

This file was deleted.

2 changes: 1 addition & 1 deletion mindsdb_sdk/__about__.py
Original file line number Diff line number Diff line change
@@ -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__ = "[email protected]"
__author__ = 'MindsDB Inc'
Expand Down
2 changes: 1 addition & 1 deletion mindsdb_sdk/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Agent:
Delete an agent by name:
>>> agents.delete('my_agent')
>>> agents.drop('my_agent')
"""
def __init__(
self,
Expand Down
19 changes: 10 additions & 9 deletions mindsdb_sdk/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -82,17 +83,18 @@ 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]:
"""
Show list of integrations (databases) on server
: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:
"""
Expand All @@ -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):
"""
Expand All @@ -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])
27 changes: 11 additions & 16 deletions mindsdb_sdk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, Describe, Set
from mindsdb_sql import parse_sql
from mindsdb_sql.exceptions import ParsingException

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -609,21 +603,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'] = '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()
Expand Down
Loading

0 comments on commit 9244b18

Please sign in to comment.