-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequest.ts
88 lines (74 loc) · 1.78 KB
/
request.ts
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
import axios from 'axios'
import once from 'lodash/once'
const a = axios.create()
export type DataSet = {
dataset: string
config: string
split: string
rows: RowWrap[]
}
export type RowWrap = {
row_idx: number
row: Row
}
export type Row = {
act: string
prompt: string
}
export type DisplayRow = Row & {
type: DisplayRowType
description?: string
}
export enum DisplayRowType {
AWESOME_CHATGPT_PROMPTS = 'AWESOME_CHATGPT_PROMPTS',
CUSTOM = 'CUSTOM',
}
const DATA_SET_URL =
'https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv'
function parseCSVLine(line: string) {
let values = []
let currentValue = ''
let inQuotes = false
for (let i = 0; i < line.length; i++) {
let char = line[i]
if (char === ',' && !inQuotes) {
values.push(currentValue)
currentValue = ''
} else if (char === '"') {
inQuotes = !inQuotes
} else {
currentValue += char
}
}
values.push(currentValue)
return values
}
export const fetchPromots = async () => {
return (await a.get<string>(DATA_SET_URL))?.data
?.split('\n')
.slice(1)
.map((item) => item.trim())
.filter((item) => Boolean(item))
.map((item) => parseCSVLine(item))
.map(([act, prompt], index) => ({
act: act?.replaceAll('"', ''),
prompt: prompt?.replace('"', '')
}))
.reverse()
}
export const retrify =
<T>(requestFun: () => Promise<T>, times = 1) =>
async () => {
for (let i = 0; i < times; i++) {
try {
return await requestFun()
} catch (error) {
// ignore
console.error(error)
}
}
}
export const PROMOT_KEY = 'PROMOT_KEY'
export const CUSTOM_PROMPT = 'CUSTOM_KEY'
export const HISTORY_KEY = 'HISTORY_KEY'
export const fetchPromotWithRetry = once(retrify(fetchPromots, 3))