-
Is that possible to build a Data Analyst Agent get data summary or data insights from pandas dataframe? Do you suggest any best practices? I am trying to simulate the below concept. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Yes, it is possible to build a Data Analyst Agent to get data summaries or insights from a pandas DataFrame. Here is a basic example using the OpenAI Function API and the
Here is a sample implementation: import pandas as pd
from llama_index.agent.openai import OpenAIAgent
from llama_index.llms.openai import OpenAI
from llama_index.experimental.query_engine.pandas import PandasQueryEngine
from llama_index.core.schema import QueryBundle
# Sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [24, 30, 22, 35],
'Salary': [70000, 80000, 60000, 90000]
}
df = pd.DataFrame(data)
# Initialize the LLM
llm = OpenAI(model="gpt-3.5-turbo-0613")
# Initialize the PandasQueryEngine
pandas_query_engine = PandasQueryEngine(df=df, llm=llm, verbose=True)
# Define a tool for querying the DataFrame
def query_dataframe(query_str):
query_bundle = QueryBundle(query_str=query_str)
response = pandas_query_engine._query(query_bundle)
return response.response
# Define tools
tools = {
'query_dataframe': query_dataframe
}
# Initialize the OpenAI agent
agent = OpenAIAgent.from_tools(tools, llm=llm, verbose=True)
# Example task: Query the DataFrame
task = {
'tool': 'query_dataframe',
'params': {'query_str': 'What is the average age and salary?'}
}
# Execute the task
result = agent.query(task)
print(result) This setup allows the agent to provide data summaries or insights from the DataFrame effectively. Note that the For more advanced use cases and to build custom agents, LlamaIndex provides a comprehensive framework that includes components for query planning, tool use, and memory modules. You can explore these components and additional resources in the LlamaIndex documentation to enhance your agent's capabilities. For further details, you can refer to the following sources:
|
Beta Was this translation helpful? Give feedback.
To measure the token usage for the agent's actions in your current setup, you can integrate the
TokenCountingHandler
from the LlamaIndex library. Here is the modified version of your code with theTokenCountingHandler
integrated: