----------------------
To extract a Pydantic object from a user query in LlamaIndex, you can use the
index.as_query_engine()
method along with the
RetrieverQueryEngine
. This allows you to support structured Pydantic outputs without additional LLM calls.
Here's a step-by-step guide:
- Define the Pydantic object you want to extract. For example, if you want to extract a biography, you can define a
Biography
class like this:
from typing import List
from pydantic import BaseModel
class Biography(BaseModel):
"""Data model for a biography."""
name: str
best_known_for: List[str]
extra_info: str
- Create your query engine using the
index.as_query_engine()
method, specifying the response_mode
and output_cls
parameters. The output_cls
should be the Pydantic class you defined earlier:
query_engine = index.as_query_engine(
response_mode="tree_summarize", output_cls=Biography
)
- You can then get a response and inspect the output by querying the engine:
response = query_engine.query("Who is Paul Graham?")
print(response.name)
print(response.best_known_for)
print(response.extra_info)
This will print the name, best known for, and extra info fields of the
Biography
object that was extracted from the query.
For more details, you can refer to the
LlamaIndex documentation on Query Engines + Pydantic Outputs ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------