File size: 1,618 Bytes
df262b6
 
 
 
 
 
 
 
 
05eb20c
 
df262b6
05eb20c
 
df262b6
da554da
df262b6
 
 
 
 
 
 
05eb20c
 
 
 
 
 
 
 
 
 
 
df262b6
 
 
 
05eb20c
 
 
 
 
 
 
 
 
 
 
 
df262b6
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
import tempfile
import os
from langchain.document_loaders import PyPDFLoader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import DirectoryLoader, TextLoader
from langchain.vectorstores import Pinecone
from PyPDF2 import PdfReader
import pinecone
from dotenv import load_dotenv
from langchain.vectorstores import Chroma

load_dotenv()
openai_api_key=os.getenv('OPENAI_API_KEY')

def create_indexes(file: tempfile) -> str:
    try:
        file_path = file.name
        reader = PyPDFLoader(file_path)
        documents = reader.load_and_split()
        embeddings = OpenAIEmbeddings(
            openai_api_key=openai_api_key
        )
        
        persist_directory = './db_metadata'

        vectordb = Chroma.from_documents(
        documents=documents,
        embedding=embeddings,
        persist_directory=persist_directory
    )

        vectordb.persist()
        
        return 'Document uploaded and index created successfully. You can chat now.'
    except Exception as e:
        return e

# def clear_indexes(pinecone_api_key: str, pinecone_environment: str, pinecone_index_name: str) -> str:
#     try:
#         pinecone.init(
#             api_key=pinecone_api_key,
#             environment=pinecone_environment
#         )
#         indexes_list = pinecone.list_indexes()
#         if pinecone_index_name in indexes_list:
#             pinecone.delete_index(name=pinecone_index_name)
#         return 'Indexes cleared.', None
#     except Exception as e:
#         return e, None