Skip to content

Commit

Permalink
ts2
Browse files Browse the repository at this point in the history
  • Loading branch information
daveatweaviate committed Sep 13, 2024
1 parent efc6c2c commit 448bfe0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 95 deletions.
90 changes: 71 additions & 19 deletions _includes/code/howto/indexes/indexes-v2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// TODO: Configure as part of the test harness
// TODO: Needs tests


var DEBUG = true;

// Imports
import weaviate, { WeaviateClient } from 'weaviate-ts-client';

Expand Down Expand Up @@ -31,14 +28,72 @@ function deleteClass(client, className: string){
// ENABLE HNSW - COLLECTION //
//////////////////////////////

async function createHNSWCollection(client, className: string){
if(DEBUG) console.log("START HNSW: " + await client.schema.exists(className))
// START EnableHNSW
// Add this import line
// import { vectorizer, dataType, configure } from 'weaviate-client';
async function createHNSWCollection(client: WeaviateClient, className: string){

const setIndexType = {
class: className,
// Add property definitions
vectorizer: 'text2vec-openai',
vectorIndexType: 'hnsw',
vectorIndexConfig: {
distance: 'cosine',
ef_construction: '256', // Dynamic list size during construction
max_connections: '128', // Maximum number of connections per node
quantizer: 'Configure.VectorIndex.Quantizer.pq()', // Quantizer configuration
ef: '-1', // Dynamic list size during search; -1 enables dynamic Ef
dynamic_ef_factor: '15', // Multiplier for dynamic Ef
dynamic_ef_min: '200', // Minimum threshold for dynamic Ef
dynamic_ef_max: '1000', // Maximum threshold for dynamic Ef
pq: {
enabled: true,
trainingLimit: 100000,
segments: 96,
},
},
};

// Add the class to the schema
await client.schema.classCreator().withClass(setIndexType).do();
}
// END EnableHNSW

if(DEBUG) console.log("END HNSW: " + await client.schema.exists(className))
//////////////////////////////
/// ENABLE HNSW - MULTIPLE ///
//////////////////////////////

// START EnableMulti
async function createMultiCollection(client: WeaviateClient, className: string){

const classWithNamedVectors = {
class: className,
vectorConfig: {
// Define a named vector
vectorForFieldOne: {
vectorizer: {
'text2vec-cohere': {
properties: ['FieldOne'],
},
},
vectorIndexType: 'hnsw',
},
// Define another named vector
vectorForFieldTwo: {
vectorizer: {
'text2vec-openai': {
properties: ['FieldTwo'],
},
},
vectorIndexType: 'flat'
},
}
// Configure properties
}

// Add the class to the schema
await client.schema.classCreator().withClass(classWithNamedVectors).do();
}
// END EnableMulti

/////////////////////////////
/// AVOID TOP LEVEL AWAIT ///
Expand All @@ -49,24 +104,21 @@ async function main(){
const className = "ConfigCollection";

const client = await getClient();
if(DEBUG) console.log(client) ;

if(DEBUG) console.log("START: " + await client.schema.exists(className))
deleteClass(client, className)
if(DEBUG) console.log("START +1: " + await client.schema.exists(className))

// Only one create can run at a time due to aynsc code
// Run enable HNSW collection code
// deleteClass(client, className)
// if(await client.schema.exists(className) != true){
// createHNSWCollection(client, className);
// }

// // Run multiple named vector collection code
deleteClass(client, className)
if(await client.schema.exists(className) != true){
createHNSWCollection(client, className);
}
createMultiCollection(client, className);
}

// // Run multiple named vector collection code
// deleteClass(client, className)
// if(await client.schema.exists(className) != true){
// createMultiCollection(client, className);
// }
}

main()
72 changes: 0 additions & 72 deletions _includes/code/howto/indexes/indexes-v3.js

This file was deleted.

7 changes: 3 additions & 4 deletions _includes/code/howto/indexes/indexes-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,16 @@ async function createHNSWCollection(client: WeaviateClient, collectionName: stri
/// ENABLE HNSW - MULTIPLE ///
//////////////////////////////

async function createMultiCollection(client: WeaviateClient, collectionName: string){
// START EnableMulti

async function createMultiCollection(client: WeaviateClient, collectionName: string){
// Add this import line
// import { vectorizer, configure } from 'weaviate-client';

await client.collections.create({
name: collectionName,
vectorizers: [
// Define a named vector
vectorizer.text2VecOpenAI({
vectorizer.text2VecCohere({
name: "vectorForFieldOne",
sourceProperties: ['FieldOne'],
vectorIndexConfig: configure.vectorIndex.hnsw()
Expand All @@ -68,8 +67,8 @@ async function createMultiCollection(client: WeaviateClient, collectionName: str
],
// Configure properties
})
// END EnableMulti
}
// END EnableMulti

/////////////////////////////
/// AVOID TOP LEVEL AWAIT ///
Expand Down

0 comments on commit 448bfe0

Please sign in to comment.