-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathmatchers.py
45 lines (33 loc) · 1.46 KB
/
matchers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import asyncio
import re
import sys
import traceback
from beeai_framework.adapters.ollama import OllamaChatModel
from beeai_framework.backend import ChatModel
from beeai_framework.emitter import Emitter
from beeai_framework.errors import FrameworkError
async def main() -> None:
emitter = Emitter.root().child(namespace=["app"])
model = OllamaChatModel()
# Match events by a concrete name (strictly typed)
emitter.on("update", lambda data, event: print(data, ": on update"))
# Match all events emitted directly on the instance (not nested)
emitter.match("*", lambda data, event: print(data, ": match all instance"))
# Match all events (included nested)
cleanup = Emitter.root().match("*.*", lambda data, event: print(data, ": match all nested"))
# Match events by providing a filter function
model.emitter.match(
lambda event: isinstance(event.creator, ChatModel), lambda data, event: print(data, ": match ChatModel")
)
# Match events by regex
emitter.match(re.compile(r"watsonx"), lambda data, event: print(data, ": match regex"))
await emitter.emit("update", "update")
await Emitter.root().emit("root", "root")
await model.emitter.emit("model", "model")
cleanup() # You can remove a listener from an emitter by calling the cleanup function it returns
if __name__ == "__main__":
try:
asyncio.run(main())
except FrameworkError as e:
traceback.print_exc()
sys.exit(e.explain())