-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathsimple_email.py
More file actions
84 lines (66 loc) · 2.45 KB
/
simple_email.py
File metadata and controls
84 lines (66 loc) · 2.45 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# pytest: ollama, e2e
import mellea
# INFO: this line will download IBM's Granite 4 Micro 3B model.
m = mellea.start_session()
print("Basic email:")
email = m.instruct("Write an email inviting interns to an office party at 3:30pm.")
print(str(email))
print("Function with user variables:")
def write_email(m: mellea.MelleaSession, name: str, notes: str) -> str:
email = m.instruct(
"Write an email to {{name}} using the notes following: {{notes}}.",
user_variables={"name": name, "notes": notes},
)
return str(email.value) # str(email) also works.
print(
write_email(
m,
"Olivia",
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
)
)
print("Email with requirements:")
def write_email_with_requirements(
m: mellea.MelleaSession, name: str, notes: str
) -> str:
email = m.instruct(
"Write an email to {{name}} using the notes following: {{notes}}.",
requirements=[
"The email should have a salutation",
"Use only lower-case letters",
],
user_variables={"name": name, "notes": notes},
)
return str(email)
print(
write_email_with_requirements(
m,
name="Olivia",
notes="Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
)
)
print("Email with rejection sampling:")
from mellea.stdlib.sampling import RejectionSamplingStrategy
def write_email_with_strategy(m: mellea.MelleaSession, name: str, notes: str) -> str:
email_candidate = m.instruct(
"Write an email to {{name}} using the notes following: {{notes}}.",
requirements=[
"The email should have a salutation",
"Use only lower-case letters",
],
strategy=RejectionSamplingStrategy(loop_budget=5),
user_variables={"name": name, "notes": notes},
return_sampling_results=True,
)
if email_candidate.success:
return str(email_candidate.result)
else:
print("Expect sub-par result.")
return str(email_candidate.sample_generations[0].value)
print(
write_email_with_strategy(
m,
"Olivia",
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
)
)