top of page

Learning Analytics and AI

AI Virtual Learning Analyst

Date

Team

2024

Individual work

My Role

AI Designer

Target Users / Audience

Students

Tools Used

Python, Visual Studio Code, Groq, Chainlit

Overview

This project creates a AI data analysis agent that allows users to ask questions about data they upload in CSV format. It uses the large language model from Groq and the Chainlit framework for the chat interface.

This project creates a AI data analysis agent using Groq and Chainlit for the user interface.


When a chat session starts (@cl.on_chat_start), it initializes the Groq language model, displays a welcome message with an image, and prompts the user to upload a CSV file. 


ree

Once a file is uploaded, it reads the data using Pandas, creates a PandasAI agent configured with the language model and specific instructions for data analysis, and stores this agent in the user's session. When the user sends a message (@cl.on_message), the code retrieves the data analysis agent and passes the user's question to it. The agent processes the question against the uploaded data and generates a response, which could be text or an image (like a chart). This response is then sent back to the user through the Chainlit interface.


ree

ree

Below is the full version of the code:


import pandas as pd
import chainlit as cl
from pandasai import Agent
from langchain_groq.chat_models import ChatGroq 


@cl.on_chat_start
async def start_chat():
    llm = ChatGroq(model_name="llama3-70b-8192")

     # Sending an image with the local file path
    elements = [
        cl.Image(name="image1", display="inline", path="./robot.jpeg")
        ]
    await cl.Message(content="Hello there, Welcome to Data Analysis Agent!", elements=elements).send()

    files = None

    #### Load external data
    while files is None:
        files = await cl.AskFileMessage(
            content="Please upload a csv file to begin!", 
            accept=["text/csv"],
            max_size_mb= 100,
            timeout = 180,
        ).send()

    # load the csv data and store in user_session
    file = files[0]

    msg = cl.Message(content=f"Processing `{file.name}`...")
    await msg.send()

    # Read csv file with pandas
    #csv_file = io.BytesIO(file.content)
    df = pd.read_csv(file.path, encoding="utf-8")

    #### Load internal Excel data
    ## Load data
    #df = pd.read_excel('schoolData.xlsx')
    
    ### Load internal CSV data
    #df = pd.read_csv('schoolData.csv')

    dataAgent = Agent(df, 
                      config={"llm": llm, "max_retries": 1, "open_charts": False},
                      description="You are a data analysis agent. Your main goal is to help non-technical users to analyze data.",
                      )
    # creating user session to store data
    cl.user_session.set('dataAgent', dataAgent)

    # Send response back to user
    # Let the user know that the system is ready
    msg.content = f"Processing `{file.name}` done. You can now ask questions!"
    await msg.update()
    


@cl.on_message
async def main(message: cl.Message):
  
    agent = cl.user_session.get('dataAgent')
    
    question = message.content
    response = agent.chat(question)

    msg= None
    if isinstance(response, str) and ".png" in response:
        image = cl.Image(path=response, name="image1", display="inline")
        # Attach the image to the message
        msg = cl.Message(content="Here is the chart:", elements=[image])
    else:
        msg = cl.Message(content=response)
    
    await msg.send()

    # Send final message
    await msg.update()


Process

Final Deliverable(s)

Here is a video of how the code works:



Wanna know more about me and my work?
Feel free to contact me!
Screenshot 2023-12-23 at 4.02.09 PM.png
Screenshot 2023-12-23 at 4.02.16 PM.png

© 2024 by Colleen Chung

bottom of page