Pydantic 树状摘要¶
在本 Notebook 中,我们将演示如何将树状摘要与结构化输出一起使用。具体来说,树状摘要用于输出 Pydantic 对象。
In [ ]
已复制!
import os
import openai
os.environ["OPENAI_API_KEY"] = "sk-..."
openai.api_key = os.environ["OPENAI_API_KEY"]
import os import openai os.environ["OPENAI_API_KEY"] = "sk-..." openai.api_key = os.environ["OPENAI_API_KEY"]
下载数据¶
In [ ]
已复制!
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
!mkdir -p 'data/paul_graham/' !wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
加载数据¶
In [ ]
已复制!
from llama_index.core import SimpleDirectoryReader
from llama_index.core import SimpleDirectoryReader
In [ ]
已复制!
reader = SimpleDirectoryReader(
input_files=["./data/paul_graham/paul_graham_essay.txt"]
)
reader = SimpleDirectoryReader( input_files=["./data/paul_graham/paul_graham_essay.txt"] )
In [ ]
已复制!
docs = reader.load_data()
docs = reader.load_data()
In [ ]
已复制!
text = docs[0].text
text = docs[0].text
摘要¶
In [ ]
已复制!
from llama_index.core.response_synthesizers import TreeSummarize
from llama_index.core.types import BaseModel
from typing import List
from llama_index.core.response_synthesizers import TreeSummarize from llama_index.core.types import BaseModel from typing import List
创建 Pydantic 模型以结构化响应¶
In [ ]
已复制!
class Biography(BaseModel):
"""Data model for a biography."""
name: str
best_known_for: List[str]
extra_info: str
class Biography(BaseModel): """传记的数据模型。""" name: str best_known_for: List[str] extra_info: str
In [ ]
已复制!
summarizer = TreeSummarize(verbose=True, output_cls=Biography)
summarizer = TreeSummarize(verbose=True, output_cls=Biography)
In [ ]
已复制!
response = summarizer.get_response("who is Paul Graham?", [text])
response = summarizer.get_response("who is Paul Graham?", [text])
5 text chunks after repacking 1 text chunks after repacking
检查响应¶
在这里,我们看到响应是我们 Biography
类的一个实例。
In [ ]
已复制!
print(response)
print(response)
name='Paul Graham' best_known_for=['Writing', 'Programming', 'Art', 'Co-founding Viaweb', 'Co-founding Y Combinator', 'Essayist'] extra_info="Paul Graham is a multi-talented individual who has made significant contributions in various fields. He is known for his work in writing, programming, art, co-founding Viaweb, co-founding Y Combinator, and his essays on startups and programming. He started his career by writing short stories and programming on the IBM 1401 computer. He later became interested in artificial intelligence and Lisp programming. He wrote a book called 'On Lisp' and focused on Lisp hacking. Eventually, he decided to pursue art and attended art school. He is known for his paintings, particularly still life paintings. Graham is also a programmer, entrepreneur, and venture capitalist. He co-founded Viaweb, an early e-commerce platform, and Y Combinator, a startup accelerator. He has written influential essays on startups and programming. Additionally, he has made contributions to the field of computer programming and entrepreneurship."
In [ ]
已复制!
print(response.name)
print(response.name)
Paul Graham
In [ ]
已复制!
print(response.best_known_for)
print(response.best_known_for)
['Writing', 'Programming', 'Art', 'Co-founding Viaweb', 'Co-founding Y Combinator', 'Essayist']
In [ ]
已复制!
print(response.extra_info)
print(response.extra_info)
Paul Graham is a multi-talented individual who has made significant contributions in various fields. He is known for his work in writing, programming, art, co-founding Viaweb, co-founding Y Combinator, and his essays on startups and programming. He started his career by writing short stories and programming on the IBM 1401 computer. He later became interested in artificial intelligence and Lisp programming. He wrote a book called 'On Lisp' and focused on Lisp hacking. Eventually, he decided to pursue art and attended art school. He is known for his paintings, particularly still life paintings. Graham is also a programmer, entrepreneur, and venture capitalist. He co-founded Viaweb, an early e-commerce platform, and Y Combinator, a startup accelerator. He has written influential essays on startups and programming. Additionally, he has made contributions to the field of computer programming and entrepreneurship.