You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -55,7 +55,7 @@ and brittle prompts with structured, maintainable, robust, and efficient AI work
55
55
56
56
## Let's setup Mellea to work locally
57
57
58
-
1.Open up a terminal, and run the following commands:
58
+
Open up a terminal, and run the following commands:
59
59
```bash
60
60
python3.11 -m venv venv
61
61
source venv/bin/activate
@@ -66,138 +66,151 @@ pip install mellea
66
66
If you see something about the Rust compiler, please confirm you are using python3.11, or python3.12 anything above that has a Rust dependency.
67
67
68
68
1. Start python:
69
-
```bash
70
-
python
71
-
```
69
+
70
+
```bash
71
+
python
72
+
```
72
73
73
74
1. Run a simple Mellea session:
74
-
```python
75
-
import mellea
76
75
77
-
m = mellea.start_session()
78
-
print(m.chat("tell me some fun trivia about IBM and the early history of AI.").content)
79
-
```
80
-
You can either add this to a file like `main.py` or run it in the python REPL, if you get output
81
-
you are set up to dig deeper with Mellea.
76
+
```python
77
+
import mellea
78
+
79
+
m = mellea.start_session()
80
+
print(m.chat("tell me some fun trivia about IBM and the early history of AI.").content)
81
+
```
82
+
You can either add this to a file like `main.py` or run it in the python REPL, if you get output
83
+
you are set up to dig deeper with Mellea.
82
84
83
85
## Simple email examples
84
86
85
87
!!! note
86
88
The following work should be done via a text editor, there should be a couple installed on your laptop, if you aren't sure raise your hand and a helper will help you out.
87
89
88
90
1. Let's leverage Mellea to do some email generation for us, the first example is a simple example:
89
-
```python
90
-
import mellea
91
-
m = mellea.start_session()
92
91
93
-
email = m.instruct("Write an email inviting interns to an office party at 3:30pm.")
94
-
print(str(email))
95
-
```
92
+
```python
93
+
import mellea
94
+
m = mellea.start_session()
95
+
96
+
email = m.instruct("Write an email inviting interns to an office party at 3:30pm.")
97
+
print(str(email))
98
+
```
96
99
97
100
1. As you can see, it outputs a standard email with only a couple lines of code, lets expand on this:
"Write an email to {{name}} using the notes following: {{notes}}.",
109
+
user_variables={"name": name, "notes": notes},
110
+
)
111
+
return email.value # str(email) also works.
109
112
110
-
print(
111
-
write_email(
112
-
m,
113
-
"Olivia",
114
-
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
113
+
114
+
print(
115
+
write_email(
116
+
m,
117
+
"Olivia",
118
+
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
119
+
)
115
120
)
116
-
)
117
-
```
118
-
With this more advance example we now have the ability to customize the email to be more directed and
119
-
personalized for the recipient. But this is just a more programmatic prompt engineering, lets see where
120
-
Mellea really shines.
121
+
```
122
+
With this more advance example we now have the ability to customize the email to be more directed and
123
+
personalized for the recipient. But this is just a more programmatic prompt engineering, lets see where
124
+
Mellea really shines.
121
125
122
126
### Simple email with boundaries and requirements
123
127
124
128
1. The first step with the power of Mellea, is adding requirements to something like this email, take a look at this first
125
-
example:
126
-
```python
127
-
import mellea
128
-
m = mellea.start_session()
129
-
130
-
defwrite_email_with_requirements(
131
-
m: mellea.MelleaSession, name: str, notes: str
132
-
) -> str:
133
-
email = m.instruct(
134
-
"Write an email to {{name}} using the notes following: {{notes}}.",
135
-
requirements=[
136
-
"The email should have a salutation",
137
-
"Use only lower-case letters",
138
-
],
139
-
user_variables={"name": name, "notes": notes},
129
+
example:
130
+
131
+
```python
132
+
import mellea
133
+
m = mellea.start_session()
134
+
135
+
def write_email_with_requirements(
136
+
m: mellea.MelleaSession, name: str, notes: str
137
+
) -> str:
138
+
email = m.instruct(
139
+
"Write an email to {{name}} using the notes following: {{notes}}.",
140
+
requirements=[
141
+
"The email should have a salutation",
142
+
"Use only lower-case letters",
143
+
],
144
+
user_variables={"name": name, "notes": notes},
145
+
)
146
+
return str(email)
147
+
148
+
149
+
print(
150
+
write_email_with_requirements(
151
+
m,
152
+
name="Olivia",
153
+
notes="Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
154
+
)
140
155
)
141
-
returnstr(email)
156
+
```
142
157
158
+
As you can see with this output now, you force the Mellea framework to start checking itself to create what you need.
159
+
Imagine this possibility, now you can start making sure your LLMs only generate things that you want. Test this theory
160
+
by changing from "only lower-case" to "only upper-case" and see that it will follow your instructions.
143
161
144
-
print(
145
-
write_email_with_requirements(
146
-
m,
147
-
name="Olivia",
148
-
notes="Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
149
-
)
150
-
)
151
-
```
152
-
As you can see with this output now, you force the Mellea framework to start checking itself to create what you need.
153
-
Imagine this possibility, now you can start making sure your LLMs only generate things that you want. Test this theory
154
-
by changing from "only lower-case" to "only upper-case" and see that it will follow your instructions.
155
162
156
-
Pretty neat eh? Lets go even deeper.
163
+
Pretty neat eh? Lets go even deeper.
157
164
158
-
Let's create an email with some sampling and have Mellea find the best option for what we are looking for:
159
-
We add two requirements to the instruction which will be added to the model request.
160
-
But we don't check yet if these requirements are satisfied, we add a strategy for validating the requirements.
165
+
166
+
Let's create an email with some sampling and have Mellea find the best option for what we are looking for:
167
+
We add two requirements to the instruction which will be added to the model request.
168
+
But we don't check yet if these requirements are satisfied, we add a strategy for validating the requirements.
161
169
162
170
1. This sampling strategy (`RejectionSamplingStrategy()`) checks if all requirements are met and if any requirement fails, the sampling strategy will sample a new email from the LLM.
163
-
```python
164
-
import mellea
165
-
m = mellea.start_session()
166
171
167
-
from mellea.stdlib.sampling import RejectionSamplingStrategy
172
+
```python
173
+
import mellea
174
+
m = mellea.start_session()
168
175
176
+
from mellea.stdlib.sampling import RejectionSamplingStrategy
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
193
-
)
194
-
)
195
-
```
196
-
You might notice it fails with the above example, because the name "Olivia" has an upper-case letter in it. Remove the `"Use only lower-case letters",` line, and it should pass on the first re-run. This brings up some interesting opportunities, so make sure that the writing you expect is within the boundaries and it'll keep trying till it gets it right.
207
+
You might notice it fails with the above example, because the name "Olivia" has an upper-case letter in it. Remove the `"Use only lower-case letters",`
208
+
line, and it should pass on the first re-run. This brings up some interesting opportunities, so make sure that the writing you expect is within the
209
+
boundaries and it'll keep trying till it gets it right.
197
210
198
211
## Instruct Validate Repair
199
212
200
-
1.The first `instruct-validate-repair` pattern is as follows:
213
+
The first `instruct-validate-repair` pattern is as follows:
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery."))
242
+
print(
243
+
write_email(
244
+
m,
245
+
"Olivia",
246
+
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
247
+
)
248
+
)
231
249
```
232
250
233
251
Most of this should look familiar by now, but the `validation_fn` and `check` should be new.
0 commit comments