import os

from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma




class Database:
    def __init__(self, directory):
        self.embeddings = OpenAIEmbeddings()
        self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
        self.directory = directory
        self.files = os.listdir(self.directory)

    def list_files(self):
        if len(self.files) == 0:
            return None
        return self.files

    def save_or_add_to_transcripts(self, name, transcript):
        persist_directory = os.path.join(self.directory, name)
        if not os.path.exists(persist_directory):
            os.makedirs(persist_directory)
        transcript_file = os.path.join(persist_directory, "transcript.txt")
        with open(transcript_file, 'a') as f:
            f.write(transcript + "\n\n")


    def load_db(self, name):
        persist_directory = os.path.join(self.directory, name)
        transcript_file = os.path.join(persist_directory, "transcript.txt")
        with open(transcript_file, 'r') as f:
            transcript = f.read()
        split_docs = self.text_splitter.split_text(transcript)
        db = Chroma.from_texts(texts=split_docs, embedding=self.embeddings)
        return db
















langchain.vectorstores.Chroma.from_texts langchain.embeddings.OpenAIEmbeddings langchain.text_splitter.RecursiveCharacterTextSplitter
[((251, 269), 'langchain.embeddings.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {}), '()\n', (267, 269), False, 'from langchain.embeddings import OpenAIEmbeddings\n'), ((299, 363), 'langchain.text_splitter.RecursiveCharacterTextSplitter', 'RecursiveCharacterTextSplitter', ([], {'chunk_size': '(500)', 'chunk_overlap': '(50)'}), '(chunk_size=500, chunk_overlap=50)\n', (329, 363), False, 'from langchain.text_splitter import RecursiveCharacterTextSplitter\n'), ((420, 446), 'os.listdir', 'os.listdir', (['self.directory'], {}), '(self.directory)\n', (430, 446), False, 'import os\n'), ((646, 680), 'os.path.join', 'os.path.join', (['self.directory', 'name'], {}), '(self.directory, name)\n', (658, 680), False, 'import os\n'), ((800, 849), 'os.path.join', 'os.path.join', (['persist_directory', '"""transcript.txt"""'], {}), "(persist_directory, 'transcript.txt')\n", (812, 849), False, 'import os\n'), ((996, 1030), 'os.path.join', 'os.path.join', (['self.directory', 'name'], {}), '(self.directory, name)\n', (1008, 1030), False, 'import os\n'), ((1057, 1106), 'os.path.join', 'os.path.join', (['persist_directory', '"""transcript.txt"""'], {}), "(persist_directory, 'transcript.txt')\n", (1069, 1106), False, 'import os\n'), ((1263, 1325), 'langchain.vectorstores.Chroma.from_texts', 'Chroma.from_texts', ([], {'texts': 'split_docs', 'embedding': 'self.embeddings'}), '(texts=split_docs, embedding=self.embeddings)\n', (1280, 1325), False, 'from langchain.vectorstores import Chroma\n'), ((696, 729), 'os.path.exists', 'os.path.exists', (['persist_directory'], {}), '(persist_directory)\n', (710, 729), False, 'import os\n'), ((743, 773), 'os.makedirs', 'os.makedirs', (['persist_directory'], {}), '(persist_directory)\n', (754, 773), False, 'import os\n')]