forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argument_extraction_v0.ts
175 lines (157 loc) · 5.39 KB
/
argument_extraction_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import nodes from '$lib/node_register';
import categories from '$lib/node_categories';
import { readFileFromGCS, uploadJSONToGCS } from '$lib/utils';
import { argument_extraction_prompt_v0, argument_extraction_system_prompt } from '$lib/prompts';
import gpt from '$lib/gpt';
import _ from 'lodash';
import { format, unwrapFunctionStore } from 'svelte-i18n';
import type { DGNodeInterface, GCSBaseData } from '$lib/node_data_types';
const $__ = unwrapFunctionStore(format);
export default class ArgumentExtractionNode {
id: string;
data: ArgumentExtractionData;
position: { x: number; y: number };
type: string;
constructor(node_data: ArgumentExtractionNodeInterface) {
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
) {
const { prompt, system_prompt } = this.data;
const csv = inputData.csv || inputData[this.data.input_ids.csv];
const open_ai_key = inputData.open_ai_key || inputData[this.data.input_ids.open_ai_key];
const cluster_extraction =
inputData.cluster_extraction || inputData[this.data.input_ids.cluster_extraction];
if (!csv || csv.length == 0 || !cluster_extraction) {
this.data.message = `${$__('missing_input_data')}`;
this.data.dirty = false;
return;
}
if (!this.data.dirty && this.data.csv_length == csv.length && this.data.gcs_path) {
let doc: any = await readFileFromGCS(this);
if (typeof doc === 'string') {
doc = JSON.parse(doc);
}
const numClaims = _.reduce(doc, (sum, value) => sum + value.claims.length, 0);
this.data.message = `${$__('loaded_from_gcs')}. ${$__('comments')}: ${
_.keys(doc).length
} ${$__('claims')}: ${numClaims}.`;
this.data.output = doc;
this.data.dirty = false;
return this.data.output;
}
if (context == 'run' && open_ai_key && (prompt || system_prompt)) {
this.data.output = {};
const clusters = JSON.stringify(cluster_extraction);
const todo = new Set(_.range(0, csv.length));
const gptPromises = [];
for (let i = 0; i < csv.length; i++) {
gptPromises.push(
(async () => {
try {
const comment = csv[i]['comment-body'];
const interview = csv[i]['interview'];
const replacements = {
comment: comment,
clusters: clusters
};
const response = await gpt(
open_ai_key,
replacements,
prompt,
system_prompt,
info,
error,
success,
i,
csv.length,
todo
);
return {
id: csv[i]['comment-id'],
...JSON.parse(response),
comment,
interview
};
} catch (err) {
error((err as Error).message);
// Return null or handle the error as desired
return null;
}
})()
);
}
info(`${$__('calling_openai')}: [${csv.length}]`);
const startTime = performance.now();
console.time($__('calling_openai'));
const results = await Promise.all(gptPromises);
console.timeEnd($__('calling_openai'));
const endTime = performance.now();
const timeTaken = endTime - startTime;
success(`${$__('calling_openai')}: ${Math.floor(timeTaken / 1000)} ${$__('seconds')}`);
results.forEach((result) => {
if (result) this.data.output[result.id] = result;
});
const numClaims = _.reduce(this.data.output, (sum, value) => sum + value.claims.length, 0);
this.data.message = `${$__('comments')}: ${_.keys(this.data.output).length} ${$__(
'claims'
)}: ${numClaims}.`;
this.data.csv_length = csv.length;
this.data.dirty = false;
await uploadJSONToGCS(this, this.data.output, slug);
return this.data.output;
} else {
this.data.message = `${$__('missing_input_data')}`;
this.data.dirty = false;
return;
}
}
}
interface ArgumentExtractionData extends GCSBaseData {
output: Record<string, any>;
text: string;
system_prompt: string;
prompt: string;
csv_length: number;
}
type ArgumentExtractionNodeInterface = DGNodeInterface & {
data: ArgumentExtractionData;
};
export const argument_extraction_node_data_v0: ArgumentExtractionNodeInterface = {
id: 'argument_extraction',
data: {
label: 'argument_extraction',
output: {},
text: '',
system_prompt: argument_extraction_system_prompt,
prompt: argument_extraction_prompt_v0,
csv_length: 0,
dirty: false,
compute_type: 'argument_extraction_v0',
input_ids: { open_ai_key: '', csv: '', cluster_extraction: '' },
category: categories.ml.id,
icon: 'argument_extraction_v0',
show_in_ui: true,
message: '',
filename: '',
size_kb: 0,
gcs_path: ''
},
position: { x: 0, y: 0 },
type: 'prompt_v0'
};
export const argument_extraction_node_v0 = new ArgumentExtractionNode(
argument_extraction_node_data_v0
);
nodes.register(ArgumentExtractionNode, argument_extraction_node_data_v0);