pinecone-io / pinecone-client

Apache License 2.0
12 stars 2 forks source link

Pinecone retriever throwing: KeyError: 'text' #7

Open kindbuds opened 1 year ago

kindbuds commented 1 year ago

Is this a new bug?

Current Behavior

My query code is below:

pinecone.init(
    api_key=os.environ.get('PINECONE_API_KEY'),  # app.pinecone.io
    environment=os.environ.get('PINECONE_ENV')  # next to API key in console
)
index = pinecone.Index(index_name)
embeddings = OpenAIEmbeddings(openai_api_key=os.environ.get('OPENAI_API_KEY'))

vectordb = Pinecone(
    index=index,
    embedding_function=embeddings.embed_query,
    text_key="text",
)
llm=ChatOpenAI(
    openai_api_key=os.environ.get('OPENAI_API_KEY'),
    temperature=0,
    model_name='gpt-3.5-turbo'
)
retriever = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=vectordb.as_retriever()
)
tools = [Tool(
    func=retriever.run,
    description=tool_desc,
    name='Product DB'
)]
memory = ConversationBufferWindowMemory(
    memory_key="chat_history",  # important to align with agent prompt (below)
    k=5,
    return_messages=True
)
agent = initialize_agent(
    agent='chat-conversational-react-description', 
    tools=tools, 
    llm=llm,
    verbose=True,
    max_iterations=3,
    early_stopping_method="generate",
    memory=memory,
)

If I run: agent({'chat_history':[], 'input':'What is a product?'})

It throws:

File "C:\Users\xxx\AppData\Local\Programs\Python\Python311\Lib\site-packages\langchain\vectorstores\pinecone.py", line 160, in similarity_search text = metadata.pop(self._text_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'text'

This is the offending block in site-packages/pinecone.py:

 for res in results["matches"]:
            # print('metadata.pop(self._text_key) = ' + metadata.pop(self._text_key))
            metadata = res["metadata"]
            text = metadata.pop(self._text_key)
            docs.append(Document(page_content=text, metadata=metadata))

If I remove my tool like the line below, everything executes (just not my tool):

tools = []

Can anyone help me fix this KeyError: 'text' issue? My versions of langchain and pinecone-client are, 0.0.147 and 2.2.1 respectively.

Expected Behavior

Retriever runs successfully and doesn't throw any KeyError errors.

Steps To Reproduce

  1. Point my above code to your Pinecone index
  2. Execute code

Relevant log output

No response

Environment

- **OS**: Windows 11
- **Language version**: 3.11.3
- **Pinecone client version**: 2.2.1

Additional Context

No response

rjarun8 commented 1 year ago

Please raise this bug under https://github.com/pinecone-io/pinecone-python-client/issues