-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathsimple_example.py
More file actions
39 lines (28 loc) · 1.31 KB
/
simple_example.py
File metadata and controls
39 lines (28 loc) · 1.31 KB
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
# pytest: ollama, e2e
import asyncio
from mellea.backends.ollama import OllamaModelBackend
from mellea.core import Backend, CBlock, Context, ModelOutputThunk
from mellea.stdlib.context import SimpleContext
async def main(backend: Backend, ctx: Context):
"""In this example, we show how executing multiple MOTs in parallel should work."""
m_states = "Missouri", "Minnesota", "Montana", "Massachusetts"
poem_thunks = []
for state_name in m_states:
mot, ctx = await backend.generate_from_context(
CBlock(f"Write a poem about {state_name}"), ctx
)
poem_thunks.append(mot)
# Notice that what we have now is a list of ModelOutputThunks, none of which are computed.
for poem_thunk in poem_thunks:
assert isinstance(poem_thunk, ModelOutputThunk)
print(f"Computed: {poem_thunk.is_computed()}")
# Let's run all of these in parallel.
await asyncio.gather(*[c.avalue() for c in poem_thunks])
# Print out the final results, which are now computed.
for poem_thunk in poem_thunks:
print(f"Computed: {poem_thunk.is_computed()}")
# And let's print out the final results.
for poem_thunk in poem_thunks:
print(poem_thunk.value)
backend = OllamaModelBackend(model_id="granite4:latest")
asyncio.run(main(backend, SimpleContext()))