将顶点ai文本嵌入响应转换为矢量表示

问题描述 投票:0回答:2

我正在尝试生成文本的向量表示,然后将其放入我的搜索数据库中以执行操作,例如语义搜索或推荐。

为此,我首先使用顶点 AI (

textembedding-gecko-multilingual@latest
) 来请求数据。不幸的是,我无法将此响应转换为可读的浮点数组。

...
// Predict request
  const [response] = await predictionServiceClient.predict(request);
  const predictions = response.predictions;
  console.log("\tPredictions:");
  for (const prediction of predictions) {
    console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`);
  }

这导致了一种非常不可用的格式,我无法将其放入我的搜索数据库中。 注意里面有向量,每个字段写成

{"numberValue":-0.08402...,"kind":"numberValue"}
:

{"predictions":[{"structValue":{"fields":{"embeddings":{"structValue":{"fields":{"statistics":{"structValue":{"fields":{"token_count":{"numberValue":1,"kind":"numberValue"},"truncated":{"boolValue":false,"kind":"boolValue"}}},"kind":"structValue"},"values":{"listValue":{"values":[{"numberValue":0.016404684633016586,"kind":"numberValue"},{"numberValue":-0.08402395248413086,"kind":"numberValue"} ....

当然我可以编写 JS 代码来解码响应,但我相信这不是最佳实践(因为语法可能会改变)。那么,如何检索内部向量并使其可读 - 可能使用 API 提供的函数?

node.js word-embedding google-cloud-vertex-ai
2个回答
0
投票

我也遇到同样的问题

我按照以下链接中的 NodeJS GCP 文档提供的示例进行操作

https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings#generative-ai-get-text-embedding-python_vertex_ai_sdk


0
投票

我找到了答案。在 Node.js 代码中,他们使用一个函数来转换。使用 toValue() 将 javascript 对象转换为

ÌValue
。您可以使用相同的方法来解码预测的响应:

const aiplatform = require('@google-cloud/aiplatform');
const {PredictionServiceClient} = aiplatform.v1;
const {helpers} = aiplatform;


helpers.fromValue(prediction); // Note: this cannot be a list of predictions
© www.soinside.com 2019 - 2024. All rights reserved.