mirror of
https://github.com/Shubhamsaboo/awesome-llm-apps.git
synced 2026-04-30 15:20:47 -05:00
refactor: Update AI Data Analyst Agent to use DuckDbTools and streamline query handling
- Replaced DuckDbAgent with DuckDbTools for loading CSV data into DuckDB. - Initialized a new Agent for data analysis with DuckDB and Pandas tools.
This commit is contained in:
@@ -1,12 +1,11 @@
|
|||||||
import json
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import csv
|
import csv
|
||||||
import streamlit as st
|
import streamlit as st
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
from agno.agent import Agent
|
||||||
from agno.models.openai import OpenAIChat
|
from agno.models.openai import OpenAIChat
|
||||||
from phi.agent.duckdb import DuckDbAgent
|
from agno.tools.duckdb import DuckDbTools
|
||||||
from agno.tools.pandas import PandasTools
|
from agno.tools.pandas import PandasTools
|
||||||
import re
|
|
||||||
|
|
||||||
# Function to preprocess and save the uploaded file
|
# Function to preprocess and save the uploaded file
|
||||||
def preprocess_and_save(file):
|
def preprocess_and_save(file):
|
||||||
@@ -74,27 +73,21 @@ if uploaded_file is not None and "openai_key" in st.session_state:
|
|||||||
# Display the columns of the uploaded data
|
# Display the columns of the uploaded data
|
||||||
st.write("Uploaded columns:", columns)
|
st.write("Uploaded columns:", columns)
|
||||||
|
|
||||||
# Configure the semantic model with the temporary file path
|
# Initialize DuckDbTools
|
||||||
semantic_model = {
|
duckdb_tools = DuckDbTools()
|
||||||
"tables": [
|
|
||||||
{
|
|
||||||
"name": "uploaded_data",
|
|
||||||
"description": "Contains the uploaded dataset.",
|
|
||||||
"path": temp_path,
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
# Initialize the DuckDbAgent for SQL query generation
|
# Load the CSV file into DuckDB as a table
|
||||||
duckdb_agent = DuckDbAgent(
|
duckdb_tools.load_local_csv_to_table(
|
||||||
model=OpenAIChat(model="gpt-4", api_key=st.session_state.openai_key),
|
path=temp_path,
|
||||||
semantic_model=json.dumps(semantic_model),
|
table="uploaded_data",
|
||||||
tools=[PandasTools()],
|
)
|
||||||
|
|
||||||
|
# Initialize the Agent with DuckDB and Pandas tools
|
||||||
|
data_analyst_agent = Agent(
|
||||||
|
model=OpenAIChat(id="gpt-4o", api_key=st.session_state.openai_key),
|
||||||
|
tools=[duckdb_tools, PandasTools()],
|
||||||
|
system_message="You are an expert data analyst. Use the 'uploaded_data' table to answer user queries. Generate SQL queries using DuckDB tools to solve the user's query. Provide clear and concise answers with the results.",
|
||||||
markdown=True,
|
markdown=True,
|
||||||
add_history_to_messages=False, # Disable chat history
|
|
||||||
followups=False, # Disable follow-up queries
|
|
||||||
read_tool_call_history=False, # Disable reading tool call history
|
|
||||||
system_prompt="You are an expert data analyst. Generate SQL queries to solve the user's query. Return only the SQL query, enclosed in ```sql ``` and give the final answer.",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Initialize code storage in session state
|
# Initialize code storage in session state
|
||||||
@@ -114,24 +107,19 @@ if uploaded_file is not None and "openai_key" in st.session_state:
|
|||||||
try:
|
try:
|
||||||
# Show loading spinner while processing
|
# Show loading spinner while processing
|
||||||
with st.spinner('Processing your query...'):
|
with st.spinner('Processing your query...'):
|
||||||
# Get the response from DuckDbAgent
|
# Get the response from the agent
|
||||||
|
response = data_analyst_agent.run(user_query)
|
||||||
response1 = duckdb_agent.run(user_query)
|
|
||||||
|
|
||||||
# Extract the content from the RunResponse object
|
# Extract the content from the response object
|
||||||
if hasattr(response1, 'content'):
|
if hasattr(response, 'content'):
|
||||||
response_content = response1.content
|
response_content = response.content
|
||||||
else:
|
else:
|
||||||
response_content = str(response1)
|
response_content = str(response)
|
||||||
response = duckdb_agent.print_response(
|
|
||||||
user_query,
|
|
||||||
stream=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Display the response in Streamlit
|
# Display the response in Streamlit
|
||||||
st.markdown(response_content)
|
st.markdown(response_content)
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
st.error(f"Error generating response from the DuckDbAgent: {e}")
|
st.error(f"Error generating response from the agent: {e}")
|
||||||
st.error("Please try rephrasing your query or check if the data format is correct.")
|
st.error("Please try rephrasing your query or check if the data format is correct.")
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
phidata
|
phidata
|
||||||
streamlit==1.41.1
|
streamlit==1.41.1
|
||||||
openai==1.58.1
|
openai==1.58.1
|
||||||
duckdb==1.1.3
|
duckdb>=1.4.1
|
||||||
pandas
|
pandas
|
||||||
numpy==1.26.4
|
numpy==1.26.4
|
||||||
agno
|
agno>=2.2.10
|
||||||
|
|||||||
Reference in New Issue
Block a user