Find answers from the community

Updated 3 months ago

I have implemented agent to query over PandasQueryEngine as tools. It works fine. However,

I have implemented agent to query over PandasQueryEngine as tools. It works fine. However, how can I get metadata once it gives the results ? I need some metadata for example for citation purposes.
k
i
12 comments
Please stand by @ihshan :) as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To get the metadata from the results of a query, you can access the metadata attribute of the response object. Here's an example of how you can do this:

Plain Text
response = agent.chat("Your query here")
print(response.metadata)


This will print out the metadata associated with the response. The metadata can include various information depending on the specific tool or query engine used.

For example, in the case of a PandasQueryEngine, the metadata might include the pandas python instructions used to generate the response:

Plain Text
# get pandas python instructions
print(response.metadata["pandas_instruction_str"])


This is demonstrated in the Pandas Query Engine example () from the knowledge sources.

Remember, the exact contents of the metadata will depend on the specific tool or query engine used. Some tools might not provide any metadata. If you need specific metadata for citation purposes, you might need to modify the tool or query engine to include this information in the metadata.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai Yeah, when I run print(response.metadata) . It provides empty . No metadata
Here is my code for query engine tools. Is there a way how it can always provide me some metadata
query_engine_tools = [
QueryEngineTool(
query_engine=ppm_query_engine,
metadata=ToolMetadata(
name="ppm_plan_view",
description=(
"Provides information about Ppm plan view data."
"Don't use your prior knowledge in answering a question"
),
),
),
QueryEngineTool(
query_engine=hubodo_query_engine,
metadata=ToolMetadata(
name="hubodometer_readings",
description=(
"Provides information about Hubodometer readings dataset."
"Don't use your prior knowledge in answering a question."
),
),
),
QueryEngineTool(
query_engine=titanic_query_engine,
metadata=ToolMetadata(
name="titanic",
description=(
"Provides information about Titanic dataset."
"Don't use your prior knowledge in answering a question."
),
),
),
QueryEngineTool(
query_engine=house_query_engine,
metadata=ToolMetadata(
name="house_pricing",
description=(
"Provides information about House pricing dataset."
"Don't use your prior knowledge in answering a question."
),
),
),
]
Let me check the knowledge base for you. One moment! :books:
----------------------
The metadata you're trying to access is the metadata of the QueryEngineTool objects, which is different from the metadata of the response. The metadata of the QueryEngineTool objects is static and set when you create the QueryEngineTool objects, as you've done in your code. This metadata is not automatically included in the response metadata.

The response metadata is generated dynamically when you make a query. It can include various information depending on the specific tool or query engine used. However, not all tools or query engines provide response metadata. If you're getting an empty dictionary when you print response.metadata, it means that the specific tool or query engine you're using is not providing any response metadata.

If you want to include the metadata of the QueryEngineTool objects in the response metadata, you would need to modify the QueryEngineTool or the query engine to include this information in the response metadata. This would likely involve modifying the query method of the query engine to include the desired metadata in the response.

Unfortunately, without access to the source code of the QueryEngineTool and the query engines, I can't provide specific instructions on how to do this. You might want to refer to the LlamaIndex documentation () or the source code of the QueryEngineTool and the query engines for more information.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai How can I make agent provides me from which engine or get some metadata of that query engine tool
Let me check the knowledge base for you. One moment! :books:
----------------------
To get the metadata of the QueryEngineTool that was used to generate a response, you can modify the call method of the QueryEngineTool to include the tool's metadata in the response. Here's an example of how you can do this:

Plain Text
class MyQueryEngineTool(QueryEngineTool):
    def call(self, *args: Any, **kwargs: Any) -> ToolOutput:
        query_str = self._get_query_str(*args, **kwargs)
        response = self._query_engine.query(query_str)
        return ToolOutput(
            content=str(response),
            tool_name=self.metadata.name,
            raw_input={"input": query_str},
            raw_output=response,
            tool_metadata=self.metadata.dict(),  # include tool metadata in the response
        )


In this example, MyQueryEngineTool is a subclass of QueryEngineTool that overrides the call method to include the tool's metadata in the response. The tool's metadata is included in the tool_metadata field of the ToolOutput object.

You can then use MyQueryEngineTool instead of QueryEngineTool when defining your query engine tools:

Plain Text
query_engine_tools = [
    MyQueryEngineTool(
        query_engine=ppm_query_engine,
        metadata=ToolMetadata(
            name="ppm_plan_view",
            description=(
                "Provides information about Ppm plan view data."
                "Don't use your prior knowledge in answering a question"
            ),
        ),
    ),
    # ...
]


Now, when you make a query and get a response, you can access the metadata of the tool that was used to generate the response like this:

Plain Text
response = agent.chat("Your query here")
print(response.tool_metadata)


This will print out the metadata of the tool that was used to generate the response.
Please note that this solution requires modifying the source code of the QueryEngineTool, which might not be possible or desirable in all situations. Also, this solution assumes that you're using a version of LlamaIndex where the ToolOutput class has a tool_metadata field. If this is not the case, you might need to modify the ToolOutput class as well.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai It does not work. Actually, all query engines within my query engine tools are PandasQueryEngine. Is there a way to provide some info or metadata of which PandasQueryEngine given agent has provided some answer
Let me check the knowledge base for you. One moment! :books:
----------------------
If you want to include some metadata about which PandasQueryEngine was used to generate a response, you can modify the query method of the PandasQueryEngine to include this information in the response metadata. Here's an example of how you can do this:

Plain Text
class MyPandasQueryEngine(PandasQueryEngine):
    def __init__(self, df, name, **kwargs):
        super().__init__(df, **kwargs)
        self.name = name

    def query(self, query_str):
        response = super().query(query_str)
        # Add the name of the query engine to the response metadata
        response.metadata["query_engine_name"] = self.name
        return response


In this example, MyPandasQueryEngine is a subclass of PandasQueryEngine that adds a name attribute to the query engine and includes this name in the response metadata. You can then use MyPandasQueryEngine instead of PandasQueryEngine when defining your query engines:

Plain Text
ppm_query_engine = MyPandasQueryEngine(df=ppm_df, name="ppm_plan_view", verbose=True)
# ...


Now, when you make a query and get a response, you can access the name of the query engine that was used to generate the response like this:

Plain Text
response = agent.chat("Your query here")
print(response.metadata["query_engine_name"])


This will print out the name of the query engine that was used to generate the response.

Please note that this solution requires modifying the source code of the PandasQueryEngine, which might not be possible or desirable in all situations. Also, this solution assumes that you're using a version of LlamaIndex where the Response class has a metadata field. If this is not the case, you might need to modify the Response class as well.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord