Skip to content

Commit 2a18878

Browse files
committed
addressing comments
1 parent 94b927f commit 2a18878

File tree

6 files changed

+72
-30
lines changed

6 files changed

+72
-30
lines changed

packages/bolt-connection/src/bolt/bolt-protocol-v6x0.transformer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function createVectorTransformer () {
4141
signature: VECTOR,
4242
isTypeInstance: object => object instanceof Vector,
4343
toStructure: vector => {
44-
const typeMarker = typeToTypeMarker[vector.type]
45-
const buffer = fixBufferEndianness(typeMarker, vector.typedArray.buffer)
44+
const typeMarker = typeToTypeMarker[vector.getType()]
45+
const buffer = fixBufferEndianness(typeMarker, vector.asTypedArray().buffer)
4646
const struct = new structure.Structure(VECTOR, [Int8Array.from([typeMarker]), new Int8Array(buffer)])
4747
return struct
4848
},

packages/core/src/vector.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,40 +37,48 @@ Object.freeze(vectorTypes)
3737
* A wrapper class for JavaScript TypedArrays that makes the driver send them as a Vector type to the database.
3838
* @access public
3939
* @exports Vector
40-
* @class A Integer class for representing a 64 bit two's-complement integer value.
40+
* @class A Vector class that wraps a JavaScript TypedArray to enable writing/reading the Neo4j Vector type.
4141
* @param {Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array} typedArray The TypedArray to convert to a vector
4242
*
4343
* @constructor
4444
*
4545
*/
4646
export default class Vector<K extends Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array> {
47-
typedArray: K
48-
type: VectorType
47+
_typedArray: K
48+
_type: VectorType
4949
constructor (typedArray: K) {
5050
if (typedArray instanceof Int8Array) {
51-
this.type = vectorTypes.INT8
51+
this._type = vectorTypes.INT8
5252
} else if (typedArray instanceof Int16Array) {
53-
this.type = vectorTypes.INT16
53+
this._type = vectorTypes.INT16
5454
} else if (typedArray instanceof Int32Array) {
55-
this.type = vectorTypes.INT32
55+
this._type = vectorTypes.INT32
5656
} else if (typedArray instanceof BigInt64Array) {
57-
this.type = vectorTypes.INT64
57+
this._type = vectorTypes.INT64
5858
} else if (typedArray instanceof Float32Array) {
59-
this.type = vectorTypes.FLOAT32
59+
this._type = vectorTypes.FLOAT32
6060
} else if (typedArray instanceof Float64Array) {
61-
this.type = vectorTypes.FLOAT64
61+
this._type = vectorTypes.FLOAT64
6262
} else {
6363
throw newError(`The neo4j Vector class is a wrapper for TypedArrays. got ${typeof typedArray}`)
6464
}
65-
this.typedArray = typedArray
65+
this._typedArray = typedArray
6666
}
6767

6868
/**
6969
* Converts the Vector back to a typedArray
7070
* @returns {Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array} - a TypedArray of the Vectors type.
7171
*/
72-
toTypedArray (): K {
73-
return this.typedArray
72+
asTypedArray (): K {
73+
return this._typedArray
74+
}
75+
76+
/**
77+
* Gets the type of the Vector
78+
* @returns {VectorType} - The type of the vector, corresponding to the type of the wrapped TypedArray.
79+
*/
80+
getType (): VectorType {
81+
return this._type
7482
}
7583
}
7684

packages/neo4j-driver-deno/lib/bolt-connection/bolt/bolt-protocol-v6x0.transformer.js

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

packages/neo4j-driver-deno/lib/core/vector.ts

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

packages/neo4j-driver-lite/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,29 @@ var driver = neo4j.driver(
390390
{ disableLosslessIntegers: true }
391391
)
392392
```
393+
394+
#### Writing and reading Vectors
395+
396+
Neo4j supports storing vector embeddings in a dedicated vector type. Sending large lists with the driver will result in significant overhead as each value will be transmitted with type information, so the 6.0.0 release of the driver introduced the Neo4j Vector type.
397+
398+
The Vector type supports signed integers of 8, 16, 32 and 64 bits, and floats of 32 and 64 bits. The Vector type is a wrapper for JavaScript TypedArrays of those types.
399+
400+
To create a neo4j Vector in your code, do the following:
401+
402+
```javascript
403+
var neo4j = require('neo4j-driver')
404+
405+
var typedArray = Float32Array.from([1, 2, 3]) //this is how to convert a regular array of numbers into a TypedArray, useful if you handle vectors as regular arrays in your code
406+
407+
var neo4jVector = neo4j.vector(typedArray) //this creates a neo4j Vector of type Float32, containing the values [1, 2, 3]
408+
409+
driver.executeQuery('CREATE (n {embeddings: $myVectorParam})', { myVectorParam: neo4jVector })
410+
```
411+
412+
To access the data in a retrieved Vector you can do the following:
413+
414+
```javascript
415+
var retrievedTypedArray = neo4jVector.toTypedArray() //This will return a TypedArray of the same type as the Vector
416+
417+
var retrievedArray = Array.from(retrievedTypedArray) //This will convert the TypedArray to a regular array of Numbers. (Not safe for Int64 arrays)
418+
```

packages/testkit-backend/src/cypher-native-binders.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ export default function CypherNativeBinders (neo4j) {
176176
})
177177
}
178178

179-
if (x.typedArray != null) {
179+
if (x.asTypedArray != null) {
180180
const dtype = typeToDType[x.type]
181-
const buffer = fixBufferEndianness(dtype, x.typedArray.buffer)
181+
const buffer = fixBufferEndianness(dtype, x.asTypedArray.buffer)
182182
const data = toHexString(new Uint8Array(buffer))
183183
return structResponse('CypherVector', { dtype, data })
184184
}

0 commit comments

Comments
 (0)