|
1 | 1 | # /// script |
2 | 2 | # requires-python = ">=3.11" |
3 | 3 | # dependencies = [ |
| 4 | +# "langchain==0.3.24", |
| 5 | +# "langchain-community==0.3.23", |
| 6 | +# "langchain-core==0.3.56", |
| 7 | +# "langchain-openai==0.3.14", |
4 | 8 | # "marimo", |
5 | 9 | # "nest-asyncio==1.6.0", |
6 | 10 | # "numpy==2.2.5", |
@@ -43,7 +47,7 @@ def _(): |
43 | 47 |
|
44 | 48 | response = client.responses.create( |
45 | 49 | 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.", |
47 | 51 | input="Khuyen Tran is a data scientist with 5 years of experience, skilled in Python and machine learning.", |
48 | 52 | ) |
49 | 53 |
|
@@ -160,5 +164,68 @@ def _(pd, unemployment_result): |
160 | 164 | return |
161 | 165 |
|
162 | 166 |
|
| 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 | + |
163 | 230 | if __name__ == "__main__": |
164 | 231 | app.run() |
0 commit comments