Skip to content

Commit c4143e2

Browse files
committed
Add notebook
1 parent 3c786ea commit c4143e2

File tree

4 files changed

+1080
-0
lines changed

4 files changed

+1080
-0
lines changed

src/examples/node/index.js

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