如何从模型中返回结构化数据:实用指南与代码示例

# 引言

在现代数据驱动的应用中,将模型输出与特定的结构化模式对齐是一个常见的需求。这种方法可用于从文本中提取数据以插入数据库或用于其他下游系统处理。在这篇文章中,我将介绍几种从模型获取结构化输出的策略。

# 主要内容

## 1. 使用 `with_structured_output()` 方法

`with_structured_output()` 是最简单且最可靠的方法,适用于支持工具/函数调用或 JSON 模式输出的模型。这种方法允许您通过一个 schema 来指定所需输出的属性名称、类型和描述。

### 支持的模型

- OpenAI
- Anthropic
- Azure
- Google
- Cohere
- NVIDIA

等等……

## 2. 使用 Pydantic 类

如果您希望模型返回一个 Pydantic 对象,只需传递所需的 Pydantic 类。Pydantic 的一个关键优势是输出会经过验证。

```python
from typing import Optional
from langchain_core.pydantic_v1 import BaseModel, Field

class Joke(BaseModel):
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(default=None, description="How funny the joke is, from 1 to 10")


structured_llm = llm.with_structured_output(Joke)
# 使用API代理服务提高访问稳定性
structured_llm.invoke("Tell me a joke about cats")

3. TypedDict 和 JSON Schema

如果不需要参数验证,您可以使用 TypedDict 或 JSON Schema。

from typing_extensions import Annotated, TypedDict

class Joke(TypedDict):
    setup: Annotated[str, ..., "The setup of the joke"]
    punchline: Annotated[str, ..., "The punchline of the joke"]
    rating: Annotated[Optional[int], None, "How funny the joke is, from 1 to 10"]

structured_llm = llm.with_structured_output(Joke)
# 使用API代理服务提高访问稳定性
structured_llm.invoke("Tell me a joke about cats")

代码示例

以下是如何使用 Pydantic 和 with_structured_output() 方法的示例:

from typing import Optional
from langchain_core.pydantic_v1 import BaseModel, Field

class Joke(BaseModel):
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(default=None, description="How funny the joke is, from 1 to 10")


structured_llm = llm.with_structured_output(Joke)
# 使用API代理服务提高访问稳定性
joke = structured_llm.invoke("Tell me a joke about cats")
print(joke)

常见问题和解决方案

  • 模型不支持工具调用/JSON 模式: 对于这些模型,您需要直接在提示中指定格式,并使用输出解析器提取结构化响应。

  • 输出解析错误: 使用 include_raw=True 可获取原始输出,便于调试。

总结和进一步学习资源

返回结构化数据是提高模型实用性的重要步骤。通过熟练掌握 Pydantic、TypedDict、JSON Schema 及相关方法,您可以更有效地利用模型输出。

进一步学习

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值