Skip to content

Commit 25dfae7

Browse files
committed
fix: pr comments and add simpler example
1 parent 7cfb1ce commit 25dfae7

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# pytest: ollama, qualitative, llm
2+
3+
"""Example of how to use async with lazy compute and streaming."""
4+
5+
import asyncio
6+
7+
from mellea.backends.model_options import ModelOption
8+
from mellea.core.base import ModelOutputThunk
9+
from mellea.stdlib.session import start_session
10+
11+
# Create a regular session. Works with functional interface as well.
12+
m = start_session()
13+
14+
15+
async def main():
16+
response: ModelOutputThunk[str] = await m.ainstruct(
17+
"Say 'We're Streaming Now!' and then add a fun fact!",
18+
strategy=None, # Cannot perform lazy compute / top level streaming if using a strategy.
19+
model_options={
20+
ModelOption.STREAM: True # Set streaming to True for top level streaming.
21+
},
22+
# await_result=True # Set await_result to True to prevent lazy compute / top-level streaming.
23+
)
24+
25+
# We can stream the response as it happens.
26+
while not response.is_computed():
27+
print(await response.astream())
28+
29+
30+
asyncio.run(main())

mellea/core/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,12 @@ def __init__(self, thunk: ModelOutputThunk[S]) -> None:
594594
raise ValueError("ComputedModelOutputThunk requires a non-None value.")
595595

596596
async def avalue(self) -> str:
597-
"""Return the value of the thunk. Use .value() instead."""
597+
"""Return the value of the thunk. Use .value instead."""
598598
assert self.value is not None, "ComputedModelOutputThunk value cannot be None"
599599
return self.value
600600

601601
async def astream(self) -> str:
602-
"""Cannot astream from ComputedModelOutputThunks. Use .value() instead."""
602+
"""Cannot astream from ComputedModelOutputThunks. Use .value instead."""
603603
raise RuntimeError(
604604
"Cannot stream from a ComputedModelOutputThunk. "
605605
"This thunk is already fully computed and does not support streaming."

mellea/stdlib/functional.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ def act(
113113
) # type: ignore[call-overload]
114114
)
115115

116+
computed = False
117+
if isinstance(out, SamplingResult):
118+
computed = out.result.is_computed()
119+
else:
120+
mot, _ = out
121+
computed = mot.is_computed()
122+
assert computed, "Synchronous functions must return a computed result."
123+
116124
return out
117125

118126

test/core/test_streaming_sync_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_await_result_false_with_sampling_still_computes():
107107

108108

109109
def test_default_behavior_unchanged():
110-
"""Test that default behavior (await_result=False) returns uncomputed thunk."""
110+
"""Test that sync functions always return computed thunks, even without explicit await_result."""
111111
with start_session() as session:
112112
# Default behavior should return uncomputed thunk for streaming
113113
result = session.instruct("Say 'hello'", strategy=None)

0 commit comments

Comments
 (0)