Skip to content

Commit

Permalink
docs(examples): Better minimal example
Browse files Browse the repository at this point in the history
Branch: main

Signed-off-by: Gabe Goodhart <[email protected]>
  • Loading branch information
gabe-l-hart committed Dec 9, 2024
1 parent 3c5303e commit b7179f1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 48 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(","))))
```
57 changes: 10 additions & 47 deletions examples/minimal.py
Original file line number Diff line number Diff line change
@@ -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(","))))
48 changes: 48 additions & 0 deletions examples/multi_listener.py
Original file line number Diff line number Diff line change
@@ -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)))

0 comments on commit b7179f1

Please sign in to comment.