Skip to content

Commit f6bf855

Browse files
KenOcheltreeGitHub Enterprise
authored andcommitted
Add jupyter notebooks for Colab
1 parent 5a2aed4 commit f6bf855

11 files changed

+1901
-0
lines changed
Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "IByYhgKy9WCo"
7+
},
8+
"source": [
9+
"# Compositionality with Generative Slots\n",
10+
"This Jupyter notebook runs on Colab demonstrates GEnerative Slots, a function whose implementation is provided by an LLM. "
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {
16+
"id": "ZIu6B1Ht927Z"
17+
},
18+
"source": [
19+
"## Install Ollama\n",
20+
"\n",
21+
"Before we get started with Mellea, we download and install ollama."
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {
28+
"id": "VDaTfltQY3Fl"
29+
},
30+
"outputs": [],
31+
"source": [
32+
"!curl -fsSL https://ollama.com/install.sh | sh\n",
33+
"!nohup ollama serve &"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"metadata": {
39+
"id": "jEl3nAk696mI"
40+
},
41+
"source": [
42+
"## Install Mellea\n",
43+
"We run `uv pip install .` to install Mellea."
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"metadata": {
50+
"id": "9EurAUSz_1yl"
51+
},
52+
"outputs": [],
53+
"source": [
54+
"import os\n",
55+
"!git clone https://github.com/generative-computing/mellea.git --quiet\n",
56+
"os.chdir(\"mellea\")\n",
57+
"!uv pip install . -qq"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {
63+
"id": "NU7bZqKA0djW"
64+
},
65+
"source": [
66+
"## Import Mellea and Create Summarizer Library\n",
67+
"Form a Summarizer library that contains a set of functions for summarizing various types of documents."
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": null,
73+
"metadata": {},
74+
"outputs": [],
75+
"source": [
76+
"from mellea import generative\n",
77+
"\n",
78+
"# The Summarizer Library\n",
79+
"@generative\n",
80+
"def summarize_meeting(transcript: str) -> str:\n",
81+
" \"\"\"Summarize the meeting transcript into a concise paragraph of main points.\"\"\"\n",
82+
"\n",
83+
"@generative\n",
84+
"def summarize_contract(contract_text: str) -> str:\n",
85+
" \"\"\"Produce a natural language summary of contract obligations and risks.\"\"\"\n",
86+
"\n",
87+
"@generative\n",
88+
"def summarize_short_story(story: str) -> str:\n",
89+
" \"\"\"Summarize a short story, with one paragraph on plot and one paragraph on braod themes.\"\"\""
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {},
95+
"source": [
96+
"## Create Decision Aide Library\n",
97+
"Form a Decision Aides library that aids in decision making for particular situations."
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": null,
103+
"metadata": {},
104+
"outputs": [],
105+
"source": [
106+
"# The Decision Aides Library\n",
107+
"@generative\n",
108+
"def propose_business_decision(summary: str) -> str:\n",
109+
" \"\"\"Given a structured summary with clear recommendations, propose a business decision.\"\"\"\n",
110+
"\n",
111+
"@generative\n",
112+
"def generate_risk_mitigation(summary: str) -> str:\n",
113+
" \"\"\"If the summary contains risk elements, propose mitigation strategies.\"\"\"\n",
114+
"\n",
115+
"@generative\n",
116+
"def generate_novel_recommendations(summary: str) -> str:\n",
117+
" \"\"\"Provide a list of novel recommendations that are similar in plot or theme to the short story summary.\"\"\""
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"## Specify Contract Functions\n",
127+
"To help us compose these libraries, we introduce a set of contracts that gate function composition and then use those contracts to short-circuit non-sensical compositions of library components:"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"metadata": {
134+
"id": "P1-mChhly4Ow"
135+
},
136+
"outputs": [],
137+
"source": [
138+
"# Compose the libraries.\n",
139+
"from typing import Literal # noqa: E402\n",
140+
"\n",
141+
"@generative\n",
142+
"def has_structured_conclusion(summary: str) -> Literal[\"yes\", \"no\"]:\n",
143+
" \"\"\"Determine whether the summary contains a clearly marked conclusion or recommendation.\"\"\"\n",
144+
"\n",
145+
"@generative\n",
146+
"def contains_actionable_risks(summary: str) -> Literal[\"yes\", \"no\"]:\n",
147+
" \"\"\"Check whether the summary contains references to business risks or exposure.\"\"\"\n",
148+
"\n",
149+
"@generative\n",
150+
"def has_theme_and_plot(summary: str) -> Literal[\"yes\", \"no\"]:\n",
151+
" \"\"\"Check whether the summary contains both a plot and thematic elements.\"\"\""
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"## Start a Mellea Session\n",
159+
"We initialize a backend running Ollama using the granite3.3-chat model."
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": null,
165+
"metadata": {
166+
"id": "-0GPZ3_ty4Ow"
167+
},
168+
"outputs": [],
169+
"source": [
170+
"from mellea import start_session # noqa: E402\n",
171+
"\n",
172+
"m = start_session()"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"Provide a meeting transcript with sample data."
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": null,
185+
"metadata": {
186+
"id": "AQGDkbeby4Ox"
187+
},
188+
"outputs": [],
189+
"source": [
190+
"transcript = \"\"\"Meeting Transcript: Market Risk Review -- Self-Sealing Stembolts Division\n",
191+
"Date: December 1, 3125\n",
192+
"Attendees:\n",
193+
"\n",
194+
"Karen Rojas, VP of Product Strategy\n",
195+
"\n",
196+
"Derek Madsen, Director of Global Procurement\n",
197+
"\n",
198+
"Felicia Zheng, Head of Market Research\n",
199+
"\n",
200+
"Tom Vega, CFO\n",
201+
"\n",
202+
"Luis Tran, Engineering Liaison\n",
203+
"\n",
204+
"Karen Rojas:\n",
205+
"Thanks, everyone, for making time on short notice. As you've all seen, we've got three converging market risks we need to address: tariffs on micro-carburetors, increased adoption of the self-interlocking leafscrew, and, believe it or not, the \"hipsterfication\" of the construction industry. I need all on deck and let's not waste time. Derek, start.\n",
206+
"\n",
207+
"Derek Madsen:\n",
208+
"Right. As of Monday, the 25% tariff on micro-carburetors sourced from the Pan-Alpha Centauri confederacy is active. We tried to pre-purchase a three-month buffer, but after that, our unit cost rises by $1.72. That's a 9% increase in the BOM cost of our core model 440 stembolt. Unless we find alternative suppliers or pass on the cost, we're eating into our already narrow margin.\n",
209+
"\n",
210+
"Tom Vega:\n",
211+
"We cannot absorb that without consequences. If we pass the cost downstream, we risk losing key mid-tier OEM clients. And with the market already sniffing around leafscrew alternatives, this makes us more vulnerable.\n",
212+
"\n",
213+
"Karen:\n",
214+
"Lets pause there. Felicia, give us the quick-and-dirty on the leafscrew.\n",
215+
"\n",
216+
"Felicia Zheng:\n",
217+
"It's ugly. Sales of the self-interlocking leafscrew—particularly in modular and prefab construction—are up 38% year-over-year. It's not quite a full substitute for our self-sealing stembolts, but they are close enough in function that some contractors are making the switch. Their appeal? No micro-carburetors, lower unit complexity, and easier training for install crews. We estimate we've lost about 12% of our industrial segment to the switch in the last two quarters.\n",
218+
"\n",
219+
"Karen:\n",
220+
"Engineering, Luis; your take on how real that risk is?\n",
221+
"\n",
222+
"Luis Tran:\n",
223+
"Technically, leafscrews are not as robust under high-vibration loads. But here's the thing: most of the modular prefab sites don not need that level of tolerance. If the design spec calls for durability over 10 years, we win. But for projects looking to move fast and hit 5-year lifespans? The leafscrew wins on simplicity and cost.\n",
224+
"\n",
225+
"Tom:\n",
226+
"So they're eating into our low-end. That's our volume base.\n",
227+
"\n",
228+
"Karen:\n",
229+
"Exactly. Now let's talk about this last one: the “hipsterfication” of construction. Felicia?\n",
230+
"\n",
231+
"Felicia:\n",
232+
"So this is wild. We're seeing a cultural shift in boutique and residential construction—especially in markets like Beckley, West Sullivan, parts of Osborne County, where clients are requesting \"authentic\" manual fasteners. They want hand-sealed bolts, visible threads, even mismatched patinas. It's an aesthetic thing. Function is almost secondary. Our old manual-seal line from the 3180s? People are hunting them down on auction sites.\n",
233+
"\n",
234+
"Tom:\n",
235+
"Well, I'm glad I don't have to live in the big cities... nothing like this would ever happen in downt-to-earth places Brooklyn, Portland, or Austin.\n",
236+
"\n",
237+
"Luis:\n",
238+
"We literally got a request from a design-build firm in Keough asking if we had any bolts “pre-distressed.”\n",
239+
"\n",
240+
"Karen:\n",
241+
"Can we spin this?\n",
242+
"\n",
243+
"Tom:\n",
244+
"If we keep our vintage tooling and market it right, maybe. But that's niche. It won't offset losses in industrial and prefab.\n",
245+
"\n",
246+
"Karen:\n",
247+
"Not yet. But we may need to reframe it as a prestige line—low volume, high margin. Okay, action items. Derek, map alternative micro-carburetor sources. Felicia, get me a forecast on leafscrew erosion by sector. Luis, feasibility of reviving manual seal production. Tom, let's scenario-plan cost pass-through vs. feature-based differentiation.\n",
248+
"\n",
249+
"Let's reconvene next week with hard numbers. Thanks, all.\"\"\""
250+
]
251+
},
252+
{
253+
"cell_type": "markdown",
254+
"metadata": {},
255+
"source": [
256+
"## Summarize the Meeting\n",
257+
"Use the contracts to short-circuit non-sensical compositions of library components."
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": null,
263+
"metadata": {
264+
"id": "mV-zS1wDy4Oz"
265+
},
266+
"outputs": [],
267+
"source": [
268+
"summary = summarize_meeting(m, transcript=transcript)\n",
269+
"\n",
270+
"if contains_actionable_risks(m, summary=summary) == \"yes\":\n",
271+
" mitigation = generate_risk_mitigation(m, summary=summary)\n",
272+
" print(f\"Mitigation: {mitigation}\")\n",
273+
"else:\n",
274+
" print(\"Summary does not contain actionable risks.\")\n",
275+
"if has_structured_conclusion(m, summary=summary) == \"yes\":\n",
276+
" decision = propose_business_decision(m, summary=summary)\n",
277+
" print(f\"Decision: {decision}\")\n",
278+
"else:\n",
279+
" print(\"Summary lacks a structured conclusion.\")"
280+
]
281+
}
282+
],
283+
"metadata": {
284+
"accelerator": "GPU",
285+
"colab": {
286+
"gpuType": "T4",
287+
"provenance": []
288+
},
289+
"kernelspec": {
290+
"display_name": "Python 3 (ipykernel)",
291+
"language": "python",
292+
"name": "python3"
293+
},
294+
"language_info": {
295+
"codemirror_mode": {
296+
"name": "ipython",
297+
"version": 3
298+
},
299+
"file_extension": ".py",
300+
"mimetype": "text/x-python",
301+
"name": "python",
302+
"nbconvert_exporter": "python",
303+
"pygments_lexer": "ipython3",
304+
"version": "3.13.5"
305+
}
306+
},
307+
"nbformat": 4,
308+
"nbformat_minor": 4
309+
}

0 commit comments

Comments
 (0)