code: import os
  import shutil
  import chromadb
  import redis
  from llama_index.core.indices import VectorStoreIndex
  from llama_index.core.storage import StorageContext
  
  from app.tools import FindEmbeddingsPath
  from llama_index.vector_stores.redis import RedisVectorStore
  from llama_index.vector_stores.chroma import ChromaVectorStore
  
  def vector_init(brain, project):
      path = FindEmbeddingsPath(project.model.name)
  
      if project.model.vectorstore == "chroma":
          db = chromadb.PersistentClient(path=path)
          chroma_collection = db.get_or_create_collection(project.model.name)
          vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
  
          storage_context = StorageContext.from_defaults(
              vector_store=vector_store)
          index = VectorStoreIndex.from_vector_store(
              vector_store, storage_context=storage_context, embed_model=brain.getEmbedding(
              project.model.embeddings))
          return index
      elif project.model.vectorstore == "redis":
          if path is None or len(os.listdir(path)) == 0:
              vector_store = RedisVectorStore(
                  redis_url="redis://" +
                  os.environ["REDIS_HOST"] +
                  ":" +
                  os.environ["REDIS_PORT"],
                  index_name=project.model.name,
                  metadata_fields=["source", "keywords"],
                  index_prefix="llama_" + project.model.name,
                  overwrite=False)
  
              storage_context = StorageContext.from_defaults(
                  vector_store=vector_store)
              index = VectorStoreIndex.from_vector_store(vector_store, storage_context=storage_context, embed_model=brain.getEmbedding(project.model.embeddings))
              return index
          else:
              return vector_load(brain, project)
  
  
  def vector_save(project):
      if project.model.vectorstore == "chroma":
          pass
      elif project.model.vectorstore == "redis":
          try:
              project.db.vector_store.persist(persist_path="")
          except BaseException:
              print("REDIS - Error saving vectors")
  
  
  def vector_load(brain, project):
      if project.model.vectorstore == "chroma":
          return vector_init(brain, project)
      if project.model.vectorstore == "redis":
          vector_store = RedisVectorStore(
              redis_url="redis://" +
              os.environ["REDIS_HOST"] +
              ":" +
              os.environ["REDIS_PORT"],
              index_name=project.model.name,
              metadata_fields=["source", "keywords"],
              index_prefix="llama_" + project.model.name,
              overwrite=False)
          return VectorStoreIndex.from_vector_store(embed_model=brain.getEmbedding(
              project.model.embeddings), vector_store=vector_store)
  
  
  def vector_list(project):
      output = []
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          collection = db.get_or_create_collection(project.model.name)
  
          docs = collection.get(
              include=["metadatas"]
          )
  
          index = 0
          for metadata in docs["metadatas"]:
              if metadata["source"] not in output:
                  output.append(metadata["source"])
              index = index + 1
  
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          keys = lredis.keys("llama_" + project.model.name + "/*")
          for key in keys:
              source = lredis.hget(key, "source")
              if source not in output:
                  output.append(source)
  
      return {"embeddings": output}
  
  
  def vector_list_source(project, source):
      output = []
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          collection = db.get_or_create_collection(project.model.name)
  
          docs = collection.get(
              include=["metadatas"]
          )
  
          index = 0
          for metadata in docs["metadatas"]:
              if metadata["source"] == source:
                  output.append(metadata["source"])
              index = index + 1
  
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          keys = lredis.keys("llama_" + project.model.name + "/*")
          for key in keys:
              sourcer = lredis.hget(key, "source").strip()
              id = lredis.hget(key, "id").strip()
              if source == sourcer:
                  output.append({"source": source, "id": id, "score": 1})
  
      return output
  
  
  def vector_info(project):
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          collection = db.get_or_create_collection(project.model.name)
  
          docs = collection.get(
              include=["metadatas"]
          )
          return len(docs["ids"])
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          keys = lredis.keys("llama_" + project.model.name + "/*")
          return len(keys)
  
  
  def vector_find_source(project, source):
      docs = []
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          collection = db.get_or_create_collection(project.model.name)
          docs = collection.get(where={'source': source})
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          keys = lredis.keys("llama_" + project.model.name + "/*")
          ids = []
          metadatas = []
          documents = []
          for key in keys:
              lsource = lredis.hget(key, "source")
              if lsource == source:
                  ids.append(key)
                  metadatas.append(
                      {"source": lsource, "keywords": lredis.hget(key, "keywords")})
                  documents.append(lredis.hget(key, "text"))
  
          docs = {"ids": ids, "metadatas": metadatas, "documents": documents}
  
      return docs
  
  
  def vector_find_id(project, id):
      output = {"id": id}
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          collection = db.get_or_create_collection(project.model.name)
          docs = collection.get(ids=[id])
          output["metadata"] = {
              k: v for k, v in docs["metadatas"][0].items() if not k.startswith('_')}
          output["document"] = docs["documents"][0]
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          ids = "llama_" + project.model.name + "/vector_" + id
          keys = lredis.hkeys(ids)
          keys = [k for k in keys if not k.startswith(
              '_') and k != "vector" and k != "text" and k != "doc_id" and k != "id"]
          data = lredis.hmget(ids, keys)
          text = lredis.hget(ids, "text")
          output["metadata"] = dict(zip(keys, data))
          output["document"] = text
  
      return output
  
  
  def vector_delete(project):
      if project.model.vectorstore == "chroma":
          try:
              embeddingsPath = FindEmbeddingsPath(project.model.name)
              shutil.rmtree(embeddingsPath, ignore_errors=True)
          except BaseException:
              pass
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          try:
              lredis.ft(project.model.name).dropindex(True)
              embeddingsPath = FindEmbeddingsPath(project.model.name)
              shutil.rmtree(embeddingsPath, ignore_errors=True)
          except BaseException:
              pass
  
  
  def vector_delete_source(project, source):
      ids = []
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          collection = db.get_or_create_collection(project.model.name)
          ids = collection.get(where={'source': source})['ids']
          if len(ids):
              collection.delete(ids)
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          keys = lredis.keys("llama_" + project.model.name + "/*")
          for key in keys:
              lsource = lredis.hget(key, "source")
              if lsource == source:
                  ids.append(key)
                  lredis.delete(key)
      return ids
  
  
  def vector_delete_id(project, id):
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          collection = db.get_or_create_collection(project.model.name)
          ids = collection.get(ids=[id])['ids']
          if len(ids):
              collection.delete(ids)
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          lredis.delete(id)
      return id
  
  
  def vector_reset(brain, project):
      if project.model.vectorstore == "chroma":
          path = FindEmbeddingsPath(project.model.name)
          db = chromadb.PersistentClient(path=path)
          db.reset()
      elif project.model.vectorstore == "redis":
          lredis = redis.Redis(
              host=os.environ["REDIS_HOST"],
              port=os.environ["REDIS_PORT"],
              decode_responses=True)
          lredis.ft(project.model.name).dropindex(True)
  
      project.db = vector_init(brain, project)
  
apis: llama_index.core.storage.StorageContext.from_defaults llama_index.vector_stores.chroma.ChromaVectorStore llama_index.vector_stores.redis.RedisVectorStore
extract_api: [((370, 408), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (388, 408), False, 'from app.tools import FindEmbeddingsPath\n'), ((469, 505), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (494, 505), False, 'import chromadb\n'), ((605, 659), 'llama_index.vector_stores.chroma.ChromaVectorStore', 'ChromaVectorStore', ([], {'chroma_collection': 'chroma_collection'}), '(chroma_collection=chroma_collection)\n', (622, 659), False, 'from llama_index.vector_stores.chroma import ChromaVectorStore\n'), ((687, 742), 'llama_index.core.storage.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'vector_store': 'vector_store'}), '(vector_store=vector_store)\n', (715, 742), False, 'from llama_index.core.storage import StorageContext\n'), ((2278, 2522), 'llama_index.vector_stores.redis.RedisVectorStore', 'RedisVectorStore', ([], {'redis_url': "('redis://' + os.environ['REDIS_HOST'] + ':' + os.environ['REDIS_PORT'])", 'index_name': 'project.model.name', 'metadata_fields': "['source', 'keywords']", 'index_prefix': "('llama_' + project.model.name)", 'overwrite': '(False)'}), "(redis_url='redis://' + os.environ['REDIS_HOST'] + ':' + os\n    .environ['REDIS_PORT'], index_name=project.model.name, metadata_fields=\n    ['source', 'keywords'], index_prefix='llama_' + project.model.name,\n    overwrite=False)\n", (2294, 2522), False, 'from llama_index.vector_stores.redis import RedisVectorStore\n'), ((2859, 2897), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (2877, 2897), False, 'from app.tools import FindEmbeddingsPath\n'), ((2911, 2947), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (2936, 2947), False, 'import chromadb\n'), ((3851, 3889), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (3869, 3889), False, 'from app.tools import FindEmbeddingsPath\n'), ((3903, 3939), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (3928, 3939), False, 'import chromadb\n'), ((4880, 4918), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (4898, 4918), False, 'from app.tools import FindEmbeddingsPath\n'), ((4932, 4968), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (4957, 4968), False, 'import chromadb\n'), ((5552, 5590), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (5570, 5590), False, 'from app.tools import FindEmbeddingsPath\n'), ((5604, 5640), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (5629, 5640), False, 'import chromadb\n'), ((6622, 6660), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (6640, 6660), False, 'from app.tools import FindEmbeddingsPath\n'), ((6674, 6710), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (6699, 6710), False, 'import chromadb\n'), ((8429, 8467), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (8447, 8467), False, 'from app.tools import FindEmbeddingsPath\n'), ((8481, 8517), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (8506, 8517), False, 'import chromadb\n'), ((9256, 9294), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (9274, 9294), False, 'from app.tools import FindEmbeddingsPath\n'), ((9308, 9344), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (9333, 9344), False, 'import chromadb\n'), ((9851, 9889), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (9869, 9889), False, 'from app.tools import FindEmbeddingsPath\n'), ((9903, 9939), 'chromadb.PersistentClient', 'chromadb.PersistentClient', ([], {'path': 'path'}), '(path=path)\n', (9928, 9939), False, 'import chromadb\n'), ((3349, 3449), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (3360, 3449), False, 'import redis\n'), ((4337, 4437), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (4348, 4437), False, 'import redis\n'), ((5210, 5310), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (5221, 5310), False, 'import redis\n'), ((5830, 5930), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (5841, 5930), False, 'import redis\n'), ((7049, 7149), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (7060, 7149), False, 'import redis\n'), ((7716, 7754), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (7734, 7754), False, 'from app.tools import FindEmbeddingsPath\n'), ((7767, 7816), 'shutil.rmtree', 'shutil.rmtree', (['embeddingsPath'], {'ignore_errors': '(True)'}), '(embeddingsPath, ignore_errors=True)\n', (7780, 7816), False, 'import shutil\n'), ((7928, 8028), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (7939, 8028), False, 'import redis\n'), ((8769, 8869), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (8780, 8869), False, 'import redis\n'), ((9580, 9680), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (9591, 9680), False, 'import redis\n'), ((10023, 10123), 'redis.Redis', 'redis.Redis', ([], {'host': "os.environ['REDIS_HOST']", 'port': "os.environ['REDIS_PORT']", 'decode_responses': '(True)'}), "(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'],\n    decode_responses=True)\n", (10034, 10123), False, 'import redis\n'), ((1088, 1332), 'llama_index.vector_stores.redis.RedisVectorStore', 'RedisVectorStore', ([], {'redis_url': "('redis://' + os.environ['REDIS_HOST'] + ':' + os.environ['REDIS_PORT'])", 'index_name': 'project.model.name', 'metadata_fields': "['source', 'keywords']", 'index_prefix': "('llama_' + project.model.name)", 'overwrite': '(False)'}), "(redis_url='redis://' + os.environ['REDIS_HOST'] + ':' + os\n    .environ['REDIS_PORT'], index_name=project.model.name, metadata_fields=\n    ['source', 'keywords'], index_prefix='llama_' + project.model.name,\n    overwrite=False)\n", (1104, 1332), False, 'from llama_index.vector_stores.redis import RedisVectorStore\n'), ((1479, 1534), 'llama_index.core.storage.StorageContext.from_defaults', 'StorageContext.from_defaults', ([], {'vector_store': 'vector_store'}), '(vector_store=vector_store)\n', (1507, 1534), False, 'from llama_index.core.storage import StorageContext\n'), ((8162, 8200), 'app.tools.FindEmbeddingsPath', 'FindEmbeddingsPath', (['project.model.name'], {}), '(project.model.name)\n', (8180, 8200), False, 'from app.tools import FindEmbeddingsPath\n'), ((8213, 8262), 'shutil.rmtree', 'shutil.rmtree', (['embeddingsPath'], {'ignore_errors': '(True)'}), '(embeddingsPath, ignore_errors=True)\n', (8226, 8262), False, 'import shutil\n'), ((1037, 1053), 'os.listdir', 'os.listdir', (['path'], {}), '(path)\n', (1047, 1053), False, 'import os\n')]