Spaces:
Runtime error
Runtime error
Commit
·
05eb20c
1
Parent(s):
21da56d
Update indexes.py
Browse files- indexes.py +29 -29
indexes.py
CHANGED
@@ -7,10 +7,13 @@ from langchain.document_loaders import DirectoryLoader, TextLoader
|
|
7 |
from langchain.vectorstores import Pinecone
|
8 |
from PyPDF2 import PdfReader
|
9 |
import pinecone
|
|
|
|
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
-
def create_indexes(file: tempfile,
|
14 |
try:
|
15 |
file_path = file.name
|
16 |
reader = PyPDFLoader(file_path)
|
@@ -18,35 +21,32 @@ def create_indexes(file: tempfile, pinecone_api_key: str, pinecone_environment:
|
|
18 |
embeddings = OpenAIEmbeddings(
|
19 |
openai_api_key=openai_api_key
|
20 |
)
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
embedding=embeddings,
|
34 |
-
index_name=pinecone_index_name
|
35 |
-
)
|
36 |
return 'Document uploaded and index created successfully. You can chat now.'
|
37 |
except Exception as e:
|
38 |
return e
|
39 |
|
40 |
-
def clear_indexes(pinecone_api_key: str, pinecone_environment: str, pinecone_index_name: str) -> str:
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
|
|
7 |
from langchain.vectorstores import Pinecone
|
8 |
from PyPDF2 import PdfReader
|
9 |
import pinecone
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
from langchain.vectorstores import Chroma
|
12 |
|
13 |
+
load_dotenv()
|
14 |
+
openai_api_key=os.getenv('OPENAI_API_KEY')
|
15 |
|
16 |
+
def create_indexes(file: tempfile, collection_name: str) -> str:
|
17 |
try:
|
18 |
file_path = file.name
|
19 |
reader = PyPDFLoader(file_path)
|
|
|
21 |
embeddings = OpenAIEmbeddings(
|
22 |
openai_api_key=openai_api_key
|
23 |
)
|
24 |
+
|
25 |
+
persist_directory = './db_metadata'
|
26 |
+
|
27 |
+
vectordb = Chroma.from_documents(
|
28 |
+
collection_name=collection_name,
|
29 |
+
documents=documents,
|
30 |
+
embedding=embeddings,
|
31 |
+
persist_directory=persist_directory
|
32 |
+
)
|
33 |
+
|
34 |
+
vectordb.persist()
|
35 |
+
|
|
|
|
|
|
|
36 |
return 'Document uploaded and index created successfully. You can chat now.'
|
37 |
except Exception as e:
|
38 |
return e
|
39 |
|
40 |
+
# def clear_indexes(pinecone_api_key: str, pinecone_environment: str, pinecone_index_name: str) -> str:
|
41 |
+
# try:
|
42 |
+
# pinecone.init(
|
43 |
+
# api_key=pinecone_api_key,
|
44 |
+
# environment=pinecone_environment
|
45 |
+
# )
|
46 |
+
# indexes_list = pinecone.list_indexes()
|
47 |
+
# if pinecone_index_name in indexes_list:
|
48 |
+
# pinecone.delete_index(name=pinecone_index_name)
|
49 |
+
# return 'Indexes cleared.', None
|
50 |
+
# except Exception as e:
|
51 |
+
# return e, None
|
52 |
|