跳到主要内容

Node.JS客户端

本页面提供 Pinecone Node.JS 客户端的安装指南、使用示例和参考文档。Pinecone Node.JS客户端

⚠️警告

这是一个"预览版"客户端,请在使用此客户端进行生产工作负载之前进行充分测试。对于此客户端不提供任何服务水平协议或技术支持承诺。预计在未来的发布版本中可能会出现潜在的破坏性变化。

入门指南

安装

使用以下Shell命令安装适用于Node.JS 17及以上版本的Node.JS客户端:

Shell

npm install @pinecone-database/pinecone

或者,您可以使用Yarn安装Pinecone:

Shell

yarn add @pinecone-database/pinecone

用法

初始化客户端

要初始化客户端,请实例化PineconeClient类并调用init方法。init方法需要一个具有apiKeyenvironment属性的对象:

JavaScript

import { PineconeClient } from "@pinecone-database/pinecone";

const pinecone = new PineconeClient();
await pinecone.init({
environment: "YOUR_ENVIRONMENT",
apiKey: "YOUR_API_KEY",
});

创建索引

以下示例创建一个没有元数据配置的索引。默认情况下,Pinecone索引所有元数据。

JavaScript

await pinecone.createIndex({
createRequest: {
name: "example-index",
dimension: 1024,
},
});

以下示例创建了一个仅索引“color”元数据字段的索引。针对此索引的查询不能基于任何其他元数据字段进行筛选。

JavaScript

await pinecone.createIndex({
createRequest: {
name: "example-index-2",
dimension: 1024,
metadataConfig: {
indexed: ["color"],
},
},
});

列出索引

以下示例记录项目中的所有索引。

JavaScript

const indexesList = await pinecone.listIndexes();

描述索引

以下示例记录了有关索引example-index的信息。

JavaScript

const indexDescription = await pinecone.describeIndex({
indexName: "example-index",
});

删除索引

以下示例删除了example-index

JavaScript

await pinecone.deleteIndex({
indexName: "example-index",
});

缩放副本

以下示例设置了example-index的副本数和Pod类型。

JavaScript

await pinecone.configureIndex({
indexName: "example-index",
patchRequest: {
replicas: 2,
podType: "p2",
},
});

描述索引统计信息

以下示例返回有关索引example-index的统计信息。

JavaScript

const index = pinecone.Index("example-index");
const indexStats = index.describeIndexStats({
describeIndexStatsRequest: {
filter: {},
},
});

更新向量

以下示例向example-index插入向量。

JavaScript

const index = pinecone.Index("example-index");
const upsertRequest = {
vectors: [
{
id: "vec1",
values: [0.1, 0.2, 0.3, 0.4],
metadata: {
genre: "drama",
},
},
{
id: "vec2",
values: [0.2, 0.3, 0.4, 0.5],
metadata: {
genre: "action",
},
},
],
namespace: "example-namespace",
};
const upsertResponse = await index.upsert({ upsertRequest });

查询索引

以下示例使用元数据过滤查询索引example-index

JavaScript

const index = pinecone.Index("example-index");
const queryRequest = {
vector: [0.1, 0.2, 0.3, 0.4],
topK: 10,
includeValues: true,
includeMetadata: true,
filter: {
genre: { $in: ["comedy", "documentary", "drama"] },
},
namespace: "example-namespace",
};
const queryResponse = await index.query({ queryRequest });

删除向量

以下示例通过ID删除向量。

JavaScript

const index = pinecone.Index("example-index");
await index.delete1({
ids: ["vec1", "vec2"],
namespace: "example-namespace",
});

获取向量

以下示例通过ID获取向量。

JavaScript

const index = pinecone.Index("example-index");
const fetchResponse = await index.fetch({
ids: ["vec1", "vec2"],
namespace: "example-namespace",
});

更新向量

以下示例按ID更新向量。

JavaScript

const index = pinecone.Index("example-index");
const updateRequest = {
id: "vec1",
values: [0.1, 0.2, 0.3, 0.4],
setMetadata: { genre: "drama" },
namespace: "example-namespace",
};
const updateResponse = await index.update({ updateRequest });

创建集合

以下示例从 example-index 创建集合 example-collection

JavaScript

const createCollectionRequest = {
name: "example-collection",
source: "example-index",
};

await pinecone.createCollection({
createCollectionRequest,
});

列出集合

以下示例返回当前项目中的集合列表。

JavaScript

const collectionsList = await pinecone.listCollections();

描述集合

以下示例返回集合 example-collection 的描述信息。

JavaScript

const collectionDescription = await pinecone.describeCollection({
collectionName: "example-collection",
});

删除集合

下面的示例代码会删除名为example-collection的集合。

JavaScript

await pinecone.deleteCollection({
collectionName: "example-collection",
});

参考文献

有关REST API或其他客户端,请参阅API参考文献

初始化()

pinecone.init(configuration: PineconeClientConfiguration)

初始化Pinecone客户端。

ParametersTypeDescription
configurationPineconeClientConfigurationThe configuration for the Pinecone client.

类型

PineconeClientConfiguration
ParametersTypeDescription
apiKeystringThe API key for the Pinecone service.
environmentstringThe cloud environment of your Pinecone project.

示例:

JavaScript

import { PineconeClient } from "@pinecone-database/pinecone";
const pinecone = new PineconeClient();
await pinecone.init({
apiKey: "YOUR_API_KEY",
environment: "YOUR_ENVIRONMENT",
});

configureIndex()

pinecone.configure_index(indexName: string, patchRequest?: PatchRequest)

配置索引以更改Pod类型和副本数。

ParametersTypeDescription
requestParametersConfigureIndexRequestIndex configuration parameters.

类型

ConfigureIndexRequest
ParametersTypeDescription
indexNamestringThe name of the index.
patchRequestPatchRequest(Optional) Patch request parameters.
PatchRequest
ParametersTypeDescription
replicasnumber(Optional) The number of replicas to configure for this index.
podTypestring(Optional) The new pod type for the index. One of s1, p1, or p2 appended with . and one of x1, x2, x4, or x8.

示例:

JavaScript

const newNumberOfReplicas = 4;
const newPodType = "s1.x4";
await pinecone.configureIndex({
indexName: "example-index",
patchRequest: {
replicas: newNumberOfReplicas,
podType: newPodType,
},
});

createCollection()

pinecone.createCollection(requestParameters: CreateCollectionOperationRequest)

从索引创建一个集合。

ParametersTypeDescription
requestParametersCreateCollectionOperationRequestCreate collection operation wrapper

类型

创建集合操作请求
ParametersTypeDescription
createCollectionRequestCreateCollectionRequestCollection request parameters.
创建集合请求
ParametersTypeDescription
namestringThe name of the collection to be created.
sourcestringThe name of the source index to be used as the source for the collection.

示例:

JavaScript

await pinecone.createCollection({
createCollectionRequest: {
name: "example-collection",
source: "example-index",
},
});

createIndex()

pinecone.createIndex(requestParameters?: CreateIndexRequest)

创建一个索引。

ParametersTypeDescription
requestParametersCreateIndexRequestCreate index operation wrapper

类型

创建索引请求

ParametersTypeDescription
createRequestCreateRequestCreate index request parameters
创建请求
ParametersTypeDescription
namestrThe name of the index to be created. The maximum length is 45 characters.
dimensionintegerThe dimensions of the vectors to be inserted in the index.
metricstr(Optional) The distance metric to be used for similarity search: 'euclidean', 'cosine', or 'dotproduct'.
podsint(Optional) The number of pods for the index to use, including replicas.
replicasint(Optional) The number of replicas.
pod_typestr(Optional) The new pod type for the index. One of s1, p1, or p2 appended with . and one of x1, x2, x4, or x8.
metadata_configobject(Optional) Configuration for the behavior of Pinecone's internal metadata index. By default, all metadata is indexed; when metadata_config is present, only specified metadata fields are indexed. To specify metadata fields to index, provide a JSON object of the following form: {"indexed": ["example_metadata_field"]}
source_collectionstr(Optional) The name of the collection to create an index from.

示例:

JavaScript

// The following example creates an index without a metadata
// configuration. By default, Pinecone indexes all metadata.
await pinecone.createIndex({
createRequest: {
name: "pinecone-index",
dimension: 1024,
},
});

// The following example creates an index that only indexes
// the 'color' metadata field. Queries against this index
// cannot filter based on any other metadata field.

await pinecone.createIndex({
createRequest: {
name: "example-index-2",
dimension: 1024,
metadata_config: {
indexed: ["color"],
},
},
});

deleteCollection()

pinecone.deleteCollection(requestParameters: DeleteCollectionRequest)

删除一个已存在的集合。

类型

ParametersTypeDescription
requestParametersDeleteCollectionRequestDelete collection request parameters

DeleteCollectionRequest

ParametersTypeDescription
collectionNamestringThe name of the collection to delete.

示例:

JavaScript

await pinecone.deleteCollection({
collectionName: "example-collection",
});

deleteIndex()

pinecone.deleteIndex(requestParameters: DeleteIndexRequest)

删除一个索引。

类型

ParametersTypeDescription
requestParametersDeleteIndexRequestDelete index request parameters
删除索引请求
ParametersTypeDescription
indexNamestringThe name of the index to delete.

示例:

JavaScript

await pinecone.deleteIndex({
indexName: "example-index",
});

描述集合

pinecone.describeCollection(requestParameters: DescribeCollectionRequest)

获取集合的描述。

Types

ParametersTypeDescription
requestParametersDescribeCollectionRequestDescribe collection request parameters
DescribeCollectionRequest
ParametersTypeDescription
collectionNamestringThe name of the collection.

Example:

JavaScript

const collectionDescription = await pinecone.describeCollection({
collectionName: "example-collection",
});

Return:

  • collectionMeta : object 集合的配置信息和部署状态。
    • name : string 集合的名称。
    • size: integer 集合的大小,以字节为单位。
    • status: string 集合的状态。

describeIndex()

pinecone.describeIndex(requestParameters: DescribeIndexRequest)

获取索引描述。

类型

ParametersTypeDescription
requestParametersDescribeIndexRequestDescribe index request parameters
DescribeIndexRequest
ParametersTypeDescription
indexNamestringThe name of the index.

类型

返回:

  • database : object
  • name : string 索引的名称。
  • dimension : integer 被插入到索引中的向量的维度。
  • metric : string 用于相似度搜索的距离度量方式,可选的值为'euclidean'、'cosine'和'dotproduct'。
  • pods : integer 索引使用的pod数量,包括副本。
  • replicas : integer 索引的副本数量。
  • pod_type : string 索引使用的pod类型,值为s1p1或者p2之一,后面跟着.x1x2x4或者x8之一。
  • metadata_config: object Pinecone内部元数据索引行为的配置信息。默认情况下将索引所有元数据。当出现metadata_config时,仅索引指定的元数据字段。要指定要索引的元数据字段,请提供以下形式的JSON对象:{"indexed": ["example_metadata_field"]}
  • status : object
  • ready : boolean 索引是否已准备好处理查询。
  • state : string 索引的状态,可能的值包括InitializingScalingUpScalingDownTerminatingReady。 示例:

JavaScript

const indexDescription = await pinecone.describeIndex({
indexName: "example-index",
});

listCollections

pinecone.listCollections()

返回项目中的集合列表。

示例:

JavaScript

const collections = await pinecone.listCollections();

返回值:

  • array of strings The names of the collections in your project.

listIndexes

pinecone.listIndexes()

返回 Pinecone 索引列表。

返回值:

  • array 类型,包含多个 strings 类型的元素,表示您项目中索引的名称。

示例:

JavaScript

const indexesList = await pinecone.listIndexes();

Index()

pinecone.Index(indexName: string)

构造一个 Index 对象。

ParametersTypeDescription
indexNamestringThe name of the index.

示例:

JavaScript

const index = pinecone.Index("example-index");

Index.delete1()

index.delete(requestParameters: Delete1Request)

从单个命名空间中按其ID删除项目。

ParametersTypeDescription
requestParametersDelete1RequestDelete request parameters

类型

Delete1Request
ParametersTypeDescription
idsArray(Optional) The IDs of the items to delete.
deleteAllboolean(Optional) Indicates that all vectors in the index namespace should be deleted.
namespacestr(Optional) The namespace to delete vectors from, if applicable.

类型

示例:

JavaScript

await index.delete1({
ids: ["example-id-1", "example-id-2"],
namespace: "example-namespace",
});

Index.describeIndexStats()

index.describeIndexStats(requestParameters: DescribeIndexStatsOperationRequest)

返回索引内容的统计信息,包括每个命名空间的向量计数和维数数量。

ParametersTypeDescription
requestParametersDescribeIndexStatsOperationRequestDescribe index stats request wrapper

类型

DescribeIndexStatsOperationRequest
ParametersTypeDescription
describeIndexStatsRequestDescribeIndexStatsRequestDescribe index stats request parameters
DescribeIndexStatsRequest
parameterTypeDescription
filterobject(Optional) A metadata filter expression.

返回:

  • namespaces : object 索引中每个命名空间的映射,从命名空间名称到其内容概要。如果存在元数据过滤表达式,则概要将仅反映匹配该表达式的向量。
  • dimension : int64 索引向量的维度。
  • indexFullness : float 索引的充实度,无论是否传递了元数据过滤表达式。此度量的粒度为10%。
  • totalVectorCount : int64 索引中向量的总数。

示例:

JavaScript

const indexStats = await index.describeIndexStats({
describeIndexStatsRequest: {},
});

了解更多关于过滤的详细信息。

Index.fetch()

index.fetch(requestParameters: FetchRequest)

Fetch操作从单个命名空间查找并返回向量(通过ID)。返回的向量包括向量数据和元数据。

ParametersTypeDescription
requestParametersFetchRequestFetch request parameters

类型

FetchRequest
ParametersTypeDescription
idsArrayThe vector IDs to fetch. Does not accept values containing spaces.
namespacestring(Optional) The namespace containing the vectors.

返回:

  • vectors : object 包含向量。
  • namespace : string 向量所属的命名空间。

示例:

JavaScript

const fetchResponse = await index.fetch({
ids: ["example-id-1", "example-id-2"],
namespace: "example-namespace",
});

Index.query()

index.query(requestParameters: QueryOperationRequest)

使用查询向量搜索命名空间。检索命名空间中最相似项目的ID及其相似度分数。

ParametersTypeDescription
requestParametersQueryOperationRequestThe query operation request wrapper.

类型

ParametersTypeDescription
queryRequestQueryRequestThe query operation request.
查询请求
ParameterTypeDescription
namespacestring(Optional) The namespace to query.
topKnumberThe number of results to return for each query.
filterobject(Optional) The filter to apply. You can use vector metadata to limit your search. See https://www.pinecone.io/docs/metadata-filtering/.
includeValuesboolean(Optional) Indicates whether vector values are included in the response. Defaults to false.
includeMetadataboolean(Optional) Indicates whether metadata is included in the response as well as the ids. Defaults to false.
vectorArray(Optional) The query vector. This should be the same length as the dimension of the index being queried. Each query() request can contain only one of the parameters id or vector.
idstring(Optional) The unique ID of the vector to be used as a query vector. Each query() request can contain only one of the parameters vector or id.

示例:

JavaScript

const queryResponse = await index.query({
queryRequest: {
namespace: "example-namespace",
topK: 10,
filter: {
genre: { $in: ["comedy", "documentary", "drama"] },
},
includeValues: true,
includeMetadata: true,
vector: [0.1, 0.2, 0.3, 0.4],
},
});

Index.update()

index.update(requestParameters: UpdateOperationRequest)

更新命名空间中的向量。如果包含一个值,它将覆盖以前的值。

如果 updateRequest 中包括 setMetadata,则其中指定字段的值将被添加或覆盖之前的值。

ParametersTypeDescription
requestParametersUpdateOperationRequestThe update operation wrapper

类型

更新操作请求
ParametersTypeDescription
updateRequestUpdateRequestThe update request.
更新请求
ParameterTypeDescription
idstringThe vector's unique ID.
valuesArray(Optional) Vector data.
setMetadataobject(Optional) Metadata to set for the vector.
namespacestring(Optional) The namespace containing the vector.

例子:

JavaScript

const updateResponse = await index.update({
updatedRequest: {
id: "vec1",
values: [0.1, 0.2, 0.3, 0.4],
setMetadata: {
genre: "drama",
},
namespace: "example-namespace",
},
});

Index.upsert()

index.upsert(requestParameters: UpsertOperationRequest)

将向量写入命名空间。如果为现有向量ID插入了新值,它将覆盖先前的值。

ParametersTypeDescription
requestParametersUpsertOperationRequestUpsert operation wrapper

类型

UpsertOperationRequest
ParametersTypeDescription
upsertRequestUpsertRequestThe upsert request.
UpsertRequest

| Parameter | Type | Description |

| vectors | Array | An array containing the vectors to upsert. Recommended batch limit is 100 vectors.
id (str) - The vector's unique id.
values ([float]) - The vector data.
metadata (object) - (Optional) Metadata for the vector. |

| namespace | string | (Optional) The namespace name to upsert vectors. |

向量
ParameterTypeDescription
idstringThe vector's unique ID.
valuesArrayVector data.
metadataobject(Optional) Metadata for the vector.

返回:

  • upsertedCount : int64 插入或更新的向量数量。

示例:

JavaScript

const upsertResponse = await index.upsert({
upsertRequest: {
vectors: [
{
id: "vec1",
values: [0.1, 0.2, 0.3, 0.4],
metadata: {
genre: "drama",
},
},
{
id: "vec2",
values: [0.1, 0.2, 0.3, 0.4],
metadata: {
genre: "comedy",
},
},
],
namespace: "example-namespace",
},
});

更新于 28天前