SDK Quickstart

Quickstart guide to upload data

Load a Data File to Pandas DataFrame

The Aito Python SDK uses Pandas DataFrame for multiple operations.

The example below shows how you can load a csv file into a DataFrame, please read the official pandas guide for further instructions. You can download an example data file here and run the code below:

import pandas as pd

reddit_df = pd.read_csv('reddit_sample.csv')

You can also use the DataFrameHandler to read data into pandas DataFrame

Infer a Table Schema

An Aito table schema describes how the table should be constructed and processed internally. You can read more about the Aito schema here

The Aito Python SDK includes a SchemaHandler that can infer an Aito table schema from a Pandas DataFrame. The example below assumes that you already have a DataFrame named reddit_df DataFrame.

from aito.utils.schema_handler import SchemaHandler
schema_handler = SchemaHandler()
reddit_schema = schema_handler.infer_table_schema_from_pandas_data_frame(reddit_df)

# Feel free to change the schema as you see fit. For example:
# Change `label` type to `String` instead of `Int`
reddit_schema['columns']['label']['type'] = 'String'
# Use a different analyzer
reddit_schema['columns']['comments']['analyzer'] = {
  "type": "token-ngram",
  "source": { "type": "language", "language": "english" },
  "minGram": 1,
  "maxGram": 3,
  "tokenSeparator": " "
}

Create Aito Schema

You can create Aito schema with an AitoClient.

Your AitoClient must be set up with the READ-WRITE API key

from aito.utils.aito_client import AitoClient
aito_client = AitoClient(instance_name="your_aito_instance_name", api_key="your_rw_api_key")
  • Create a table schema

    # Aito table schema example
    table_schema = {
      'type': 'table',
      'columns': {
        'label': {'nullable': False, 'type': 'Int'},
        'comment': {'nullable': False, 'type': 'Text', 'analyzer': 'en'},
        'author': {'nullable': False, 'type': 'Text', 'analyzer': 'en'},
        'subreddit': {'nullable': False, 'type': 'String'},
        'score': {'nullable': False, 'type': 'Int'},
        'ups': {'nullable': False, 'type': 'Int'},
        'downs': {'nullable': False, 'type': 'Int'},
        'date': {'nullable': False, 'type': 'String'},
        'created_utc': {'nullable': False, 'type': 'Text'},
        'parent_comment': {'nullable': False, 'type': 'Text','analyzer': 'en'
        }
      }
    }
    
    aito_client.put_table_schema(table_name='reddit', table_schema=table_schema)
    
    # Check your table schema in Aito
    aito_client.get_table_schema(table_name=table_name)
    
  • Create a database schema

    # Aito DB schema example
    database_schema = {
      'schema': {
        'reddit': {
          'type': 'table',
          'columns': {
            'label': {'nullable': False, 'type': 'Int'},
            'comment': {'nullable': False, 'type': 'Text', 'analyzer': 'en'},
            'author': {'nullable': False, 'type': 'Text', 'analyzer': 'en'},
            'subreddit': {'nullable': False, 'type': 'String'},
            'score': {'nullable': False, 'type': 'Int'},
            'ups': {'nullable': False, 'type': 'Int'},
            'downs': {'nullable': False, 'type': 'Int'},
            'date': {'nullable': False, 'type': 'String'},
            'created_utc': {'nullable': False, 'type': 'Text'},
            'parent_comment': {'nullable': False, 'type': 'Text','analyzer': 'en'
            }
          }
        }
      }
    }
    aito_client.put_database_schema(database_schema=database_schema)
    
    # Check your DB schema in Aito
    aito_client.get_database_schema()
    

Upload Data

You can create an Aito schema with the AitoClient.

Your AitoClient must be set up with the READ-WRITE API key

from aito.utils.aito_client import AitoClient
aito_client = AitoClient(instance_name="your_aito_instance_name", api_key="your_rw_api_key")
  • Upload a list of table entries

    entries = [
      {
        'label': 0,
        'comment': 'it was.',
        'author': 'renden123',
        'subreddit': 'CFB',
        'score': 4,
        'ups': -1,
        'downs': -1,
        'date': '2016-11',
        'created_utc': '2016-11-22 21:32:03',
        'parent_comment': "Wasn't it 2010?"
      }
    ]
    aito_client.populate_table_entries(table_name='reddit', entries=entries)
    
  • Upload a Pandas DataFrame

    # convert DataFrame to list of entries
    entries = df.to_dict(orient="records")
    aito_client.populate_table_entries(table_name='reddit', entries=entries)
    
  • Upload a gzipped ndjson file

    with file_path.open(mode='rb') as in_f:
      aito_client.populate_table_by_file_upload(table_name='table_name', binary_file_object=in_f)
    

Delete data

You can delete data with the AitoClient.

Your AitoClient must be set up with the READ-WRITE API key

Execute Queries

You can execute queries with the AitoClient.

Your AitoClient can be set up with the READ-ONLY API key

Query a Table Entries

# query the first 10 entries of a table
aito_client.query_table_entries(table_name='table_name')

Custom Query

# make a prediction
response = aito_client.request(
  req_method='POST',
  endpoint='/api/v1/_predict',
  query={
    'from': 'invoice',
    'where': {
      'description': 'a very long invoice description'
    },
    'predict': 'sales_rep'
  }
)

Executing multiple queries asynchronously

# predict with different descriptions

descriptions = ['first description', 'second description', 'third description']

responses = aito_client.async_requests(
  methods=['POST'] * len(descriptions),
  endpoints=['/api/v1/_predict'] * len(descriptions),
  queries=[
    {
      'from': 'invoice',
      'where': {
        'description': desc
      },
      'predict': 'sales_rep'
    }
    for desc in descriptions
  ]
)