Pinecone SDK update literally kills me Query data and generating embeddings

The code

import pinecone
from transformers import AutoModel, AutoTokenizer
import torch
# Replace with your actual API key and environment
api_key = ""  # Your API key
environment = "us-east-1"  # Your environment
# Initialize Pinecone
pinecone.configure(api_key=api_key, environment=environment)
# Specify the index name
index_name = "my-index"
# Check if the index exists
if index_name not in pinecone.list_indexes():
    raise ValueError(f"Index '{index_name}' does not exist. Please create it first.")
# Load the Pinecone index
index = pinecone.Index(index_name)
# Load Llama2 model and tokenizer
model_name = "TheBloke/Llama-2-13B-chat-GGML"  # Replace with your model name
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# Function to generate embeddings
def generate_embeddings(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
    return outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
# Query for similarity search
query = "What are Anxiety Disorders?"
query_embedding = generate_embeddings(query)
# Perform similarity search
results = index.query(queries=[query_embedding.tolist()], top_k=3)
# Display results
print("Results:", results)

The error

AttributeError Traceback (most recent call last) Cell In[3], [line 10](vscode-notebook-cell:?execution_count=3&line=10) [7](vscode-notebook-cell:?execution_count=3&line=7) environment = "us-east-1" # Your environment [9](vscode-notebook-cell:?execution_count=3&line=9) # Initialize Pinecone ---> [10](vscode-notebook-cell:?execution_count=3&line=10) pinecone.configure(api_key=api_key, environment=environment) [12](vscode-notebook-cell:?execution_count=3&line=12) # Specify the index name [13](vscode-notebook-cell:?execution_count=3&line=13) index_name = "my-index" AttributeError: module 'pinecone' has no attribute 'configure'

@hurairaabu1986 Welcome to the community. Your original post contained your API key. I have edited the post and removed it, but I strongly recommend that you rotate your API key ASAP.


Regarding your error message, the error is saying the Pinecone module does not have a configure function. This is correct, the right way to initialise the Pinecone module is by using pinecone.Pinecone(). There are also other mistakes further down:

  • You do not have to include the environment.
  • query() does not have a queries parameter. It takes a single vector.

Here is the complete code example.

import pinecone
from transformers import AutoModel, AutoTokenizer
import torch
# Replace with your actual API key and environment
api_key = ""  # Your API key
# Initialize Pinecone
pc = pinecone.Pinecone(api_key=api_key)
# Specify the index name
index_name = "my-index"
# Check if the index exists
if index_name not in pc.list_indexes():
    raise ValueError(f"Index '{index_name}' does not exist. Please create it first.")
# Load the Pinecone index
index = pc.Index(index_name)
# Load Llama2 model and tokenizer
model_name = "TheBloke/Llama-2-13B-chat-GGML"  # Replace with your model name
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# Function to generate embeddings
def generate_embeddings(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
    return outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
# Query for similarity search
query = "What are Anxiety Disorders?"
query_embedding = generate_embeddings(query)
# Perform similarity search
results = index.query(vector=query_embedding.tolist(), top_k=3)
# Display results
print("Results:", results)

You might have to change generate_embeddings(). The query needs to be a single vector. If you’re tokening the query string, then you will want to query each generated embedding.

Where did you get this code from, was it from one of our examples?