0Pricing
Vector Databases: Pinecone, Weaviate & pgvector · บทเรียน

การค้นข้อมูลเวกเตอร์ใน Pinecone

ดำเนินการค้นหาความคล้ายคลึงใน Pinecone อย่างมีประสิทธิภาพ เพื่อค้นคืนเวกเตอร์ที่เกี่ยวข้องตามเวกเตอร์ฝังตัวของคำค้น

การค้นข้อมูลเวกเตอร์ใน Pinecone เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vector Databases: Pinecone, Weaviate & pgvector และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro to Pinecone Querying

After setting up your index and adding data, the next crucial step is to retrieve relevant information. This is where querying comes in!

Querying in Pinecone means finding vectors in your index that are most similar to a given "query vector." It's how you perform semantic search, recommendations, and more.

Your Query as a Vector

Just like the data you stored, your search query also needs to be converted into a vector. This "query vector" is then compared against all vectors in your Pinecone index.

  • Embedding Model: You use the same embedding model that generated your stored vectors to create your query vector.
  • Similarity: Pinecone calculates the distance or similarity between your query vector and indexed vectors.

Generating a Query Embedding

Before you can query Pinecone, you need an embedding for your search term. Let's say you want to find documents similar to "machine learning models."

You'd pass "machine learning models" through your chosen embedding model (e.g., OpenAI's text-embedding-ada-002) to get a vector representation.

Introducing `index.query()`

Pinecone's client provides a straightforward method for querying: index.query(). This method is your gateway to finding similar vectors.

Key parameters you'll often use:

  • vector: The embedding of your query.
  • top_k: How many similar results you want.
  • include_metadata: Whether to return associated metadata.
  • include_values: Whether to return the raw vector values.

Your First Pinecone Query

Let's perform a simple query. We'll use a placeholder vector for now, assuming it's already generated. Remember to replace YOUR_API_KEY and YOUR_ENVIRONMENT.

import os
from pinecone import Pinecone, Index

# Initialize Pinecone (replace with your actual API key and environment)
# In a real app, use environment variables!
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
pc = Pinecone(api_key=api_key, environment=environment)

index_name = "my-first-index"
index = pc.Index(index_name)

# A dummy query vector (in reality, this would be an embedding)
query_vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8] # Example 8-dim vector

# Perform the query
query_results = index.query(
    vector=query_vector,
    top_k=3 # Get the 3 most similar results
)

print("Query Results:")
for match in query_results.matches:
    print(f"ID: {match.id}, Score: {match.score:.2f}")

Deciphering Query Matches

The query_results object contains a list of matches. Each match represents a similar vector found in your index.

  • id: The unique identifier of the matched vector.
  • score: A numerical value indicating similarity. Higher scores (closer to 1 for cosine, closer to 0 for Euclidean) mean higher similarity.
  • values: The raw vector (if include_values=True).
  • metadata: Any associated metadata (if include_metadata=True).

Limiting Results with `top_k`

The top_k parameter is crucial for controlling how many results Pinecone returns. It specifies the number of nearest neighbors you want to retrieve.

  • If top_k=1, you get only the single most similar vector.
  • If top_k=10, you get the top 10 most similar vectors.

Choose top_k based on how many relevant items your application needs.

Getting More Context: Metadata

Often, you don't just want the ID and score; you need the original content or other properties associated with the vector. This is where include_metadata comes in.

  • Set include_metadata=True to retrieve the dictionary of metadata stored with each vector.
  • You can also set include_values=True to get the actual vector array of the matched item, though this is less common for basic retrieval.

Combining Query with Filters (Preview)

Pinecone allows you to refine your similarity searches by adding filters based on the metadata you stored with your vectors.

For example, you could search for similar items only within a specific category or by a certain author.

We'll dive deeper into powerful metadata filtering in a later lesson, but know that it's a key feature for precise searches.

Practical Query with Metadata

Let's expand our previous example to include metadata in the results. For this to work, we'd need to have upserted data with metadata in a previous step.

This example assumes an index with vectors and associated metadata (e.g., {"genre": "sci-fi"}).

import os
from pinecone import Pinecone, Index

# Initialize Pinecone
api_key = os.environ.get("PINECONE_API_KEY", "YOUR_API_KEY")
environment = os.environ.get("PINECONE_ENVIRONMENT", "YOUR_ENVIRONMENT")
pc = Pinecone(api_key=api_key, environment=environment)

index_name = "my-first-index"
index = pc.Index(index_name)

# A dummy query vector
query_vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]

# Perform query, including metadata
query_results = index.query(
    vector=query_vector,
    top_k=2, # Get top 2 results
    include_metadata=True # Request metadata
)

print("Detailed Query Results:")
for match in query_results.matches:
    print(f"ID: {match.id}, Score: {match.score:.2f}, Metadata: {match.metadata}")

Query Parameter Check

You want to retrieve the 5 most similar vectors from your Pinecone index. You also need to see the original metadata associated with each matched vector.

Which combination of parameters should you use in your index.query() call?

Querying Pinecone: Recap

Great job! You've learned how to query your Pinecone index to find similar vectors.

  • Queries use a query vector, typically generated by the same embedding model.
  • The index.query() method is used, with key parameters like vector, top_k, include_metadata, and include_values.
  • Results include id and a score indicating similarity.

Next, we'll explore more advanced ways to refine your searches!

คำถามที่พบบ่อย

บทเรียน “การค้นข้อมูลเวกเตอร์ใน Pinecone” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การค้นข้อมูลเวกเตอร์ใน Pinecone” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การค้นข้อมูลเวกเตอร์ใน Pinecone”

ดำเนินการค้นหาความคล้ายคลึงใน Pinecone อย่างมีประสิทธิภาพ เพื่อค้นคืนเวกเตอร์ที่เกี่ยวข้องตามเวกเตอร์ฝังตัวของคำค้น คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การค้นข้อมูลเวกเตอร์ใน Pinecone” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม

ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างดัชนี Pinecone
  2. การเพิ่มหรืออัปเดตข้อมูลใน Pinecone
  3. การค้นข้อมูลเวกเตอร์ใน Pinecone
  4. ทำความเข้าใจราคาและพ็อดของ Pinecone
← กลับไปที่ Vector Databases: Pinecone, Weaviate & pgvector