From b7179f13500043b554c7bcbfdd2a5db5da1d48e8 Mon Sep 17 00:00:00 2001 From: Gabe Goodhart Date: Mon, 9 Dec 2024 12:24:29 -0700 Subject: [PATCH] docs(examples): Better minimal example Branch: main Signed-off-by: Gabe Goodhart --- README.md | 20 ++++++++++++- examples/minimal.py | 57 +++++++------------------------------- examples/multi_listener.py | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 examples/multi_listener.py diff --git a/README.md b/README.md index 70bac10..80b5f6c 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,24 @@ The key to the functionality of `groupings` is the ability to store a given grou When subscribing an [actor](#actors) to topics on the [message queue](#message-queue), a `SubscriptionManager` is used to bind the `actor`'s inference function(s) to the appropriate groupings. It is the `SubscriptionManager` which takes care of adding messages to the `grouping` and dispatching the result to the actor if (and only if) a group completes based on the input message. -## Minimal Example +## Examples +You can find all the examples in [./examples](./examples/). Here's the simplest "Hello World" to get you started: +```py +from caikit.interfaces.common.data_model import StrSequence +from caikit_compose import MQ_FACTORY, Message + +mq = MQ_FACTORY.construct({"type":"LOCAL"}) +mq.create_topic("input") + +def greet(msg: Message): + for name in msg.unwrapped.values: + print(f"Hello {name}!") + +mq.subscribe("input", "", greet) + +while True: + x = input("X: ") + mq.publish("input", Message.from_data(StrSequence(x.split(",")))) +``` diff --git a/examples/minimal.py b/examples/minimal.py index b971a82..043c081 100644 --- a/examples/minimal.py +++ b/examples/minimal.py @@ -1,48 +1,11 @@ -""" -Minimal example of multiple listeners on the message queue -""" - -# Standard -from functools import partial -import operator - -# First Party -from caikit.core import DataObjectBase, dataobject - -# Local +from caikit.interfaces.common.data_model import StrSequence from caikit_compose import MQ_FACTORY, Message - - -@dataobject -class Number(DataObjectBase): - val: float - - -def operation(mq, topic, c, oper, msg): - if op := getattr(operator, oper, None): - mq.publish( - topic, - Message.from_data( - Number(op(msg.unwrapped.val, c)), - data_id=msg.header.data_id, - metadata={"oper": oper}, - ), - ) - - -def report(msg): - oper = msg.nested_get("metadata.oper") - print(f"RESULT {oper}: {msg.unwrapped.val}") - - -if __name__ == "__main__": - mq = MQ_FACTORY.construct({"type": "LOCAL", "config": {"threads": 0}}) - mq.create_topic("input") - mq.create_topic("output") - c_val = float(input("C Val: ")) - for op in ["mul", "truediv", "add", "sub"]: - mq.subscribe("input", op, partial(operation, mq, "output", c_val, op)) - mq.subscribe("output", "", report) - while True: - x = float(input("X: ")) - mq.publish("input", Message.from_data(Number(x))) +mq = MQ_FACTORY.construct({"type":"LOCAL"}) +mq.create_topic("input") +def greet(msg: Message): + for name in msg.unwrapped.values: + print(f"Hello {name}!") +mq.subscribe("input", "", greet) +while True: + x = input("X: ") + mq.publish("input", Message.from_data(StrSequence(x.split(",")))) diff --git a/examples/multi_listener.py b/examples/multi_listener.py new file mode 100644 index 0000000..b971a82 --- /dev/null +++ b/examples/multi_listener.py @@ -0,0 +1,48 @@ +""" +Minimal example of multiple listeners on the message queue +""" + +# Standard +from functools import partial +import operator + +# First Party +from caikit.core import DataObjectBase, dataobject + +# Local +from caikit_compose import MQ_FACTORY, Message + + +@dataobject +class Number(DataObjectBase): + val: float + + +def operation(mq, topic, c, oper, msg): + if op := getattr(operator, oper, None): + mq.publish( + topic, + Message.from_data( + Number(op(msg.unwrapped.val, c)), + data_id=msg.header.data_id, + metadata={"oper": oper}, + ), + ) + + +def report(msg): + oper = msg.nested_get("metadata.oper") + print(f"RESULT {oper}: {msg.unwrapped.val}") + + +if __name__ == "__main__": + mq = MQ_FACTORY.construct({"type": "LOCAL", "config": {"threads": 0}}) + mq.create_topic("input") + mq.create_topic("output") + c_val = float(input("C Val: ")) + for op in ["mul", "truediv", "add", "sub"]: + mq.subscribe("input", op, partial(operation, mq, "output", c_val, op)) + mq.subscribe("output", "", report) + while True: + x = float(input("X: ")) + mq.publish("input", Message.from_data(Number(x)))