Pseudo Code Generator

Data Agent Setup and Interaction Pseudo Code

This document outlines a pseudo code for setting up an agent that imports employee data, interacts with users via chat, answers queries, and trains using specific visual data queries. It includes code for initializing data, querying, and


Empty image or helper icon

Prompt

import os

import pandas as pd

from pandasai import Agent

employees_data = {
    "EmployeeID": [1, 2, 3, 4, 5],
    "Name": ["John", "Emma", "Liam", "Olivia", "William"],
    "Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}

salaries_data = {
    "EmployeeID": [1, 2, 3, 4, 5],
    "Salary": [5000, 6000, 4500, 7000, 5500],
}

employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)

# By default, unless you choose a different LLM, it will use BambooLLM.
# You can get your free API key signing up at https://pandabi.ai (you can also configure it in your .env file)
os.environ["PANDASAI_API_KEY"] = "your-api-key"

agent = Agent([employees_df, salaries_df], memory_size=10)

# Chat with the agent
response = agent.chat("Who gets paid the most?")
print(response)


# Get Clarification Questions
questions = agent.clarification_questions("Who gets paid the most?")
for question in questions:
    print(question)

# Explain how the chat response is generated
response = agent.explain()
print(response)

# Train with data
queries = [
    "Display the distribution of ages in the population.",
    "Visualize the distribution of product ratings.",
    "Show the distribution of household incomes in a region.",
]

codes = [
    "display_age_distribution()",
    "visualize_product_ratings_distribution()",
    "show_household_incomes_distribution_in_region()",
]

agent.train(queries, codes)

print("Done")

Answer

Pseudo Code for Importing Data, Chatting with the Agent, and Training the Agent

Libraries and Data Preparation

  1. Import Required Libraries:

    • Import the os library.
    • Import the pandas library as pd.
  2. Data Initialization:

    • Create a dictionary employees_data with the keys:
      • "EmployeeID" -> List of employee IDs.
      • "Name" -> List of employee names.
      • "Department" -> List of departments.
    • Create a dictionary salaries_data with the keys:
      • "EmployeeID" -> List of employee IDs.
      • "Salary" -> List of salaries.
  3. Convert Dictionaries to DataFrames:

    • Convert employees_data to a DataFrame called employees_df.
    • Convert salaries_data to a DataFrame called salaries_df.

Agent Setup and Chat Interaction

  1. Set API Key:

    • Set the environment variable PANDASAI_API_KEY to "your-api-key".
  2. Create Agent:

    • Initialize an Agent with the DataFrames [employees_df, salaries_df].
    • Set the agent's memory size to 10.
  3. Query the Agent:

    • Chat with the agent by asking, "Who gets paid the most?" Save the response.
    • Print the response.

Clarification Questions and Explanation

  1. Get Clarification Questions:

    • Request clarification questions from the agent for the query "Who gets paid the most?".
    • Print each question.
  2. Explain Response:

    • Ask the agent to explain how the chat response was generated.
    • Print the explanation.

Agent Training

  1. Prepare Training Data:

    • Create a list queries with questions related to data visualization and distribution:
      • "Display the distribution of ages in the population."
      • "Visualize the distribution of product ratings."
      • "Show the distribution of household incomes in a region."
    • Create a list codes with corresponding function calls:
      • "display_age_distribution()"
      • "visualize_product_ratings_distribution()"
      • "show_household_incomes_distribution_in_region()"
  2. Train the Agent:

    • Train the agent using the queries and codes.
  3. Indicate Completion:

    • Print "Done".

Complete Pseudo Code

# Import Required Libraries
import os
import pandas as pd

# Initialize Data
employees_data = {
    "EmployeeID": [1, 2, 3, 4, 5],
    "Name": ["John", "Emma", "Liam", "Olivia", "William"],
    "Department": ["HR", "Sales", "IT", "Marketing", "Finance"],
}

salaries_data = {
    "EmployeeID": [1, 2, 3, 4, 5],
    "Salary": [5000, 6000, 4500, 7000, 5500],
}

# Convert to DataFrames
employees_df = pd.DataFrame(employees_data)
salaries_df = pd.DataFrame(salaries_data)

# Set API Key
os.environ["PANDASAI_API_KEY"] = "your-api-key"

# Create Agent
agent = Agent([employees_df, salaries_df], memory_size=10)

# Chat with the Agent
response = agent.chat("Who gets paid the most?")
print(response)

# Get Clarification Questions
questions = agent.clarification_questions("Who gets paid the most?")
for question in questions:
    print(question)

# Explain Chat Response
response = agent.explain()
print(response)

# Prepare Training Data
queries = [
    "Display the distribution of ages in the population.",
    "Visualize the distribution of product ratings.",
    "Show the distribution of household incomes in a region.",
]

codes = [
    "display_age_distribution()",
    "visualize_product_ratings_distribution()",
    "show_household_incomes_distribution_in_region()",
]

# Train the Agent
agent.train(queries, codes)

# Indicate Completion
print("Done")

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document outlines a pseudo code for setting up an agent that imports employee data, interacts with users via chat, answers queries, and trains using specific visual data queries. It includes code for initializing data, querying, and training the agent.