Skip to main content

smarter.ai Python API

Project description

made-with-python PyPi version

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:

    1. Visit the Studio
    2. Start a new new Experiment
    3. Chose a Full Pipeline Template.
    4. Follow the wizard.
    5. Go to Build -> Experiment Editor.
    6. From Container: Environment Drag & drop a python component.
    7. From Blank: Starter Templates Drag & drop Python Code Template on top of the added Environment component.
    8. Double-click on the Python Code Template newly added Component.
    9. 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:

    1. Import smarterai:
          from smarterai import *
      
    2. A class called SmarterComponent.
    3. SmarterComponent should inherit from SmarterPlugin:
          class SmarterComponent(SmarterPlugin):
      
    4. The class should have a method invoke with the following signature:
          def invoke(self, port: str, message: SmarterMessage, sender: SmarterSender) -> Optional[SmarterMessage]:
      

Example 1

This is the basic interface for a python based component.

from smarterai import SmarterPlugin, SmarterMessage, SmarterSender, Optional


class SmarterComponent(SmarterPlugin):
    def invoke(self, port: str, message: SmarterMessage, sender: SmarterSender) -> 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 smarterai import SmarterPlugin, SmarterMessage, SmarterSender, Optional


class SmarterComponent(SmarterPlugin):
    def __init__(self):
        self.port_fn_mapper = {'boot': self.boot}

    def boot(self, message: SmarterMessage, sender: SmarterSender) -> Optional[SmarterMessage]:
        # Write code here
        return

    def invoke(self, port: str, message: SmarterMessage, sender: SmarterSender) -> Optional[SmarterMessage]:
        print(f"Received the message '{message}' on port '{port}'")
        if port in self.port_fn_mapper:
            self.port_fn_mapper[port](message, sender)
        return

Example 3

If your component needs to send messages to other components, then you can use sender.

from smarterai import SmarterPlugin, SmarterMessage, SmarterSender, Optional


class SmarterComponent(SmarterPlugin):
    def __init__(self):
        self.port_fn_mapper = {'boot': self.boot, 'start': self.start}

    def boot(self, message: SmarterMessage, sender: SmarterSender) -> Optional[SmarterMessage]:
        # Write code here
        return

    def start(self, message: SmarterMessage, sender: SmarterSender):
        port = 'out'
        new_message = SmarterMessage({'name': 'Smarter AI'})
        sender.send_message(message=new_message, port=port)

    def invoke(self, port: str, message: SmarterMessage, sender: SmarterSender) -> Optional[SmarterMessage]:
        print(f"Received the message '{message}' on port '{port}'")
        if port in self.port_fn_mapper:
            self.port_fn_mapper[port](message, sender)
        return

Example 4

If your component needs to set data to front-end patterns.

from smarterai import SmarterPlugin, SmarterMessage, SmarterSender, Optional


class SmarterComponent(SmarterPlugin):
    def __init__(self):
        self.port_fn_mapper = {'boot': self.boot, 'start': self.start}

    def boot(self, message: SmarterMessage, sender: SmarterSender) -> Optional[SmarterMessage]:
        # Write code here
        return

    def start(self, message: SmarterMessage, sender: SmarterSender):
        pattern = 'text_field'
        data = 'some value'
        sender.set_data(pattern=pattern, data=data)

    def invoke(self, port: str, message: SmarterMessage, sender: SmarterSender) -> Optional[SmarterMessage]:
        print(f"Received the message '{message}' on port '{port}'")
        if port in self.port_fn_mapper:
            self.port_fn_mapper[port](message, sender)
        return

Credits

Authored by Nevine Soliman and Carlos Medina (smarter.ai - All rights reserved)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smarterai-0.4.0.tar.gz (6.9 kB view details)

Uploaded Source

File details

Details for the file smarterai-0.4.0.tar.gz.

File metadata

  • Download URL: smarterai-0.4.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.9

File hashes

Hashes for smarterai-0.4.0.tar.gz
Algorithm Hash digest
SHA256 18376d6fefcfc71c48aa7494bb8905d6b84e6eaa4adc53d2c6b687457aea5364
MD5 e5732f9d9e6f6ff7ee30a9e52cca8b84
BLAKE2b-256 ff04271702088fd4dbd3df6084df19624095db2d687e9d95da5818d40918b3d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page