Pydantic程序指南¶
使用guidance通过LlamaIndex生成结构化数据。
使用guidance,您可以通过强制LLM输出所需的标记来确保输出结构是正确的。 当您使用容量较低的模型(例如当前的开源模型)时,这将特别有帮助,否则模型可能难以生成符合所需输出模式的有效输出。
如果您在colab上打开这个笔记本,您可能需要安装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程序
from pydantic import BaseModel
class Guidance(BaseModel):
title: str
description: str
steps: List[str]
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,
)
运行程序以获得结构化输出。 蓝色突出显示的文本是我们指定的变量,绿色突出显示的文本是由LLM生成的。
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)])