PineconeProtocolError: Failed to connect; did you specify the correct index name

import os
from dotenv import load_dotenv
import streamlit as st
import openai
from langchain.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
from langchain.chat_models import ChatOpenAI
from langchain.memory import ChatMessageHistory
import pinecone

# Load environment variables
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
pinecone_api_key = os.getenv("PINECONE_API_KEY")
pinecone_index = os.getenv("PINECONE_INDEX")
pinecone_env = os.getenv("PINECONE_ENV")

# Initialize Pinecone
pinecone.init(api_key=pinecone_api_key, environment=pinecone_env)

# Initialize OpenAI Embeddings
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)

# Connect to the existing Pinecone index
index = pinecone.Index(index_name=pinecone_index)
docsearch = Pinecone(index, embeddings.embed_query, "text")

# Streamlit UI
st.title("PDF Document Chatbot")

# Initialize the chat model
chat = ChatOpenAI(temperature=0, openai_api_key=openai_api_key)

# Set up chat message history
history = ChatMessageHistory()

# Chat input
user_input = st.text_input("Ask your question:")
if st.button("Send"):
    # Retrieve top 5 relevant chunks
    top_results = docsearch.similarity_search(query=user_input, k=5)

    # Concatenate the relevant chunks
    context = " ".join([result.data for result in top_results])

    # Generate response using OpenAI Chat Completion
    history.add_user_message(user_input)
    ai_response = chat(context=context, message=user_input)
    history.add_ai_message(ai_response.content)

    # Display chat history
    st.write("Chat History:")
    for message in history.messages:
        st.write(message.sender + ": " + message.content)

I’m getting this error log when submitting a question on the UI:

 % streamlit run chat.py

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://10.2.0.2:8501

  For better performance, install the Watchdog module:

  $ xcode-select --install
  $ pip install watchdog
            
2023-04-02 14:05:32.559 Uncaught app exception
Traceback (most recent call last):
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1042, in _validate_conn
    conn.connect()
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connection.py", line 414, in connect
    self.sock = ssl_wrap_socket(
                ^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 517, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 1075, in _create
    self.do_handshake()
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 1346, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [Errno 54] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/utils/error_handling.py", line 17, in inner_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/index.py", line 449, in query
    response = self._vector_api.query(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 776, in __call__
    return self.callable(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/api/vector_operations_api.py", line 716, in __query
    return self.call_with_http_info(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 838, in call_with_http_info
    return self.api_client.call_api(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 413, in call_api
    return self.__call_api(resource_path, method,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 200, in __call_api
    response_data = self.request(
                    ^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 459, in request
    return self.rest_client.POST(url,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/rest.py", line 271, in POST
    return self.request("POST", url,
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/client/rest.py", line 157, in request
    r = self.pool_manager.request(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/request.py", line 78, in request
    return self.request_encode_body(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/request.py", line 170, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/poolmanager.py", line 376, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connectionpool.py", line 787, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/packages/six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1042, in _validate_conn
    conn.connect()
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/connection.py", line 414, in connect
    self.sock = ssl_wrap_socket(
                ^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 449, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 517, in wrap_socket
    return self.sslsocket_class._create(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 1075, in _create
    self.do_handshake()
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 1346, in do_handshake
    self._sslobj.do_handshake()
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/Users/black/gpt/b2/chat.py", line 43, in <module>
    top_results = docsearch.similarity_search(query=user_input, k=5)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/langchain/vectorstores/pinecone.py", line 148, in similarity_search
    results = self._index.query(
              ^^^^^^^^^^^^^^^^^^
  File "/Users/black/.pyenv/versions/3.11.2/lib/python3.11/site-packages/pinecone/core/utils/error_handling.py", line 25, in inner_func
    raise PineconeProtocolError(f'Failed to connect; did you specify the correct index name?') from e
pinecone.core.exceptions.PineconeProtocolError: Failed to connect; did you specify the correct index name?

Any ideas what’s wrong?

It seems to connect fine to Pinecone.

1 Like

same, don’t know the reason yet. Is there a request rate limit for Pinecone? · Issue #159 · pinecone-io/pinecone-python-client (github.com)

2 Likes

Having the same issues here

Not sure if its related to being on the free tier or it’s a general issue everyone is expriencing

So if you’re using Streamlit the issue might be that multiple index are being created. The docs mention that you should Reuse connections. We recommend you reuse the same pinecone.Index() instance when you are upserting and querying the same index.

To solve this, I used Streamlit’s cached resource to cache the database index and reuse it across the different network calls that happen.

Hope it helps :slight_smile:

Hi Mike, can you provide an example of this - I’m unfamiliar with how streamlines cache resource decorator works.

Are you referring to caching the pinecone.init function?

@st.cache_resource
def start_pinecone():
      pinecone.init(
          api_key=PINECONE_API_KEY,  # find at app.pinecone.io
          environment=PINECONE_API_ENV  # next to api key in console)

or pinecone index variable?

@st.cache_resource
def get_pinecone_index():
    PINECONE_INDEX_NAME = "demo2"
    return PINECONE_INDEX_NAME
PINECONE_INDEX_NAME=get_pinecone_index()

Ok, I just figured it out - what Mike meant was use streamlits cache resources command on the load_pinecone_existing_index function.

example below:

docsearch = Pinecone.from_existing_index(index_name=PINECONE_INDEX_NAME, embedding=embeddings)

becomes

@st.cache_resource
def load_pinecone_existing_index():
    pass
    docsearch = Pinecone.from_existing_index(index_name=PINECONE_INDEX_NAME, embedding=embeddings)
    return docsearch
docsearch=load_pinecone_existing_index()

I get this error randomly every now and then.

Here’s the complete Traceback:

>  Traceback (most recent call last):
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 714, in urlopen
>      httplib_response = self._make_request(
>                         ^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 466, in _make_request
>      six.raise_from(e, None)
>    File "<string>", line 3, in raise_from
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 461, in _make_request
>      httplib_response = conn.getresponse()
>                         ^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/miniconda3/envs/python11/lib/python3.11/http/client.py", line 1375, in getresponse
>      response.begin()
>    File "/Users/joaoabrantis/miniconda3/envs/python11/lib/python3.11/http/client.py", line 318, in begin
>      version, status, reason = self._read_status()
>                                ^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/miniconda3/envs/python11/lib/python3.11/http/client.py", line 287, in _read_status
>      raise RemoteDisconnected("Remote end closed connection without"
>  http.client.RemoteDisconnected: Remote end closed connection without response
>  
>  During handling of the above exception, another exception occurred:
>  
>  Traceback (most recent call last):
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/utils/error_handling.py", line 17, in inner_func
>      return func(*args, **kwargs)
>             ^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/index.py", line 455, in query
>      response = self._vector_api.query(
>                 ^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 776, in __call__
>      return self.callable(self, *args, **kwargs)
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/api/vector_operations_api.py", line 716, in __query
>      return self.call_with_http_info(**kwargs)
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 838, in call_with_http_info
>      return self.api_client.call_api(
>             ^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 413, in call_api
>      return self.__call_api(resource_path, method,
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 200, in __call_api
>      response_data = self.request(
>                      ^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 459, in request
>      return self.rest_client.POST(url,
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/rest.py", line 271, in POST
>      return self.request("POST", url,
>             ^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/client/rest.py", line 157, in request
>      r = self.pool_manager.request(
>          ^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/request.py", line 78, in request
>      return self.request_encode_body(
>             ^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/request.py", line 170, in request_encode_body
>      return self.urlopen(method, url, **extra_kw)
>             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/poolmanager.py", line 376, in urlopen
>      response = conn.urlopen(method, u.request_uri, **kw)
>                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 798, in urlopen
>      retries = retries.increment(
>                ^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/util/retry.py", line 550, in increment
>      raise six.reraise(type(error), error, _stacktrace)
>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/packages/six.py", line 769, in reraise
>      raise value.with_traceback(tb)
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 714, in urlopen
>      httplib_response = self._make_request(
>                         ^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 466, in _make_request
>      six.raise_from(e, None)
>    File "<string>", line 3, in raise_from
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 461, in _make_request
>      httplib_response = conn.getresponse()
>                         ^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/miniconda3/envs/python11/lib/python3.11/http/client.py", line 1375, in getresponse
>      response.begin()
>    File "/Users/joaoabrantis/miniconda3/envs/python11/lib/python3.11/http/client.py", line 318, in begin
>      version, status, reason = self._read_status()
>                                ^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/miniconda3/envs/python11/lib/python3.11/http/client.py", line 287, in _read_status
>      raise RemoteDisconnected("Remote end closed connection without"
>  urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
>  
>  The above exception was the direct cause of the following exception:
>  
>  Traceback (most recent call last):
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/main.py", line 45, in chat
>      reply, citations = agent.api_call(user_input, chat_history)
>                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/simple_lib/agent.py", line 36, in api_call
>      parsed_reply, sources = tool.run(**function_args), tool.sources
>                              ^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/simple_lib/abs_tools.py", line 89, in run
>      results_2 = index.query(vector=embedded_query, top_k=6, include_metadata=True)['matches']
>                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>    File "/Users/joaoabrantis/github/alpacalaw/function_leis/venv/lib/python3.11/site-packages/pinecone/core/utils/error_handling.py", line 25, in inner_func
>      raise PineconeProtocolError(f'Failed to connect; did you specify the correct index name?') from e
>  pinecone.core.exceptions.PineconeProtocolError: Failed to connect; did you specify the correct index name?
```
1 Like

@tonyphoang Thank you for this starter code! To deploy in Streamlit Cloud, I modified your answer a bit and it works with the pinecone starter package.

@st.cache_resource
def load_pinecone(pages, index_name, embeddings=OpenAIEmbeddings()):
    # initialize pinecone
    pinecone.init(
    api_key=st.secrets.pinecone.api_key,  
    environment=st.secrets.pinecone.env,
    )
    if index_name not in pinecone.list_indexes():
        # we create a new index
        pinecone.create_index(
            name=index_name,
            metric='cosine',
            dimension=1536  
            )
    docsearch = Pinecone.from_documents(pages, embeddings, index_name=index_name)
    return docsearch

docsearch = load_pinecone(pages, index_name)