Chroma Frameworkは、文変圧器モデルを使用してテキストの埋め込みを管理および検索するために設計されたPythonベースのアプリケーションです。このフレームワークにより、ユーザーはテキスト埋め込みのコレクションを作成し、新しいドキュメントを追加し、入力クエリに基づいて最も近いテキストをクエリできます。
management管理の埋め込み⛩️->テキスト埋め込みのコレクションを作成および管理します。
ドキュメントの追加- >メタデータを使用してコレクションに新しいドキュメントを追加します。
?テキスト検索? - >埋め込みモデルを使用して、特定のクエリに最も近いテキストを見つけます。
ダイナミックパス処理- >プロジェクトディレクトリに対するファイルパスを自動的に決定します。
リポジトリをクローンします:
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/ :構成ファイルが含まれています。
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)に基づいてライセンスされています。詳細については、ライセンスファイルを参照してください。
調整したい具体的な詳細がある場合、または含めたい追加セクションがある場合はお知らせください!