Skip to content

Commit b7179f1

Browse files
committed
docs(examples): Better minimal example
Branch: main Signed-off-by: Gabe Goodhart <[email protected]>
1 parent 3c5303e commit b7179f1

File tree

3 files changed

+77
-48
lines changed

3 files changed

+77
-48
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ The key to the functionality of `groupings` is the ability to store a given grou
8383

8484
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.
8585

86-
## Minimal Example
86+
## Examples
8787

88+
You can find all the examples in [./examples](./examples/). Here's the simplest "Hello World" to get you started:
8889

90+
```py
91+
from caikit.interfaces.common.data_model import StrSequence
92+
from caikit_compose import MQ_FACTORY, Message
93+
94+
mq = MQ_FACTORY.construct({"type":"LOCAL"})
95+
mq.create_topic("input")
96+
97+
def greet(msg: Message):
98+
for name in msg.unwrapped.values:
99+
print(f"Hello {name}!")
100+
101+
mq.subscribe("input", "", greet)
102+
103+
while True:
104+
x = input("X: ")
105+
mq.publish("input", Message.from_data(StrSequence(x.split(","))))
106+
```

examples/minimal.py

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,11 @@
1-
"""
2-
Minimal example of multiple listeners on the message queue
3-
"""
4-
5-
# Standard
6-
from functools import partial
7-
import operator
8-
9-
# First Party
10-
from caikit.core import DataObjectBase, dataobject
11-
12-
# Local
1+
from caikit.interfaces.common.data_model import StrSequence
132
from caikit_compose import MQ_FACTORY, Message
14-
15-
16-
@dataobject
17-
class Number(DataObjectBase):
18-
val: float
19-
20-
21-
def operation(mq, topic, c, oper, msg):
22-
if op := getattr(operator, oper, None):
23-
mq.publish(
24-
topic,
25-
Message.from_data(
26-
Number(op(msg.unwrapped.val, c)),
27-
data_id=msg.header.data_id,
28-
metadata={"oper": oper},
29-
),
30-
)
31-
32-
33-
def report(msg):
34-
oper = msg.nested_get("metadata.oper")
35-
print(f"RESULT {oper}: {msg.unwrapped.val}")
36-
37-
38-
if __name__ == "__main__":
39-
mq = MQ_FACTORY.construct({"type": "LOCAL", "config": {"threads": 0}})
40-
mq.create_topic("input")
41-
mq.create_topic("output")
42-
c_val = float(input("C Val: "))
43-
for op in ["mul", "truediv", "add", "sub"]:
44-
mq.subscribe("input", op, partial(operation, mq, "output", c_val, op))
45-
mq.subscribe("output", "", report)
46-
while True:
47-
x = float(input("X: "))
48-
mq.publish("input", Message.from_data(Number(x)))
3+
mq = MQ_FACTORY.construct({"type":"LOCAL"})
4+
mq.create_topic("input")
5+
def greet(msg: Message):
6+
for name in msg.unwrapped.values:
7+
print(f"Hello {name}!")
8+
mq.subscribe("input", "", greet)
9+
while True:
10+
x = input("X: ")
11+
mq.publish("input", Message.from_data(StrSequence(x.split(","))))

examples/multi_listener.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Minimal example of multiple listeners on the message queue
3+
"""
4+
5+
# Standard
6+
from functools import partial
7+
import operator
8+
9+
# First Party
10+
from caikit.core import DataObjectBase, dataobject
11+
12+
# Local
13+
from caikit_compose import MQ_FACTORY, Message
14+
15+
16+
@dataobject
17+
class Number(DataObjectBase):
18+
val: float
19+
20+
21+
def operation(mq, topic, c, oper, msg):
22+
if op := getattr(operator, oper, None):
23+
mq.publish(
24+
topic,
25+
Message.from_data(
26+
Number(op(msg.unwrapped.val, c)),
27+
data_id=msg.header.data_id,
28+
metadata={"oper": oper},
29+
),
30+
)
31+
32+
33+
def report(msg):
34+
oper = msg.nested_get("metadata.oper")
35+
print(f"RESULT {oper}: {msg.unwrapped.val}")
36+
37+
38+
if __name__ == "__main__":
39+
mq = MQ_FACTORY.construct({"type": "LOCAL", "config": {"threads": 0}})
40+
mq.create_topic("input")
41+
mq.create_topic("output")
42+
c_val = float(input("C Val: "))
43+
for op in ["mul", "truediv", "add", "sub"]:
44+
mq.subscribe("input", op, partial(operation, mq, "output", c_val, op))
45+
mq.subscribe("output", "", report)
46+
while True:
47+
x = float(input("X: "))
48+
mq.publish("input", Message.from_data(Number(x)))

0 commit comments

Comments
 (0)