45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
from langchain_huggingface import HuggingFaceEmbeddings
|
||
from langchain_chroma import Chroma # TODO plus tard, ramplacer par PG Vector
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
# Permet de garder ChromaDB en mémoire.
|
||
# Cette classe est un Singleton, il n'y en aura qu'une seule et unique instance à tout moment
|
||
# https://refactoring.guru/design-patterns/singleton
|
||
class VectorDatabase:
|
||
instance = None
|
||
|
||
def __new__(cls): # Selon https://www.geeksforgeeks.org/python/singleton-pattern-in-python-a-complete-guide/
|
||
if cls.instance is None:
|
||
cls.instance = super().__new__(cls)
|
||
# J'initialise les attributs à None ici, permet de tester si la classe a déjà été init une première fois ou non
|
||
cls.instance.__embeddings = None
|
||
cls.instance.__chroma = None
|
||
return cls.instance
|
||
|
||
def __init__(self):
|
||
if self.__embeddings is not None: return
|
||
|
||
base_dir:str = Path(sys.argv[0]).resolve().parent.as_posix() # Récupérer le chemin vers le point d'entrée du programme
|
||
bdd_path:str = base_dir + "/chroma_db/"
|
||
|
||
self.__embeddings = HuggingFaceEmbeddings(model_name="jinaai/jina-embeddings-v3", model_kwargs={"trust_remote_code": True})
|
||
self.__chroma = Chroma(
|
||
persist_directory=bdd_path,
|
||
embedding_function=self.__embeddings
|
||
)
|
||
|
||
def getChroma(self)->Chroma:
|
||
return self.__chroma
|
||
|
||
def getEmbeddings(self)->'Embeddings Hugging Face':
|
||
return self.__embeddings
|
||
|
||
if __name__ == "__main__":
|
||
|
||
test1 = VectorDatabase()
|
||
print('TEST 1 INIT')
|
||
test2 = VectorDatabase()
|
||
|
||
print(test1 is test2)
|
||
assert test1 is test2 |