smarter.ai Python API
Project description
Smarter API for Python
This API allows communication between any python based Component on the smarter.ai platform.
User Installation
The latest released version are available at the Python Package Index (PyPI).
To install using pip:
pip install smarterai
Usage
-
For starters an account needs to be created at our platform. So visit our website and create an account at smarter.ai.
-
Then in order for the python code to be accessible for the smarter.ai platform, follow these steps:
- Visit the Studio
- Start a new new Experiment
- Chose a Full Pipeline Template.
- Follow the wizard.
- Go to Build -> Experiment Editor.
- From Container: Environment Drag & drop a python component.
- From Blank: Starter Templates Drag & drop Python Code Template on top of the added Environment component.
- Double-click on the Python Code Template newly added Component.
- Edit/upload your code there.
-
You can then start building your code by copy-pasting the code found in the examples below.
-
The Python Component's interface needs to consist of the following:
- Imports:
from typing import Optional from smarterai import SmarterApp, SmarterMessage, SmarterApi
- A class called
SmarterComponent
. SmarterComponent
should inherit fromSmarterApp
:
class SmarterComponent(SmarterApp):
- The class should have a method
invoke
with the following signature:
def invoke(self, port: str, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
Example 1
This is the basic interface for a python based component.
from typing import Optional
from smarterai import SmarterApp, SmarterMessage, SmarterApi
class SmarterComponent(SmarterApp):
def invoke(self, port: str, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
print(f"Received the message '{message}' on port '{port}'")
return
Example 2
If your component needs initializing/booting before it starts running. Then a method boot
needs to be defined.
from typing import Optional
from smarterai import SmarterApp, SmarterMessage, SmarterApi
class SmarterComponent(SmarterApp):
def __init__(self):
self.port_fn_mapper = {'boot': self.boot}
def boot(self, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
# Write code here
return
def invoke(self, port: str, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
print(f"Received the message '{message}' on port '{port}'")
if port in self.port_fn_mapper:
self.port_fn_mapper[port](message, smarter_api)
return
Example 3
If your component needs to send messages to other components, then you can use smarter_api.
from typing import Optional
from smarterai import SmarterApp, SmarterMessage, SmarterApi
class SmarterComponent(SmarterApp):
def __init__(self):
self.port_fn_mapper = {'boot': self.boot, 'start': self.start}
def boot(self, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
# Write code here
return
def start(self, message: SmarterMessage, smarter_api: SmarterApi) -> None:
port = 'out'
user_id = message.get('userId')
new_message = SmarterMessage({'name': f'Smarter AI Welcomes {user_id}'})
smarter_api.send_message(message=new_message, port=port)
def invoke(self, port: str, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
print(f"Received the message '{message}' on port '{port}'")
if port in self.port_fn_mapper:
self.port_fn_mapper[port](message, smarter_api)
return
Example 4
If your component needs to set data to front-end patterns.
from typing import Optional
from smarterai import SmarterApp, SmarterMessage, SmarterApi
class SmarterComponent(SmarterApp):
def __init__(self):
self.port_fn_mapper = {'boot': self.boot, 'start': self.start}
def boot(self, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
# Write code here
return
def start(self, message: SmarterMessage, smarter_api: SmarterApi) -> None:
pattern = "text_field"
data = 'some value'
smarter_api.set_data(pattern=pattern, data=data)
def invoke(self, port: str, message: SmarterMessage, smarter_api: SmarterApi) -> Optional[SmarterMessage]:
print(f"Received the message '{message}' on port '{port}'")
if port in self.port_fn_mapper:
self.port_fn_mapper[port](message, smarter_api)
return
Credits
Authored by Nevine Soliman and Carlos Medina (smarter.ai - All rights reserved)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.