langchain icon indicating copy to clipboard operation
langchain copied to clipboard

AttributeError: 'str' object has no attribute 'upsert' in Pinecone Hybrid Search

Open levalencia opened this issue 1 year ago • 1 comments

System Info

0.0.228

Who can help?

@lbsnrs

Information

  • [ ] The official example notebooks/scripts
  • [ ] My own modified scripts

Related Components

  • [ ] LLMs/Chat Models
  • [ ] Embedding Models
  • [ ] Prompts / Prompt Templates / Prompt Selectors
  • [ ] Output Parsers
  • [ ] Document Loaders
  • [X] Vector Stores / Retrievers
  • [ ] Memory
  • [ ] Agents / Agent Executors
  • [ ] Tools / Toolkits
  • [ ] Chains
  • [ ] Callbacks/Tracing
  • [ ] Async

Reproduction

Following tutorial here: https://python.langchain.com/docs/modules/data_connection/retrievers/integrations/pinecone_hybrid_search

bm25_encoder = BM25Encoder().default()
embed = OpenAIEmbeddings(deployment=OPENAI_EMBEDDING_DEPLOYMENT_NAME, model=OPENAI_EMBEDDING_MODEL_NAME, chunk_size=1)
 
retriever = PineconeHybridSearchRetriever(
        embeddings=embed, sparse_encoder=bm25_encoder, index="cubigometadatanotindexed"
)
retriever.add_texts(["foo", "bar", "FirstName0003384 is a guy", "FirstName0003381 is a girl"])
result = retriever.get_relevant_documents("Who is FirstName0003381?")

I get this error:


AttributeError: 'str' object has no attribute 'upsert'
Traceback:
File "C:\Users\xx\anaconda3\envs\xxChatbotv3\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 552, in _run_script
    exec(code, module.__dict__)
File "C:\Users\xx\repos\xxChatbotv1\app\pages\Pinecone Hybrid Search.py", line 116, in <module>
    main()
File "C:\Users\xx\repos\xxChatbotv1\app\pages\Pinecone Hybrid Search.py", line 112, in main
    retriever.add_texts(["foo", "bar", "FirstName0003384 is a guy", "hello"])
File "C:\Users\xx\anaconda3\envs\zzChatbotv3\Lib\site-packages\langchain\retrievers\pinecone_hybrid_search.py", line 121, in add_texts
    create_index(
File "C:\Users\xx\anaconda3\envs\zzChatbotv3\Lib\site-packages\langchain\retrievers\pinecone_hybrid_search.py", line 98, in create_index
    index.upsert(vectors)
    ^^^^^^^^^^^^

Expected behavior

The texts should be added to the index without error

levalencia avatar Jul 11 '23 08:07 levalencia

Answer generated by a 🤖

Answer

I understand that you're encountering an AttributeError: 'str' object has no attribute 'upsert' when trying to add texts to the Pinecone Hybrid Search Retriever in LangChain. This error is likely due to the index parameter in the PineconeHybridSearchRetriever being a string, when it should be a Pinecone Index object.

To resolve this issue, you should replace the string "cubigometadatanotindexed" with a Pinecone Index object. Here's how you can do it:

import os
import pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Pinecone

pinecone.init(api_key=os.environ["PINECONE_API_KEY"], environment=os.environ["PINECONE_ENV"])
embeddings = OpenAIEmbeddings()
index_name = "cubigometadatanotindexed"

# create new index if it doesn't exist
if index_name not in pinecone.list_indexes():
    pinecone.create_index(index_name, dimension=1536)

index = pinecone.Index(index_name)

retriever = PineconeHybridSearchRetriever(
        embeddings=embed, sparse_encoder=bm25_encoder, index=index
)
retriever.add_texts(["foo", "bar", "FirstName0003384 is a guy", "FirstName0003381 is a girl"])
result = retriever.get_relevant_documents("Who is FirstName0003381?")

This code first checks if the provided index exists in your Pinecone project and creates a new index if it doesn't exist. Then, it initializes the index as a Pinecone Index object, which can be used in the PineconeHybridSearchRetriever.

Please note that this is one potential solution based on the information provided. If this doesn't resolve your issue, or if your experience contradicts this solution, I recommend checking if you're using the latest version of LangChain, as updates often contain bug fixes and improvements.

If you believe this is a bug that could impact other users, we encourage you to make a pull request with a proposed fix. Your contributions are valuable in improving the LangChain framework.

Sources


This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

dosubot[bot] avatar Jul 11 '23 12:07 dosubot[bot]

Hello @levalencia, not sure if this is the cause of issue, but there's a small typo in word environment in the tutorial that you've linked.

pinecone.init(api_key=api_key, enviroment=env)

gaurang98671 avatar Jul 12 '23 22:07 gaurang98671

Hi, @levalencia. I'm Dosu, and I'm helping the LangChain team manage our backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you encountered an AttributeError in the Pinecone Hybrid Search module. The error message stated that the 'str' object has no attribute 'upsert'. Another user named dosu-beta provided a potential solution, suggesting that the issue may be due to the index parameter being a string instead of a Pinecone Index object. I provided code to replace the string with a Pinecone Index object. Additionally, gaurang98671 pointed out a small typo in the tutorial linked by you.

Before we close this issue, we wanted to check if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your understanding and contribution to the LangChain project.

dosubot[bot] avatar Oct 11 '23 16:10 dosubot[bot]