#Type for "vector" datatype

15 messages · Page 1 of 1 (latest)

halcyon bluff
#
export default async function embed(sentence: string): Promise<Float32Array> {
  const result = await extractor(sentence, {
    pooling: "mean",
    normalize: true,
  });

  return result.data as Float32Array; // the vector
}

Hello, consider this code. Here I have cast result.data. Normally, result is a tensor and result.data is of type DataArray. This doesn't align with the data Float32Array so I've just cast it, is it ok/recommended or is there a better way of doing it pls.

ocean viper
#

casting is generally not recommended, no

#

if the types are wrong or too vague, a cast may be fine, but it's more of an exception than a rule

halcyon bluff
#

yep I see, will try to see if I can come out with something

autumn fossil
#

in what way does it not align? why can't you use it as a DataArray?

halcyon bluff
autumn fossil
#

but also: is this about tricking the type system, or about the actual runtime data format? if the latter, then you need to actually perform the conversion rather than just asserting the type

halcyon bluff
#

I will give that a look but basically, I'm using node-postgres and I needed to store an array in my postgresql db. For it be able to store arrays, node-postgres (I think, I forgot if I read that somewhere or it was AI generated), it should be of type number[]

autumn fossil
#

in case you don't know this: as doesn't actually do anything at runtime. the compiled javascript code will just be return result.data. so if the runtime format of result.data is not what your database driver expects then you'll probably get runtime errors (or worse: silent data corruption)

halcyon bluff
#

ah ok

#

as just silent the warning but nothing change as such?

autumn fossil
#

yeah, it's called a "type assertion" because it asserts the type of the value

#

typescript in general doesn't generate much runtime code for you. it's mostly just type-level stuff that gets erased, while the runtime code you get in the emit is basically what you wrote

#

and as is not a javascript keyword, but a typescript one