Package: smarterai
Smarter API for PythonThis API allows communication between any python based Component on thesmarter.aiplatform.User InstallationThe latest released version are available at the Python Package Index (PyPI).To install using pip:pipinstallsmarteraiUsageFor starters an account needs to be created at our platform. So visit our website and create an account
atsmarter.ai.Then in order for the python code to be accessible for thesmarter.aiplatform, follow
these steps:Visit theStudioStart a newnew ExperimentChose a Full Pipeline Template.Follow the wizard.Go to Build -> Experiment Editor.FromContainer: EnvironmentDrag & drop a python component.FromBlank: Starter TemplatesDrag & 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:fromtypingimportOptionalfromsmarteraiimportSmarterApp,SmarterMessage,SmarterApiA class calledSmarterComponent.SmarterComponentshould inherit fromSmarterApp:classSmarterComponent(SmarterApp):The class should have a methodinvokewith the following signature:definvoke(self,port:str,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:Example 1This is the basic interface for a python based component.fromtypingimportOptionalfromsmarteraiimportSmarterApp,SmarterMessage,SmarterApiclassSmarterComponent(SmarterApp):definvoke(self,port:str,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:print(f"Received the message '{message}' on port '{port}'")returnExample 2If your component needs initializing/booting before it starts running. Then a methodbootneeds to be defined.fromtypingimportOptionalfromsmarteraiimportSmarterApp,SmarterMessage,SmarterApiclassSmarterComponent(SmarterApp):def__init__(self):self.port_fn_mapper={'boot':self.boot}defboot(self,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:# Write code herereturndefinvoke(self,port:str,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:print(f"Received the message '{message}' on port '{port}'")ifportinself.port_fn_mapper:self.port_fn_mapper[port](message,smarter_api)returnExample 3If your component needs to send messages to other components, then you can use smarter_api.fromtypingimportOptionalfromsmarteraiimportSmarterApp,SmarterMessage,SmarterApiclassSmarterComponent(SmarterApp):def__init__(self):self.port_fn_mapper={'boot':self.boot,'start':self.start}defboot(self,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:# Write code herereturndefstart(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)definvoke(self,port:str,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:print(f"Received the message '{message}' on port '{port}'")ifportinself.port_fn_mapper:self.port_fn_mapper[port](message,smarter_api)returnExample 4If your component needs to set data to front-end patterns.fromtypingimportOptionalfromsmarteraiimportSmarterApp,SmarterMessage,SmarterApiclassSmarterComponent(SmarterApp):def__init__(self):self.port_fn_mapper={'boot':self.boot,'start':self.start}defboot(self,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:# Write code herereturndefstart(self,message:SmarterMessage,smarter_api:SmarterApi)->None:pattern="text_field"data='some value'smarter_api.set_data(pattern=pattern,data=data)definvoke(self,port:str,message:SmarterMessage,smarter_api:SmarterApi)->Optional[SmarterMessage]:print(f"Received the message '{message}' on port '{port}'")ifportinself.port_fn_mapper:self.port_fn_mapper[port](message,smarter_api)returnCreditsAuthored by Nevine Soliman and Carlos Medina (smarter.ai - All rights reserved)