-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path01-intro.py
More file actions
127 lines (90 loc) · 2.11 KB
/
01-intro.py
File metadata and controls
127 lines (90 loc) · 2.11 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "llm==0.24.2",
# "llm-anthropic==0.15.1",
# "marimo",
# "mohtml==0.1.7",
# "pydantic==2.11.3",
# "python-dotenv==1.1.0",
# ]
# ///
import marimo
__generated_with = "0.13.6"
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _():
import llm
from dotenv import load_dotenv
load_dotenv(".env")
return (llm,)
@app.cell
def _(llm):
model = llm.get_model("gpt-4o-mini")
resp = model.prompt("Write me a haiku about Python")
return model, resp
@app.cell
def _(resp):
resp.json()
return
@app.cell
def _(model):
from pydantic import BaseModel
class Haiku(BaseModel):
poem: str
class Haikus(BaseModel):
topic: str
haikus: list[Haiku]
out = model.prompt("Haiku about Python", schema=Haikus)
return BaseModel, out
@app.cell
def _(out):
import json
json.loads(out.json()["content"])
return (json,)
@app.cell
def _(model):
convo = model.conversation()
_ = convo.prompt("Give me a haiku about Python")
print(_.text())
print("\n")
_ = convo.prompt("Give me another one about Snakes")
print(_.text())
return
@app.cell
def _(mo, model):
conversation = model.conversation()
chat_widget = mo.ui.chat(lambda messages: conversation.prompt(messages[-1].content))
chat_widget
return
@app.cell
def _(BaseModel, json, mo, model):
class Summary(BaseModel):
title: str
summary: str
pros: list[str]
cons: list[str]
def summary(text_in):
resp = model.prompt(
f"Make a summary of the following text: {text_in}",
schema=Summary)
return json.loads(resp.json()["content"])
text_widget = mo.ui.text_area(
label="Input to summary function"
).form()
text_widget
return summary, text_widget
@app.cell
def _(summary, text_widget):
from pprint import pprint
pprint(summary(text_widget.value))
return
@app.cell
def _():
return
if __name__ == "__main__":
app.run()