-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsetup.ts
152 lines (126 loc) · 4.27 KB
/
setup.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
import { Pinecone } from '../index';
import {
diffPrefix,
generateRecords,
globalNamespaceOne,
prefix,
randomString,
randomIndexName,
sleep,
} from './test-helpers';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
const setup = async () => {
let apiKey: string;
if (process.env['PINECONE_API_KEY'] === undefined) {
throw new Error('PINECONE_API_KEY environment variable not set');
} else {
apiKey = process.env['PINECONE_API_KEY'];
}
const client = new Pinecone({ apiKey: apiKey });
// both of these processes create the external resources, and then store the names in the GITHUB_OUTPUT env var
await Promise.all([createServerlessIndex(client), createAssistant(client)]);
};
// main entrypoint
setup();
async function createServerlessIndex(client: Pinecone) {
let serverlessIndexName = randomIndexName('serverless-integration');
const indexes = await client.listIndexes();
const serverlessIndex = indexes.indexes?.find(
(index) => index.spec.serverless
);
serverlessIndexName = serverlessIndex?.name || serverlessIndexName;
const createAndSeedNewServerlessIndex = async (newIndexName: string) => {
// Create serverless index for data plane tests
await client.createIndex({
name: newIndexName,
dimension: 2,
metric: 'dotproduct',
spec: {
serverless: {
cloud: 'aws',
region: 'us-west-2',
},
},
waitUntilReady: true,
tags: { project: 'pinecone-integration-tests-serverless' },
});
// Seed index with data
const recordsToUpsert = generateRecords({
prefix: prefix,
dimension: 2,
quantity: 10,
withSparseValues: false,
withMetadata: true,
});
// (Upsert 1 record with a different prefix, so can test prefix filtering)
const oneRecordWithDiffPrefix = generateRecords({
prefix: diffPrefix,
dimension: 2,
quantity: 1,
withSparseValues: false,
withMetadata: true,
});
const allRecords = [...oneRecordWithDiffPrefix, ...recordsToUpsert];
// upsert records into namespace
await client
.index(newIndexName)
.namespace(globalNamespaceOne)
.upsert(allRecords);
// wait for records to become available
await sleep(25000);
};
// if there's not an existing serverlessIndex, create one
if (!serverlessIndex) {
await createAndSeedNewServerlessIndex(serverlessIndexName);
}
// Capture output in GITHUB_OUTPUT env var when run in CI; necessary to pass across tests
console.log(`SERVERLESS_INDEX_NAME=${serverlessIndexName}`);
}
async function createAssistant(client: Pinecone) {
// Set up an Assistant and upload a file to it
const assistantName = randomString(5);
await client.createAssistant({
name: assistantName,
instructions: 'test-instructions',
metadata: { key: 'valueOne', keyTwo: 'valueTwo' },
region: 'us',
});
await sleep(5000);
try {
await client.describeAssistant(assistantName);
} catch (e) {
console.log('Error getting assistant:', e);
}
const assistant = client.Assistant(assistantName);
// Capture output in GITHUB_OUTPUT env var when run in CI; necessary to pass across tests
console.log(`ASSISTANT_NAME=${assistantName}`);
const tempFileName = path.join(os.tmpdir(), `tempfile-${Date.now()}.txt`);
// Capture output in GITHUB_OUTPUT env var when run in CI; necessary to pass across tests
console.log(`TEST_FILE=${tempFileName}`);
try {
const data = 'This is some temporary data';
fs.writeFileSync(tempFileName, data);
console.log(`Temporary file created: ${tempFileName}`);
} catch (err) {
console.error('Error writing file:', err);
}
// Add a small delay to ensure file system sync
await sleep(1000);
// Upload file to assistant so chat works
const file = await assistant.uploadFile({
path: tempFileName,
metadata: { key: 'valueOne', keyTwo: 'valueTwo' },
});
console.log('File uploaded:', file);
// Another sleep b/c it currently takes a *long* time for a file to be available
await sleep(30000);
// Delete file from local file system
try {
fs.unlinkSync(path.resolve(process.cwd(), tempFileName));
console.log(`Temporary file deleted: ${tempFileName}`);
} catch (err) {
console.error('Error deleting file:', err);
}
}