如何使用 LangChain 与不同的 Pydantic 版本
- Pydantic v2 于 2023 年 6 月发布(https://docs.pydantic.dev/2.0/blog/pydantic-v2-final/)。
- v2 包含许多重大变更(https://docs.pydantic.dev/2.0/migration/)。
- Pydantic 1 将于 2024 年 6 月停止使用。LangChain 将在不久的将来放弃对 Pydantic 1 的支持,并可能在内部迁移到 Pydantic 2。时间表暂定为 9 月。此更改将伴随主 langchain 软件包的小版本升级至 0.3.x 版本。
截至目前langchain>=0.0.267
,LangChain 允许用户安装 Pydantic V1 或 V2。
在内部,LangChain 继续通过 Pydantic 2 的 v1 命名空间使用Pydantic V1 。
由于 Pydantic 不支持混合 .v1 和 .v2 对象,因此用户在将 LangChain 与 Pydantic 结合使用时应注意一些问题。
虽然 LangChain 在某些 API(如下所列)中支持 Pydantic V2 对象,但建议用户继续使用 Pydantic V1 对象,直到 LangChain 0.3 发布。
1. 将 Pydantic 对象传递给 LangChain API
大多数用于工具使用的 LangChain API (见下表)已更新为接受 Pydantic v1 或 v2 对象。
- Pydantic v1 对象对应于
pydantic.BaseModel
ifpydantic 1
is installed 的子类或pydantic.v1.BaseModel
ifpydantic 2
is installed 的子类。 - Pydantic v2 对象对应于
pydantic.BaseModel
ifpydantic 2
已安装的子类。
API | 派丹蒂克 1 | Pydantic 2 |
---|---|---|
BaseChatModel.bind_tools |
是的 | langchain-core>=0.2.23,适合的合作伙伴包版本 |
BaseChatModel.with_structured_output |
是的 | langchain-core>=0.2.23,适合的合作伙伴包版本 |
Tool.from_function |
是的 | langchain-核心>=0.2.23 |
StructuredTool.from_function |
是的 | langchain-核心>=0.2.23 |
bind_tools
通过或API接受 pydantic v2 对象的合作伙伴软件包with_structured_output
:
软件包名称 | pydantic v1 | pydantic v2 |
---|---|---|
朗尚-米斯特拉莱 | 是的 | >=0.1.11 |
人类链 | 是的 | >=0.1.21 |
langchain-robocorp | 是的 | >=0.0.10 |
langchain-openai | 是的 | >=0.1.19 |
langchain 烟花 | 是的 | >=0.1.5 |
langchain-aws | 是的 | >=0.1.15 |
将来,其他合作伙伴软件包将会更新以接受 Pydantic v2 对象。
如果您仍然看到这些 API 或接受 Pydantic 对象的其他 API 存在问题,请打开一个问题,我们会解决它。
例子:
在此之前langchain-core<0.2.23
,传递给 LangChain API 时使用 Pydantic v1 对象。
from langchain_openai import ChatOpenAI
from pydantic.v1 import BaseModel # <-- Note v1 namespace
class Person(BaseModel):
"""Personal information"""
name: str
model = ChatOpenAI()
model = model.with_structured_output(Person)
model.invoke('Bob is a person.')
之后langchain-core>=0.2.23
,传递给 LangChain API 时使用 Pydantic v1 或 v2 对象。
from langchain_openai import ChatOpenAI
from pydantic import BaseModel
class Person(BaseModel):
"""Personal information"""
name: str
model = ChatOpenAI()
model = model.with_structured_output(Person)
model.invoke('Bob is a person.')
2. LangChain 模型的子类化
因为 LangChain 内部使用 Pydantic v1,所以如果您要对 LangChain 模型进行子类化,则应该使用 Pydantic v1 原语。
示例 1:通过继承进行扩展
是的
from pydantic.v1 import validator
from langchain_core.tools import BaseTool
class CustomTool(BaseTool): # BaseTool is v1 code
x: int = Field(default=1)
def _run(*args, **kwargs):
return "hello"
@validator('x') # v1 code
@classmethod
def validate_x(cls, x: int) -> int:
return 1
CustomTool(
name='custom_tool',
description="hello",
x=1,
)
将 Pydantic v2 原语与 Pydantic v1 原语混合可能会引发神秘错误
不
from pydantic import Field, field_validator # pydantic v2
from langchain_core.tools import BaseTool
class CustomTool(BaseTool): # BaseTool is v1 code
x: int = Field(default=1)
def _run(*args, **kwargs):
return "hello"
@field_validator('x') # v2 code
@classmethod
def validate_x(cls, x: int) -> int:
return 1
CustomTool(
name='custom_tool',
description="hello",
x=1,
)
3. 禁用 Pydantic v2 模型中使用的 LangChain 对象的运行时验证
例如,
from typing import Annotated
from langchain_openai import ChatOpenAI # <-- ChatOpenAI uses pydantic v1
from pydantic import BaseModel, SkipValidation
class Foo(BaseModel): # <-- BaseModel is from Pydantic v2
model: Annotated[ChatOpenAI, SkipValidation()]
Foo(model=ChatOpenAI(api_key="hello"))
4:如果运行 Pydantic 2,LangServe 无法生成 OpenAPI 文档
如果您使用的是 Pydantic 2,则将无法使用 LangServe 生成 OpenAPI 文档。
如果您需要 OpenAPI 文档,您可以选择安装 Pydantic 1:
pip install pydantic==1.10.17
或者使用APIHandler
LangChain 中的对象为您的 API 手动创建路由。
看:https://python.langchain.com/v0.2/docs/langserve/#pydantic
版权声明:《 【LangChain0.2 】如何使用 LangChain 与不同的 Pydantic 版本 》为漠北星火原创文章,转载请注明出处!
最后编辑:2024-8-20 15:08:00