Find answers from the community

Updated 5 months ago

how can i extract a pydantic object(API parmas) from a user query?

At a glance
how can i extract a pydantic object(API parmas) from a user query?
k
C
4 comments
Please stand by @CrazyMonk as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
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:

  1. 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:
Plain Text
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


  1. 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:
Plain Text
query_engine = index.as_query_engine(
    response_mode="tree_summarize", output_cls=Biography
)


  1. You can then get a response and inspect the output by querying the engine:
Plain Text
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:
----------------------
@kapa.ai i am guessing i need to have a vector index for this, but i am just interseted in parsing the pydantic object from the user query
@kapa.ai i am guessing i need to have a vector index for this, but i am just interested in parsing the pydantic object from the user query
Add a reply
Sign up and join the conversation on Discord