forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_embeddings_v0.test.ts
143 lines (131 loc) · 3.58 KB
/
gpt_embeddings_v0.test.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
import { describe, it, vi, expect, beforeEach } from 'vitest';
import GPTEmbeddingsNode, { gpt_embeddings_node_data } from '$lib/compute/gpt_embeddings_v0';
import deepCopy from 'deep-copy';
import { getDataset } from './test/mocks/dataset';
describe('GPTEmbeddingsNode class', () => {
let node;
let inputData;
const timeout = 60000;
const dataset = getDataset();
const mockEmbedding = [0.1, 0.2, 0.3, 0.4, 0.5];
beforeEach(() => {
vi.mock('openai', () => ({
default: vi.fn().mockImplementation(() => ({
embeddings: {
create: vi.fn().mockResolvedValue({
data: [{ embedding: [0.1, 0.2, 0.3, 0.4, 0.5] }]
})
}
}))
}));
node = new GPTEmbeddingsNode(deepCopy(gpt_embeddings_node_data));
inputData = {
open_ai_key: 'test_open_ai_key',
data: [{ claim: 'The sky is blue.' }, { claim: 'Grass is green.' }]
};
}, timeout);
it(
'should compute embeddings for input data',
async () => {
vi.mock('$lib/utils', () => ({
readFileFromGCS: vi.fn(() =>
Promise.resolve(JSON.stringify([{ embedding: [0.1, 0.2, 0.3, 0.4, 0.5] }]))
),
uploadJSONToGCS: vi.fn(() => Promise.resolve())
}));
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null,
dataset
);
expect(output).toEqual({
embeddings: [
{ values: mockEmbedding, id: 'The sky is blue.' },
{ values: mockEmbedding, id: 'Grass is green.' }
]
});
expect(node.data.length).toEqual(2);
expect(node.data.message).toContain('computed_embeddings');
},
timeout
);
it(
'should not compute embeddings if no open_ai_key is provided',
async () => {
delete inputData.open_ai_key;
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null,
dataset
);
expect(output).toBeUndefined();
},
timeout
);
it(
'should load embeddings from GCS if data length matches and save_to_gcs is true',
async () => {
vi.mock('$lib/utils', () => ({
readFileFromGCS: vi.fn(() =>
Promise.resolve(
JSON.stringify([
{ values: [0.1, 0.2, 0.3, 0.4, 0.5], id: 'The sky is blue.' },
{ values: [0.1, 0.2, 0.3, 0.4, 0.5], id: 'Grass is green.' }
])
)
),
uploadJSONToGCS: vi.fn(() => Promise.resolve())
}));
node.data.length = 2;
node.data.save_to_gcs = true;
node.data.gcs_path = 'gs://test_bucket/test_path';
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null,
dataset
);
expect(output).toEqual({
embeddings: [
{ values: mockEmbedding, id: 'The sky is blue.' },
{ values: mockEmbedding, id: 'Grass is green.' }
]
});
expect(node.data.message).toContain('loaded_from_gcs');
},
timeout
);
it(
'should handle no data input',
async () => {
inputData.data = undefined;
const output = await node.compute(
inputData,
'run',
console.log,
console.error,
console.log,
'test_slug',
null,
dataset
);
expect(output).toBeUndefined();
expect(node.data.message).toContain('no_data');
},
timeout
);
});