️ Peringatan: SDK ini masih dalam keadaan alfa. Meskipun sebagian besar dibangun dan fungsional, itu mungkin mengalami perubahan saat kami terus meningkatkan UX. Silakan coba dan beri kami umpan balik Anda, tetapi ketahuilah bahwa pembaruan dapat memperkenalkan perubahan yang melanggar.
Dokumentasi dapat ditemukan di sini.
Versi karat: Diuji dengan versi karat 1.78.0
Sebelum Anda dapat menggunakan Pinecone SDK, Anda harus mendaftar untuk akun dan menemukan kunci API Anda di dasbor konsol Pinecone di https://app.pinecone.io.
Jalankan cargo add pinecone-sdk untuk menambahkan peti sebagai ketergantungan, atau tambahkan garis pinecone-sdk = "0.1.2" ke file Cargo.toml Anda di bawah [dependencies] . Lebih lanjut tentang peti dapat ditemukan di sini.
Kelas PineconeClient adalah titik utama masuk ke karat SDK. Parameter dapat langsung dilewatkan sebagai Options , atau membaca variabel lingkungan sebagai berikut. Lebih detail:
PINECONE_API_KEY . Jika disahkan sebagai None , klien akan mencoba membaca dalam nilai variabel lingkungan.None , akan mencoba membaca dalam variabel lingkungan yang disebut PINECONE_CONTROLLER_HOST . Jika ini bukan variabel lingkungan, itu akan default ke https://api.pinecone.io .Ada beberapa cara untuk membuat instantiate klien, dirinci di bawah ini:
Inisialisasi struct PineconeClientConfig dengan parameter, dan hubungi config menggunakan struct.
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" ) ; Gunakan fungsi default_client() , yang setara dengan membangun struct PineconeClientConfig dengan semua bidang yang diatur ke None . Kunci API dan host pesawat kontrol (opsional) akan dibaca dari variabel lingkungan.
let pinecone : PineconeClient = pinecone_sdk :: pinecone :: default_client ( ) . expect ( "Failed to create Pinecone instance" ) ; Contoh berikut membuat indeks tanpa server di wilayah AWS us-east-1 . Untuk informasi lebih lanjut tentang ketersediaan tanpa server dan regional, lihat indeks pemahaman
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 ? ; Contoh berikut menciptakan indeks pod di wilayah 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_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 ? ;Indeks POD mendukung beberapa bidang konfigurasi opsional. Contoh berikut membangun indeks pod dengan beberapa spesifikasi untuk bidang ini.
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 ? ; Contoh berikut mencantumkan semua indeks dalam proyek Anda.
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 ? ; Contoh berikut mengembalikan informasi tentang indeks 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 ? ; Mengkonfigurasi indeks mengambil dalam tiga parameter opsional - enum penghapusan penghapusan, jumlah replika, dan jenis pod. Perlindungan penghapusan dapat diperbarui untuk jenis indeks apa pun, sedangkan jumlah replika dan jenis pod hanya dapat diperbarui untuk indeks POD.
Contoh berikut menonaktifkan perlindungan penghapusan untuk indeks 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 ? ; Contoh berikut mengubah index-name indeks untuk memiliki 6 replika dan tipe pod s1 . Jenis perlindungan penghapusan tidak akan diubah dalam kasus ini.
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 ? ; Contoh berikut menghapus index-name indeks.
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 ? ; Contoh berikut mengembalikan statistik tentang indeks dengan index-host . Tanpa filter
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 ? ;Dengan filter
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 ? ; Contoh berikut meningkatkan dua vektor ke dalam indeks dengan 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 ? ; Ada dua cara yang didukung untuk menanyakan indeks.
Contoh berikut menanyakan indeks dengan index-host untuk vektor dengan ID vector-id , dan mengembalikan 10 pertandingan teratas.
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 ? ; Contoh berikut menanyakan indeks dengan index-host untuk vektor dengan nilai [1.0, 2.0, 3.0, 4.0] , dan mengembalikan 10 pertandingan teratas.
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 ? ; Ada tiga cara yang didukung untuk menghapus vektor.
Contoh berikut menghapus vektor dengan ID vector-id di namespace 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 ? ; Contoh berikut menghapus vektor yang memenuhi filter di namespace 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 ? ; Contoh berikut menghapus semua vektor di namespace 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 ? ; Contoh berikut mengambil vektor dengan ID 1 dan 2 dari namespace default.
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 ? ; Contoh berikut memperbarui vektor dengan ID vector-id di namespace untuk memiliki nilai [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 ? ; Contoh berikut mencantumkan vektor di namespace 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 ? ; Contoh berikut membuat collection-name koleksi dalam index-name indeks.
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 ? ; Contoh berikut mencantumkan semua koleksi dalam suatu proyek.
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 ? ; Contoh berikut menjelaskan collection-name koleksi.
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 ? ; Contoh berikut menghapus collection-name koleksi.
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 ? ;Jika Anda ingin memberikan kontribusi, atau mendapatkan pengaturan secara lokal untuk mengembangkan Pinecone Rust Client, silakan lihat panduan berkontribusi kami