Pydantic 程序指南¶
通过 LlamaIndex 使用 guidance 生成结构化数据。
借助 guidance,您可以通过强制大语言模型输出指定标记来确保输出结构的正确性。
这对于使用低容量模型(例如当前的开源模型)特别有帮助,否则这些模型难以生成符合预期输出模式的有效结果。
如果您在 Colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙。
In [ ]:
Copied!
%pip install llama-index-program-guidance
%pip install llama-index-program-guidance
In [ ]:
Copied!
!pip install llama-index
!pip install llama-index
In [ ]:
Copied!
from pydantic import BaseModel
from typing import List
from guidance.llms import OpenAI
from llama_index.program.guidance import GuidancePydanticProgram
from pydantic import BaseModel
from typing import List
from guidance.llms import OpenAI
from llama_index.program.guidance import GuidancePydanticProgram
定义输出架构
In [ ]:
Copied!
class Song(BaseModel):
title: str
length_seconds: int
class Album(BaseModel):
name: str
artist: str
songs: List[Song]
class Song(BaseModel):
title: str
length_seconds: int
class Album(BaseModel):
name: str
artist: str
songs: List[Song]
定义指导性 Pydantic 程序
In [ ]:
Copied!
program = GuidancePydanticProgram(
output_cls=Album,
prompt_template_str=(
"Generate an example album, with an artist and a list of songs. Using"
" the movie {{movie_name}} as inspiration"
),
guidance_llm=OpenAI("text-davinci-003"),
verbose=True,
)
program = GuidancePydanticProgram(
output_cls=Album,
prompt_template_str=(
"Generate an example album, with an artist and a list of songs. Using"
" the movie {{movie_name}} as inspiration"
),
guidance_llm=OpenAI("text-davinci-003"),
verbose=True,
)
运行程序以获取结构化输出。
蓝色高亮文本是由我们指定的变量,绿色高亮文本是由大语言模型生成的。
In [ ]:
Copied!
output = program(movie_name="The Shining")
output = program(movie_name="The Shining")
Generate an example album, with an artist and a list of songs. Using the movie The Shining as inspiration ```json { "name": "The Shining", "artist": "Jack Torrance", "songs": [{ "title": "All Work and No Play", "length_seconds": "180", }, { "title": "The Overlook Hotel", "length_seconds": "240", }, { "title": "The Shining", "length_seconds": "210", }], } ```
输出是一个有效的 Pydantic 对象,我们可以用它来调用函数/API。
In [ ]:
Copied!
output
output
Out[ ]:
Album(name='The Shining', artist='Jack Torrance', songs=[Song(title='All Work and No Play', length_seconds=180), Song(title='The Overlook Hotel', length_seconds=240), Song(title='The Shining', length_seconds=210)])