Tryin to connect to Pinecone but the code only work in Jupyter Notebook, not as Python Script

210 views Asked by At

I am trying to connect with my Pinecone vector database to retrieve information from it. I’ve tried my code in Jupyter notebook, it works fine. But when I try to run the code as a whole script, it does not work any more. My code looks like this: 



import os
import pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Pinecone
from dotenv import load_dotenv
load_dotenv()

pinecone.init(
    api_key=os.getenv("PINECONE_API_KEY"),
    environment='gcp-starter'# this thing can be found at your acc at https://app.pinecone.io/
)

# load pinecone
index_name = "itl-knl-base"
embeddings = OpenAIEmbeddings()
# if index_name is not None: 
#     print(pinecone.describe_index(index_name))

docsearch = Pinecone.from_existing_index(index_name, embeddings)
docsearch.similarity_search("số ngày nghỉ có tăng theo thâm niên làm việc không?", k=3)

and the error message I am getting: 


(chatbot) sPyno features $ /Users/spinokiem/miniconda3/envs/inno23/bin/python /Users/spinokiem/Documents/Spino_DS_prj/building_a_chatbot/src/features/app.py                    11:56
Traceback (most recent call last):
  File "/Users/spinokiem/Documents/Spino_DS_prj/building_a_chatbot/src/features/app.py", line 24, in <module>
    docsearch = Pinecone.from_existing_index(index_name, embeddings)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/langchain/vectorstores/pinecone.py", line 437, in from_existing_index
    pinecone_index = cls.get_pinecone_index(index_name, pool_threads)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/langchain/vectorstores/pinecone.py", line 354, in get_pinecone_index
    indexes = pinecone.list_indexes()  # checks if provided index exists
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/pinecone/manage.py", line 185, in list_indexes
    response = api_instance.list_indexes()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 776, in __call__
    return self.callable(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/pinecone/core/client/api/index_operations_api.py", line 1130, in __list_indexes
    return self.call_with_http_info(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/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/spinokiem/miniconda3/envs/inno23/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/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 200, in __call_api
    response_data = self.request(
                    ^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/pinecone/core/client/api_client.py", line 439, in request
    return self.rest_client.GET(url,
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/pinecone/core/client/rest.py", line 236, in GET
    return self.request("GET", url,
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/pinecone/core/client/rest.py", line 202, in request
    r = self.pool_manager.request(method, url,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/urllib3/_request_methods.py", line 110, in request
    return self.request_encode_url(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/urllib3/_request_methods.py", line 143, in request_encode_url
    return self.urlopen(method, url, **extra_kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/urllib3/poolmanager.py", line 443, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/urllib3/connectionpool.py", line 790, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/urllib3/connectionpool.py", line 496, in _make_request
    conn.request(
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/urllib3/connection.py", line 394, in request
    self.putheader(header, value)
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/site-packages/urllib3/connection.py", line 308, in putheader
    super().putheader(header, *values)
  File "/Users/spinokiem/miniconda3/envs/inno23/lib/python3.11/http/client.py", line 1263, in putheader
    if _is_illegal_header_value(values[i]):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected string or bytes-like object, got 'NoneType'


The TypeError does not make sense to me. I’ve definitely pass a string in this: 


docsearch = Pinecone.from_existing_index(index_name, embeddings)

I don’t know how to fix this, can anybody help

I’ve checked the order of the code, the API key to make sure they’re right, I’ve even tried to set up a new Python environment to run it again, but still got the same issue.

2

There are 2 answers

0
Siddhant Panpatil On

The NoneType object is likely the result of os.getenv("PINECONE_API_KEY") returning None because the environment variable PINECONE_API_KEY is not set in the environment where the script is running. Maybe try hardcoding the API key

0
Yilmaz On

Most likely you did not set PINECONE_API_KEY correctly. you should add a guard to your app:

api_key = os.getenv("PINECONE_API_KEY")

if api_key is None:
    print("PINECONE_API_KEY is NOne")
    # handle the error situation here
else:
    pinecone.init(
        api_key=api_key,
        environment='gcp-starter'
    )