Metadata-Version: 2.1
Name: ai-server-sdk
Version: 0.0.12
Summary: Utility package to connect to AI Server instances.
Author: Thomas Trankle
License: MIT
Description-Content-Type: text/markdown
Requires-Dist: requests

# **AI Server**

*ai-server-sdk* is a python client SDK to connect to the AI Server

## Using this package you can:

 - Inference with Models you have acces to within the server
 - Create Pandas DataFrame from Databases connections
 - Pull Storage objects
 - Run pixel and get the direct output or full json response.
 - Pull data products from an existing insight using REST API.

## **Install**

    pip install ai-server-sdk


## **Usage**

To interract with an ai-server instance, import the `ai_server` package and connect via RESTServer.

*Note*: secret and access keys are required


### Setup
```python
>>> import ai_server

# define access keys
>>> loginKeys = {"secretKey":"<your_secret_key>","accessKey":"<your_access_key>"}

# create connection object by passing in the secret key, access key and base url for the api
>>> server_connection = ai_server.RESTServer(access_key=loginKeys['accessKey'], secret_key=loginKeys['secretKey'], base='<Your deployed server Monolith URL>')
```

### Inference with different Model Engines
```python
# define a question and grab the engine id from the server
>>> question = 'What is the capital of France?'
>>> engine_id = "2c6de0ff-62e0-4dd0-8380-782ac4d40245"

### Option 1 - Use ModelEngine directly
>>> model = ai_server.ModelEngine(engine_id = engine_id, insight_id = server_connection.cur_insight)
>>> model.ask(question = question)
[{'response': 'The capital of France is Paris.',
  'messageId': '0a80c2ce-76f9-4466-b2a2-8455e4cab34a',
  'roomId': '28261853-0e41-49b0-8a50-df34e8c62a19'}]

### Option 2 - Use the Driver class
>>> driver = ai_server.Driver(insight_id = server_connection.cur_insight)
>>> driver.run_model(question = question, engine_id = engine_id)
['The capital of France is Paris.']
```

### Connect to Databases and execute create, read, and delete operations

#### Run the passed string query against the engine.  The query passed must be in the structure that the specific engine implementation.
```python
# Create an relation to database based on the engine identifier
>>> engine_id = "4a1f9466-4e6d-49cd-894d-7d22182344cd"
>>> database = ai_server.DatabaseEngine(engine_id = engine_id, insight_id=a.cur_insight)
>>> database.execQuery(query='SELECT PATIENT, HEIGHT, WEIGHT FROM diab LIMIT 4')
```
|    |   PATIENT |   HEIGHT |   WEIGHT |
|---:|----------:|---------:|---------:|
|  0 |     20337 |       64 |      114 |
|  1 |      3750 |       64 |      161 |
|  2 |     40785 |       67 |      187 |
|  3 |     12778 |       72 |      145 |


execQuery commands can also be run through the Driver class
```python
### Use the Driver class
>>> driver = ai_server.Driver(insight_id = server_connection.cur_insight)
>>> driver.run_database(query = 'SELECT PATIENT, HEIGHT, WEIGHT FROM diab LIMIT 4', engine_id = engine_id)
```
|    |   PATIENT |   HEIGHT |   WEIGHT |
|---:|----------:|---------:|---------:|
|  0 |     20337 |       64 |      114 |
|  1 |      3750 |       64 |      161 |
|  2 |     40785 |       67 |      187 |
|  3 |     12778 |       72 |      145 |

#### Run the passed string query against the engine as an insert query. Query must be in the structure that the specific engine implementation
```python
>>> database.insertData(query = 'INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...)')
```

#### Run a delete query on the database
```python
>>> database.removeData(query='DELETE FROM diab WHERE age=19;')
```

### Using REST API to pull data product from an Insight
```python
# define the Project ID
>>> projectId = '30991037-1e73-49f5-99d3-f28210e6b95c'

# define the Insight ID
>>> inishgtId = '26b373b3-cd52-452c-a987-0adb8817bf73'

# define the SQL for the data product you want to query within the insight
>>> sql = 'select * FROM DATA_PRODUCT_123'

# if you dont provide one of the following, it will ask you to provide it via prompt
>>> diabetes_df = server_connection.import_data_product(project_id = projectId, insight_id = inishgtId, sql = sql)
>>> diabetes_df.head()
```
|    |   AGE |   PATIENT |   WEIGHT |
|---:|------:|----------:|---------:|
|  0 |    19 |      4823 |      119 |
|  1 |    19 |     17790 |      135 |
|  2 |    20 |      1041 |      159 |
|  3 |    20 |      2763 |      274 |
|  4 |    20 |      3750 |      161 |


### Get the output or JSON response of any pixel
```python
# run the pixel and get the output
>>> server_connection.run_pixel('1+1')
2

# run the pixel and get the entire json response
>>> server_connection.run_pixel('1+1', full_response=True)
{'insightID': '8b419eaf-df7d-4a7f-869e-8d7d59bbfde8',
 'sessionTimeRemaining': '7196',
 'pixelReturn': [{'pixelId': '3',
   'pixelExpression': '1 + 1 ;',
   'isMeta': False,
   'output': 2,
   'operationType': ['OPERATION']}]}
```

---
