Using ChatGPT-3 to derive insights from your sales reporting
“I have stared long enough at the glowing flat rectangles of computer screens. Let us give more time for doing things in the real world . . . plant a plant, walk the dogs, read a real book, go to the opera.”
―?Edward R. Tufte,?Envisioning Information
I'm fresh off having listened to a fantastic podcast featuring Edward Tufte (widely considered a visionary in the field of data visualisation and representation) and my synapses are firing in all kinds of directions thinking about data.
Particularly, thinking about the way in which people - because of all our wonderful differences - don't all see, understand or consume data the same way, and how a large part of what we do in software engineering is trying to present data in a way that's going to provide maximum benefit for those that consume it.
Let's examine for instance, the borderline archetypical sales report for a retail business. In our fictional scenario for the sake of this piece, we're running a national chain of retail stores selling Unicorn tears.
Hey. Don't start on me. There's a market out there for everything.
Despite being nation-wide, we're pretty basic in the way we deal with sales reporting, so every week we send out an email with tabular data in it highlighting the sales for our stores.
Now this format might work for some people, but there are plenty who'd find a big chunk of tabular data intimidating. It could be that they aren't great with numbers, or they're vision impaired, or even that they're just too time poor to review the data in detail. They're looking for a summary, the insights - they want the headlines.
Now you could convert the data into visual aids using charts and graphs - that's one way to provide a better view for you consumers, but I think the talk-of-the-town ChatGPT-3 model presents us with another way to tackle turning this data into something meaningful that a human can consume quickly and easily.
What about we leverage GPT-3 to convert tabular data into natural language insights? Interested? Well come on in!
The Setup
To create our data scenario for the Unicorn Tears brand, I'm going to spin up some resources in Google Cloud Platform (GCP). To store our sample sales data, I'll be using the cheap, efficient and totally serverless Google Firestore Database service. It'll provide an excellent NoSQL store for our sales documents.
Then I'll be wiring up a Google Cloud Function that can be invoked via HTTP to read the raw data from Firestore, pass it to GPT-3 with a natural language request, and get back something that - hopefully - provides useful insights into the data!
Here's the basic setup for our Unicorn Tears S.A.D service (Serverless Analytics Distiller)... why am I so dark today?
Here's what our Firestore Database collection looks like:
The Code
And now let's examine the c# code we'll be using in our Cloud Function to
领英推荐
After adding the required Google Cloud libraries in our Function.cs (the main class for the Cloud Function) we set up the structure of the response that will go back to the client on invocation. You'll notice that I've decorated the 'SalesData' class with [FirestoreData] and [FirestoreProperty] decorators. This will make it easy for us to convert the documents into POCO (Plain Old C# Object) when the time comes.
Here's our actual function handler. I've created a ChatGPT 'service' that will do the interacting with the GPT-3 API (we'll look at that in a moment) and the rest of the handler is fairly straightforward. We make a connection to our Firestore DB, get our sales collection, and iterate through it obtaining all the sales documents. Once we have them all, we pass them to our ChatGPT service prefixed by a natural language query that goes something like this... "Using this dataset, provide summary, trend and statistical analysis:".
Lastly, let's have a look at my bare bones ChatGPT service (which is really just a HttpClient to interact with OpenAI's model). I've created a class to hold the structure of the API response, and a single method calle GetModelResponse(). This is what will accept my natural language prompt and data. I then use my authentication token (which you can get from your OpenAI account) and make a request to the /completions endpoint. Just to touch on a couple of the values shown in the request.
Tokens = this is essentially how complex (and expensive) you want your response to be. Complexity of requests and responses are measured in tokens, which is almost-but-not-quite analogous to 'word count'. Your max tokens includes the tokens consumed in your request string, so in the below example I've set the max tokens to the absolute limit (4096) minus the number of tokens in the request itself. (1840). That should give me the longest response allowable for the model.
Model = You can choose which of the OpenAI models you want to be processing your request. They range from the simplest and fastest (text-ada-001) to the slowest and most complex which I'm using below. There's a list of model options in the OpenAI website.
Temperature = This equates to how risky or 'creative' you want the model to be constructing a response. A higher temperature (max of 1) I'm assuming means you will get more interesting - but potentially nonsense - responses, a lower temperature means safer, but more mundane responses. To me, it sounds not too dissimilar to the concept of entropy in reinforcement learning. A measure of how risky you want your model to be. Someone with more AI/ML knowledge could probably comment here!
The Result
Okay. So with our Cloud Function ready to go, let's fire it up on my local machine for now, and see if we can't get an interesting and human digestible summary of our Unicorn Tears sales data!
PS C:\Users\JMatson\source\repos\FirestoreAndChatGPT> dotnet run?
2023-02-04T04:57:55.598Z [Google.Cloud.Functions.Hosting.EntryPoint] [info] Serving function FirestoreAndChatGPT.Function
2023-02-04T04:57:55.700Z [Microsoft.Hosting.Lifetime] [info] Now listening on: https://127.0.0.1:8080
2023-02-04T04:57:55.701Z [Microsoft.Hosting.Lifetime] [info] Application started. Press Ctrl+C to shut down.
2023-02-04T04:57:55.701Z [Microsoft.Hosting.Lifetime] [info] Hosting environment: Production
2023-02-04T04:57:55.701Z [Microsoft.Hosting.Lifetime] [info] Content root path: C:\Users\JMatson\source\repos\FirestoreAndChatGPT
2023-02-04T04:58:19.467Z [Microsoft.AspNetCore.Hosting.Diagnostics] [info] Request starting HTTP/1.1 POST https://localhost:8080/? 0?
With dotnet run invoked, our cloud function is now running on localhost, effectively as a barebones API endpoint. Let's invoke it in Postman!
Well how about that? It worked! We get back a list of our raw sales figures from Firebase, along with an accurate and an insightful natural language summary of the data! How awesome is that? Imagine the potential here for providing automatically constructed executive summaries? Or as a way to enhance existing reports that contain 'dense' numerical data that can be time consuming for someone to pluck useful information from? In case the screenshot is a little hard to read, here's what GPT-3 made of our Unicorn Tears sales:
"Summary: This dataset contains sales figures for the brand UNICORN_TEARS from three stores in two regions, VIC and NSW, from January 22nd to February 3rd, 2023.
Trend: Sales figures for UNICORN_TEARS increased over the period, with the highest sales figures occurring in the last week of January and the first week of February. The highest sales figures were recorded in the VIC region, with the Chadstone store recording the highest sales figures overall.
Statistical Analysis: The mean sales figure for UNICORN_TEARS over the period was 61577.2, with a standard deviation of 20,837.3. The median sales figure was 60542.5. The maximum sales figure was 98,875 and the minimum sales figure was 0."
This was a very simplistic example, but hopefully you - like me - find these types of explorations a bit of a spark for further research, exploration and innovation. Thanks for reading, and feel free to leave comments on this article!
PS - No Unicorns were harmed in the making of this article....
DevOps Engineer at Wesfarmers Health
1 年Awesome article James.... love your ideas !
Senior Cyber Security Contractor (Hannan Partners)
1 年You are a unicorn James. Your ability to conceptualise alternate ways to integrate solutions to achieve innovative outcomes is 4th dimensional (next level). We are privileged to have you in our team. Interested to hear how this will evolve to benefit our customers and business.??????
COO at fullstop.ai
1 年This is the key step in enabling automation of descriptive analytics and very well executed. How responsive / trainable is chagpt to including trend based measures (YoY, MoM etc.) and rounding number outputs to more business friendly text, such as 675k rather then 675,675?
Helping to grow value through people
1 年Your brain is amazing! I just love it every time you let loose and help us mere mortals get a glimpse of your genius. Love it.
Solution Architecture ,Technology Consulting and Developer | CRM , CX , Digital Commerce , Retail | SAP S/4 HANA
1 年Thank you for sharing this. It raises the question of when companies like Microsoft will embed GPT into their enterprise applications, such as Dynamics and Teams, for example. Then, you would be able to ask questions of this nature and receive answers specific to the organization. It would function like a general computing platform where you can request reports, analytics, or create a process flow that spans multiple systems. It could also access the organization's code repositories, JIRA/ServiceNow tickets and Confluence pages, providing answers on how things work and how to fix them.