Back to Llama Index

Pydantic Tree Summarize

docs/examples/response_synthesizers/pydantic_tree_summarize.ipynb

0.14.211.8 KB
Original Source

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/response_synthesizers/pydantic_tree_summarize.ipynb" target="_parent"></a>

Pydantic Tree Summarize

In this notebook, we demonstrate how to use tree summarize with structured outputs. Specifically, tree summarize is used to output pydantic objects.

python
import os
import openai

os.environ["OPENAI_API_KEY"] = "sk-..."
openai.api_key = os.environ["OPENAI_API_KEY"]

Download Data

python
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'

Load Data

python
from llama_index.core import SimpleDirectoryReader
python
reader = SimpleDirectoryReader(
    input_files=["./data/paul_graham/paul_graham_essay.txt"]
)
python
docs = reader.load_data()
python
text = docs[0].text

Summarize

python
from llama_index.core.response_synthesizers import TreeSummarize
from llama_index.core.types import BaseModel
from typing import List

Create pydantic model to structure response

python
class Biography(BaseModel):
    """Data model for a biography."""

    name: str
    best_known_for: List[str]
    extra_info: str
python
summarizer = TreeSummarize(verbose=True, output_cls=Biography)
python
response = summarizer.get_response("who is Paul Graham?", [text])

Inspect the response

Here, we see the response is in an instance of our Biography class.

python
print(response)
python
print(response.name)
python
print(response.best_known_for)
python
print(response.extra_info)