คำเตือน: SDK นี้ยังอยู่ในสถานะอัลฟ่า ในขณะที่ส่วนใหญ่ถูกสร้างขึ้นและใช้งานได้ แต่มันอาจได้รับการเปลี่ยนแปลงในขณะที่เรายังคงปรับปรุง UX โปรดลองใช้และให้ความคิดเห็นของคุณ แต่โปรดทราบว่าการอัปเดตอาจแนะนำการเปลี่ยนแปลงที่แตกหัก
เอกสารสามารถพบได้ที่นี่
เวอร์ชันสนิม: ทดสอบกับ Rust เวอร์ชัน 1.78.0
ก่อนที่คุณจะใช้ Pinecone SDK คุณต้องลงทะเบียนสำหรับบัญชีและค้นหาคีย์ API ของคุณในแผงควบคุมคอนโซล Pinecone ที่ https://app.pinecone.io
เรียก cargo add pinecone-sdk เพื่อเพิ่มลังเป็นการพึ่งพาหรือเพิ่มบรรทัด pinecone-sdk = "0.1.2" ลงในไฟล์ cargo.toml ของคุณภายใต้ [dependencies] เพิ่มเติมเกี่ยวกับลังสามารถพบได้ที่นี่
คลาส 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" ) ; ตัวอย่างต่อไปนี้สร้างดัชนีที่ไม่มีเซิร์ฟเวอร์ในภูมิภาค us-east-1 ของ AWS สำหรับข้อมูลเพิ่มเติมเกี่ยวกับความพร้อมใช้งานแบบไม่มีเซิร์ฟเวอร์และภูมิภาคดูดัชนีการทำความเข้าใจ
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 ? ; ตัวอย่างต่อไปนี้สร้างดัชนี POD ในภูมิภาค us-east-1 ของ AWS
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 ? ; การกำหนดค่าดัชนีใช้เวลาในพารามิเตอร์เสริมสามตัวคือ enum deletionprotection จำนวนของแบบจำลองและประเภท 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 ? ; ตัวอย่างต่อไปนี้ upserts เวกเตอร์สองตัวลงในดัชนีด้วยโฮสต์ 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 ? ; มีสองวิธีที่รองรับการสอบถามดัชนี
ตัวอย่างต่อไปนี้สอบถามดัชนีด้วยโฮสต์ index-host สำหรับเวกเตอร์ด้วย ID vector-id และส่งคืนการจับคู่ 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 ? ; ตัวอย่างต่อไปนี้สอบถามดัชนีด้วยโฮสต์ index-host สำหรับเวกเตอร์ที่มีค่า [1.0, 2.0, 3.0, 4.0] และส่งคืนการแข่งขัน 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 ? ; มีสามวิธีที่รองรับการลบเวกเตอร์
ตัวอย่างต่อไปนี้ลบเวกเตอร์ด้วย id vector-id ในเนมสเปซ 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 ? ;
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 ? ; ตัวอย่างต่อไปนี้อัปเดตเวกเตอร์ด้วย ID vector-id ใน namespace ซเพื่อให้มีค่า [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 ? ; ตัวอย่างต่อไปนี้สร้าง collection-name ในดัชนี index-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 โปรดดูคู่มือการสนับสนุนของเรา