Skip to content

Symplify usage #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ You can establish a connection to the MindsDB server using the SDK. Here are som

```python
import mindsdb_sdk
server = mindsdb_sdk.connect()
server = mindsdb_sdk.connect('http://127.0.0.1:47334')
con = mindsdb_sdk.connect()
con = mindsdb_sdk.connect('http://127.0.0.1:47334')
```

#### Connect to the MindsDB Cloud

```python
import mindsdb_sdk
server = mindsdb_sdk.connect(login='[email protected]', password='-')
server = mindsdb_sdk.connect('https://cloud.mindsdb.com', login='[email protected]', password='-')
con = mindsdb_sdk.connect(login='[email protected]', password='-')
con = mindsdb_sdk.connect('https://cloud.mindsdb.com', login='[email protected]', password='-')
```

#### Connect to a MindsDB Pro server

```python
import mindsdb_sdk
server = mindsdb_sdk.connect('http://<YOUR_INSTANCE_IP>', login='[email protected]', password='-', is_managed=True)
con = mindsdb_sdk.connect('http://<YOUR_INSTANCE_IP>', login='[email protected]', password='-', is_managed=True)
```

## Basic usage
Expand All @@ -43,7 +43,7 @@ Once connected to the server, you can perform various operations. Here are some

```python
# Get a list of databases
databases = server.list_databases()
databases = con.databases.list()

# Get a specific database
database = databases[0] # Database type object
Expand All @@ -53,24 +53,27 @@ query = database.query('select * from table1')
print(query.fetch())

# Create a table
table = database.create_table('table2', query)
table = database.tables.create('table2', query)

# Get a project
project = server.get_project('proj')
project = con.projects.proj

# or use mindsdb project
project = con

# Perform an SQL query within a project
query = project.query('select * from database.table join model1')

# Create a view
view = project.create_view('view1', query=query)
view = project.views.create('view1', query=query)

# Get a list of views
views = project.list_views()
views = project.views.list()
view = views[0]
df = view.fetch()

# Get a list of models
models = project.list_models()
models = project.models.list()
model = models[0]

# Use a model for prediction
Expand All @@ -83,7 +86,7 @@ timeseries_options = {
'window': 5,
'horizon': 1
}
model = project.create_model(
model = project.models.create(
'rentals_model',
predict='price',
query=query,
Expand All @@ -98,6 +101,10 @@ You can find more examples in this [Google colab notebook](
https://colab.research.google.com/drive/1QouwAR3saFb9ffthrIs1LSH5COzyQa11#scrollTo=k6IbwsKRPQCR
)

## Examples

https://github.com/mindsdb/mindsdb_python_sdk/tree/staging/examples

## API Documentation

The API documentation for the MindsDB SDK can be found at https://mindsdb.github.io/mindsdb_python_sdk/. You can generate the API documentation locally by following these steps:
Expand Down
4 changes: 2 additions & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
requests
pandas == 1.3.5
mindsdb-sql >= 0.5.0, < 0.6.0
pandas >= 1.3.5
mindsdb-sql >= 0.7.0, < 0.8.0

sphinx
sphinx-rtd-theme
4 changes: 2 additions & 2 deletions docs/source/database.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Database
Databases
----------------------------

.. automodule:: mindsdb_sdk.database
.. automodule:: mindsdb_sdk.databases
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/source/handlers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Handlers
-------------------------

.. automodule:: mindsdb_sdk.handlers
:members:
:undoc-members:
:show-inheritance:
11 changes: 11 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ Base usage
query=query,
)

More

More examples
-----------

`<https://github.com/mindsdb/mindsdb_python_sdk/examples>`_

API documentation
=================
Expand All @@ -105,9 +110,15 @@ API documentation

server
database
handlers
ml_engines
project
model
tables
views
query
jobs


Indices and tables
------------------
Expand Down
7 changes: 7 additions & 0 deletions docs/source/jobs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Jobs
-------------------------

.. automodule:: mindsdb_sdk.jobs
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/source/ml_engines.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ML Engines
-------------------------

.. automodule:: mindsdb_sdk.ml_engines
:members:
:undoc-members:
:show-inheritance:
4 changes: 2 additions & 2 deletions docs/source/model.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Model
Models
-------------------------

.. automodule:: mindsdb_sdk.model
.. automodule:: mindsdb_sdk.models
:members:
:undoc-members:
:show-inheritance:
4 changes: 2 additions & 2 deletions docs/source/project.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Project
Projects
---------------------------

.. automodule:: mindsdb_sdk.project
.. automodule:: mindsdb_sdk.projects
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/source/tables.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Tables
-------------------------

.. automodule:: mindsdb_sdk.tables
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/source/views.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Views
-------------------------

.. automodule:: mindsdb_sdk.views
:members:
:undoc-members:
:show-inheritance:
37 changes: 37 additions & 0 deletions examples/home_rentals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import mindsdb_sdk

con = mindsdb_sdk.connect()

# connect to database
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
# because table with schema we are using .get
tbl = db.tables.get('demo_data.home_rentals')

# create model
model = con.models.create(
'home_rentals_model',
predict='rental_price',
query=tbl
)

# wait till training complete
model.wait_complete()

# make prediction for first 3 rows
result = model.predict(tbl.limit(3))



25 changes: 25 additions & 0 deletions examples/using_openai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import mindsdb_sdk

con = mindsdb_sdk.connect()

openai_handler = con.ml_handlers.openai

# create ml engine
openai = con.ml_engines.create(
'openai',
handler=openai_handler,
# handler='openai', # <-- another option to define handler
connection_data={'api_key': ''}
)

# create model
model = con.models.create(
'open1',
predict='answer',
engine=openai, # created ml engine
prompt_template='answer question: {{q}}'
)

# use model
model.predict({'q': 'size of the sun'})
43 changes: 43 additions & 0 deletions examples/working_with_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import mindsdb_sdk
import pandas as pd

con = mindsdb_sdk.connect()

# get user's database (connected to mindsdb as rental_db)
db = con.databases.rental_db

# get table
table1 = db.tables.house_sales


# ---- 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 from csv file
df = pd.read_csv('my_data.csv')
table3 = db.tables.create('my_table', df)


# ---- insert into table ----

# insert to table2 first 10 rows from table1
table2.insert(table1.limit(10))


# ---- update data in table ----

# get all rows with type=house from table1 and update values in table2 using key ('saledate', 'type', 'bedrooms')
table2.update(
table1.filter(type='house'),
on=['saledate', 'type', 'bedrooms']
)


# ---- delete rows from table ----

# delete all rows where bedrooms=2
table2.delete(bedrooms=2)


6 changes: 5 additions & 1 deletion mindsdb_sdk/connect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from mindsdb_sdk.server import Server

from mindsdb_sdk.connectors.rest_api import RestAPI


def connect(url: str = None, login: str = None, password: str = None, is_managed: bool = False) -> Server:
"""
Expand Down Expand Up @@ -39,4 +41,6 @@ def connect(url: str = None, login: str = None, password: str = None, is_managed
# is local
url = 'http://127.0.0.1:47334'

return Server(url=url, login=login, password=password, is_managed=is_managed)
api = RestAPI(url, login, password, is_managed)

return Server(api)
Loading