使用 Langchain Pydantic,我可以定义嵌套对象字段并将密钥传递到嵌套字段吗?

问题描述 投票:0回答:1

我一直在使用 Pydantic 通过 Langchain 定义 genAi 数据输出的模式。创建嵌套对象非常简单,这是我的架构的模拟版本:

class Models(BaseModel):
    people: list[People] = Field(description="names of the people related fields mentioned in the text.")

class People(BaseModel):
    name: str = Field(description="name of the person being discussed")
    title: str = Field(description="job title of the person being discussed")

我们得到了一致的良好响应:

{
  "people": [
    {
      "name": "John",
      "title": "black-smith",
    },
    {
      "name": "Jane",
      "title": "deer hunter",
    },
  ]
}

我想做的是在这里定义一个嵌套字段类型,进一步定义给定每个字段的信息。我发现这很难描述,但我正在寻找的输出是:

{
  "people": [
    {
      "name": {"value": "John", "context": "his name was John,"},
      "title": {"value": "black-smith", "context": "he was a black-smith"},
    },
    {
      "name": {"value": "Jane", "context": "Jane like to hunt deer in her spare time"},
      "title": {"value": "deer hunter", "context": "Jane like to hunt deer in her spare time"},
    },
  ]
}

现在让模型在手动编写提示时输出此内容很简单,并且它可以很好地一致地提供上下文和所有内容。但对于如何在 Pydantic 中构建它,我能想到的最好的办法就是乏味且重复:

class Models(BaseModel):
    people: list[People] = Field(description="names of the people related fields mentioned in the text.")

class People(BaseModel):
    name: dict = Field(description="an object with 2 keys. The first being 'value' which includes the name of the person being discussed, and the second being 'context' which includes the snippet from the text where the value was taken from")
    title: dict = Field(description="an object with 2 keys. The first being 'value' which includes the job title of the person being discussed, and the second being 'context' which includes the snippet from the text where the value was taken from")

我正在寻找一种在 Pydantic 中定义 Field 类型的方法来抽象这一点,例如:

class Models(BaseModel):
    people: list[People] = Field(description="names of the people related fields mentioned in the text.")

class People(BaseModel):
    name: dict = NestedField(description="the name of the person being discussed")
    title: dict = NestedField(description="the job title of the person being discussed")

class NestedField:
   value: str = Field(description="the value of the field (using the parent description)")
   context: str = Field(description="snippet of text from the text where the value was found")

如有任何建议,我们将不胜感激,谢谢

python-3.x pydantic py-langchain pydantic-v2
1个回答
0
投票

如果我的问题正确,您可以执行以下操作:

class Context(BaseModel):
   value: str = Field(description="the value of the field (using the parent description)")
   context: str = Field(description="snippet of text from the text where the value was found")


class People(BaseModel):
    name: Context = Field(description="the name of the person being discussed")
    title: Context = Field(description="the job title of the person being discussed")


class Models(BaseModel):
    people: list[People] = Field(description="names of the people related fields mentioned in the text.")
© www.soinside.com 2019 - 2024. All rights reserved.