การนำเข้าเวกเตอร์

การสร้างแอปพลิเคชัน AI ด้วย Pinecone

James Chapman

Curriculum Manager, DataCamp

การสร้างและเชื่อมต่อกับ index

pc = Pinecone(api_key="API_KEY")

pc.create_index(
    name='datacamp-index',
    dimension=1536,
    spec=ServerlessSpec(
        cloud='aws', 
        region='us-east-1'
    )
)

index = pc.Index('datacamp-index')
การสร้างแอปพลิเคชัน AI ด้วย Pinecone

การนำเข้าเวกเตอร์

vectors = [
    {
        "id": "0",
        "values": [0.025525547564029694, ..., 0.0188823901116848]
    },
        ...,
    {
        "id": "9",
        "values": [0.020712468773126602, ..., 0.006418442353606224]
    },
]
การสร้างแอปพลิเคชัน AI ด้วย Pinecone

การตรวจสอบจำนวนมิติ

vector_dims = [len(vector['values']) == 1536 for vector in vectors]

all(vector_dims)
True
PineconeApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Date': 'Fri, 17 May 2024 10:54:57 GMT', ...
HTTP response body: {"code":3,"message":"Vector dimension 256 does not match the
dimension of the index 1536","details":[]}
การสร้างแอปพลิเคชัน AI ด้วย Pinecone

การ upsert เวกเตอร์

  • .upsert(): อัปเดตหรือแทรก
index.upsert(

vectors=vectors
)
index.describe_index_stats()
{'dimension': 1536,
 'index_fullness': 0.0,
 'namespaces': {'': {'vector_count': 10}},
 'total_vector_count': 10}
การสร้างแอปพลิเคชัน AI ด้วย Pinecone

การนำเข้าเวกเตอร์พร้อม metadata

vectors = [
    {
        "id": "0",
        "values": [0.025525547564029694, ..., 0.0188823901116848]
        "metadata": {"genre": "productivity", "year": 2020}
    },
        ...,
]
  • Metadata: ข้อมูลเกี่ยวกับข้อมูล!
    • นำไปใช้กับ metadata filteringบทที่ 2
การสร้างแอปพลิเคชัน AI ด้วย Pinecone

การ upsert เวกเตอร์พร้อม metadata

index.upsert(
    vectors=vectors
)

อย่าลืมใช้โครงสร้างนี้:

    {
        "id": "0",
        "values": [0.025525547564029694, ..., 0.0188823901116848]
        "metadata": {"genre": "productivity", "year": 2020}
    },
        ...,
การสร้างแอปพลิเคชัน AI ด้วย Pinecone

มาฝึกกันเถอะ!

การสร้างแอปพลิเคชัน AI ด้วย Pinecone

Preparing Video For Download...