KNN数据集节点
概述
KNN(K-最近邻)数据集节点用于在给定嵌入向量的情况下查找数据集中的k个最近邻。当您想要在数据集中找到与给定项目最相似的项目时,此节点特别有用。
节点在运行图时需要上下文中有一个可用的数据集提供者。数据集提供者负责管理数据集的存储和检索。在Rivet应用程序中,数据集提供者会自动为您处理。
有关数据集的更多信息,请参阅用户指南中的Data Studio部分。
- 输入
- 输出
- 编辑器设置
输入项
标题 | 数据类型 | 描述 | 默认值 | 备注 |
---|---|---|---|---|
Embedding | object | The embedding to use as the reference point for finding the nearest neighbors. | (required) | |
Dataset ID | string | The ID of the dataset to search for the nearest neighbors. This input is only available if Use Dataset ID Input is enabled. | (required if if the input toggle for Dataset ID is enabled) | The input will be coerced into a string if it is not a string. |
K | number | The number of nearest neighbors to find. This input is only available if Use K Input is enabled. | (required if if the input toggle for K is enabled) | The input will be coerced into a number if it is not a number. |
输出
标题 | 数据类型 | 描述 | 备注 |
---|---|---|---|
Nearest Neighbors | object[] | An array of the k nearest neighbors. Each object in the array contains the ID, distance, and data of a neighbor. | The output will be an array of objects. Each object represents a neighbor and contains the ID, distance, and data of the neighbor. |
编辑器设置
设置 | 描述 | 默认值 | 使用输入切换 | 输入数据类型 |
---|---|---|---|---|
Dataset | The ID of the dataset to search for the nearest neighbors. | (required) | Yes | string |
K | The number of nearest neighbors to find. | 5 | Yes | number |
示例1:在数据集中查找最近邻
- 首先,在Data Studio中创建一个名为
Employees
的新数据集。 - 在新图表中,添加一个Append to Dataset Node并将数据集ID设置为
Employees
。 - 添加一个Text Node并将值设为
John Doe
。我们将获取这个文本节点的嵌入向量。 - 添加一个Get Embedding Node并将Text Node连接到它。将Get Embedding Node的
Embedding
输出连接到Append to Dataset Node的Embedding
输入。 - 将文本节点连接到追加到数据集节点的
Data
输入端口。 - 运行图表。这会将"John Doe"及其嵌入添加到
Employees
数据集中。 - 将文本节点的值更改为
Jane Doe
并再次运行图表。这会将"Jane Doe"及其嵌入附加到Employees
数据集中。
- 接下来,创建一个新图表,添加一个KNN数据集节点,并将数据集ID设置为
Employees
,K值设为1。 - 添加一个Text Node并将值设置为
Jane
。我们将查找与此文本节点最近的邻居。 - 添加一个Get Embedding Node并将Text Node连接到它。将Get Embedding Node的
Embedding
输出连接到KNN Dataset Node的Embedding
输入。 - 运行图形。这将在
Employees
数据集中找到与"Jane"最近的邻居,即"Jane Doe"。
错误处理
如果在运行图时上下文环境中没有可用的数据集提供程序,KNN数据集节点将会报错。如果没有提供Embedding
输入,它也会报错。
常见问题
问:什么是数据集提供者?
A: 数据集提供者是一个负责管理数据集存储和检索的对象。当图表运行时,它会被传递给上下文。数据集提供者必须实现DatasetProvider
接口,该接口包含获取、存放和删除数据集的方法。更多信息请参阅API参考文档。
问:有没有办法在不使用数据集的情况下获取最近邻?
您可以使用Vector KNN Node通过其他集成(如Pinecone)来查找最近邻。
问:如果数据集不存在会发生什么?
A: 如果数据集不存在,KNN Dataset Node 将会报错。