Skip to content

Commit 844a6fb

Browse files
add pydantic ai example
1 parent a8f9e2d commit 844a6fb

File tree

4 files changed

+182
-3
lines changed

4 files changed

+182
-3
lines changed

.github/workflows/publish-marimo.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches: [master]
66
paths:
7-
- 'data_science_tools/polars_vs_pandas.py'
7+
- '*.py'
88
- '.github/workflows/publish-marimo.yml'
99

1010
jobs:
@@ -20,11 +20,12 @@ jobs:
2020
python-version: '3.11'
2121

2222
- name: Install Marimo
23-
run: pip install marimo numpy pandas polars seaborn matplotlib
23+
run: pip install marimo
2424

2525
- name: Export Marimo Notebook
2626
run: |
27-
marimo export html-wasm data_science_tools/polars_vs_pandas.py -o build/data_science_tools/polars_vs_pandas --mode run --show-code
27+
marimo export html-wasm data_science_tools/polars_vs_pandas.py -o build/data_science_tools/polars_vs_pandas --show-code
28+
marimo export html-wasm llm/pydantic_ai_examples.py -o build/llm/pydantic_ai_examples --show-code
2829
2930
- name: Upload Pages Artifact
3031
uses: actions/upload-pages-artifact@v3
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "marimo",
5+
# ]
6+
# ///

data_science_tools/polars_vs_pandas.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "marimo",
5+
# "numpy==2.2.5",
6+
# "pandas==2.2.3",
7+
# "polars==1.27.1",
8+
# ]
9+
# ///
110
import marimo
211

312
__generated_with = "0.13.0"

llm/pydantic_ai_examples.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "marimo",
5+
# "nest-asyncio==1.6.0",
6+
# "openai==1.76.0",
7+
# "pandas==2.2.3",
8+
# "pydantic==2.11.3",
9+
# "pydantic-ai==0.1.4",
10+
# "pydantic-ai-slim[duckduckgo]==0.1.4",
11+
# ]
12+
# ///
13+
14+
import marimo
15+
16+
__generated_with = "0.13.0"
17+
app = marimo.App(width="medium")
18+
19+
20+
@app.cell(hide_code=True)
21+
def _(mo):
22+
mo.md(r"""# Introduction""")
23+
return
24+
25+
26+
@app.cell
27+
def _():
28+
import marimo as mo
29+
30+
return (mo,)
31+
32+
33+
@app.cell
34+
def _():
35+
import os
36+
37+
from openai import OpenAI
38+
39+
client = OpenAI(
40+
api_key=os.environ.get("OPENAI_API_KEY"),
41+
)
42+
43+
response = client.responses.create(
44+
model="gpt-4o-mini-2024-07-18",
45+
instructions="Extract first name, last name, years of experience, and primary skill from the job applicant description.",
46+
input="Khuyen Tran is a data scientist with 5 years of experience, skilled in Python and machine learning.",
47+
)
48+
49+
print(response.output_text)
50+
return
51+
52+
53+
@app.cell(hide_code=True)
54+
def _():
55+
# Core Workflow
56+
return
57+
58+
59+
@app.cell(hide_code=True)
60+
def _():
61+
import nest_asyncio
62+
63+
nest_asyncio.apply()
64+
return
65+
66+
67+
@app.cell
68+
def _():
69+
from typing import List
70+
71+
from pydantic import BaseModel
72+
from pydantic_ai import Agent
73+
74+
return Agent, BaseModel, List
75+
76+
77+
@app.cell
78+
def _(BaseModel, List):
79+
class ApplicantProfile(BaseModel):
80+
first_name: str
81+
last_name: str
82+
experience_years: int
83+
primary_skill: List[str]
84+
85+
return (ApplicantProfile,)
86+
87+
88+
@app.cell
89+
def _(Agent, ApplicantProfile):
90+
agent = Agent(
91+
"gpt-4o-mini-2024-07-18",
92+
system_prompt="Extract name, years of experience, and primary skill from the job applicant description.",
93+
output_type=ApplicantProfile,
94+
)
95+
96+
result = agent.run_sync(
97+
"Khuyen Tran is a data scientist with 5 years of experience, skilled in Python and machine learning."
98+
)
99+
print(result.output)
100+
return (result,)
101+
102+
103+
@app.cell
104+
def _(result):
105+
result.output.model_dump()
106+
return
107+
108+
109+
@app.cell
110+
def _(result):
111+
import pandas as pd
112+
113+
df = pd.DataFrame(result.output.model_dump())
114+
df
115+
return (pd,)
116+
117+
118+
@app.cell(hide_code=True)
119+
def _(mo):
120+
mo.md(r"""# Using the DuckDuckGo Search Tool""")
121+
return
122+
123+
124+
@app.cell
125+
def _(BaseModel, List):
126+
class UnemploymentDataSource(BaseModel):
127+
title: List[str]
128+
description: List[str]
129+
url: List[str]
130+
131+
return (UnemploymentDataSource,)
132+
133+
134+
@app.cell
135+
def _(Agent, UnemploymentDataSource):
136+
from pydantic_ai.common_tools.duckduckgo import duckduckgo_search_tool
137+
138+
# Define the agent with DuckDuckGo search tool
139+
search_agent = Agent(
140+
"gpt-4o-mini-2024-07-18",
141+
tools=[duckduckgo_search_tool()],
142+
system_prompt="Search DuckDuckGo and return links or resources that match the query.",
143+
output_type=UnemploymentDataSource,
144+
)
145+
146+
# Run a search for unemployment rate dataset
147+
unemployment_result = search_agent.run_sync(
148+
"Monthly unemployment rate dataset for US from 2018 to 2024"
149+
)
150+
151+
print(unemployment_result.output)
152+
return (unemployment_result,)
153+
154+
155+
@app.cell
156+
def _(pd, unemployment_result):
157+
unemployment_df = pd.DataFrame(unemployment_result.output.model_dump())
158+
unemployment_df
159+
return
160+
161+
162+
if __name__ == "__main__":
163+
app.run()

0 commit comments

Comments
 (0)