Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { marked } from "https://cdnjs.cloudflare.com/ajax/libs/marked/14.1.0/lib/marked.esm.min.js";
import { generateMerchantTransactionData } from "https://esm.town/v/weaverwhale/generateMerchantTransactionData";
import { Hono } from "npm:hono";
import { OpenAI } from "npm:openai";
const trendGPT = async (data, onData) => {
const openai = new OpenAI();
// Start the OpenAI stream
const chatStream = await openai.chat.completions.create({
messages: [
{
role: "system",
content: `
You are a helpful assistant for a data analysis.
I have a JSON file with merchant information such as their payment processor, currency, and all associated transactions.
The transaction data has the order time, status (fulfilled, pending, declined/refunded), and country/region of origin.
Your entire job is to consumes this data.
First, finds trends such as:
- regions of high fraud
- percieved severity of the fraud
- regions with high refunds
- percieved cause of the refunds
Then, provide the following information:
- specific merchants experiencing said trends
- profit/loss analysis of said merchants
- how much each merchant has refunded, pending, and declined
Format the data in markdown format.
Format any chartable data in mermaid format.
Provide any tabular data in table format.
Sort this data highest to lowest values.
Make sure to use the proper currency symbol for each currency.
Don't give me the abbreviation I provided, just the symbol.
Don't tell me they are mermaid charts, just include the charts where relevant.
Some good examples for mermaid charts are:
- distribution of fraud throughout regions (pie: https://mermaid.js.org/syntax/pie.html)
- distribution of refunds throughout regions (pie)
Some good examples for tables are:
- top 10 merchants with highest revenue
- top 10 merchants with highest refunds
Never use fake data, never make up numbers.
Please be concinse in your analysis, this is important for the data anlysis' job.
It's going to the boss, so the report you provide is very integral to business decisions.
Please don't tell me about yourself; just speak confidently, like the real data analyst.
`.replaceAll("\n", ""),
},
{
role: "system",
content: `
`.replaceAll("\n", ""),
},
{
role: "user",
content: JSON.stringify(data),
},
],
model: "chatgpt-4o-latest",
max_tokens: 4000,
temperature: 0,
stream: true,
});
let result = "";
try {
for await (const chunk of chatStream) {
const content = chunk.choices?.[0]?.delta?.content || "";
result += content;
if (onData) onData(result); // Replace the content with the new result
}
} catch (err) {
console.error("Error during stream processing:", err);
throw new Error("An error occurred while streaming the response.");
}
return result;
};
const trendsData = generateMerchantTransactionData();
const app = new Hono();
const styles = "<style>* { font-family: sans-serif; }</style>";
const mermaidClient = `
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
</script>
`;
const renderer = new marked.Renderer();
renderer.code = (code) => {
if (code.lang === "mermaid") return `<pre class="mermaid">${code.text}</pre>`;
return `<pre>${code.text}</pre>`;
};
app.get("/", async (c) => {
weaverwhale-findtrendsusinggpt.web.val.run
August 28, 2024