配x 警告:此SDK仍处于α州。尽管它主要是建立和功能的,但随着我们继续改善UX,它可能会发生变化。请尝试一下,并给我们您的反馈,但请注意,更新可能会引入破坏变化。
可以在此处找到文档。
Rust版本:使用Rust版本1.78.0测试
在使用Pinecone SDK之前,您必须注册一个帐户,并在https://app.pinecone.io上的Pinecone控制台仪表板中找到API键。
运行cargo add pinecone-sdk将板条箱添加为依赖关系,或在[dependencies]下的货物文件中添加pinecone-sdk = "0.1.2" 。有关板条箱的更多信息可以在这里找到。
PineconeClient类是进入Rust SDK的主要点。参数可以直接传递为Options ,也可以通过以下环境变量读取。更多细节:
PINECONE_API_KEY的环境变量传递。如果None通过,客户将尝试在环境变量值中读取。None )将尝试在称为PINECONE_CONTROLLER_HOST的环境变量中读取。如果它不是环境变量,则将默认为https://api.pinecone.io 。有几种实例化客户端的方法,以下详细介绍:
用参数初始化PineconeClientConfig结构,然后使用结构调用config 。
use pinecone_sdk :: pinecone :: { PineconeClient , PineconeClientConfig } ;
let config = PineconeClientConfig {
api_key : Some ( "INSERT_API_KEY" . to_string ( ) ) ,
control_plane_host : Some ( "INSERT_CONTROLLER_HOST" . to_string ( ) ) ,
.. Default :: default ( )
} ;
let pinecone : PineconeClient = config . client ( ) . expect ( "Failed to create Pinecone instance" ) ;使用default_client()函数,它等效于构造所有PineconeClientConfig ,将所有字段设置为None 。 API键和控制平面主机(可选)将从环境变量中读取。
let pinecone : PineconeClient = pinecone_sdk :: pinecone :: default_client ( ) . expect ( "Failed to create Pinecone instance" ) ;以下示例在AWS的us-east-1区域中创建了无服务器索引。有关无服务器和区域可用性的更多信息,请参阅理解索引
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Metric , Cloud , WaitPolicy , IndexModel } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let index_description : IndexModel = pinecone . create_serverless_index (
"index-name" , // Name of the index
10 , // Dimension of the vectors
Metric :: Cosine , // Distance metric
Cloud :: Aws , // Cloud provider
"us-east-1" , // Region
WaitPolicy :: NoWait // Timeout
) . await ? ;以下示例在AWS的us-east-1区域中创建了POD索引。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Metric , Cloud , WaitPolicy , IndexModel } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let index_description : IndexModel = pinecone . create_pod_index (
"index-name" , // Index name
10 , // Dimension
Metric :: Cosine , // Distance metric
"us-east-1" , // Region
"p1.x1" , // Pod type
1 , // Number of pods
None , // Number of replicas
None , // Number of shards
None , // Metadata to index
None , // Source collection
WaitPolicy :: NoWait // Wait policy
) . await ? ;POD索引支持几个可选的配置字段。下面的示例构建了带有某些规范的POD索引。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Metric , Cloud , WaitPolicy , IndexModel } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let index_description : IndexModel = pinecone . create_pod_index (
"index-name" , // Index name
10 , // Dimension
Metric :: Cosine , // Distance metric
"us-east-1" , // Region
"p1.x1" , // Pod type
1 , // Number of pods
Some ( 1 ) , // Number of replicas
Some ( 1 ) , // Number of shards
Some ( // Metadata fields to index
& vec ! [ "genre" ,
"title" ,
"imdb_rating" ] ) ,
Some ( "collection" ) , // Source collection
WaitPolicy :: NoWait // Wait policy
) . await ? ; 以下示例列出了项目中的所有索引。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: IndexList ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let index_list : IndexList = pinecone . list_indexes ( ) . await ? ; 以下示例返回有关索引index-name的信息。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: IndexModel ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let index_description : IndexModel = pinecone . describe_index ( "index-name" ) . await ? ; 配置索引带有三个可选参数 - 删除保护枚举,副本数量和POD类型。可以为任何索引类型更新删除保护,而复制品的数量和POD类型只能为POD索引更新。
以下示例禁用索引index-name的删除保护。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: IndexModel ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let index_description : IndexModel = pinecone . configure_index ( "index-name" , Some ( DeletionProtection :: Disabled ) , None , None ) . await ? ;以下示例将索引index-name更改为具有6个副本和POD类型s1 。在这种情况下,删除保护类型将不会更改。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: IndexModel ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let index_description : IndexModel = pinecone . configure_index ( "index-name" , None , Some ( 6 ) , Some ( "s1" ) ) . await ? ; 以下示例删除索引index-name 。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
pinecone . delete_index ( "index-name" ) . await ? ; 下面的示例返回有关主机index-host的索引的统计信息。没有过滤器
use std :: collections :: BTreeMap ;
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: DescribeIndexStatsResponse ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let response : DescribeIndexStatsResponse = index . describe_index_stats ( None ) . await ? ;使用过滤器
use std :: collections :: BTreeMap ;
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Value , Kind , Metadata , DescribeIndexStatsResponse } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let mut fields = BTreeMap :: new ( ) ;
let kind = Some ( Kind :: StringValue ( "value" . to_string ( ) ) ) ;
fields . insert ( "field" . to_string ( ) , Value { kind } ) ;
let response : DescribeIndexStatsResponse = index . describe_index_stats ( Some ( Metadata { fields } ) ) . await ? ; 以下示例将两个向量带入主机index-host的索引中。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Vector , UpsertResponse } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let vectors = [ Vector {
id : "id1" . to_string ( ) ,
values : vec ! [ 1.0 , 2.0 , 3.0 , 4.0 ] ,
sparse_values : None ,
metadata : None ,
} , Vector {
id : "id2" . to_string ( ) ,
values : vec ! [ 2.0 , 3.0 , 4.0 , 5.0 ] ,
sparse_values : None ,
metadata : None ,
} ] ;
let response : UpsertResponse = index . upsert ( & vectors , & "namespace" . into ( ) ) . await ? ; 查询索引有两种支持的方法。
以下示例查询具有ID vector-id的主机index-host索引,并返回前10个匹配项。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Namespace , QueryResponse } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
// Connect to index at host "index-host"
let mut index = pinecone . index ( "index-host" ) . await ? ;
// Query the vector with id "vector-id" in the namespace "namespace"
let response : QueryResponse = index . query_by_id (
"vector-id" . to_string ( ) ,
10 ,
& Namespace :: default ( ) ,
None ,
None ,
None
) . await ? ;下面的示例查询使用值[1.0, 2.0, 3.0, 4.0]的向量的主机index-host索引,并返回前10个匹配项。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Namespace , QueryResponse } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let vector = vec ! [ 1.0 , 2.0 , 3.0 , 4.0 ] ;
let response : QueryResponse = index . query_by_value (
vector ,
None ,
10 ,
& Namespace :: default ( ) ,
None ,
None ,
None
) . await ? ; 有三种删除向量的支持方法。
以下示例在名称namespace中使用ID vector-id删除向量。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let ids = [ "vector-id" ]
index . delete_by_id ( & ids , & "namespace" . into ( ) ) . await ? ;以下示例删除了满足命名空间namespace中过滤器的向量。
use std :: collections :: BTreeMap ;
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Metadata , Value , Kind , Namespace } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut fields = BTreeMap :: new ( ) ;
let kind = Some ( Kind :: StringValue ( "value" . to_string ( ) ) ) ;
fields . insert ( "field" . to_string ( ) , Value { kind } ) ;
index . delete_by_filter ( Metadata { fields } , & "namespace" . into ( ) ) . await ? ;以下示例删除了名称空间namespace中的所有向量。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
index . delete_all ( & "namespace" . into ( ) ) . await ? ; 以下示例从默认名称空间中获取使用IDS 1和2向量。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: FetchResponse ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let vectors = & [ "1" , "2" ] ;
let response : FetchResponse = index . fetch ( vectors , & Default :: default ( ) ) . await ? ; 以下示例在名称namespace中使用ID vector-id更新了向量,以具有[1.0, 2.0, 3.0, 4.0]值。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: UpdateResponse ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let response : UpdateResponse = index . update ( "vector-id" , vec ! [ 1.0 , 2.0 , 3.0 , 4.0 ] , None , None , & "namespace" . into ( ) ) . await ? ; 以下示例列出了名称空间namespace中的向量。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: { Namespace , ListResponse } ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let mut index = pinecone . index ( "index-host" ) . await ? ;
let response : ListResponse = index . list ( & "namespace" . into ( ) , None , None , None ) . await ? ;以下示例在索引index-name中创建集合collection-name 。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: CollectionModel ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let collection : CollectionModel = pinecone . create_collection ( "collection-name" , "index-name" ) . await ? ; 以下示例列出了项目中的所有收藏。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: CollectionList ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let collection_list : CollectionList = pinecone . list_collections ( ) . await ? ; 以下示例描述了collection-name 。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
use pinecone_sdk :: models :: CollectionModel ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
let collection : CollectionModel = pinecone . describe_collection ( "collection-name" ) . await ? ; 以下示例删除了collection-name 。
use pinecone_sdk :: pinecone :: PineconeClientConfig ;
let config = PineconeClientConfig {
api_key : Some ( ' << PINECONE_API_KEY >> ' ) ,
.. Default :: default ( )
} ;
let pinecone = config . client ( ) ? ;
pinecone . delete_collection ( "collection-name" ) . await ? ;如果您想做出贡献或在本地设置以开发Pinecone Rust客户端,请参阅我们的贡献指南