OpenAI Pydantic 程序¶
本指南展示如何通过 LlamaIndex 使用新版 OpenAI API生成结构化数据。用户只需指定一个 Pydantic 对象即可。
我们演示两种场景:
- 提取为
Album对象(可包含歌曲对象列表) - 提取为
DirectoryTree对象(可包含递归的 Node 对象)
提取到 Album 结构¶
这是一个将输出解析为 Album 模式的简单示例,该模式可以包含多首歌曲。
如果您在 Colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙。
%pip install llama-index-llms-openai
%pip install llama-index-program-openai
%pip install llama-index
from pydantic import BaseModel
from typing import List
from llama_index.program.openai import OpenAIPydanticProgram
模型中无文档字符串¶
定义输出模式(不含文档字符串)
class Song(BaseModel):
title: str
length_seconds: int
class Album(BaseModel):
name: str
artist: str
songs: List[Song]
定义 openai pydantic 程序
prompt_template_str = """\
Generate an example album, with an artist and a list of songs. \
Using the movie {movie_name} as inspiration.\
"""
program = OpenAIPydanticProgram.from_defaults(
output_cls=Album, prompt_template_str=prompt_template_str, verbose=True
)
运行程序以获取结构化输出。
output = program(
movie_name="The Shining", description="Data model for an album."
)
Function call: Album with args: {
"name": "The Shining",
"artist": "Various Artists",
"songs": [
{
"title": "Main Title",
"length_seconds": 180
},
{
"title": "Opening Credits",
"length_seconds": 120
},
{
"title": "The Overlook Hotel",
"length_seconds": 240
},
{
"title": "Redrum",
"length_seconds": 150
},
{
"title": "Here's Johnny!",
"length_seconds": 200
}
]
}
在模型中使用文档字符串¶
class Song(BaseModel):
"""Data model for a song."""
title: str
length_seconds: int
class Album(BaseModel):
"""Data model for an album."""
name: str
artist: str
songs: List[Song]
prompt_template_str = """\
Generate an example album, with an artist and a list of songs. \
Using the movie {movie_name} as inspiration.\
"""
program = OpenAIPydanticProgram.from_defaults(
output_cls=Album, prompt_template_str=prompt_template_str, verbose=True
)
运行程序以获取结构化输出。
output = program(movie_name="The Shining")
Function call: Album with args: {
"name": "The Shining",
"artist": "Various Artists",
"songs": [
{
"title": "Main Title",
"length_seconds": 180
},
{
"title": "Opening Credits",
"length_seconds": 120
},
{
"title": "The Overlook Hotel",
"length_seconds": 240
},
{
"title": "Redrum",
"length_seconds": 150
},
{
"title": "Here's Johnny",
"length_seconds": 200
}
]
}
输出是一个有效的 Pydantic 对象,我们可以用它来调用函数/API。
output
Album(name='The Shining', artist='Various Artists', songs=[Song(title='Main Title', length_seconds=180), Song(title='Opening Credits', length_seconds=120), Song(title='The Overlook Hotel', length_seconds=240), Song(title='Redrum', length_seconds=150), Song(title="Here's Johnny", length_seconds=200)])
流式传输部分中间态 Pydantic 对象¶
我们可以使用 program 的 stream_partial_objects() 方法,在 Pydantic 输出类的有效中间实例生成时立即进行流式传输,而无需等待函数调用生成完整的 JSON 🔥
首先定义输出用的 Pydantic 类
from pydantic import BaseModel, Field
class CharacterInfo(BaseModel):
"""Information about a character."""
character_name: str
name: str = Field(..., description="Name of the actor/actress")
hometown: str
class Characters(BaseModel):
"""List of characters."""
characters: list[CharacterInfo] = Field(default_factory=list)
现在我们将使用提示模板初始化程序
from llama_index.program.openai import OpenAIPydanticProgram
prompt_template_str = "Information about 3 characters from the movie: {movie}"
program = OpenAIPydanticProgram.from_defaults(
output_cls=Characters, prompt_template_str=prompt_template_str
)
最后我们使用 stream_partial_objects() 方法流式传输部分对象
for partial_object in program.stream_partial_objects(movie="Harry Potter"):
# send the partial object to the frontend for better user experience
print(partial_object)
提取 Album 列表(支持并行函数调用)¶
借助 OpenAI 最新推出的并行函数调用功能,我们现在可以从单个提示中同时提取多个结构化数据!
为此,我们需要:
- 选择一个最新模型(例如
gpt-3.5-turbo-1106),并且 - 在
OpenAIPydanticProgram中将allow_multiple设为 True(否则只会返回第一个对象,并抛出警告)。
from llama_index.llms.openai import OpenAI
prompt_template_str = """\
Generate 4 albums about spring, summer, fall, and winter.
"""
program = OpenAIPydanticProgram.from_defaults(
output_cls=Album,
llm=OpenAI(model="gpt-3.5-turbo-1106"),
prompt_template_str=prompt_template_str,
allow_multiple=True,
verbose=True,
)
output = program()
Function call: Album with args: {"name": "Spring", "artist": "Various Artists", "songs": [{"title": "Blossom", "length_seconds": 180}, {"title": "Sunshine", "length_seconds": 240}, {"title": "Renewal", "length_seconds": 200}]}
Function call: Album with args: {"name": "Summer", "artist": "Beach Boys", "songs": [{"title": "Beach Party", "length_seconds": 220}, {"title": "Heatwave", "length_seconds": 260}, {"title": "Vacation", "length_seconds": 180}]}
Function call: Album with args: {"name": "Fall", "artist": "Autumn Leaves", "songs": [{"title": "Golden Days", "length_seconds": 210}, {"title": "Harvest Moon", "length_seconds": 240}, {"title": "Crisp Air", "length_seconds": 190}]}
Function call: Album with args: {"name": "Winter", "artist": "Snowflakes", "songs": [{"title": "Frosty Morning", "length_seconds": 190}, {"title": "Snowfall", "length_seconds": 220}, {"title": "Cozy Nights", "length_seconds": 250}]}
输出是一个有效的 Pydantic 对象列表。
output
[Album(name='Spring', artist='Various Artists', songs=[Song(title='Blossom', length_seconds=180), Song(title='Sunshine', length_seconds=240), Song(title='Renewal', length_seconds=200)]), Album(name='Summer', artist='Beach Boys', songs=[Song(title='Beach Party', length_seconds=220), Song(title='Heatwave', length_seconds=260), Song(title='Vacation', length_seconds=180)]), Album(name='Fall', artist='Autumn Leaves', songs=[Song(title='Golden Days', length_seconds=210), Song(title='Harvest Moon', length_seconds=240), Song(title='Crisp Air', length_seconds=190)]), Album(name='Winter', artist='Snowflakes', songs=[Song(title='Frosty Morning', length_seconds=190), Song(title='Snowfall', length_seconds=220), Song(title='Cozy Nights', length_seconds=250)])]
提取到 Album(流式处理)¶
我们还支持通过 stream_list 函数流式处理对象列表。
该创意的全部荣誉归于 openai_function_call 代码库:https://github.com/jxnl/openai_function_call/tree/main/examples/streaming_multitask
prompt_template_str = "{input_str}"
program = OpenAIPydanticProgram.from_defaults(
output_cls=Album,
prompt_template_str=prompt_template_str,
verbose=False,
)
output = program.stream_list(
input_str="make up 5 random albums",
)
for obj in output:
print(obj.json(indent=2))
提取为 DirectoryTree 对象¶
这个实现直接受到 jxnl 优秀仓库的启发:https://github.com/jxnl/openai_function_call。
该仓库展示了如何利用 OpenAI 的函数 API 来解析递归的 Pydantic 对象。核心要求是需要用一个非递归的 Pydantic 对象来"包裹"递归对象。
这里我们以"目录"场景为例进行演示:通过 DirectoryTree 对象包裹递归的 Node 对象,从而解析文件结构。
# NOTE: defining recursive objects in a notebook causes errors
from directory import DirectoryTree, Node
DirectoryTree.schema()
{'title': 'DirectoryTree',
'description': 'Container class representing a directory tree.\n\nArgs:\n root (Node): The root node of the tree.',
'type': 'object',
'properties': {'root': {'title': 'Root',
'description': 'Root folder of the directory tree',
'allOf': [{'$ref': '#/definitions/Node'}]}},
'required': ['root'],
'definitions': {'NodeType': {'title': 'NodeType',
'description': 'Enumeration representing the types of nodes in a filesystem.',
'enum': ['file', 'folder'],
'type': 'string'},
'Node': {'title': 'Node',
'description': 'Class representing a single node in a filesystem. Can be either a file or a folder.\nNote that a file cannot have children, but a folder can.\n\nArgs:\n name (str): The name of the node.\n children (List[Node]): The list of child nodes (if any).\n node_type (NodeType): The type of the node, either a file or a folder.',
'type': 'object',
'properties': {'name': {'title': 'Name',
'description': 'Name of the folder',
'type': 'string'},
'children': {'title': 'Children',
'description': 'List of children nodes, only applicable for folders, files cannot have children',
'type': 'array',
'items': {'$ref': '#/definitions/Node'}},
'node_type': {'description': 'Either a file or folder, use the name to determine which it could be',
'default': 'file',
'allOf': [{'$ref': '#/definitions/NodeType'}]}},
'required': ['name']}}}
program = OpenAIPydanticProgram.from_defaults(
output_cls=DirectoryTree,
prompt_template_str="{input_str}",
verbose=True,
)
input_str = """
root
├── folder1
│ ├── file1.txt
│ └── file2.txt
└── folder2
├── file3.txt
└── subfolder1
└── file4.txt
"""
output = program(input_str=input_str)
Function call: DirectoryTree with args: {
"root": {
"name": "root",
"children": [
{
"name": "folder1",
"children": [
{
"name": "file1.txt",
"children": [],
"node_type": "file"
},
{
"name": "file2.txt",
"children": [],
"node_type": "file"
}
],
"node_type": "folder"
},
{
"name": "folder2",
"children": [
{
"name": "file3.txt",
"children": [],
"node_type": "file"
},
{
"name": "subfolder1",
"children": [
{
"name": "file4.txt",
"children": [],
"node_type": "file"
}
],
"node_type": "folder"
}
],
"node_type": "folder"
}
],
"node_type": "folder"
}
}
输出是一个完整的目录树结构,包含递归的 Node 对象。
output
DirectoryTree(root=Node(name='root', children=[Node(name='folder1', children=[Node(name='file1.txt', children=[], node_type=<NodeType.FILE: 'file'>), Node(name='file2.txt', children=[], node_type=<NodeType.FILE: 'file'>)], node_type=<NodeType.FOLDER: 'folder'>), Node(name='folder2', children=[Node(name='file3.txt', children=[], node_type=<NodeType.FILE: 'file'>), Node(name='subfolder1', children=[Node(name='file4.txt', children=[], node_type=<NodeType.FILE: 'file'>)], node_type=<NodeType.FOLDER: 'folder'>)], node_type=<NodeType.FOLDER: 'folder'>)], node_type=<NodeType.FOLDER: 'folder'>))