Skip to content

Commit

Permalink
add pinecone benchmarks and quick fixes for Astra benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 20, 2024
1 parent 13588e1 commit 5ea1ce6
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 56 deletions.
22 changes: 22 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@ $ env ASTRA_CONNECTION_STRING="https://abc-us-east-2.apps.astra.datastax.com/...
"name": "benchmark-findone-mongoose-vector-astra",
"totalTimeMS": 20436
}
```

## Pinecone Benchmarks

First, install the Pinecone client:

```
npm install @pinecone-database/pinecone --no-save
```

Then set up sample data:

```
```
env PINECONE_INDEX=your-index-here env PINECONE_API_KEY=your-api-key-here ./node_modules/.bin/ts-node benchmarks/setup-pinecone-vector-example.ts
```
```

Then run Pinecone benchmarks:

```
env PINECONE_INDEX=your-index-here env PINECONE_API_KEY=your-api-key-here ./node_modules/.bin/ts-node benchmarks/benchmark-findone-pinecone.ts
```
36 changes: 36 additions & 0 deletions benchmarks/benchmark-findone-pinecone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
main().then(
() => { process.exit(0); },
err => {
console.error(err);
process.exit(-1);
}
);

async function main() {
let Pinecone;
try {
Pinecone = require('@pinecone-database/pinecone').Pinecone;

Check failure on line 12 in benchmarks/benchmark-findone-pinecone.ts

View workflow job for this annotation

GitHub Actions / lint

Require statement not part of import statement
} catch (err) {
console.log('{"name":"benchmark-findone-pinecone"}');
return;
}

if (!process.env.PINECONE_API_KEY || !process.env.PINECONE_INDEX) {
console.log('{"name":"benchmark-findone-pinecone"}');
return;
}

const pinecone = new Pinecone();

const vector = [1, ...Array(1535).fill(0)];
const index = process.env.PINECONE_INDEX;
const start = Date.now();
for (let i = 0; i < 100; ++i) {
await pinecone.index(index).query({ topK: 1, vector, includeValues: true });
}
const results = {
name: 'benchmark-findone-pinecone',
totalTimeMS: Date.now() - start
};
console.log(JSON.stringify(results, null, ' '));
}
96 changes: 49 additions & 47 deletions benchmarks/setup-astra-vector-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,59 @@ main().then(
);

async function main() {
if (!process.env.ASTRA_CONNECTION_STRING) {
throw new Error('Must set ASTRA_CONNECTION_STRING');
}
if (!process.env.ASTRA_COLLECTION_NAME) {
throw new Error('Must set ASTRA_COLLECTION_NAME');
}
await mongoose.connect(process.env.ASTRA_CONNECTION_STRING, {
isAstra: true
} as mongoose.ConnectOptions);
if (!process.env.ASTRA_CONNECTION_STRING) {
throw new Error('Must set ASTRA_CONNECTION_STRING');
}
if (!process.env.ASTRA_COLLECTION_NAME) {
throw new Error('Must set ASTRA_COLLECTION_NAME');
}
await mongoose.connect(process.env.ASTRA_CONNECTION_STRING, {
isAstra: true,
useHTTP2: false
} as mongoose.ConnectOptions);

const Vector = mongoose.model(
'Vector',
new mongoose.Schema({
$vector: {
type: [Number]
},
prompt: {
type: String,
required: true
}
}, {
collectionOptions: { vector: { dimension: 1536, metric: 'cosine' } },
autoCreate: false
}),
process.env.ASTRA_COLLECTION_NAME
);
const Vector = mongoose.model(
'Vector',
new mongoose.Schema({
$vector: {
type: [Number]
},
prompt: {
type: String,
required: true
}
}, {
collectionOptions: { vector: { dimension: 1536, metric: 'cosine' } },
autoCreate: false
}),
process.env.ASTRA_COLLECTION_NAME
);

console.log('Recreating collection...');
await Vector.db.dropCollection(process.env.ASTRA_COLLECTION_NAME);
await Vector.createCollection();
console.log('Recreating collection...');
await Vector.db.dropCollection(process.env.ASTRA_COLLECTION_NAME);
await Vector.createCollection();

console.log('Creating vectors...');
const numVectors = 1000;
for (let i = 0; i < numVectors; ++i) {
console.log(`${i} / ${numVectors}`);
const $vector = Array(1536).fill(0);
$vector[i] = 1;
for (let j = 0; j < 3; ++j) {
try {
await Vector.create({ $vector, prompt: `Test ${i}` });
break;
} catch (err) {
if (j >= 2) {
throw err;
}
await new Promise(resolve => setTimeout(resolve, 100));
console.log('Creating vectors...');
const numVectors = 1000;
const start = Date.now();
for (let i = 0; i < numVectors; ++i) {
console.log(`${i} / ${numVectors}`);
const $vector = Array(1536).fill(0);
$vector[i] = 1;
for (let j = 0; j < 3; ++j) {
try {
await Vector.create({ $vector, prompt: `Test ${i}` });
break;
} catch (err) {
if (j >= 2) {
throw err;
}
await new Promise(resolve => setTimeout(resolve, 100));
}
}
}

}
}

console.log('Done');
process.exit(0);
console.log('Done', Date.now() - start);
process.exit(0);
}
38 changes: 38 additions & 0 deletions benchmarks/setup-pinecone-vector-example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Pinecone } from '@pinecone-database/pinecone';

main().then(
() => { process.exit(0); },
err => {
console.error(err);
process.exit(-1);
}
);

async function main() {
if (!process.env.PINECONE_API_KEY || !process.env.PINECONE_INDEX) {
throw new Error('Cannot run pinecone setup without an API key');
}

const pinecone = new Pinecone();

console.log('Creating vectors...');

const numVectors = 1000;
const index = process.env.PINECONE_INDEX;
const start = Date.now();
for (let i = 0; i < numVectors; ++i) {
console.log(`${i} / ${numVectors}`);
const values: number[] = Array(1536).fill(0);
values[i] = 1;
await pinecone.index(index).upsert([{
id: '' + i,
values,
metadata: {
prompt: `Test ${i}`
}
}]);
}

console.log('Done', Date.now() - start);
process.exit(0);
}
18 changes: 9 additions & 9 deletions benchmarks/upload-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ async function main() {
'BenchmarkResult',
new mongoose.Schema({
githash: {
type: String,
required: true
type: String,
required: true
},
name: {
type: String,
required: true
type: String,
required: true
},
totalTimeMS: {
type: Number
type: Number
}
}, { autoCreate: true, timestamps: true })
);
Expand All @@ -53,12 +53,12 @@ async function main() {
fs.readFileSync(path.join('.', 'benchmarks', file), 'utf8')
);
if (totalTimeMS == null) {
continue;
continue;
}
const doc = await BenchmarkResult.findOneAndUpdate(
{ githash, name },
{ totalTimeMS },
{ upsert: true, returnDocument: 'after' }
{ githash, name },
{ totalTimeMS },
{ upsert: true, returnDocument: 'after' }
);
console.log(doc);
}
Expand Down

0 comments on commit 5ea1ce6

Please sign in to comment.