langchain-ai / langchain

šŸ¦œšŸ”— Build context-aware reasoning applications
https://python.langchain.com
MIT License
95.32k stars 15.47k forks source link

vectorstore.similarity_search error / Error: AttributeError: 'str' object has no attribute 'query' #28028

Open naveen-vsamy opened 2 weeks ago

naveen-vsamy commented 2 weeks ago

Checked other resources

Example Code

The following code:

Create the connection to Bedrock

bedrock_runtime = boto3.client(service_name='bedrock-runtime', region_name='us-east-1', aws_access_key_id='aws_access_key_id', aws_secret_access_key='aws_secret_access_key' )

os.environ['PINECONE_API_KEY']='pinecone_api_key' index_name = "whale" embedding_model = BedrockEmbeddings(client=bedrock_runtime,model_id="amazon.titan-embed-text-v1" )

vectorstore = PineconeVectorStore(embedding = embedding_model, index=index_name)

similar_search_results = vectorstore.similarity_search(query = "say something...", k = 3)

Raised this following error: AttributeError: 'str' object has no attribute 'query'

Error Message and Stack Trace (if applicable)

embedding_model = BedrockEmbeddings(client=bedrock_runtime,model_id="amazon.titan-embed-text-v1" ) Traceback (most recent call last): File "c:\Users\91822\Documents\Python Scripts\test.py", line 22, in similar_search_results = vectorstore.similarity_search(query = "say something...", k = 3) File "C:\Users\91822\AppData\Roaming\Python\Python310\site-packages\langchain_pinecone\vectorstores.py", line 379, in similarity_search docs_and_scores = self.similarity_search_with_score( File "C:\Users\91822\AppData\Roaming\Python\Python310\site-packages\langchain_pinecone\vectorstores.py", line 321, in similarity_search_with_score return self.similarity_search_by_vector_with_score( File "C:\Users\91822\AppData\Roaming\Python\Python310\site-packages\langchain_pinecone\vectorstores.py", line 338, in similarity_search_by_vector_with_score results = self._index.query( AttributeError: 'str' object has no attribute 'query'

Description

I'm trying to do a similarity search on my pinecone vector store through langchain's PineconeVectorStore module. The function is supposed to recieve a string as input arg named "query". But it is throwing the mentioned error.

System Info

System Information

OS: Windows OS Version: 10.0.19045 Python Version: 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)]

Package Information

langchain_core: 0.3.15 langchain: 0.3.7 langchain_community: 0.3.5 langsmith: 0.1.142 langchain_pinecone: 0.2.0 langchain_text_splitters: 0.3.2

Optional packages not installed

langgraph langserve

Other Dependencies

aiohttp: 3.9.5 async-timeout: 4.0.2 dataclasses-json: 0.6.7 httpx: 0.27.2 httpx-sse: 0.4.0 jsonpatch: 1.33 numpy: 1.23.2 orjson: 3.10.11 packaging: 24.2 pinecone-client: 5.0.1 pydantic: 2.9.2 pydantic-settings: 2.6.1 PyYAML: 6.0.2 requests: 2.28.2 requests-toolbelt: 1.0.0 SQLAlchemy: 2.0.35 tenacity: 8.2.1 typing-extensions: 4.12.2

naveen-vsamy commented 2 weeks ago

When defining "vectorstore" I passed "index" as arg instead of index_name. I changed the code and it worked fine

naveen-vsamy commented 2 weeks ago

But still the error was misleading

myke11j commented 1 week ago

In your initial code, when you passed a string in index parameter which should actually be pinecone.Index

vectorstore = PineconeVectorStore(embedding = embedding_model, index=index_name)

When you invoke any search function, it calls index.query which is a function from pinecone, but becauuse you passed a String, it tried to call "index" function on a string which is not part of string object. That's why you get the error

'str' object has no attribute 'query', which is not an error from lang-chain but it's a Python error.

Here's the source code for reference https://github.com/langchain-ai/langchain/blob/master/libs/partners/pinecone/langchain_pinecone/vectorstores.py#L338