0

Hi I'm trying to create a pinecone index and write embeddings to it as per below code, but i'm keep getting AttributeError: module 'pinecone' has no attribute 'Index' error, i checked and found that its due to langchain and pinecone version miss matches so tried to install lesser version of pinecone-client==2.2.4 that also not working, please help.

import os
from dotenv import load_dotenv,find_dotenv
from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI

load_dotenv(find_dotenv(),override=True)

from pinecone import Pinecone, ServerlessSpec  # if you're initializing a new index

from dotenv import load_dotenv, find_dotenv

pc = Pinecone()


# loading PDF, DOCX and TXT files as LangChain Documents
def load_document(file):
    import os
    name, extension = os.path.splitext(file)

    if extension == '.pdf':
        from langchain.document_loaders import PyPDFLoader
        print(f'Loading {file}')
        loader = PyPDFLoader(file)
    elif extension == '.docx':
        from langchain.document_loaders import Docx2txtLoader
        print(f'Loading {file}')
        loader = Docx2txtLoader(file)
    elif extension == '.txt':
        # from langchain.document_loaders import TextLoader
        from langchain_community.document_loaders import TextLoader
        loader = TextLoader(file)
    else:
        print('Document format is not supported!')
        return None

    data = loader.load()
    return data 

data = load_document('file.txt')

def chunk_data(data, chunk_size=256):
    from langchain.text_splitter import RecursiveCharacterTextSplitter
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=0)
    chunks = text_splitter.split_documents(data)
    return chunks

def insert_or_fetch_embeddings(index_name, chunks):
    # importing the necessary libraries and initializing the Pinecone client
    import pinecone
    from langchain_community.vectorstores import Pinecone
    from langchain_openai import OpenAIEmbeddings
    from pinecone import ServerlessSpec

    pc = pinecone.Pinecone()

    embeddings = OpenAIEmbeddings(model='text-embedding-3-small', dimensions=1536)  # 512 works as well

    # loading from existing index
    if index_name in pc.list_indexes().names():
        print(f'Index {index_name} already exists. Loading embeddings ... ', end='')
        vector_store = Pinecone.from_existing_index(index_name, embeddings)
        print('Ok')
    else:
        # creating the index and embedding the chunks into the index
        print(f'Creating index {index_name} and embeddings ...', end='')

        # creating a new index
        pc.create_index(
            name=index_name,
            dimension=1536,
            metric='cosine',
            spec=ServerlessSpec(
                cloud="aws",
                region="us-east-1"
            )
        )

        # processing the input documents, generating embeddings using the provided `OpenAIEmbeddings` instance,
        # inserting the embeddings into the index and returning a new Pinecone vector store object.
        vector_store = Pinecone.from_documents(chunks, embeddings, index_name=index_name)
        print('Ok')

        return vector_store

index_name = 'askadocument'
vector_store = insert_or_fetch_embeddings(index_name=index_name, chunks=chunks)

llm = ChatOpenAI(model="gpt-4.1-mini", temperature=0.3)

retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': 2})

chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)

answer = chain.invoke(query)
requirements:
openai
langchain
langchain_openai
langchain_experimental
langchainhub
pinecone
python-dotenv
tiktoken
docx2txt
pypdf
pinecone-client==2.2.4

p.s: please don't down vote the question.

2
  • please, read and follow guidelines on posting minimal reproducible example, show all inputs/outputs produced (copy/paste only, no screenshots !) Commented Jun 29 at 23:10
  • Can you check to make sure that your app is using the packages from the correct Python location? Maybe it’s not picking up a virtual environment or it's hitting your global Python packages. You can try this to make sure that it’s hitting the packages you expect: import sys print(sys.path) Commented Jul 15 at 19:34

1 Answer 1

0

The issue is coming from this line-

vector_store = Pinecone.from_documents(chunks, embeddings, index_name=index_name)

add this code and it should resolve the error-

from langchain_pinecone import PineconeVectorStore

vector_store = PineconeVectorStore.from_documents(
    chunks,
    index_name = index_name,
    embedding = embeddings
)

Attaching the documentation link I followed to resolve this same issue-

https://docs.pinecone.io/integrations/langchain#initializing-a-vector-store

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.