Chroma Framework는 문장 변압기 모델을 사용하여 텍스트 임베딩을 관리하고 검색하도록 설계된 파이썬 기반 응용 프로그램입니다. 이 프레임 워크를 통해 사용자는 텍스트 임베드 모음을 만들고 새 문서를 추가하며 입력 쿼리를 기반으로 가장 가까운 텍스트를 쿼리 할 수 있습니다.
⛩️ 임베딩 관리 ⛩️ -> 텍스트 임베딩 컬렉션을 만들고 관리합니다.
문서 추가 -> 메타 데이터를 사용하여 컬렉션에 새 문서 추가.
? 텍스트 검색 ? -> 임베딩 모델을 사용하여 주어진 쿼리에 가장 가까운 텍스트를 찾으십시오.
동적 경로 처리 -> 프로젝트 디렉토리에 대한 파일 경로를 자동으로 결정합니다.
저장소 복제 :
git clone https://github.com/yourusername/chromadb_framework프로젝트 디렉토리로 이동하십시오 .
cd chromadb_framework필요한 종속성을 설치하십시오 (해당되는 경우) .
pip install -r requirements.txtPython 3.x가 설치되어 있는지 확인하십시오.
실행하여 응용 프로그램을 실행합니다.
python main.py스크린 프롬프트를 따라 임베딩 및 검색 텍스트를 관리하십시오.
? project-root
├── ? config
│ ├── ? __ init __ .py
│ └── ? constants.py
│
├── ? src
│ ├── ? __ init __ .py
│ ├── ? client.py
│ ├── ? collection.py
│ └── ? data.py
│
├── ? utils
│ ├── ? __ init __ .py
│ └── ? helpers.py
│
├── ? .gitignore
├── ? .gitattributes
└── ? main.pyconfig.py/ : configuration 파일이 포함되어 있습니다.
SRC/ : 소스 코드 파일이 포함되어 있습니다.
Utils/ : 유틸리티 기능이 포함되어 있습니다.
.gitignore : Git (예 : 가상 환경, 구축 인공물)에 의해 무시할 파일 및 디렉토리를 지정합니다.
.gitattributes : 저장소의 다른 운영 체제에서 일관된 라인 엔딩을 보장합니다.
main.py : 응용 프로그램의 진입 점. 설정 초기화, 임베딩 작업을 처리하며 텍스트 검색을 관리합니다.
from config . constants import MODEL_NAME , COLLECTION_NAME , INPUT_QUERY
from src . client import get_client
from src . collection import get_or_create_collection , add_collection , find_closest_texts
from src . data import get_data
from utils . helpers import set_def_llm , get_path
def main ():
model_name = MODEL_NAME
collection_name = COLLECTION_NAME
input_query = INPUT_QUERY
my_client = get_client ()
my_folder_path = get_path ()
embedding_function = set_def_llm ( model_name )
my_collection = get_or_create_collection ( my_client , collection_name , embedding_function = embedding_function )
my_documents , my_metadatas , my_ids = get_data ( my_folder_path )
add_collection ( my_collection , my_documents , my_metadatas , my_ids )
my_closest_texts = find_closest_texts ( my_collection , input_query )
print ( "Closest text(s):" , my_closest_texts )
if __name__ == "__main__" :
main ()Helpers.py : 모델을 설정하고 경로를 얻기위한 유틸리티 기능.
from os . path import abspath , dirname , join
from chromadb . utils import embedding_functions
def set_def_llm ( model_name = None ):
try :
if model_name :
return embedding_functions . SentenceTransformerEmbeddingFunction ( model_name = model_name )
else :
return embedding_functions . DefaultEmbeddingFunction ()
except Exception as e :
print ( f"An error occurred while setting the sentence transformer. n " )
return None
def get_path ( folder_name = "texts" ):
try :
current_path = dirname ( abspath ( __file__ ))
project_path = dirname ( current_path )
full_path = join ( project_path , folder_name )
return full_path
except Exception as e :
print ( f"An error occurred while getting the folder path. n " )client.py : 데이터베이스 클라이언트를 생성하는 기능.
from chromadb import PersistentClient
def get_client ( path = "vector_db" ):
try :
client = PersistentClient ( path = path )
return client
except FileNotFoundError :
print ( f"Database directory not found:" )
except Exception as e :
print ( f"An error occurred while creating the client: { e } " )Collection.py : 컬렉션 및 검색 텍스트를 관리하는 기능.
def get_or_create_collection ( client , name , embedding_function ):
try :
return client . get_or_create_collection ( name = name , embedding_function = embedding_function )
except Exception as e :
print ( f"An error occurred while creating the collection: { e } " )
def add_collection ( collection , documents , metadatas , ids ):
try :
collection . add (
documents = documents ,
metadatas = metadatas ,
ids = ids
)
except Exception as e :
print ( f"An error occurred while adding to the collection: { e } " )
def find_closest_texts ( collection , input_query , n_results = 2 ):
try :
closest_text_names = list ()
results = collection . query (
query_texts = [ input_query ],
include = [ "metadatas" ],
n_results = n_results
)
for item in results [ "metadatas" ][ 0 ]:
closest_text_names . append ( item [ "source" ])
return closest_text_names
except Exception as e :
print ( f"An error occurred while finding the closest text: { e } " )data.py : 지정된 폴더에서 데이터를 검색하는 기능.
from os import listdir
from os . path import join
def get_data ( folder_path ):
try :
documents = list ()
metadatas = list ()
ids = list ()
id_count = 1
for file_name in listdir ( folder_path ):
if file_name . endswith ( ".txt" ):
file_path = join ( folder_path , file_name )
id = "id" + str ( id_count )
with open ( file_path ) as file :
content = file . read ()
documents . append ( content )
metadatas . append ({ "source" : file_name })
ids . append ( id )
id_count += 1
return documents , metadatas , ids
except Exception as e :
print ( f"An error occurred while creating the data: { e } " )
return [], [], []이 프로젝트는 GNU General Public License v3.0 (GPL -3.0)에 따라 라이센스 파일을 참조하십시오.
조정하려는 특정 세부 정보가 있거나 포함하려는 추가 섹션이 있는지 알려주십시오!