Unable to use Langchain Pinecone from_texts

I am facing some difficulties in working with Langchain’s pinecone. Can someone please indicate what mistake i am doing in the below python code ?

PINECONE_API_KEY = “xxxxxxxxxxxx”
INDEX_NAME = “demo1”
OPENAI_API_KEY = “xxxxxxxxxxxx”
import os
import openai
from langchain_openai import OpenAIEmbeddings
from pinecone import Pinecone as PineconeClient
from langchain_community.vectorstores import Pinecone as PineconeLang

pc = PineconeClient(api_key=PINECONE_API_KEY)

embedding = OpenAIEmbeddings(openai_api_key = OPENAI_API_KEY)

my_note = PineconeLang.from_texts(texts= “abcdefgh”,embedding = embedding, index_name = INDEX_NAME)

print(type(my_note))

I am getting an error -
pinecone.exceptions.PineconeConfigurationError: You haven’t specified an Api-Key.

Where do I need to specify the API-Key?

Hi @rajsbhatta, can you share the full stacktrace of the error? Just to make sure we have all of the relevant context.

Hi @Cory_Pinecone ,
Thanks for your reply. Below is the error from the terminal -

Traceback (most recent call last):
File “D:\AI\dummy.py”, line 16, in
my_note = PineconeLang.from_texts(texts= “abcdefgh”,embedding = embedding, index_name = INDEX_NAME)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “…Python311\site-packages\langchain_community\vectorstores\pinecone.py”, line 438, in from_texts
pinecone_index = cls.get_pinecone_index(index_name, pool_threads)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “…Python311\site-packages\langchain_community\vectorstores\pinecone.py”, line 369, in get_pinecone_index
pinecone_instance = pinecone.Pinecone(
^^^^^^^^^^^^^^^^^^
File “…Python311\site-packages\pinecone\control\pinecone.py”, line 95, in init
self.config = PineconeConfig.build(api_key=api_key, host=host, additional_headers=additional_headers, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “…Python311\site-packages\pinecone\config\pinecone_config.py”, line 12, in build
return ConfigBuilder.build(api_key=api_key, host=host, additional_headers=additional_headers, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “…Python311\site-packages\pinecone\config\config.py”, line 45, in build
raise PineconeConfigurationError(“You haven’t specified an Api-Key.”)
pinecone.exceptions.PineconeConfigurationError: You haven’t specified an Api-Key.

import os
os.environ[‘PINECONE_API_KEY’] = PINECONE_API_KEY

This will set the environment variable that is being looked for

1 Like

Thanks much @tim.rennie
It is working now. If you don’t mind, can you please suggest why my code wasn’t working when I had mentioned the key directly in my code (see the first line)?

All I can say is that the langchain class in python is using that method to look for the credentials. Seems counterintuitive, but that is how it is programmed for the from_documents class.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Can you help me too? I am able to see my vectors in hosted application but when I try to retrieve it using the function Pinecone.from_existing_index(), and this is where I am getting some error :Traceback (most recent call last):
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\django\views\decorators\csrf.py”, line 56, in wrapper_view
return view_func(*args, **kwargs)
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\django\views\generic\base.py”, line 104, in view
return self.dispatch(request, *args, **kwargs)
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\rest_framework\views.py”, line 509, in dispatch
response = self.handle_exception(exc)
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\rest_framework\views.py”, line 469, in handle_exception
self.raise_uncaught_exception(exc)
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\rest_framework\views.py”, line 480, in raise_uncaught_exception
raise exc
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\rest_framework\views.py”, line 506, in dispatch
response = handler(request, *args, **kwargs)
File “C:\Users\user\Project\nudge2edge\Project Momentum\rag\rag_pinecone.py”, line 26, in get
docsearch = PineconeLang.from_existing_index(index_name, embeddings, pc)
File “C:\Users\user\Project\nudge2edge\backendenv\lib\site-packages\langchain\vectorstores\pinecone.py”, line 410, in from_existing_index
return cls(pinecone.Index(index_name), embedding, text_key, namespace)
TypeError: Index.init() missing 1 required positional argument: ‘host’.

Here is my code.
from langchain.embeddings import OpenAIEmbeddings
from nudge2edge_prototype.settings import OPENAI_API_KEY,PINECONE_API_KEY
from pinecone import Pinecone as PineconeClient
from langchain.vectorstores import Pinecone as PineconeLang

    pc = PineconeClient(api_key=PINECONE_API_KEY)
    index_name = "test-1"
    index = pc.Index(index_name)
    print(index.describe_index_stats())
    embeddings = OpenAIEmbeddings(openai_api_key = OPENAI_API_KEY)
    query = "”        
    docsearch = PineconeLang.from_existing_index(index_name, embeddings, pc)
    docs = docsearch.similarity_search(query, include_metadata=True)
    print(docs)

I have kept my pinecone and openAI key in .env only