นี่คือ Node.js SDK อย่างเป็นทางการสำหรับ Pinecone เขียนด้วย TypeScript
v0.x ให้ตรวจสอบ คู่มือการย้ายถิ่น v1v1.x ให้ตรวจสอบ คู่มือการโยกย้าย V2ตัวอย่างที่แสดงใน readme นี้มีจุดประสงค์เพื่อกระชับ สำหรับตัวอย่างที่สมจริงยิ่งขึ้นสำรวจตัวอย่างเหล่านี้:
2.x เป็น 3.x มีการเปลี่ยนแปลงที่เกี่ยวข้องกับการดำเนินการ configureIndex ในการอัปเดตนี้ โครงสร้างของวัตถุที่ผ่านเมื่อการกำหนดค่าดัชนีได้เปลี่ยนไปรวมถึง deletionProtection ขณะนี้ฟิลด์ podType และ replicas สามารถอัปเดตผ่านวัตถุ spec.pod ดูการกำหนดค่าดัชนีที่ใช้ POD สำหรับตัวอย่างของรหัส
2.x : มีการเปลี่ยนแปลงมากมายในรุ่นนี้เพื่อรองรับการเสนอดัชนี Serverless ใหม่ของ Pinecone การเปลี่ยนแปลงจะครอบคลุมรายละเอียดใน คู่มือการโยกย้าย V2 ดัชนีที่ไม่มีเซิร์ฟเวอร์มีให้เฉพาะในรุ่น 2.x รุ่นหรือมากกว่า1.x : รีลีสนี้ย้าย SDK ออกจากเบต้าอย่างเป็นทางการและมีการเปลี่ยนแปลงจำนวนมากที่ต้องได้รับการแก้ไขเมื่ออัปเกรดจากรุ่น 0.x ดู คู่มือการโยกย้าย V1 สำหรับรายละเอียด Pinecone typescript SDK เข้ากันได้กับ typeScript> = 4.1 และ Node> = 18.x
npm install @pinecone-database/pinecone
Pinecone typescript SDK มีไว้สำหรับ ใช้ฝั่งเซิร์ฟเวอร์เท่านั้น การใช้ SDK ภายในบริบทของเบราว์เซอร์สามารถ เปิดเผยคีย์ API ของคุณได้ หากคุณปรับใช้ SDK เพื่อผลิตในเบราว์เซอร์ โปรดหมุนปุ่ม API ของคุณ
จำเป็นต้องมีคีย์ API เพื่อเริ่มต้นลูกค้า สามารถส่งผ่านโดยใช้ตัวแปรสภาพแวดล้อมหรือในรหัสผ่านวัตถุการกำหนดค่า รับคีย์ API ในคอนโซล
ตัวแปรสภาพแวดล้อมที่ใช้ในการกำหนดค่าคีย์ API สำหรับไคลเอนต์มีดังต่อไปนี้:
PINECONE_API_KEY= " your_api_key " PINECONE_API_KEY เป็นตัวแปรที่ต้องการเท่านั้น เมื่อตั้งค่าตัวแปรสภาพแวดล้อมนี้ตัวสร้างไคลเอนต์ไม่จำเป็นต้องมีอาร์กิวเมนต์เพิ่มเติมใด ๆ
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ; หากคุณต้องการผ่านการกำหนดค่าในรหัสคอนสตรัคเตอร์ยอมรับวัตถุกำหนดค่าที่มีค่า apiKey นี่คือวัตถุที่คุณจะผ่านคุณสมบัติเช่น maxRetries (ค่าเริ่มต้นถึง 3 ) สำหรับการดำเนินการ retryable ( upsert , update และ configureIndex )
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( {
apiKey : 'your_api_key' ,
maxRetries : 5 ,
} ) ; หากการตั้งค่าเครือข่ายของคุณต้องการให้คุณโต้ตอบกับ Pinecone ผ่านพร็อกซีคุณสามารถผ่าน ProxyAgent ที่กำหนดเองจากไลบรารี undici ด้านล่างนี้เป็นตัวอย่างของวิธีการสร้าง ProxyAgent undici งานที่กำหนดเส้นทางการรับส่งข้อมูลเครือข่ายผ่านเซิร์ฟเวอร์พร็อกซี mitm ในขณะที่กดปุ่มจุดสิ้นสุดของ Pinecone /indexes
หมายเหตุ: กลยุทธ์ต่อไปนี้ขึ้นอยู่กับการใช้งาน fetch ดั้งเดิมของ Node ซึ่งเปิดตัวใน Node V16 และเสถียรใน Node V21 หากคุณใช้โหนดเวอร์ชัน 18-21 คุณอาจประสบปัญหาที่เกิดจากความไม่แน่นอนของคุณสมบัติ ขณะนี้ยังไม่มีปัญหาที่ทราบเกี่ยวกับพร็อกซ์ในโหนด V18+
import {
Pinecone ,
type PineconeConfiguration ,
} from '@pinecone-database/pinecone' ;
import { Dispatcher , ProxyAgent } from 'undici' ;
import * as fs from 'fs' ;
const cert = fs . readFileSync ( 'path-to-your-mitm-proxy-cert-pem-file' ) ;
const client = new ProxyAgent ( {
uri : '<your proxy server URI>' ,
requestTls : {
port : '<your proxy server port>' ,
ca : cert ,
host : '<your proxy server host>' ,
} ,
} ) ;
const customFetch = (
input : string | URL | Request ,
init : RequestInit | undefined
) => {
return fetch ( input , {
... init ,
dispatcher : client as Dispatcher ,
keepalive : true , # optional
} ) ;
} ;
const config : PineconeConfiguration = {
apiKey :
'<your Pinecone API key, available in your dashboard at app.pinecone.io>' ,
fetchApi : customFetch ,
} ;
const pc = new Pinecone ( config ) ;
const indexes = async ( ) => {
return await pc . listIndexes ( ) ;
} ;
indexes ( ) . then ( ( response ) => {
console . log ( 'My indexes: ' , response ) ;
} ) ; อย่างน้อยที่สุดในการสร้างดัชนีแบบไม่มีเซิร์ฟเวอร์คุณต้องระบุ name dimension และ spec dimension ระบุขนาดของเวกเตอร์ที่คุณตั้งใจจะเก็บไว้ในดัชนี ตัวอย่างเช่นหากความตั้งใจของคุณคือการจัดเก็บและสอบถาม Embeddings (เวกเตอร์) ที่สร้างขึ้นด้วยโมเดล Textembedding-ADA-002 ของ OpenAI คุณจะต้องสร้างดัชนีที่มีมิติ 1536 เพื่อให้ตรงกับผลลัพธ์ของรุ่นนั้น
spec กำหนดวิธีการปรับใช้ดัชนี สำหรับดัชนีที่ไม่มีเซิร์ฟเวอร์คุณจะกำหนดเฉพาะคลาวด์และภูมิภาคที่ควรโฮสต์ดัชนี สำหรับดัชนีที่ใช้ POD คุณกำหนดสภาพแวดล้อมที่ควรโฮสต์ดัชนีประเภทและขนาดของ POD ที่จะใช้และลักษณะดัชนีอื่น ๆ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับความพร้อมใช้งานที่ไม่มีเซิร์ฟเวอร์และภูมิภาคให้ดูดัชนีการทำความเข้าใจ
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . createIndex ( {
name : 'sample-index' ,
dimension : 1536 ,
spec : {
serverless : {
cloud : 'aws' ,
region : 'us-west-2' ,
} ,
} ,
tags : { team : 'data-science' } ,
} ) ; ในการสร้างดัชนีที่ใช้ POD คุณจะกำหนด pod ในวัตถุ spec ซึ่งมี environment ที่ควรโฮสต์ดัชนีและขนาด podType และ pods ที่จะใช้ ฟิลด์การกำหนดค่าที่เป็นตัวเลือกจำนวนมากช่วยให้สามารถควบคุมทรัพยากรฮาร์ดแวร์และความพร้อมใช้งานได้มากขึ้น หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับวัตถุประสงค์ของฟิลด์เหล่านี้ดูดัชนีการทำความเข้าใจและดัชนีตามขนาดพ็อด
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . createIndex ( {
name : 'sample-index-2' ,
dimension : 1536 ,
metric : 'dotproduct' ,
spec : {
pod : {
environment : 'us-east4-gcp' ,
pods : 2 ,
podType : 'p1.x2' ,
metadataConfig : {
indexed : [ 'product_type' ] ,
} ,
} ,
tags : { team : 'data-science' } ,
} ,
// This option tells the client not to throw if the index already exists.
suppressConflicts : true ,
// This option tells the client not to resolve the promise until the
// index is ready.
waitUntilReady : true ,
} ) ; เมธอด createIndex ออกคำร้องขอสร้าง API ที่ส่งคืนอย่างรวดเร็ว แต่ดัชนีผลลัพธ์ไม่พร้อมทันทีสำหรับการเพิ่มการสืบค้นหรือการดำเนินการข้อมูลอื่น ๆ คุณสามารถใช้วิธี describeIndex เพื่อค้นหาสถานะของดัชนีและดูว่าพร้อมใช้งานหรือไม่
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . describeIndex ( 'serverless-index' ) ;
// {
// name: 'serverless-index',
// dimension: 1536,
// metric: 'cosine',
// host: 'serverless-index-4zo0ijk.svc.us-west2-aws.pinecone.io',
// deletionProtection: 'disabled',
// spec: {
// serverless: {
// cloud: 'aws',
// region: 'us-west-2'
// }
// },
// status: {
// ready: false,
// state: 'Initializing'
// }
// } หากคุณผ่านตัวเลือก waitUntilReady ไคลเอนต์จะจัดการกับการอัปเดตการเลือกตั้งสำหรับการอัปเดตสถานะในดัชนีที่สร้างขึ้นใหม่ สัญญาที่ส่งคืนโดย createIndex จะไม่ได้รับการแก้ไขจนกว่าสถานะดัชนีจะระบุว่าพร้อมที่จะจัดการการดำเนินการข้อมูล สิ่งนี้มีประโยชน์อย่างยิ่งสำหรับการทดสอบการรวมโดยที่การสร้างดัชนีในขั้นตอนการตั้งค่าจะตามมาทันทีด้วยการดำเนินการข้อมูล
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . createIndex ( {
name : 'serverless-index' ,
dimension : 1536 ,
spec : {
serverless : {
cloud : 'aws' ,
region : 'us-west-2' ,
} ,
} ,
waitUntilReady : true ,
} ) ; บันทึก
ดัชนีที่ไม่มีเซิร์ฟเวอร์และสตาร์ทเตอร์ไม่รองรับคอลเลกชัน
ในขณะที่คุณใช้ Pinecone สำหรับสิ่งต่าง ๆ เพิ่มเติมคุณอาจต้องการสำรวจการกำหนดค่าดัชนีที่แตกต่างกันด้วยข้อมูลเวกเตอร์เดียวกัน คอลเลกชันเป็นวิธีที่ง่ายในการทำเช่นนี้ ดูวิธีการลูกค้าอื่น ๆ สำหรับการทำงานกับคอลเลกชันที่นี่
ระบุว่าคุณมีคอลเลกชันที่มีอยู่:
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . describeCollection ( 'product-description-embeddings' ) ;
// {
// name: 'product-description-embeddings',
// size: 543427063,
// status: 'Ready',
// dimension: 2,
// vectorCount: 10001498,
// environment: 'us-east4-gcp'
// } หมายเหตุ: สำหรับดัชนีที่ใช้ POD คุณสามารถระบุ sourceCollection ที่จะสร้างดัชนี คอลเลกชันจะต้องอยู่ในสภาพแวดล้อมเดียวกันกับดัชนี
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . createIndex ( {
name : 'product-description-p1x1' ,
dimension : 256 ,
metric : 'cosine' ,
spec : {
pod : {
environment : 'us-east4-gcp' ,
pods : 1 ,
podType : 'p1.x1' ,
sourceCollection : 'product-description-embeddings' ,
} ,
} ,
} ) ;เมื่อดัชนีใหม่พร้อมก็ควรมีข้อมูลทั้งหมดที่อยู่ในคอลเลกชันพร้อมที่จะสอบถาม
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . index ( 'product-description-p2x2' ) . describeIndexStats ( ) ;
// {
// namespaces: { '': { recordCount: 78000 } },
// dimension: 256,
// indexFullness: 0.9,
// totalRecordCount: 78000
// } คุณสามารถกำหนดค่าดัชนีทั้งแบบ Serverless และ Pod ด้วย deletionProtection ดัชนีใด ๆ ที่มีคุณสมบัตินี้ตั้งค่าเป็น 'enabled' จะไม่ถูกลบ โดยค่าเริ่มต้น deletionProtection จะถูกตั้งค่าเป็น 'disabled' หากไม่ได้ให้เป็นส่วนหนึ่งของคำขอ createIndex ในการเปิดใช้งาน deletionProtection คุณสามารถผ่านค่าในขณะที่เรียก createIndex
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . createIndex ( {
name : 'deletion-protected-index' ,
dimension : 1536 ,
metric : 'cosine' ,
deletionProtection : 'enabled' ,
spec : {
serverless : {
cloud : 'aws' ,
region : 'us-west-2' ,
} ,
} ,
} ) ; ในการปิดใช้งานการป้องกันการลบคุณสามารถใช้การดำเนินการ configureIndex
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . configureIndex ( 'deletion-protected-index' , {
deletionProtection : 'disabled' ,
} ) ; คุณสามารถสร้างหรือกำหนดค่าดัชนี Serverless และ Pod ด้วยแท็ก ดัชนีสามารถเก็บจำนวนแท็กโดยพลการโดยการสรุปข้อมูลเมตาที่คุณต้องการแนบกับวัตถุดัชนีเองเช่นการเป็นเจ้าของทีมโครงการหรือข้อมูลอื่น ๆ ที่เกี่ยวข้อง
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
// Create index with tag
await pc . createIndex ( {
name : 'tag-index' ,
dimension : 1536 ,
metric : 'cosine' ,
spec : {
serverless : {
cloud : 'aws' ,
region : 'us-west-2' ,
} ,
} ,
tags : { team : 'data-science' } , // index tag
} ) ;
// Configure index with a new tag
await pc . configureIndex ( 'tag-index' , {
tags : { project : 'recommendation' } , // new index tag
} ) ;
// Delete an existing tag
await pc . configureIndex ( 'tag-index' , {
tags : { project : '' } , // Pass an empty string to an existing key to delete a tag; this will delete the `project` tag
} ) ; คุณสามารถดึงคำอธิบายของดัชนีใด ๆ ด้วยชื่อโดยใช้ describeIndex
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . describeIndex ( 'serverless-index' ) ;
// {
// name: 'serverless-index',
// dimension: 1536,
// metric: 'cosine',
// host: 'serverless-index-4zo0ijk.svc.us-west2-aws.pinecone.io',
// deletionProtection: 'disabled',
// spec: {
// serverless: {
// cloud: 'aws',
// region: 'us-west-2'
// },
// },
// status: {
// ready: true,
// state: 'Ready'
// }
// }บันทึก
ส่วนนี้ใช้กับดัชนีที่ใช้ POD เท่านั้น ด้วยดัชนีที่ไม่มีเซิร์ฟเวอร์คุณจะไม่กำหนดค่าทรัพยากรการคำนวณหรือการจัดเก็บใด ๆ แต่ดัชนีแบบไม่มีเซิร์ฟเวอร์โดยอัตโนมัติขึ้นอยู่กับการใช้งานโดยอัตโนมัติ
คุณสามารถปรับจำนวนแบบจำลองหรือสเกลเป็นขนาดฝักที่ใหญ่กว่า (ระบุด้วย podType ) ดูดัชนีตามสเกลพ็อด คุณไม่สามารถปรับลดขนาดฝักหรือเปลี่ยนประเภทพ็อดฐาน
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . configureIndex ( 'pod-index' , {
spec : {
pod : {
replicas : 2 ,
podType : 'p1.x4' ,
} ,
} ,
} ) ;
const config = await pc . describeIndex ( 'pod-index' ) ;
// {
// name: 'pod-index',
// dimension: 1536,
// metric: 'cosine',
// host: 'pod-index-4zo0ijk.svc.us-east1-gcp.pinecone.io',
// deletionProtection: 'disabled',
// spec: {
// pod: {
// environment: 'us-east1-gcp',
// replicas: 2,
// shards: 2,
// podType: 'p1.x4',
// pods: 4,
// metadataConfig: [Object],
// sourceCollection: undefined
// }
// },
// status: {
// ready: true,
// state: 'ScalingUpPodSize'
// }
// }ดัชนีจะถูกลบตามชื่อ
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . deleteIndex ( 'sample-index' ) ; คำสั่ง listIndexes ส่งคืนวัตถุด้วยอาร์เรย์ของโมเดลดัชนีภายใต้ indexes
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . listIndexes ( ) ;
// {
// indexes: [
// {
// name: 'serverless-index',
// dimension: 1536,
// metric: 'cosine',
// host: 'serverless-index-4zo0ijk.svc.us-west2-aws.pinecone.io',
// deletionProtection: 'disabled',
// spec: {
// serverless: {
// cloud: 'aws',
// region: 'us-west-2',
// },
// },
// status: {
// ready: true,
// state: 'Ready',
// },
// },
// {
// name: 'pod-index',
// dimension: 1536,
// metric: 'cosine',
// host: 'pod-index-4zo0ijk.svc.us-west2-aws.pinecone.io',
// deletionProtection: 'disabled',
// spec: {
// pod: {
// environment: 'us-west2-aws',
// replicas: 1,
// shards: 1,
// podType: 'p1.x1',
// pods: 1,
// },
// },
// status: {
// ready: true,
// state: 'Ready',
// },
// },
// ],
// } บันทึก
ดัชนีที่ไม่มีเซิร์ฟเวอร์และสตาร์ทเตอร์ไม่รองรับคอลเลกชัน
คอลเลกชันเป็นสำเนาแบบคงที่ของดัชนีที่ใช้พ็อดซึ่งอาจใช้ในการสร้างการสำรองข้อมูลเพื่อสร้างสำเนาของดัชนีหรือเพื่อทำการทดลองด้วยการกำหนดค่าดัชนีที่แตกต่างกัน หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับคอลเลกชัน Pinecone ดูคอลเลกชันความเข้าใจ
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . createCollection ( {
name : 'collection-name' ,
source : 'index-name' ,
} ) ; การโทร API นี้ควรกลับมาอย่างรวดเร็ว แต่การสร้างคอลเลกชันอาจใช้เวลาจากนาทีถึงชั่วโมงขึ้นอยู่กับขนาดของดัชนีต้นทางและการกำหนดค่าของดัชนี ใช้ describeCollection เพื่อตรวจสอบสถานะของคอลเลกชัน
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . deleteCollection ( 'collection-name' ) ; คุณสามารถใช้ listCollections เพื่อยืนยันการลบ
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const describeCollection = await pc . describeCollection ( 'collection3' ) ;
// {
// name: 'collection3',
// size: 3126700,
// status: 'Ready',
// dimension: 3,
// vectorCount: 1234,
// environment: 'us-east1-gcp',
// } คำสั่ง listCollections ส่งคืนวัตถุด้วยอาร์เรย์ของโมเดลคอลเลกชันภายใต้ collections
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const list = await pc . listCollections ( ) ;
// {
// collections: [
// {
// name: 'collection1',
// size: 3089687,
// status: 'Ready',
// dimension: 3,
// vectorCount: 17378,
// environment: 'us-west1-gcp',
// },
// {
// name: 'collection2',
// size: 208309,
// status: 'Ready',
// dimension: 3,
// vectorCount: 1000,
// environment: 'us-east4-gcp',
// },
// ];
// } ดัชนี PINECONE สนับสนุนการดำเนินการสำหรับการทำงานกับข้อมูลเวกเตอร์โดยใช้การดำเนินการเช่น UPSERT, QUERY, FETCH และ DELETE
ในการดำเนินการข้อมูลบนดัชนีคุณกำหนดเป้าหมายโดยใช้วิธี index
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'test-index' ) ;
// Now perform index operations
await index . fetch ( [ '1' ] ) ; อาร์กิวเมนต์แรกคือชื่อของดัชนีที่คุณกำหนดเป้าหมาย มีอาร์กิวเมนต์ตัวเลือกที่สองสำหรับการจัดให้มีการแทนที่โฮสต์ดัชนี การให้อาร์กิวเมนต์ที่สองนี้ช่วยให้คุณข้ามพฤติกรรมเริ่มต้นของ SDK ในการแก้ไขโฮสต์ดัชนีของคุณผ่านชื่อดัชนีที่ให้ไว้ คุณสามารถค้นหาโฮสต์ดัชนีของคุณได้ในคอนโซล Pinecone หรือโดยใช้การดำเนินการ describeIndex หรือ listIndexes
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'test-index' , 'my-index-host-1532-svc.io' ) ;
// Now perform index operations against: https://my-index-host-1532-svc.io
await index . fetch ( [ '1' ] ) ; หากคุณกำลังจัดเก็บข้อมูลเมตาไว้ข้างค่าเวกเตอร์ของคุณคุณสามารถส่งพารามิเตอร์ประเภทไปยัง index() เพื่อให้ได้ typeChecking typeChecking ที่เหมาะสม
import { Pinecone , PineconeRecord } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
type MovieMetadata = {
title : string ,
runtime : number ,
genre : 'comedy' | 'horror' | 'drama' | 'action'
}
// Specify a custom metadata type while targeting the index
const index = pc . index < MovieMetadata > ( 'test-index' ) ;
// Now you get type errors if upserting malformed metadata
await index . upsert ( [ {
id : '1234' ,
values : [
... . // embedding values
] ,
metadata : {
genre : 'Gone with the Wind' ,
runtime : 238 ,
genre : 'drama' ,
// @ts-expect-error because category property not in MovieMetadata
category : 'classic'
}
} ] )
const results = await index . query ( {
vector : [
... // query embedding
] ,
filter : { genre : { '$eq' : 'drama' } }
} )
const movie = results . matches [ 0 ] ;
if ( movie . metadata ) {
// Since we passed the MovieMetadata type parameter above,
// we can interact with metadata fields without having to
// do any typecasting.
const { title , runtime , genre } = movie . metadata ;
console . log ( `The best match in drama was ${ title } ` )
} โดยค่าเริ่มต้นการดำเนินการข้อมูลทั้งหมดจะเกิดขึ้นภายในเนมสเปซเริ่มต้นของ '' หากคุณกำลังทำงานกับเนมสเปซที่ไม่ใช่ default อื่น ๆ คุณสามารถกำหนดเป้าหมายเนมสเปซโดยการผูกมัดการโทรไปยัง namespace()
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'test-index' ) . namespace ( 'ns1' ) ;
// Now perform index operations in the targeted index and namespace
await index . fetch ( [ '1' ] ) ;ดูใช้เนมสเปซสำหรับข้อมูลเพิ่มเติม
Pinecone คาดว่าบันทึกไว้ในดัชนีจะมีแบบฟอร์มต่อไปนี้:
type PineconeRecord = {
id : string ;
values : Array < number > ;
sparseValues ?: Array < number > ;
metadata ?: object ;
} ;ในการเพิ่มเวกเตอร์บางตัวคุณสามารถใช้ไคลเอนต์ได้เช่นนั้น:
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
// Target an index
const index = pc . index ( 'sample-index' ) ;
// Prepare your data. The length of each array
// of vector values must match the dimension of
// the index where you plan to store them.
const vectors = [
{
id : '1' ,
values : [ 0.236 , 0.971 , 0.559 ] ,
sparseValues : { indices : [ 0 , 1 ] , values : [ 0.236 , 0.34 ] } , // Optional; for hybrid search
} ,
{
id : '2' ,
values : [ 0.685 , 0.111 , 0.857 ] ,
sparseValues : { indices : [ 0 , 1 ] , values : [ 0.345 , 0.98 ] } , // Optional; for hybrid search
} ,
] ;
// Upsert the data into your index
await index . upsert ( vectors ) ; ตอนนี้คุณสามารถนำเข้าเวกเตอร์ได้จากการจัดเก็บวัตถุ Import เป็นการดำเนินการแบบอะซิงโครนัสที่ดำเนินการมานานซึ่งนำเข้าเร็กคอร์ดจำนวนมากลงในดัชนี Pinecone Serverless
ในการนำเข้าเวกเตอร์จากการจัดเก็บวัตถุจะต้องเก็บไว้ในไฟล์ Parquet และปฏิบัติตามรูปแบบไฟล์ที่จำเป็น การจัดเก็บวัตถุของคุณจะต้องปฏิบัติตามโครงสร้างไดเรกทอรีที่จำเป็น
ตัวอย่างต่อไปนี้นำเข้าเวกเตอร์จากถัง Amazon S3 ลงในดัชนี Pinecone Serverless:
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const indexName = 'sample-index' ;
await pc . createIndex ( {
name : indexName ,
dimension : 10 ,
spec : {
serverless : {
cloud : 'aws' ,
region : 'eu-west-1' ,
} ,
} ,
} ) ;
const index = pc . Index ( indexName ) ;
const storageURI = 's3://my-bucket/my-directory/' ;
await index . startImport ( storageURI , 'continue' ) ; // "Continue" will avoid aborting the operation if errors are encountered.
// {
// "id": "import-id"
// }คุณสามารถเริ่มยกเลิกและตรวจสอบสถานะของการดำเนินการนำเข้าทั้งหมดหรือหนึ่งรายการ
หมายเหตุ:
Import ทำงานเฉพาะกับดัชนีแบบไม่มีเซิร์ฟเวอร์Import อยู่ในตัวอย่างสาธารณะ เมื่อทำการทดลองกับการดำเนินการข้อมูลบางครั้งก็มีประโยชน์ที่จะทราบจำนวนระเบียน/เวกเตอร์ที่เก็บไว้ในแต่ละเนมสเปซ ในกรณีนั้นให้กำหนดเป้าหมายดัชนีและใช้คำสั่ง describeIndexStats()
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'example-index' ) ;
await index . describeIndexStats ( ) ;
// {
// namespaces: {
// '': { recordCount: 10 }
// foo: { recordCount: 2000 },
// bar: { recordCount: 2000 }
// },
// dimension: 1536,
// indexFullness: 0,
// totalRecordCount: 4010
// }วิธีการสืบค้นยอมรับตัวเลือกจำนวนมาก มิติของเวกเตอร์แบบสอบถามจะต้องตรงกับมิติของดัชนีของคุณ
type QueryOptions = {
topK : number ; // number of results desired
vector ?: Array < number > ; // must match dimension of index
sparseVector ?: {
indices : Array < integer > ; // indices must fall within index dimension
values : Array < number > ; // indices and values arrays must have same length
} ;
id ?: string ;
includeMetadata : boolean ;
includeValues : boolean ;
} ; ตัวอย่างเช่นในการค้นหาโดยค่าเวกเตอร์คุณจะผ่านพารามิเตอร์ vector ในการกำหนดค่าตัวเลือก เพื่อเห็นแก่ความกะทัดรัดตัวอย่างเวกเตอร์แบบสอบถามนี้มีขนาดเล็ก (มิติ 2) แต่ในกรณีการใช้งานที่สมจริงยิ่งขึ้นเวกเตอร์แบบสอบถามนี้จะเป็นการฝังตัวที่ถูกส่งออกโดยแบบจำลอง ดูรหัสตัวอย่างเพื่อดูตัวอย่างที่สมจริงมากขึ้นเกี่ยวกับวิธีการใช้ query
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'my-index' ) ;
await index . query ( { topK : 3 , vector : [ 0.22 , 0.66 ] } ) ;
// {
// matches: [
// {
// id: '556',
// score: 1.00000012,
// values: [],
// sparseValues: undefined,
// metadata: undefined
// },
// {
// id: '137',
// score: 1.00000012,
// values: [],
// sparseValues: undefined,
// metadata: undefined
// },
// {
// id: '129',
// score: 1.00000012,
// values: [],
// sparseValues: undefined,
// metadata: undefined
// }
// ],
// namespace: '',
// usage: {
// readUnits: 5
// }
// } คุณรวมถึงตัวเลือกในการ includeMetadata: true หรือ includeValues: true ถ้าคุณต้องการข้อมูลนี้ โดยค่าเริ่มต้นสิ่งเหล่านี้จะไม่ถูกส่งคืนเพื่อให้การตอบกลับมีขนาดเล็ก
โปรดจำไว้ว่าการดำเนินการข้อมูลเกิดขึ้นภายในบริบทของ namespace ดังนั้นหากคุณทำงานกับเนมสเปซและไม่เห็นผลลัพธ์ที่คาดหวังคุณควรตรวจสอบว่าคุณกำลังกำหนดเป้าหมายเนมสเปซที่ถูกต้องด้วยแบบสอบถามของคุณ
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
// Target the index and namespace
const index = pc . index ( 'my-index' ) . namespace ( 'my-namespace' ) ;
const results = await index . query ( { topK : 3 , vector : [ 0.22 , 0.66 ] } ) ; คุณสามารถสืบค้นได้โดยใช้ค่าเวกเตอร์ของระเบียนที่มีอยู่ในดัชนีโดยผ่านรหัสระเบียน โปรดทราบว่าระเบียนที่มี ID ที่ระบุอาจอยู่ในการตอบสนองของการดำเนินการนี้
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'my-index' ) ;
const results = await index . query ( { topK : 10 , id : '1' } ) ; หากคุณกำลังทำงานกับเวกเตอร์ที่มีความหนาแน่นเบาบางคุณสามารถเพิ่มค่าเวกเตอร์กระจัดกระจายเพื่อทำการค้นหาแบบไฮบริด
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
await pc . createIndex ( {
name : 'hybrid-search-index' ,
metric : 'dotproduct' , // Note: dot product is the only distance metric supported for hybrid search
dimension : 2 ,
spec : {
pod : {
environment : 'us-west4-gcp' ,
podType : 'p2.x1' ,
} ,
} ,
waitUntilReady : true ,
} ) ;
const index = pc . index ( 'hybrid-search-index' ) ;
const hybridRecords = [
{
id : '1' ,
values : [ 0.236 , 0.971 ] , // dense vectors
sparseValues : { indices : [ 0 , 1 ] , values : [ 0.236 , 0.34 ] } , // sparse vectors
} ,
{
id : '2' ,
values : [ 0.685 , 0.111 ] ,
sparseValues : { indices : [ 0 , 1 ] , values : [ 0.887 , 0.243 ] } ,
} ,
] ;
await index . upsert ( hybridRecords ) ;
const query = 'What is the most popular red dress?' ;
// ... send query to dense vector embedding model and save those values in `denseQueryVector`
// ... send query to sparse vector embedding model and save those values in `sparseQueryVector`
const denseQueryVector = [ 0.236 , 0.971 ] ;
const sparseQueryVector = { indices : [ 0 , 1 ] , values : [ 0.0 , 0.34 ] } ;
// Execute a hybrid search
await index . query ( {
topK : 3 ,
vector : denseQueryVector ,
sparseVector : sparseQueryVector ,
} ) ; คุณอาจต้องการอัปเดต values เวกเตอร์ sparseValues หรือ metadata ระบุ ID และค่าแอตทริบิวต์ที่คุณต้องการอัปเดต
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'imdb-movies' ) ;
await index . update ( {
id : '18593' ,
metadata : { genre : 'romance' } ,
} ) ; เมธอด listPaginated สามารถใช้เพื่อแสดงรายการรหัสบันทึกที่ตรงกับคำนำหน้า ID เฉพาะในรูปแบบ paginated ด้วยการกำหนด ID บันทึกอย่างชาญฉลาดสิ่งนี้สามารถใช้เพื่อช่วยจำลองความสัมพันธ์แบบลำดับชั้นระหว่างบันทึกที่แตกต่างกันเช่นเมื่อมีการฝังตัวสำหรับชิ้นส่วนหรือชิ้นส่วนที่เกี่ยวข้องกับเอกสารเดียวกันหลายชิ้น
หมายเหตุ:
prefix คำนำหน้าเริ่มต้นเป็นสตริงว่างซึ่งส่งคืนรหัสเวกเตอร์ทั้งหมดในดัชนีของคุณ100 เวกเตอร์ IDs หากไม่ได้ระบุ limit ดังนั้นหากมีรหัสเวกเตอร์น้อยกว่า 100 ตัวที่ตรงกับ prefix ในดัชนีของคุณและคุณไม่ได้ระบุ limit paginationToken ของคุณจะ undefined ตัวอย่างต่อไปนี้แสดงวิธีดึงรหัสทั้งสองหน้าของเวกเตอร์สำหรับเวกเตอร์ที่มี IDs มีคำนำหน้า doc1# โดยสมมติว่ามี limit 3 และเอกสาร doc1 ที่ถูกแบ่งออกเป็น 4 เวกเตอร์
const pc = new Pinecone ( ) ;
const index = pc . index ( 'my-index' ) . namespace ( 'my-namespace' ) ;
// Fetch the 1st 3 vector IDs matching prefix 'doc1#'
const results = await index . listPaginated ( { limit : 3 , prefix : 'doc1#' } ) ;
console . log ( results ) ;
// {
// vectors: [
// { id: 'doc1#01' }
// { id: 'doc1#02' }
// { id: 'doc1#03' }
// ...
// ],
// pagination: {
// next: 'eyJza2lwX3Bhc3QiOiJwcmVUZXN0LS04MCIsInByZWZpeCI6InByZVRlc3QifQ=='
// },
// namespace: 'my-namespace',
// usage: { readUnits: 1 }
// }
// Fetch the final vector ID matching prefix 'doc1#' using the paginationToken returned by the previous call
const nextResults = await index . listPaginated ( {
prefix : 'doc1#' ,
paginationToken : results . pagination ?. next ,
} ) ;
console . log ( nextResults ) ;
// {
// vectors: [
// { id: 'doc1#04' }
// ],
// pagination: undefined,
// namespace: 'my-namespace',
// usage: { readUnits: 1 }
// } import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'my-index' ) ;
const fetchResult = await index . fetch ( [ 'id-1' , 'id-2' ] ) ; เพื่อความสะดวกมีวิธีการที่เกี่ยวข้องกับการลบหลายวิธี คุณสามารถตรวจสอบผลลัพธ์ของการดำเนินการลบได้โดยพยายาม fetch() บันทึกหรือดูสรุปดัชนีด้วย describeIndexStats()
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'my-index' ) ;
await index . deleteOne ( 'id-to-delete' ) ; import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'my-index' ) ;
await index . deleteMany ( [ 'id-1' , 'id-2' , 'id-3' ] ) ;หมายเหตุ: การลบโดยตัวกรองข้อมูลเมตาใช้จะใช้กับดัชนีที่ใช้ POD เท่านั้น
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'albums-database' ) ;
await index . deleteMany ( { genre : 'rock' } ) ; บันทึก
ดัชนีในสภาพแวดล้อม GCP-Starter ไม่รองรับเนมสเปซ
หากต้องการ nuke ทุกอย่างในเนมสเปซที่กำหนดเป้าหมายให้ใช้วิธีการ deleteAll
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const index = pc . index ( 'my-index' ) ;
await index . namespace ( 'foo-namespace' ) . deleteAll ( ) ; หากคุณไม่ได้ระบุเนมสเปซบันทึกในเนมสเปซเริ่มต้น '' จะถูกลบ
โต้ตอบกับ API การอนุมานของ Pinecone (ปัจจุบันอยู่ในตัวอย่างสาธารณะ) API การอนุมาน Pinecone เป็นบริการที่ให้คุณเข้าถึงโมเดลการอนุมานที่โฮสต์บนโครงสร้างพื้นฐานของ Pinecone
หมายเหตุ:
รุ่นที่รองรับ:
ส่งข้อความไปยัง API การอนุมานของ Pinecone เพื่อสร้าง embeddings สำหรับเอกสารและการสืบค้น
import { Pinecone } from '@pinecone-database/pinecone' ;
const client = new Pinecone ( { apiKey : '<Your API key from app.pinecone.io>' } ) ;
const embeddingModel = 'multilingual-e5-large' ;
const documents = [
'Turkey is a classic meat to eat at American Thanksgiving.' ,
'Many people enjoy the beautiful mosques in Turkey.' ,
] ;
const docParameters = {
inputType : 'passage' ,
truncate : 'END' ,
} ;
async function generateDocEmbeddings ( ) {
try {
return await client . inference . embed (
embeddingModel ,
documents ,
docParameters
) ;
} catch ( error ) {
console . error ( 'Error generating embeddings:' , error ) ;
}
}
generateDocEmbeddings ( ) . then ( ( embeddingsResponse ) => {
if ( embeddingsResponse ) {
console . log ( embeddingsResponse ) ;
}
} ) ;
// << Upsert documents into Pinecone >>
const userQuery = [ 'How should I prepare my turkey?' ] ;
const queryParameters = {
inputType : 'query' ,
truncate : 'END' ,
} ;
async function generateQueryEmbeddings ( ) {
try {
return await client . inference . embed (
embeddingModel ,
userQuery ,
queryParameters
) ;
} catch ( error ) {
console . error ( 'Error generating embeddings:' , error ) ;
}
}
generateQueryEmbeddings ( ) . then ( ( embeddingsResponse ) => {
if ( embeddingsResponse ) {
console . log ( embeddingsResponse ) ;
}
} ) ;
// << Send query to Pinecone to retrieve similar documents >> เอกสาร Rerank ในการสั่งซื้อที่เกี่ยวข้องกับการสืบค้น
หมายเหตุ: score แสดงถึงการวัดที่แน่นอนของความเกี่ยวข้องของการสืบค้นและคู่ที่กำหนด ทำให้เป็นมาตรฐานระหว่าง [0, 1] score แสดงถึงความเกี่ยวข้องของรายการและการสืบค้นที่เกี่ยวข้องอย่างใกล้ชิดโดยมีคะแนนใกล้เคียงกับ 1 แสดงความเกี่ยวข้องที่สูงขึ้น
import { Pinecone } from '@pinecone-database/pinecone' ;
const pc = new Pinecone ( ) ;
const rerankingModel = 'bge-reranker-v2-m3' ;
const myQuery = 'What are some good Turkey dishes for Thanksgiving?' ;
// Option 1: Documents as an array of strings
const myDocsStrings = [
'I love turkey sandwiches with pastrami' ,
'A lemon brined Turkey with apple sausage stuffing is a classic Thanksgiving main' ,
'My favorite Thanksgiving dish is pumpkin pie' ,
'Turkey is a great source of protein' ,
] ;
// Option 1 response
const response = await pc . inference . rerank (
rerankingModel ,
myQuery ,
myDocsStrings
) ;
console . log ( response ) ;
// {
// model: 'bge-reranker-v2-m3',
// data: [
// { index: 1, score: 0.5633179, document: [Object] },
// { index: 2, score: 0.02013874, document: [Object] },
// { index: 3, score: 0.00035419367, document: [Object] },
// { index: 0, score: 0.00021485926, document: [Object] }
// ],
// usage: { rerankUnits: 1 }
// }
// Option 2: Documents as an array of objects
const myDocsObjs = [
{
title : 'Turkey Sandwiches' ,
body : 'I love turkey sandwiches with pastrami' ,
} ,
{
title : 'Lemon Turkey' ,
body : 'A lemon brined Turkey with apple sausage stuffing is a classic Thanksgiving main' ,
} ,
{
title : 'Thanksgiving' ,
body : 'My favorite Thanksgiving dish is pumpkin pie' ,
} ,
{
title : 'Protein Sources' ,
body : 'Turkey is a great source of protein' ,
} ,
] ;
// Option 2: Options object declaring which custom key to rerank on
// Note: If no custom key is passed via `rankFields`, each doc must contain a `text` key, and that will act as the default)
const rerankOptions = {
topN : 3 ,
returnDocuments : false ,
rankFields : [ 'body' ] ,
parameters : {
inputType : 'passage' ,
truncate : 'END' ,
} ,
} ;
// Option 2 response
const response = await pc . inference . rerank (
rerankingModel ,
myQuery ,
myDocsObjs ,
rerankOptions
) ;
console . log ( response ) ;
// {
// model: 'bge-reranker-v2-m3',
// data: [
// { index: 1, score: 0.5633179, document: undefined },
// { index: 2, score: 0.02013874, document: undefined },
// { index: 3, score: 0.00035419367, document: undefined },
// ],
// usage: { rerankUnits: 1 }
//} การทดสอบทั้งหมดเกิดขึ้นโดยอัตโนมัติใน CI และได้รับการกำหนดค่าโดยใช้การกระทำของ GitHub และเวิร์กโฟลว์ซึ่งอยู่ในไดเรกทอรี .github ของ repo นี้
ดูการสนับสนุน. MD สำหรับข้อมูลเพิ่มเติม