Vector Databases: Pinecone, Weaviate & pgvector · Lekcja

Tworzenie indeksu Pinecone

Nauczy się Pan/Pani konfigurować pierwszy indeks Pinecone, określając wymiary, metrykę i inne kluczowe parametry.

Lekcja 1 z 411 kroki

Tworzenie indeksu Pinecone to bezpłatna lekcja Vector Databases: Pinecone, Weaviate & pgvector na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Vector Databases: Pinecone, Weaviate & pgvector, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Vector Databases: Pinecone, Weaviate & pgvector zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Welcome to Pinecone!

Hello! Today, we're diving into Pinecone, a leading vector database. It's designed to store and search billions of vectors incredibly fast.

Think of it as a specialized search engine for your AI's understanding of data, helping it find similar items based on their 'meaning'.

What is a Vector Index?

Before you can store and search vectors in Pinecone, you need an index.

An index is like a specialized table where your vector data (embeddings) will live. It's configured to handle vectors of a specific size and compare them using a particular mathematical method, ensuring efficient similarity searches.

Key Parameter: Dimensions

Every vector has a specific dimension, which is simply the number of values (or features) it contains. For example, a vector [0.1, 0.5, 0.2] has 3 dimensions.

When creating a Pinecone index, you must specify the dimension. This dimension must match the dimension of the embedding vectors you plan to store in it. Mismatched dimensions will cause errors!

Key Parameter: Distance Metric

To find 'similar' vectors, Pinecone needs to know how to calculate the 'distance' or 'similarity' between them. This is done using a distance metric.

  • Cosine Similarity: Measures the angle between vectors. Good for text embeddings.
  • Euclidean Distance: Measures the straight-line distance. Smaller values mean more similar.
  • Dot Product: Often used in recommendation systems, can be faster.

Choose the metric that best suits how your embeddings were generated.

Setting Up Your Pinecone Client

First, you need to initialize the Pinecone client in your Python code. This connects your application to the Pinecone service using your API key and environment.

Replace the placeholders with your actual Pinecone API key and environment (e.g., 'gcp-starter', 'us-west-2').

from pinecone import Pinecone, PodSpec

# Replace with your actual API key and environment
# Get these from your Pinecone console
api_key = "YOUR_API_KEY"
environment = "YOUR_ENVIRONMENT" 

pc = Pinecone(api_key=api_key, environment=environment)

print("Pinecone client initialized!")

Creating Your First Index

Now, let's create a Pinecone index! You'll use the create_index() method, specifying the index name, vector dimension, and distance metric.

We'll also use PodSpec to select the environment. For beginners, 'gcp-starter' is a free, convenient option.

from pinecone import Pinecone, PodSpec

# Assume pc is already initialized
# Replace with your actual API key and environment
api_key = "YOUR_API_KEY"
environment = "YOUR_ENVIRONMENT" # e.g., "gcp-starter"
pc = Pinecone(api_key=api_key, environment=environment)

index_name = "my-first-index"
dimension = 1536 # Common for OpenAI ada-002 embeddings
metric = "cosine" # Common for text embeddings

# Check if index already exists to avoid errors
if index_name not in pc.list_indexes():
    pc.create_index(
        name=index_name,
        dimension=dimension,
        metric=metric,
        spec=PodSpec(environment=environment) # Use your chosen environment
    )
    print(f"Index '{index_name}' created!")
else:
    print(f"Index '{index_name}' already exists.")

Checking Index Status

Index creation isn't instant. It takes a moment for Pinecone to provision the resources. You should always check if your index is ready before trying to use it to upsert data.

The describe_index() method provides status information, including whether the index is 'ready'.

from pinecone import Pinecone, PodSpec
import time

# Assume pc is initialized and index_name is defined
# Replace with your actual API key and environment
api_key = "YOUR_API_KEY"
environment = "YOUR_ENVIRONMENT"
pc = Pinecone(api_key=api_key, environment=environment)
index_name = "my-first-index" # Or the name of your new index

# Wait for the index to be ready
# (This loop might run indefinitely if index creation fails)
if index_name in pc.list_indexes():
    while not pc.describe_index(index_name).status['ready']:
        print(f"Waiting for index '{index_name}' to be ready...")
        time.sleep(1)
    
    print(f"Index '{index_name}' is ready!")
else:
    print(f"Index '{index_name}' does not exist. Please create it first.")

Advanced Pod Configuration

For production applications or larger datasets, you might need more control over your index's infrastructure. The PodSpec allows you to configure:

  • Pod Type: Choose more powerful computing resources (e.g., p1.x1).
  • Replicas: Increase for higher availability and read throughput.
  • Shards: Partition data across multiple servers for scalability.

The 'gcp-starter' environment handles these settings automatically for you.

from pinecone import Pinecone, PodSpec

# Assume pc is initialized
# pc = Pinecone(api_key="...", environment="...")

# Example of creating an index with advanced PodSpec settings
# This is commented out because it requires a non-starter environment
# and may incur costs.
# pc.create_index(
#     name="prod-index",
#     dimension=768,
#     metric="euclidean",
#     spec=PodSpec(
#         environment="us-west-2", # A non-starter environment
#         pod_type="p1.x1",        # A more powerful pod type
#         replicas=2,              # 2 copies of your index for redundancy
#         shards=1                 # Data partitioning
#     )
# )

print("Advanced PodSpec settings are for fine-tuning performance and scale.")

Best Practices for Index Names

Choose clear and descriptive names for your Pinecone indexes. This helps you manage multiple indexes in your project.

  • Use lowercase letters, numbers, and hyphens.
  • Avoid special characters or spaces.
  • Make them unique within your project.
  • Consider including the purpose or data source (e.g., product-catalog-embeddings, qa-docs-v2).

Index Creation Check

You've learned about the essential components needed to create a Pinecone index. Let's test your knowledge!

Recap: Pinecone Index Creation

Great job! You've learned the fundamentals of creating a Pinecone index.

  • An index is crucial for storing and searching vector embeddings.
  • Essential parameters are index name, vector dimension, and distance metric (e.g., cosine, euclidean).
  • You initialize the Pinecone client with your API key and environment.
  • Always check the index status to ensure it's ready before use.
  • PodSpec allows for advanced configuration, especially for production.

Next, we'll learn how to populate your index with actual data!

Bezpłatny start

Ucz się Vector Databases: Pinecone, Weaviate & pgvector dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Tworzenie indeksu Pinecone” jest bezpłatna?

Tak — pełny tekst „Tworzenie indeksu Pinecone” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Vector Databases: Pinecone, Weaviate & pgvector, przejdź na CoddyKit PRO. Kurs Vector Databases: Pinecone, Weaviate & pgvector zawiera 4 lekcji w sumie.

Co nauczysz się w „Tworzenie indeksu Pinecone”?

Nauczy się Pan/Pani konfigurować pierwszy indeks Pinecone, określając wymiary, metrykę i inne kluczowe parametry. Ćwiczysz Vector Databases: Pinecone, Weaviate & pgvector z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Vector Databases: Pinecone, Weaviate & pgvector?

Nie wymagamy żadnego doświadczenia. Vector Databases: Pinecone, Weaviate & pgvector w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Tworzenie indeksu Pinecone”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Vector Databases: Pinecone, Weaviate & pgvector?

Tak. Każda lekcja Vector Databases: Pinecone, Weaviate & pgvector zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Tworzenie indeksu Pinecone
  2. Upsert danych do Pinecone
  3. Wykonywanie zapytań o dane wektorowe w Pinecone
  4. Zrozumienie cen i podów Pinecone
← Powrót do Vector Databases: Pinecone, Weaviate & pgvector