Skip to content

Commit 889aa90

Browse files
committed
fix: pr comments and add simpler example
1 parent 9768fd9 commit 889aa90

7 files changed

Lines changed: 9583 additions & 6 deletions

File tree

cli/fix/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""CLI for fixing async calls after top-level ainstruct, aquery, and aact contract change.."""
22

3-
from enum import Enum
3+
from enum import StrEnum
44

55
import typer
66

77
fix_app = typer.Typer(name="fix", help="Fix code for API changes.")
88

99

10-
class FixMode(str, Enum):
10+
class FixMode(StrEnum):
1111
ADD_AWAIT_RESULT = "add-await-result"
1212
ADD_STREAM_LOOP = "add-stream-loop"
1313

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
@@ -627,12 +627,12 @@ def __init__(self, thunk: ModelOutputThunk[S]) -> None:
627627
raise ValueError("ComputedModelOutputThunk requires a non-None value.")
628628

629629
async def avalue(self) -> str:
630-
"""Return the value of the thunk. Use .value() instead."""
630+
"""Return the value of the thunk. Use .value instead."""
631631
assert self.value is not None, "ComputedModelOutputThunk value cannot be None"
632632
return self.value
633633

634634
async def astream(self) -> str:
635-
"""Cannot astream from ComputedModelOutputThunks. Use .value() instead."""
635+
"""Cannot astream from ComputedModelOutputThunks. Use .value instead."""
636636
raise RuntimeError(
637637
"Cannot stream from a ComputedModelOutputThunk. "
638638
"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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ disable_error_code = [
315315
# -----------------------------
316316

317317
[tool.codespell]
318-
ignore-words-list = 'mellea,hashi,noo,Asai,asai,nd,mot,rouge,Rouge,Strat,Wight,Aer,aer,preemptable'
318+
ignore-words-list = 'mellea,hashi,noo,Asai,asai,nd,mot,rouge,Rouge,Strat,strat,Wight,Aer,aer,preemptable'
319319
check-filenames = true
320320
check-hidden = false
321321
regex = "(?<![a-z])[a-z'`]+|[A-Z][a-z'`]*|[a-z]+'[a-z]*|[a-z]+(?=[_-])|[a-z]+(?=[A-Z])|\\d+"

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)

uv.lock

Lines changed: 9539 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)