File size: 1,370 Bytes
f0fc5f8
 
 
fa9f031
f0fc5f8
cc2ce8c
f0fc5f8
d98ba57
 
fa9f031
 
6e28a81
fa9f031
 
 
f0fc5f8
 
cc2ce8c
 
 
 
 
 
 
 
d98ba57
cc2ce8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0fc5f8
 
 
 
 
 
 
6e28a81
cc2ce8c
6e28a81
f0fc5f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Pinecone
# More info at https://docs.pinecone.io/docs/langchain
# And https://python.langchain.com/docs/integrations/vectorstores/pinecone
import os
import pinecone
from langchain.vectorstores import Chroma, Pinecone

PERSIST_DIRECTORY = "./chroma_db"

try:
    from dotenv import load_dotenv

    load_dotenv()
except:
    pass


def get_vectorstore(embeddings_function):
    if has_pinecone_config():
        return get_pinecone_vectorstore(embeddings_function)
    return get_chroma_vectore_store(embeddings_function)


def get_chroma_vectore_store(embedding_function):
    return Chroma(
        persist_directory=PERSIST_DIRECTORY, embedding_function=embedding_function
    )


def has_pinecone_config():
    return all(
        key in os.environ
        for key in [
            "PINECONE_API_KEY",
            "PINECONE_API_ENVIRONMENT",
            "PINECONE_API_INDEX",
        ]
    )


def get_pinecone_vectorstore(embeddings_function, text_key="content"):
    # initialize pinecone
    pinecone.init(
        api_key=os.getenv("PINECONE_API_KEY"),  # find at app.pinecone.io
        environment=os.getenv("PINECONE_API_ENVIRONMENT"),  # next to api key in console
    )

    index_name = os.getenv("PINECONE_API_INDEX")
    vectorstore = Pinecone.from_existing_index(
        index_name, embeddings_function, text_key=text_key
    )
    return vectorstore