pinecone-io / pinecone-python-client

The Pinecone Python client
https://www.pinecone.io/docs
Apache License 2.0
309 stars 80 forks source link

[Bug] Pinecone has no attribute 'from_texts' #388

Closed Mukku27 closed 2 months ago

Mukku27 commented 2 months ago

Describe the bug When attempting to create an embedding using Pinecone in a Google Colab notebook, an AttributeError is raised. The functionality that was working previously to generate embeddings from text chunks is now broken.

Error information The full stack trace of the error is as follows:

AttributeError                            Traceback (most recent call last)
<ipython-input-18-dbd3c6635021> in <cell line: 8>()
      6 
      7 #creating Embedding for each of the text Chunk & storing
----> 8 docsearch=pinecone.from_texts([t.page_cont
This should effectively communicate the issue you encountered using the provided bug report template.ent for t in text_chunks],embedding,index_name=index_name)

/usr/local/lib/python3.10/dist-packages/pinecone/control/pinecone.py in from_texts(*args, **kwargs)
    700     @staticmethod
    701     def from_texts(*args, **kwargs):
--> 702         raise AttributeError(_build_langchain_attribute_error_message("from_texts"))
    703 
    704     @staticmethod

AttributeError: from_texts is not a top-level attribute of the Pinecone class provided Pinecone has no attribute 'from_texts'
by pinecone's official python package developed at https://github.com/pinecone-io/pinecone-python-client. You may have a name collision with an export from another dependency in your project that wraps Pinecone functionality and exports a similarly named class. Please refer to the following knowledge base article for more information: https://docs.pinecone.io/troubleshooting/pinecone-attribute-errors-with-langchain

Steps to reproduce the issue locally

  1. Run a Google Colab notebook.
  2. Install the necessary packages, including Pinecone and any dependencies related to LangChain.
  3. Attempt to create an embedding with the following code snippet:
    docsearch = pinecone.from_texts([t.page_content for t in text_chunks], embedding, index_name=index_name)
  4. The error occurs when executing this code.

Environment

Additional context The issue seems to be related to a name collision between Pinecone's from_texts method and another dependency that might be wrapping Pinecone functionality. I am using the notebook environment in Google Colab, and a screenshot of the error is attached for reference.


mcpaddy commented 2 months ago

@Mukku27 Thank you for raising this issue.

The Python Pinecone SDK does not have the function from_text() that's usually provided by Langchain. There was an issue when using an older version of Langchain where there could be a Python namespace name collision, so I would ensure that it is up to date and you have made the code changes,

If you can share the code or the Colab, we can help you further.

You might also find our Langchain guide. useful too

Mukku27 commented 2 months ago

Screenshot from 2024-09-07 15-26-21

Mukku27 commented 2 months ago

This is one cell of the whole notebook

mcpaddy commented 2 months ago

Thank you for the code. Unfortunately, it's incorrect. As I mentioned previously, Pinecone does not have a from_text function. It comes from Langchain.

Here is the correct way to use from_texts() from Langchain.

    import os
    from langchain_pinecone import PineconeVectorStore
    from langchain_openai import OpenAIEmbeddings
    from langchain_community.document_loaders import TextLoader
    from langchain_text_splitters import CharacterTextSplitter

    os.environ['OPENAI_API_KEY'] = '<YOUR_OPENAI_API_KEY>'
    os.environ['PINECONE_API_KEY'] = '<YOUR_PINECONE_API_KEY>'

    index_name = "<YOUR_PINECONE_INDEX_NAME>"
    embeddings = OpenAIEmbeddings()

    texts = ["Tonight, I call on the Senate to: Pass the Freedom to Vote Act.", "ne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court.", "One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence."]

    vectorstore_from_texts = PineconeVectorStore.from_texts(
        texts,
        index_name=index_name,
        embedding=embeddings
    )

There are examples in our Langchain guide.

Mukku27 commented 2 months ago

Thank you @mcpaddy