Skip to content

Commit d41eef3

Browse files
authored
Merge pull request #35 from yhwang/update-the-last-mellea-code-section
fix formatting issue and last code snippet of lab 7
2 parents 4444c85 + d0e7dcd commit d41eef3

File tree

2 files changed

+120
-101
lines changed

2 files changed

+120
-101
lines changed

docs/lab-7/README.md

Lines changed: 116 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ and brittle prompts with structured, maintainable, robust, and efficient AI work
5555

5656
## Let's setup Mellea to work locally
5757

58-
1. Open up a terminal, and run the following commands:
58+
Open up a terminal, and run the following commands:
5959
```bash
6060
python3.11 -m venv venv
6161
source venv/bin/activate
@@ -66,138 +66,151 @@ pip install mellea
6666
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.
6767

6868
1. Start python:
69-
```bash
70-
python
71-
```
69+
70+
```bash
71+
python
72+
```
7273

7374
1. Run a simple Mellea session:
74-
```python
75-
import mellea
7675

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.
8284

8385
## Simple email examples
8486

8587
!!! note
8688
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.
8789
8890
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()
9291

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+
```
9699

97100
1. As you can see, it outputs a standard email with only a couple lines of code, lets expand on this:
98-
```python
99-
import mellea
100-
m = mellea.start_session()
101101

102-
def write_email(m: mellea.MelleaSession, name: str, notes: str) -> str:
103-
email = m.instruct(
104-
"Write an email to {{name}} using the notes following: {{notes}}.",
105-
user_variables={"name": name, "notes": notes},
106-
)
107-
return email.value # str(email) also works.
102+
```python
103+
import mellea
104+
m = mellea.start_session()
108105
106+
def write_email(m: mellea.MelleaSession, name: str, notes: str) -> str:
107+
email = m.instruct(
108+
"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.
109112
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+
)
115120
)
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.
121125

122126
### Simple email with boundaries and requirements
123127

124128
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-
def write_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+
)
140155
)
141-
return str(email)
156+
```
142157

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.
143161

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.
155162

156-
Pretty neat eh? Lets go even deeper.
163+
Pretty neat eh? Lets go even deeper.
157164

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.
161169

162170
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()
166171

167-
from mellea.stdlib.sampling import RejectionSamplingStrategy
172+
```python
173+
import mellea
174+
m = mellea.start_session()
168175
176+
from mellea.stdlib.sampling import RejectionSamplingStrategy
169177
170-
def write_email_with_strategy(m: mellea.MelleaSession, name: str, notes: str) -> str:
171-
email_candidate = m.instruct(
172-
"Write an email to {{name}} using the notes following: {{notes}}.",
173-
requirements=[
174-
"The email should have a salutation",
175-
"Use only lower-case letters",
176-
],
177-
strategy=RejectionSamplingStrategy(loop_budget=5),
178-
user_variables={"name": name, "notes": notes},
179-
return_sampling_results=True,
178+
179+
def write_email_with_strategy(m: mellea.MelleaSession, name: str, notes: str) -> str:
180+
email_candidate = m.instruct(
181+
"Write an email to {{name}} using the notes following: {{notes}}.",
182+
requirements=[
183+
"The email should have a salutation",
184+
"Use only lower-case letters",
185+
],
186+
strategy=RejectionSamplingStrategy(loop_budget=5),
187+
user_variables={"name": name, "notes": notes},
188+
return_sampling_results=True,
189+
)
190+
if email_candidate.success:
191+
return str(email_candidate.result)
192+
else:
193+
print("Expect sub-par result.")
194+
return email_candidate.sample_generations[0].value
195+
196+
197+
print(
198+
write_email_with_strategy(
199+
m,
200+
"Olivia",
201+
"Olivia helped the lab over the last few weeks by organizing intern events, advertising the speaker series, and handling issues with snack delivery.",
202+
)
180203
)
181-
if email_candidate.success:
182-
return str(email_candidate.result)
183-
else:
184-
print("Expect sub-par result.")
185-
return email_candidate.sample_generations[0].value
186204
205+
```
187206
188-
print(
189-
write_email_with_strategy(
190-
m,
191-
"Olivia",
192-
"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.
197210
198211
## Instruct Validate Repair
199212
200-
1. The first `instruct-validate-repair` pattern is as follows:
213+
The first `instruct-validate-repair` pattern is as follows:
201214
202215
```python
203216
import mellea
@@ -226,8 +239,13 @@ def write_email(m: mellea.MelleaSession, name: str, notes: str) -> str:
226239
227240
228241
m = mellea.start_session()
229-
print(write_email(m, "Olivia",
230-
"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+
)
231249
```
232250
233251
Most of this should look familiar by now, but the `validation_fn` and `check` should be new.

mkdocs.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ markdown_extensions:
8686
- pymdownx.critic
8787
- pymdownx.details
8888
- pymdownx.emoji:
89-
emoji_index: !!python/name:materialx.emoji.twemoji
90-
emoji_generator: !!python/name:materialx.emoji.to_svg
89+
emoji_index: !!python/name:material.extensions.emoji.twemoji
90+
emoji_generator: !!python/name:material.extensions.emoji.to_svg
9191
- pymdownx.highlight
9292
- pymdownx.inlinehilite
9393
- pymdownx.keys
@@ -100,7 +100,8 @@ markdown_extensions:
100100
- name: mermaid
101101
class: mermaid
102102
format: !!python/name:pymdownx.superfences.fence_code_format
103-
- pymdownx.tabbed
103+
- pymdownx.tabbed:
104+
alternate_style: true
104105
- pymdownx.tasklist:
105106
custom_checkbox: true
106107
- pymdownx.tilde

0 commit comments

Comments
 (0)