forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt_v0.ts
139 lines (121 loc) · 3.61 KB
/
gpt_v0.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import nodes from '$lib/node_register';
import categories from '$lib/node_categories';
import { readFileFromGCS, uploadJSONToGCS } from '$lib/utils';
import gpt from '$lib/gpt';
import _ from 'lodash';
import { gpt_v0_prompt } from '$lib/prompts';
import { format, unwrapFunctionStore } from 'svelte-i18n';
import type { DGNodeInterface, GCSBaseData } from '$lib/node_data_types';
const $__ = unwrapFunctionStore(format);
export default class GPTNode {
id: string;
data: GPTData;
position: { x: number; y: number };
type: string;
constructor(node_data) {
const { id, data, position, type } = node_data;
this.id = id;
this.data = data;
this.position = position;
this.type = type;
}
async compute(
inputData: Record<string, any>,
context: string,
info: (arg: string) => void,
error: (arg: string) => void,
success: (arg: string) => void,
slug: string,
Cookies: any
) {
let text = inputData[this.data.input_ids.text] || [];
const open_ai_key = inputData.open_ai_key || inputData[this.data.input_ids.open_ai_key];
if (_.isPlainObject(text) || (_.isArray(text) && !_.isEmpty(text))) {
text = JSON.stringify(text, null, 2);
}
if (!this.data.dirty && this.data.text_length == text.length && this.data.gcs_path) {
let doc = await readFileFromGCS(this);
if (typeof doc === 'string') {
doc = JSON.parse(doc);
}
this.data.output = doc;
this.data.message = `${$__('loaded_from_gcs')}`;
this.data.dirty = false;
return this.data.output;
}
if (context == 'run' && open_ai_key) {
this.data.text_length = text.length;
const { prompt, system_prompt, prompt_suffix } = this.data;
info(`${$__('computing')} ${$__(this.data.label)}`);
let i = 0;
const interval = setInterval(() => {
this.data.message = `${$__('computing')} ${$__(this.data.label)} ${'.'.repeat(i % 4)}`;
info(this.data.message);
i++;
}, 5000);
const todo = new Set([1]);
const result = await gpt(
open_ai_key,
{ text: text },
prompt_suffix ? prompt + prompt_suffix : prompt,
system_prompt,
info,
error,
success,
0,
1,
todo,
this.data.response_format
);
clearInterval(interval);
if (this.data.response_format?.type == 'json_object') this.data.output = JSON.parse(result);
else this.data.output = result;
this.data.dirty = false;
this.data.message = `${$__('output_ready')}`;
success(this.data.message);
await uploadJSONToGCS(this, this.data.output, slug);
return this.data.output;
} else {
this.data.message = `${$__('missing_input_data')}`;
this.data.dirty = false;
return;
}
}
}
interface GPTData extends GCSBaseData {
output: any;
text_length: number;
prompt: string;
prompt_suffix: string;
system_prompt: string;
response_format: any;
}
type GPTNodeInterface = DGNodeInterface & {
data: GPTData;
};
export const gpt_node_data: GPTNodeInterface = {
id: 'gpt',
data: {
label: 'GPT',
output: {},
prompt: gpt_v0_prompt,
system_prompt: 'You are a helpful assistant.',
prompt_suffix: '',
compute_type: 'gpt_v0',
text_length: 0,
dirty: false,
input_ids: { open_ai_key: '', text: '' },
category: categories.ml.id,
icon: 'gpt_v0',
response_format: null,
show_in_ui: true,
message: '',
filename: '',
size_kb: 0,
gcs_path: ''
},
position: { x: 0, y: 0 },
type: 'gpt_v0'
};
export const gpt_node = new GPTNode(gpt_node_data);
nodes.register(GPTNode, gpt_node);