Here's my code:
import pinecone
from pinecone import Pinecone, ServerlessSpec
from langchain.vectorstores import Pinecone as pc1
import os
# We initialize pinecone
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
# Create a new pinecone index
#cloud = os.getenv("PINECONE_ENV_KEY")
#region = "us-central1"
#spec = ServerlessSpec(cloud=cloud, region=region)
#pc.create_index(name="langchain", dimension=1536, metric="cosine", spec=spec)
# We define the name of our index (in case the index is already created)
index_name = "langchain"
# The OpenAI embedding model `text-embedding-ada-002 uses 1536 dimensions`. We use the Pinecone library of LangChain
if index_name not in pc.list_indexes().names():
print("No index")
else:
db_Pinecone = pc1.from_documents(pdf_chunks, embeddings, index_name=index_name)
And I get the following error:
AttributeError Traceback (most recent call last)
Cell In[53], line 22
20 print("No index")
21 else:
---> 22 db_Pinecone = pc1.from_documents(pdf_chunks, embeddings, index_name=index_name)
File ~/.local/lib/python3.8/site-packages/langchain/vectorstores/base.py:307, in VectorStore.from_documents(cls, documents, embedding, **kwargs)
305 texts = [d.page_content for d in documents]
306 metadatas = [d.metadata for d in documents]
--> 307 return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs)
File ~/.local/lib/python3.8/site-packages/langchain/vectorstores/pinecone.py:206, in Pinecone.from_texts(cls, texts, embedding, metadatas, ids, batch_size, text_key, index_name, namespace, **kwargs)
200 except ImportError:
201 raise ValueError(
202 "Could not import pinecone python package. "
203 "Please install it with `pip install pinecone-client`."
204 )
--> 206 indexes = pinecone.list_indexes() # checks if provided index exists
208 if index_name in indexes:
209 index = pinecone.Index(index_name)
File ~/.local/lib/python3.8/site-packages/pinecone/deprecation_warnings.py:51, in list_indexes(*args, **kwargs)
40 def list_indexes(*args, **kwargs):
41 example = """
42 from pinecone import Pinecone
43
(...)
49 # do something
50 """
---> 51 raise AttributeError(_build_class_migration_message('list_indexes', example))
AttributeError: list_indexes is no longer a top-level attribute of the pinecone package.
To use list_indexes, please create a client instance and call the method there instead.
Example:
from pinecone import Pinecone
pc = Pinecone(api_key='YOUR_API_KEY')
index_name = "quickstart" # or your index name
if index_name not in pc.list_indexes().names():
# do something
In which it says on line 206 that the list_indexes() function from the Pinecone Langchain Python library is no longer a top-level attribute of the pinecone package.(AttributeError)
Does that mean the pinecone package is broken?
From the langchain-Github Issues-page i found this solution from the dosubot(bot):
Based on the error message you provided, it seems that the pinecone.init method is no longer available in the latest version of the pinecone-client library. Instead, you should create an instance of the Pinecone class directly.
Here's how you can modify your PineconeConnected class to work with the updated pinecone-client library:
import pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
from langchain.schema import Document
class PineconeConnected():
def __init__(self, index_name: str, pinecone_api_key: str, pinecone_env: str, openai_key: str):
embeddings = OpenAIEmbeddings(openai_api_key=openai_key)
self.pinecone = pinecone.Pinecone(api_key=pinecone_api_key)
self.vector_db = Pinecone.from_existing_index(index_name, embeddings) # VectorStore object with the reference + Pinecone index loaded
def query(query:str, book_title=None):
pass
In this updated code, I replaced the pinecone.init(api_key=pinecone_api_key) line with self.pinecone = pinecone.Pinecone(api_key=pinecone_api_key). This creates an instance of the Pinecone class with your API key, which you can then use to interact with the Pinecone service.
Please note that you might need to adjust other parts of your code that rely on the pinecone.init method, as this method is no longer available in the latest version of the pinecone-client library.
I hope this helps! If you have any other questions or run into any issues, please let me know
I ran above code in my Jupyter-notebook it gives me perfect output.You will find name in my output which is the index name
Output: