Skip to content
This repository was archived by the owner on Dec 2, 2025. It is now read-only.

Commit f62da01

Browse files
add example to pydantic ai
1 parent 20d3ed1 commit f62da01

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

llm/pydantic_ai_examples.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# /// script
22
# requires-python = ">=3.11"
33
# dependencies = [
4+
# "langchain==0.3.24",
5+
# "langchain-community==0.3.23",
6+
# "langchain-core==0.3.56",
7+
# "langchain-openai==0.3.14",
48
# "marimo",
59
# "nest-asyncio==1.6.0",
610
# "numpy==2.2.5",
@@ -43,7 +47,7 @@ def _():
4347

4448
response = client.responses.create(
4549
model="gpt-4o-mini-2024-07-18",
46-
instructions="Extract first name, last name, years of experience, and primary skill from the job applicant description.",
50+
instructions="Extract name, years of experience, and primary skill from the job applicant description.",
4751
input="Khuyen Tran is a data scientist with 5 years of experience, skilled in Python and machine learning.",
4852
)
4953

@@ -160,5 +164,68 @@ def _(pd, unemployment_result):
160164
return
161165

162166

167+
@app.cell(hide_code=True)
168+
def _(mo):
169+
mo.md(r"""## Comparison with LangChain Structured Output""")
170+
return
171+
172+
173+
@app.cell
174+
def _(BaseModel, List):
175+
from typing import Optional
176+
177+
class RecipeExtractor(BaseModel):
178+
ingredients: List[str]
179+
instructions: str
180+
cook_time: Optional[str]
181+
182+
return (RecipeExtractor,)
183+
184+
185+
@app.cell
186+
def _(Agent, RecipeExtractor):
187+
recipe_agent = Agent(
188+
"gpt-4o-mini-2024-07-18",
189+
system_prompt="Pull ingredients, instructions, and cook time.",
190+
output_type=RecipeExtractor,
191+
)
192+
193+
recipe_result = recipe_agent.run_sync(
194+
"Sugar, flour, cocoa, eggs, and milk. Mix, bake at 350F for 30 min."
195+
)
196+
print(recipe_result.output)
197+
print(recipe_result.output.cook_time)
198+
return
199+
200+
201+
@app.cell
202+
def _(RecipeExtractor):
203+
from langchain_core.messages import HumanMessage, SystemMessage
204+
from langchain_openai import ChatOpenAI
205+
206+
# Initialize the chat model
207+
model = ChatOpenAI(model="gpt-4o-mini-2024-07-18", temperature=0)
208+
209+
# Bind the response formatter schema
210+
model_with_tools = model.bind_tools([RecipeExtractor])
211+
212+
# Create a list of messages to send to the model
213+
messages = [
214+
SystemMessage("Pull ingredients, instructions, and cook time."),
215+
HumanMessage(
216+
"Sugar, flour, cocoa, eggs, and milk. Mix, bake at 350F for 30 min."
217+
),
218+
]
219+
220+
# Invoke the model with the prepared messages
221+
ai_msg = model_with_tools.invoke(messages)
222+
223+
# Access the tool calls made during the model invocation
224+
print(ai_msg.tool_calls[0])
225+
print(ai_msg.tool_calls[0]["args"]["cook_time"])
226+
227+
return
228+
229+
163230
if __name__ == "__main__":
164231
app.run()

0 commit comments

Comments
 (0)