-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathm_serve_example_simple.py
More file actions
51 lines (40 loc) · 1.42 KB
/
m_serve_example_simple.py
File metadata and controls
51 lines (40 loc) · 1.42 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
# pytest: ollama, e2e
"""Example to run m serve."""
from typing import Any
import mellea
from cli.serve.models import ChatMessage
from mellea.core import ModelOutputThunk, Requirement, SamplingResult
from mellea.stdlib.context import ChatContext
from mellea.stdlib.requirements import simple_validate
from mellea.stdlib.sampling import RejectionSamplingStrategy
session = mellea.start_session(ctx=ChatContext())
def validate_hi_bob(email: str) -> bool:
return email.startswith("Hi Bob!")
def validate_email_len(email: str) -> bool:
return len(email) < 50
def serve(
input: list[ChatMessage],
requirements: list[str] | None = None,
model_options: None | dict = None,
) -> ModelOutputThunk | SamplingResult:
"""Takes a prompt as input and runs it through an M program."""
requirements = requirements if requirements else []
message = input[-1].content
reqs = [
Requirement(
"Keep this under 50 words",
validation_fn=simple_validate(validate_email_len),
),
Requirement(
"Add a 'Hi Bob!' at the top of the output",
validation_fn=simple_validate(validate_hi_bob),
),
*requirements,
]
result = session.instruct(
description=message, # type: ignore
requirements=reqs, # type: ignore
strategy=RejectionSamplingStrategy(loop_budget=3),
model_options=model_options,
)
return result