You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"What weighs more a pound of bricks or a pound of feathers"
1566
+
)
1559
1567
1560
-
structured_model.invoke(
1561
-
"What weighs more a pound of bricks or a pound of feathers"
1562
-
)
1568
+
# -> AnswerWithJustification(
1569
+
# answer='They weigh the same',
1570
+
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
1571
+
# )
1572
+
```
1563
1573
1564
-
# -> AnswerWithJustification(
1565
-
# answer='They weigh the same',
1566
-
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
1567
-
# )
1568
-
```
1574
+
Example: Pydantic schema (`include_raw=True`):
1569
1575
1570
-
Example: Pydantic schema (include_raw=True):
1571
-
```python
1572
-
from pydantic import BaseModel
1576
+
```python
1577
+
from pydantic import BaseModel
1573
1578
1574
1579
1575
-
class AnswerWithJustification(BaseModel):
1576
-
'''An answer to the user question along with justification for the answer.'''
1580
+
class AnswerWithJustification(BaseModel):
1581
+
'''An answer to the user question along with justification for the answer.'''
1577
1582
1578
-
answer: str
1579
-
justification: str
1583
+
answer: str
1584
+
justification: str
1580
1585
1581
1586
1582
-
model = ChatModel(model="model-name", temperature=0)
1583
-
structured_model = model.with_structured_output(
1584
-
AnswerWithJustification, include_raw=True
1585
-
)
1587
+
model = ChatModel(model="model-name", temperature=0)
1588
+
structured_model = model.with_structured_output(
1589
+
AnswerWithJustification, include_raw=True
1590
+
)
1586
1591
1587
-
structured_model.invoke(
1588
-
"What weighs more a pound of bricks or a pound of feathers"
1589
-
)
1590
-
# -> {
1591
-
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
1592
-
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
1593
-
# 'parsing_error': None
1594
-
# }
1595
-
```
1592
+
structured_model.invoke(
1593
+
"What weighs more a pound of bricks or a pound of feathers"
1594
+
)
1595
+
# -> {
1596
+
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
1597
+
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
1598
+
# 'parsing_error': None
1599
+
# }
1600
+
```
1596
1601
1597
-
Example: Dict schema (include_raw=False):
1598
-
```python
1599
-
from pydantic import BaseModel
1600
-
from langchain_core.utils.function_calling import convert_to_openai_tool
1602
+
Example: `dict` schema (`include_raw=False`):
1601
1603
1604
+
```python
1605
+
from pydantic import BaseModel
1606
+
from langchain_core.utils.function_calling import convert_to_openai_tool
1602
1607
1603
-
class AnswerWithJustification(BaseModel):
1604
-
'''An answer to the user question along with justification for the answer.'''
1605
1608
1606
-
answer: str
1607
-
justification: str
1609
+
class AnswerWithJustification(BaseModel):
1610
+
'''An answer to the user question along with justification for the answer.'''
"What weighs more a pound of bricks or a pound of feathers"
1616
-
)
1617
-
# -> {
1618
-
# 'answer': 'They weigh the same',
1619
-
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
"What weighs more a pound of bricks or a pound of feathers"
1622
+
)
1623
+
# -> {
1624
+
# 'answer': 'They weigh the same',
1625
+
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
0 commit comments