-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathm_serve_example_streaming.py
More file actions
43 lines (34 loc) · 1.24 KB
/
m_serve_example_streaming.py
File metadata and controls
43 lines (34 loc) · 1.24 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
# pytest: ollama, e2e
"""Example to run m serve with true streaming support."""
from typing import Any
import mellea
from cli.serve.models import ChatMessage
from mellea.backends.model_options import ModelOption
from mellea.core import ComputedModelOutputThunk, ModelOutputThunk
from mellea.stdlib.context import SimpleContext
session = mellea.start_session(ctx=SimpleContext())
async def serve(
input: list[ChatMessage],
requirements: list[str] | None = None,
model_options: dict[str, Any] | None = None,
) -> ModelOutputThunk | ComputedModelOutputThunk:
"""Support both normal and streaming responses from the same example.
Returns a computed result for non-streaming requests and an uncomputed thunk
for streaming requests.
"""
del requirements
message = input[-1].content or ""
is_streaming = bool((model_options or {}).get(ModelOption.STREAM, False))
if is_streaming:
return await session.ainstruct(
description=message,
strategy=None,
model_options=model_options,
await_result=False,
)
return await session.ainstruct(
description=message,
strategy=None,
model_options=model_options,
await_result=True,
)