-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdemo.py
More file actions
103 lines (76 loc) · 1.94 KB
/
Copy pathdemo.py
File metadata and controls
103 lines (76 loc) · 1.94 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
import marimo
__generated_with = "0.17.7"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Structured Output
""")
return
@app.cell
def _():
from smartfunc import backend
from openai import OpenAI
from dotenv import load_dotenv
from pydantic import BaseModel
load_dotenv(".env")
client = OpenAI()
class Summary(BaseModel):
summary: str
pros: list[str]
cons: list[str]
return Summary, backend, client
@app.cell
def _(Summary, backend, client):
@backend(client, model="gpt-4o-mini", response_format=Summary)
def analyze_pokemon(name: str) -> str:
return f"Describe the following pokemon: {name}"
return (analyze_pokemon,)
@app.cell
def _(analyze_pokemon):
result = analyze_pokemon("pikachu")
print(result.summary)
print(result.pros)
print(result.cons)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Images
You can also return conversations, which let's you re-use OpenAI's SDK for messages.
""")
return
@app.cell
def _():
url = "https://c02.purpledshub.com/uploads/sites/41/2023/01/How-to-see-the-Wolf-Moon-in-2023--4bb6bb7.jpg?w=940&webp=1"
return (url,)
@app.cell
def _(backend, client):
@backend(client, model="gpt-4o-mini")
def desc_image(url: str) -> str:
return [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe the following image:"},
{
"type": "image_url",
"image_url": {"url": url},
},
],
}
]
return (desc_image,)
@app.cell
def _(desc_image, url):
desc_image(url)
return
@app.cell
def _():
return
if __name__ == "__main__":
app.run()