Skip to content

Commit aa58b09

Browse files
committed
add node example
1 parent ac221e6 commit aa58b09

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed

src/examples/node/index.js

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
const {
2+
Configuration,
3+
PipelinesApi,
4+
ExtractionApi,
5+
UploadsApi,
6+
ConnectorsApi, FilesApi
7+
} = require("@vectorize-io/vectorize-client");
8+
const fs = require("fs");
9+
10+
11+
const token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3NDExNjAwMzgsImF1ZCI6IjU4Mjg5M2JhLTI5MWMtNGVjNy1hNmVlLWU4NWMyNjg4ODgxNyIsInJvbGUiOiJhZG1pbiIsImF1dGhvcml6YXRpb25fZGV0YWlscyI6W3sibmFtZSI6IlJFVFJJRVZBTF9BQ0NFU1NfVE9LRU4iLCJpc1N0YW5kYXJkUm9sZSI6dHJ1ZSwicGVybWlzc2lvbnMiOnsiVmVyc2lvbiI6IjEuMCIsIlN0YXRlbWVudCI6W3siQWN0aW9uIjpbIk9yZzpQaXBlbGluZXM6UmV0cmlldmFsIl0sIlJlc291cmNlIjpbIi9vcmdhbml6YXRpb24vNTgyODkzYmEtMjkxYy00ZWM3LWE2ZWUtZTg1YzI2ODg4ODE3Il0sIkVmZmVjdCI6IkFsbG93In1dfX1dLCJzdWIiOiJ2ZWN0b3JpemUifQ.ENZ0XwrtldldHYwaImHt_TybDife9OHNZcNNQFBnyDu6I1RLtGHE0itrXizamrQRbRYfy9n7_SrCnqDBgSZDf0lrR6zqwd2bBOq1XvwRhK8bDKgUwO9wbDVOqVjzlVCTNOegwpVNT3joOf3rsNBQUFl-MkYFfb8e00s7a0z782IaMxf__688FOpMXP-d7mr5bafI76xyRDYNQUrBVVV11kaqVnIn-jT0aXR2hIlZF4jZoqaRMrghwkcNANFPL30iy6_kRDRtpDjup54z6ElH-_gWgzJRBBaaWWbN5BJy1o3k6kDEb_T5eUDZEUWBE2OlAxeaa9dxARWZgotE395LwQ"
12+
const org = "582893ba-291c-4ec7-a6ee-e85c26888817"
13+
14+
15+
async function createPipeline() {
16+
try {
17+
const api = new Configuration({
18+
accessToken: token,
19+
basePath: "https://api-dev.vectorize.io/v1"
20+
});
21+
const pipelinesApi = new PipelinesApi(api);
22+
const uploadsApi = new UploadsApi(api);
23+
const connectorsApi = new ConnectorsApi(api);
24+
const extractionApi = new ExtractionApi(api);
25+
26+
const sourceResponse = await connectorsApi.createSourceConnector({
27+
organization: org,
28+
requestBody: [
29+
{type: "FILE_UPLOAD", name: "My first upload connector"}
30+
]
31+
});
32+
const sourceConnectorId = sourceResponse.connectors[0].id;
33+
34+
const fileBuffer = fs.readFileSync("path/to/file.pdf");
35+
36+
const uploadResponse = await uploadsApi.startFileUploadToConnector({
37+
organization: org,
38+
connectorId: sourceConnectorId,
39+
startFileUploadToConnectorRequest: {
40+
name: "file.pdf",
41+
contentType: "application/pdf",
42+
// add additional metadata that will be stored along with each chunk in the vector database
43+
metadata: JSON.stringify({"mymeta": true})
44+
}
45+
})
46+
47+
const fetchResponse = await fetch(uploadResponse.uploadUrl, {
48+
method: 'PUT',
49+
body: fileBuffer,
50+
headers: {
51+
'Content-Type': 'application/pdf'
52+
},
53+
});
54+
if (!fetchResponse.ok) {
55+
throw new Error(`Failed to upload file: ${fetchResponse.statusText}`);
56+
}
57+
58+
const aiPlatformResponse = await connectorsApi.getAIPlatformConnectors({
59+
organization: org
60+
});
61+
const builtinAIPlatformId = aiPlatformResponse.aiPlatformConnectors.find((connector) => connector.type === "VECTORIZE").id;
62+
63+
const destinationResponse = await connectorsApi.getDestinationConnectors({
64+
organization: org
65+
});
66+
const builtinVectorDatabaseId = destinationResponse.destinationConnectors.find((connector) => connector.type === "VECTORIZE").id;
67+
68+
69+
const response = await pipelinesApi.createPipeline({
70+
organization: org,
71+
pipelineConfigurationSchema: {
72+
pipelineName: "My Pipeline From API",
73+
sourceConnectors: [{id: sourceConnectorId, type: "FILE_UPLOAD", config: {}}],
74+
destinationConnector: {
75+
id: builtinVectorDatabaseId,
76+
type: "VECTORIZE",
77+
config: {}
78+
},
79+
aiPlatform: {
80+
id: builtinAIPlatformId,
81+
type: "VECTORIZE",
82+
config: {}
83+
},
84+
schedule: {type: "manual"}
85+
}
86+
87+
})
88+
const pipelineId = response.data.id;
89+
console.log(`Pipeline Id: ${pipelineId}`)
90+
91+
92+
} catch (error) {
93+
console.error(error?.response);
94+
console.error(await error?.response?.text());
95+
throw error
96+
}
97+
}
98+
99+
100+
async function pipelineFunctions(pipelineId) {
101+
try {
102+
const api = new Configuration({
103+
accessToken: token,
104+
basePath: "https://api-dev.vectorize.io/v1"
105+
});
106+
const pipelinesApi = new PipelinesApi(api);
107+
const uploadsApi = new UploadsApi(api);
108+
const connectorsApi = new ConnectorsApi(api);
109+
const extractionApi = new ExtractionApi(api);
110+
const documentsResponse = await pipelinesApi.retrieveDocuments({
111+
organization: org,
112+
pipeline: pipelineId,
113+
retrieveDocumentsRequest: {
114+
question: "<your-question>",
115+
numResults: 5,
116+
}
117+
});
118+
console.log(documentsResponse.documents)
119+
120+
const response = await pipelinesApi.startDeepResearch({
121+
organization: org,
122+
pipeline: pipelineId,
123+
startDeepResearchRequest: {
124+
// make sure to include a relevant prompt here
125+
query: "Generate a report on financial status of company XX",
126+
// optionally enable additional search on the web
127+
webSearch: false
128+
}
129+
});
130+
const researchId = response.researchId;
131+
while (true) {
132+
const result = await pipelinesApi.getDeepResearchResult({
133+
organization: org,
134+
pipeline: pipelineId,
135+
researchId: researchId
136+
})
137+
if (result.ready) {
138+
if (result.data.success) {
139+
console.log(result.data.markdown)
140+
} else {
141+
console.log("Deep Research failed: ", result.data.error)
142+
}
143+
break
144+
} else {
145+
console.log("not ready")
146+
}
147+
}
148+
149+
} catch
150+
(error) {
151+
console.error(error?.response);
152+
console.error(await error?.response?.text());
153+
throw error
154+
}
155+
}
156+
157+
158+
async function extraction() {
159+
try {
160+
const api = new Configuration({
161+
accessToken: token,
162+
basePath: "https://api-dev.vectorize.io/v1"
163+
});
164+
const pipelinesApi = new PipelinesApi(api);
165+
const uploadsApi = new UploadsApi(api);
166+
const connectorsApi = new ConnectorsApi(api);
167+
const filesApi = new FilesApi(api);
168+
const extractionApi = new ExtractionApi(api);
169+
170+
const contentType = "application/pdf";
171+
const startResponse = await filesApi.startFileUpload({
172+
organization: org,
173+
startFileUploadRequest: {
174+
name: "My File",
175+
contentType
176+
}
177+
});
178+
179+
const fileBuffer = fs.readFileSync("path/to/file.pdf");
180+
const fetchResponse = await fetch(startResponse.uploadUrl, {
181+
method: 'PUT',
182+
body: fileBuffer,
183+
headers: {
184+
'Content-Type': contentType
185+
},
186+
});
187+
if (!fetchResponse.ok) {
188+
throw new Error(`Failed to upload file: ${fetchResponse.statusText}`);
189+
}
190+
191+
const response = await extractionApi.startExtraction({
192+
organization: org,
193+
startExtractionRequest: {
194+
fileId: startResponse.fileId,
195+
// the extraction will also chunk the file as it would do in a RAG pipeline
196+
chunkSize: 512,
197+
}
198+
})
199+
const extractionId = response.extractionId;
200+
201+
while (true) {
202+
const result = await extractionApi.getExtractionResult({
203+
organization: org,
204+
extractionId: extractionId,
205+
})
206+
if (result.ready) {
207+
if (result.data.success) {
208+
console.log(result.data.text)
209+
} else {
210+
console.log("Extraction failed: ", result.data.error)
211+
}
212+
break
213+
} else {
214+
console.log("not ready")
215+
}
216+
}
217+
} catch
218+
(error) {
219+
console.error(error?.response);
220+
console.error(await error?.response?.text());
221+
throw error
222+
}
223+
}
224+
225+
async function main() {
226+
const pipelineId = await createPipeline();
227+
await pipelineFunctions(pipelineId);
228+
await extraction();
229+
230+
231+
}
232+
233+
main()

src/examples/node/package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/examples/node/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "vectorize-example",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"author": "",
9+
"license": "ISC",
10+
"description": "",
11+
"dependencies": {
12+
"@vectorize-io/vectorize-client": "^0.1.2"
13+
}
14+
}

0 commit comments

Comments
 (0)