Effectively Analyze Survey Responses with AI
Last Updated on February 16, 2024 by Editorial Team
Author(s): muffaddal qutbuddin
Originally published on Towards AI.
In the fast-paced world of business, understanding the voice of the customer has never been more crucial. Companies of all sizes leverage surveys to tap into their customersβ thoughts and preferences, seeking to bridge the gap between service delivery and customer expectations.
The problem with survey data, however, is that they come in many shapes. Different teams run different kinds of surveys. The shape and structure of data produced from surveys vary greatly. This then becomes a challenge for the teams to analyze the results. They have to understand the structure and analyze each survey differently to get the value out.
With the advancement of AI, specifically through the innovative use of LangChain, we now build utilities with the ability to not only analyze survey data quickly and efficiently but also extract actionable insights that can significantly reduce time to insight.
This article explains how to build an AI agent with LangChain that can help you analyze the survey data with a few clicks.
Note: I have used dummy survey data for the sake of presentation.
Before we dive into the technicalities, Letβs first see the outputs AI generated for the provided survey data.
Are you able to understand what the survey is about from the executive summary below?
The above image illustrates a surveyβs executive summary generated by an AI agent.
Here is how AI generates insights for each of the survey questions.
Pretty neat right? Here are the insights for the another question.
The best part about this AI agent is that you can provide any sort of data structure and It will comprehend and produce insights all by itself.
We can do so much by guiding the AI agent allowing it to analyze and produce insights however we like.
In this article, I will help you build an AI agent that analyzes the survey data from Google Sheets and produces summary insights as depicted above.
Follow me to explore AIβs power in solving business problems.
Why An AI Agent and Not a Chat AI?
While chatbots powered by AI can provide answers based on existing datasets, they cannot autonomously analyze data based on the suggested actions. Our objective is to develop an AI-driven tool for analyzing diverse survey data structures, ensuring businesses can easily extract valuable insights from customer feedback.
For example, you may have a survey data set where each row represents a question and answer, while other teams may have surveys where each column is a question and row contains answers.
Our AI agent needs to have thinking capabilities enabling it to understand the data and process it.
On the other hand, say you have a centralized system where all the survey exists you can build AI around that system resulting in improved performance over an AI agent compared to what we are building. Our AI agent first needs to think about what it is working on then asses which column to use etc.
The agent is also helpful when you need to join survey data with other internal data sets such as user behavior. Since AI can think, It can join surveys with behavior data using user IDs to further enhance the insights.
Build AI Agent With Langchain to Read Survey Data
To build our AI agent for survey analysis, We will use Langchain to build the AI Agent. Langchian's flexible abstractions and extensive toolkit allow developers to build context-aware, reasoning LLM applications.
Following is how the agent will instantiate and produce insights:
Read Data From Google Spreadsheets using Python
We read data from Google Spreadsheets since most of the surveys are dumped into Spreadsheets. You can replace it with other data sources as required. Just make sure the final data is in a data frame. Why dataframe you ask? I will explain that in a bit.
The following Python script, utilizing the gspread
library, demonstrates how to extract survey data for AI analysis
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
def read_google_sheet(spreadsheet_url):
# Set up the credentials
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
try:
# authorize the clientsheet
client = gspread.authorize(creds)
# get the instance of the Spreadsheet
sheet = client.open_by_url(spreadsheet_url)
# Select the worksheet by title
worksheet = sheet.get_worksheet(0)
# Get all values from the worksheet
data = worksheet.get_all_values()
records_df = pd.DataFrame(data[1:], columns=data[0])
return records_df
except HttpError as err:
print(err)
Note: The above code requires you to get the JSON key of the credential created within Google Cloud. Past the key in the same folder as the code with the name βcredentials.jsonβ
Create AI Agent Using Langchain
Next, we need to instantiate an AI agent that can work on the data that we fetched in the previous step.
The following dependencies are required to be imported to initiate our agent.
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from langchain.llms import OpenAI
from langchain.agents.agent_types import AgentType
We will usecreate_pandas_dataframe_agent
Langchain agent as it allows to work with the dataframe out of the box without the need to import any additional tools.
For this article, we will employ Open AIβs ChatGPT-4-turbo model. Because it is the best model Open AI has to offer as of writing this article with 3x cheaper cost. However, feel free to test other models out there to gauge their performance. In my testing, Open AI was outperforming the most.
The following code creates the chat using the Chat GPT 4 Model.
open_api_key = <your open ai key here>
model ="gpt-4-1106-preview"
chat = ChatOpenAI(openai_api_key = open_api_key,model = model , temperature=0.0)
Note: make sure to insert the Open AI key for variable open_api_key.
To provide context to our Agent we will add a prefix to ensure it knows the details about the dataset. Following is a very basic prefix for the Agent we can employ.
prefix = """
You are working with 1 pandas dataframe in Python named df1
[add the details about the survey here to provide context to the AI]
they orders on Savyour's partner brands via Savyour Mobile app.
you should use the tools below to answer the question posed of you:
"""
You need to provide the context of the survey to the AI. This will help AI understand the reason behind the survey and generate keeping context in mind.
Note: Keep the below line as it is used by create_pandas_dataframe_agent for instructions on what tool to use.you should use the tools below to answer the question posed of you
Lastly, we create our agent that works with the survey data to get the insights we require. Here is the code
agent = create_pandas_dataframe_agent(chat, df, PREFIX = prefix, verbose = True, max_iterations=10, handle_parsing_errors=True )
Verbose true allows us to monitor the steps taken by the agent to reach the result. Max iteration ensures the agent isnβt stuck in the never-ending loop and handling parse error passes the error encountered to the agent to rethink the steps it took that resulted in the error.
With this, we are ready to generate the insights for our survey as illustrated in the images depicted at the start. I will use this test survey data to produce the output.
Get Insights from survey results using AI Agent
Generate Executive Summary Of User Responses
To generate the executive summary we have to pass the agent command that makes it consider all the questions and answers to generate the summary. Below is an example prompt to attain the summary.
agent.run("""write an executive summary that succinctly captures the key information about to the questions answered by users. Ensure the summary is informative, and provides a comprehensive overview.
Consider both the questions and answers to generate the summary.""")
Feel free to modify the prompt to get more favorable answers.
Extract Insights For Individual Survey Questions
To get insights for each of the questions we will first extract out the list of questions from the survey. Since we are working to make the agent dynamic, we will ask AI to get us the questions list first. Following is the code to get the questions list out.
# get the list of questions as text
result = agent.run("give me list of questions in the data set. I need only the questions as a list nothing else. Don't even add any other text just the list of questions")
# convert string of questions into list
import ast
questions_list = ast.literal_eval(result)
Next, we will pass each of the questions to the agent asked to get the insights using the below code
for question in questions_list:
result = agent.run(f""" You are an expert growth marketing manager. Extract an important insights from the question below, you need to only consider and filter the dataset for this question. Use % to show distribution of answers as well. Find any insights or suggestions for improvements based on people responses.
{question}""")
print("### For Question: "+ question)
print(result)
Here are the insights.
And just like that. You can use this Colab to get you the insights with a few clicks. Just past the Spreadsheet URL id and run the code to get the insights.
Final Thoughts
Having an AI agent that can think and process commands to perform the task and get results is powerful. We can connect the survey with the userβs behavior analytics from Mixpanel or Google Analytics to further improve the insights. We can monitor usersβ in-app behavior and how they respond to the survey to identify why they responded to the question, allowing marketers and growth managers to improve their efforts further and drive growth.
Summary
In navigating the complexities of survey data, the traditional analytical approaches often fall short. This article has illustrated how leveraging AI, particularly through LangChain, revolutionizes our ability to extract actionable insights from diverse data sets. The power of AI transforms raw survey responses into strategic intelligence, enabling businesses to make informed decisions with unprecedented precision.
As we stand on the brink of this transformative era, the potential for AI to enhance our understanding of customer feedback and drive innovation is immense. I urge you to take the leap and explore the capabilities of AI in survey analysis. By harnessing the insights offered by AI, you can propel your strategies forward, ensuring that your business not only keeps pace with the digital age but leads the way.
Embrace the future of data analysis with AI, and unlock the full potential of your survey data to inform and guide your business decisions
Similar Reads
Build a Recommendation System using Google Cloud Recommendation AI
Implement a highly advanced recommender system using Google Cloud Recommendation AI
towardsdatascience.com
RFM Analysis using BigQuery ML
User Segmentation using RFM analysis in BigQuery ML and Visualization in the Data Studio.
towardsdatascience.com
User Segmentation Based on Purchase History
Improve ad campaign by segmenting users based on their purchase behavior.
towardsdatascience.com
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI