-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathlazy_fib_sample.py
More file actions
63 lines (49 loc) · 1.81 KB
/
lazy_fib_sample.py
File metadata and controls
63 lines (49 loc) · 1.81 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# pytest: ollama, e2e
import asyncio
from mellea.backends.ollama import OllamaModelBackend
from mellea.core import Backend, CBlock, Context, ModelOutputThunk
from mellea.stdlib.components import SimpleComponent
from mellea.stdlib.context import SimpleContext
backend = OllamaModelBackend("granite4:latest")
async def _fib_sample(
backend: Backend, ctx: Context, x: CBlock, y: CBlock
) -> ModelOutputThunk | None:
sc = SimpleComponent(
instruction="What is x+y? Respond with the number only.", x=x, y=y
)
answer_mot, _ = await backend.generate_from_context(action=sc, ctx=SimpleContext())
# This is a fundamental thing: it means computation must occur.
# We need to be able to read this off at c.g. construction time.
value = await answer_mot.avalue()
try:
int(value)
return answer_mot
except Exception:
return None
async def fib_sampling_version(
backend: Backend, ctx: Context, x: CBlock, y: CBlock
) -> ModelOutputThunk | None:
for i in range(5):
sample = await _fib_sample(backend, ctx, x, y)
if sample is not None:
return sample
else:
continue
return None
async def fib_sampling_version_main(backend: Backend, ctx: Context):
fibs: list[CBlock | ModelOutputThunk] = []
for i in range(20):
if i == 0 or i == 1:
fibs.append(CBlock(f"{i}"))
else:
mot = await fib_sampling_version(backend, ctx, fibs[i - 1], fibs[i - 2])
if mot is not None:
fibs.append(mot)
for x_i, x in enumerate(fibs):
match x:
case ModelOutputThunk():
n = await x.avalue()
print(n)
case CBlock():
print(x.value)
asyncio.run(fib_sampling_version_main(backend, SimpleContext()))