Get startedGet started for free

Querying vectors for semantic search

In this exercise, you'll create a query vector from the question, 'What is in front of the Notre Dame Main Building?'. Using this embedded query, you'll query the 'squad_dataset' namespace from the 'pinecone-datacamp' index and return the top five most similar vectors.

This exercise is part of the course

Vector Databases for Embeddings with Pinecone

View Course

Exercise instructions

  • Initialize the Pinecone client with your API key (the OpenAI client is available as client).
  • Create a query vector by embedding the query provided with the same OpenAI embedding model you used for embedding the other vectors.
  • Query the "squad_dataset" namespace using query_emb, returning the top five most similar results.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initialize the Pinecone client
pc = Pinecone(api_key="____")
index = pc.Index('pinecone-datacamp')

query = "What is in front of the Notre Dame Main Building?"

# Create the query vector
query_response = ____(
    input=____,
    model="text-embedding-3-small"
)
query_emb = query_response.data[0].embedding

# Query the index and retrieve the top five most similar vectors
retrieved_docs = ____

for result in retrieved_docs['matches']:
    print(f"{result['id']}: {round(result['score'], 2)}")
    print('\n')
Edit and Run Code