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
方法需要一个具有apiKey
和environment
属性的对象:
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客户端。
Parameters | Type | Description |
---|---|---|
configuration | PineconeClientConfiguration | The configuration for the Pinecone client. |
类型
PineconeClientConfiguration
Parameters | Type | Description |
---|---|---|
apiKey | string | The API key for the Pinecone service. |
environment | string | The 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类型和副本数。
Parameters | Type | Description |
---|---|---|
requestParameters | ConfigureIndexRequest | Index configuration parameters. |
类型
ConfigureIndexRequest
Parameters | Type | Description |
---|---|---|
indexName | string | The name of the index. |
patchRequest | PatchRequest | (Optional) Patch request parameters. |
PatchRequest
Parameters | Type | Description |
---|---|---|
replicas | number | (Optional) The number of replicas to configure for this index. |
podType | string | (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)
从索引创建一个集合。
Parameters | Type | Description |
---|---|---|
requestParameters | CreateCollectionOperationRequest | Create collection operation wrapper |
类型
创建集合操作请求
Parameters | Type | Description |
---|---|---|
createCollectionRequest | CreateCollectionRequest | Collection request parameters. |
创建集合请求
Parameters | Type | Description |
---|---|---|
name | string | The name of the collection to be created. |
source | string | The 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)
创建一个索引。
Parameters | Type | Description |
---|---|---|
requestParameters | CreateIndexRequest | Create index operation wrapper |
类型
创建索引请求
Parameters | Type | Description |
---|---|---|
createRequest | CreateRequest | Create index request parameters |
创建请求
Parameters | Type | Description |
---|---|---|
name | str | The name of the index to be created. The maximum length is 45 characters. |
dimension | integer | The dimensions of the vectors to be inserted in the index. |
metric | str | (Optional) The distance metric to be used for similarity search: 'euclidean', 'cosine', or 'dotproduct'. |
pods | int | (Optional) The number of pods for the index to use, including replicas. |
replicas | int | (Optional) The number of replicas. |
pod_type | str | (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_config | object | (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_collection | str | (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)
删除一个已存在的集合。
类型
Parameters | Type | Description |
---|---|---|
requestParameters | DeleteCollectionRequest | Delete collection request parameters |
DeleteCollectionRequest
Parameters | Type | Description |
---|---|---|
collectionName | string | The name of the collection to delete. |
示例:
JavaScript
await pinecone.deleteCollection({
collectionName: "example-collection",
});
deleteIndex()
pinecone.deleteIndex(requestParameters: DeleteIndexRequest)
删除一个索引。
类型
Parameters | Type | Description |
---|---|---|
requestParameters | DeleteIndexRequest | Delete index request parameters |
删除索引请求
Parameters | Type | Description |
---|---|---|
indexName | string | The name of the index to delete. |
示例:
JavaScript
await pinecone.deleteIndex({
indexName: "example-index",
});
描述集合
pinecone.describeCollection(requestParameters: DescribeCollectionRequest)
获取集合的描述。
Types
Parameters | Type | Description |
---|---|---|
requestParameters | DescribeCollectionRequest | Describe collection request parameters |
DescribeCollectionRequest
Parameters | Type | Description |
---|---|---|
collectionName | string | The 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)
获取索引描述。
类型
Parameters | Type | Description |
---|---|---|
requestParameters | DescribeIndexRequest | Describe index request parameters |
DescribeIndexRequest
Parameters | Type | Description |
---|---|---|
indexName | string | The name of the index. |
类型
返回:
database
:object
name
:string
索引的名称。dimension
:integer
被插入到索引中的向量的维度。metric
:string
用于相似度搜索的距离度量方式,可选的值为'euclidean'、'cosine'和'dotproduct'。pods
:integer
索引使用的pod数量,包括副本。replicas
:integer
索引的副本数量。pod_type
:string
索引使用的pod类型,值为s1
、p1
或者p2
之一,后面跟着.
和x1
、x2
、x4
或者x8
之一。metadata_config
:object
Pinecone内部元数据索引行为的配置信息。默认情况下将索引所有元数据。当出现metadata_config
时,仅索引指定的元数据字段。要指定要索引的元数据字段,请提供以下形式的JSON对象:{"indexed": ["example_metadata_field"]}
。status
:object
ready
:boolean
索引是否已准备好处理查询。state
:string
索引的状态,可能的值包括Initializing
、ScalingUp
、ScalingDown
、Terminating
和Ready
。 示例:
JavaScript
const indexDescription = await pinecone.describeIndex({
indexName: "example-index",
});
listCollections
pinecone.listCollections()
返回项目中的集合列表。
示例:
JavaScript
const collections = await pinecone.listCollections();
返回值:
array
ofstrings
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 对象。
Parameters | Type | Description |
---|---|---|
indexName | string | The name of the index. |
示例:
JavaScript
const index = pinecone.Index("example-index");
Index.delete1()
index.delete(requestParameters: Delete1Request)
从单个命名空间中按其ID删除项目。
Parameters | Type | Description |
---|---|---|
requestParameters | Delete1Request | Delete request parameters |
类型
Delete1Request
Parameters | Type | Description |
---|---|---|
ids | Array | (Optional) The IDs of the items to delete. |
deleteAll | boolean | (Optional) Indicates that all vectors in the index namespace should be deleted. |
namespace | str | (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)
返回索引内容的统计信息,包括每个命名空间的向量计数和维数数量。
Parameters | Type | Description |
---|---|---|
requestParameters | DescribeIndexStatsOperationRequest | Describe index stats request wrapper |
类型
DescribeIndexStatsOperationRequest
Parameters | Type | Description |
---|---|---|
describeIndexStatsRequest | DescribeIndexStatsRequest | Describe index stats request parameters |
DescribeIndexStatsRequest
parameter | Type | Description |
---|---|---|
filter | object | (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)。返回的向量包括向量数据和元数据。
Parameters | Type | Description |
---|---|---|
requestParameters | FetchRequest | Fetch request parameters |
类型
FetchRequest
Parameters | Type | Description |
---|---|---|
ids | Array | The vector IDs to fetch. Does not accept values containing spaces. |
namespace | string | (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及其相似度分数。
Parameters | Type | Description |
---|---|---|
requestParameters | QueryOperationRequest | The query operation request wrapper. |
类型
Parameters | Type | Description |
---|---|---|
queryRequest | QueryRequest | The query operation request. |
查询请求
Parameter | Type | Description |
---|---|---|
namespace | string | (Optional) The namespace to query. |
topK | number | The number of results to return for each query. |
filter | object | (Optional) The filter to apply. You can use vector metadata to limit your search. See https://www.pinecone.io/docs/metadata-filtering/. |
includeValues | boolean | (Optional) Indicates whether vector values are included in the response. Defaults to false . |
includeMetadata | boolean | (Optional) Indicates whether metadata is included in the response as well as the ids. Defaults to false . |
vector | Array | (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 . |
id | string | (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
,则其中指定字段的值将被添加或覆盖之前的值。
Parameters | Type | Description |
---|---|---|
requestParameters | UpdateOperationRequest | The update operation wrapper |
类型
更新操作请求
Parameters | Type | Description |
---|---|---|
updateRequest | UpdateRequest | The update request. |
更新请求
Parameter | Type | Description |
---|---|---|
id | string | The vector's unique ID. |
values | Array | (Optional) Vector data. |
setMetadata | object | (Optional) Metadata to set for the vector. |
namespace | string | (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插入了新值,它将覆盖先前的值。
Parameters | Type | Description |
---|---|---|
requestParameters | UpsertOperationRequest | Upsert operation wrapper |
类型
UpsertOperationRequest
Parameters | Type | Description |
---|---|---|
upsertRequest | UpsertRequest | The 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. |
向量
Parameter | Type | Description |
---|---|---|
id | string | The vector's unique ID. |
values | Array | Vector data. |
metadata | object | (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天前