ddi-fw 0.0.247__tar.gz → 0.0.249__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/PKG-INFO +1 -1
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/pyproject.toml +1 -1
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/langchain/faiss_storage.py +109 -2
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw.egg-info/PKG-INFO +1 -1
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/README.md +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/setup.cfg +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/datasets/__init__.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/datasets/core.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/datasets/dataset_splitter.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/datasets/db_utils.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/datasets/setup_._py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/langchain/__init__.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/langchain/chroma_storage.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/langchain/embeddings.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/langchain/sentence_splitter.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/langchain/storage.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ml/__init__.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ml/evaluation_helper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ml/ml_helper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ml/model_wrapper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ml/pytorch_wrapper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ml/tensorflow_wrapper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ml/tracking_service.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ner/__init__.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ner/mmlrestclient.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/ner/ner.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/pipeline/__init__.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/pipeline/multi_modal_combination_strategy.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/pipeline/multi_pipeline.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/pipeline/multi_pipeline_org.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/pipeline/ner_pipeline.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/pipeline/pipeline.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/__init__.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/categorical_data_encoding_checker.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/enums.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/json_helper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/kaggle.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/numpy_utils.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/package_helper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/py7zr_helper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/utils.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/utils/zip_helper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/vectorization/__init__.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/vectorization/feature_vector_generation.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw/vectorization/idf_helper.py +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw.egg-info/SOURCES.txt +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw.egg-info/dependency_links.txt +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw.egg-info/requires.txt +0 -0
- {ddi_fw-0.0.247 → ddi_fw-0.0.249}/src/ddi_fw.egg-info/top_level.txt +0 -0
@@ -3,7 +3,7 @@ import pandas as pd
|
|
3
3
|
from uuid import uuid4
|
4
4
|
from langchain_community.vectorstores.faiss import FAISS
|
5
5
|
from langchain_community.docstore.in_memory import InMemoryDocstore
|
6
|
-
from typing import Callable, Optional, Dict, Any
|
6
|
+
from typing import Callable, Optional, Dict, Any, Type
|
7
7
|
from langchain_core.documents import Document
|
8
8
|
import numpy as np # optional, if you're using NumPy vectors
|
9
9
|
from langchain_core.embeddings import Embeddings
|
@@ -11,6 +11,8 @@ from langchain_core.embeddings import Embeddings
|
|
11
11
|
from pydantic import BaseModel, Field
|
12
12
|
from langchain_core.embeddings import Embeddings
|
13
13
|
from langchain_core.vectorstores import VectorStore
|
14
|
+
from ddi_fw.utils import get_import
|
15
|
+
from langchain.document_loaders import DataFrameLoader
|
14
16
|
|
15
17
|
class BaseVectorStoreManager(BaseModel):
|
16
18
|
embeddings: Optional[Embeddings] = None
|
@@ -54,7 +56,9 @@ class FaissVectorStoreManager(BaseVectorStoreManager):
|
|
54
56
|
# uuids = [str(uuid4()) for _ in range(len(docs))]
|
55
57
|
# self.vector_store.add_documents(documents=docs, ids=uuids)
|
56
58
|
|
57
|
-
def initialize_embedding_dict(self):
|
59
|
+
def initialize_embedding_dict(self, **kwargs):
|
60
|
+
vector_db_persist_directory = kwargs.get("vector_db_persist_directory")
|
61
|
+
self.load(vector_db_persist_directory)
|
58
62
|
df = self.as_dataframe(formatter_fn=custom_formatter )
|
59
63
|
type_dict = (
|
60
64
|
df.groupby('type')
|
@@ -240,3 +244,106 @@ def custom_formatter(document: Document, vector: np.ndarray) -> Dict[str, Any]:
|
|
240
244
|
"type": document.metadata.get("type", None),
|
241
245
|
"embedding": vector
|
242
246
|
}
|
247
|
+
|
248
|
+
def load_configuration(config_file):
|
249
|
+
"""
|
250
|
+
Load the configuration from a JSON file.
|
251
|
+
"""
|
252
|
+
import json
|
253
|
+
with open(config_file, 'r') as f:
|
254
|
+
config = json.load(f)
|
255
|
+
return config
|
256
|
+
|
257
|
+
|
258
|
+
def generate_embeddings(
|
259
|
+
df,
|
260
|
+
vector_store_manager_type:Type[BaseVectorStoreManager],
|
261
|
+
config_file,
|
262
|
+
new_model_names,
|
263
|
+
collections,
|
264
|
+
persist_directory="embeddings",
|
265
|
+
):
|
266
|
+
"""
|
267
|
+
Generate embeddings for collections based on a configuration file.
|
268
|
+
|
269
|
+
collections: List of collections that contain metadata for embedding generation.
|
270
|
+
config_file: Path to the configuration file containing model settings.
|
271
|
+
new_model_names: List of model names to generate embeddings for.
|
272
|
+
vector_store_manager_type: Class type of the vector store manager (e.g., FaissVectorStoreManager or ChromaVectorStoreManager)
|
273
|
+
"""
|
274
|
+
if not collections and not config_file:
|
275
|
+
raise ValueError("Either 'collections' or 'config_file' must be provided.")
|
276
|
+
if collections and config_file:
|
277
|
+
raise ValueError("Only one of 'collections' or 'config_file' should be provided.")
|
278
|
+
|
279
|
+
if not collections:
|
280
|
+
collections = load_configuration(config_file)
|
281
|
+
|
282
|
+
for collection_config in collections:
|
283
|
+
id = collection_config['id']
|
284
|
+
name = collection_config['name']
|
285
|
+
|
286
|
+
if name not in new_model_names:
|
287
|
+
continue
|
288
|
+
|
289
|
+
embedding_model_type = collection_config.get('embedding_model_type')
|
290
|
+
text_splitters_types = collection_config.get('text_splitters_types')
|
291
|
+
batch_size = collection_config.get('batch_size')
|
292
|
+
partial_df_size = collection_config.get('partial_dataframe_size')
|
293
|
+
columns = collection_config.get('columns')
|
294
|
+
page_content_columns = collection_config.get('page_content_columns')
|
295
|
+
persist_dir = f'{persist_directory}/{id}'
|
296
|
+
|
297
|
+
# Load embedding model
|
298
|
+
try:
|
299
|
+
model_kwargs = collection_config.get('model_kwargs')
|
300
|
+
model = get_import(embedding_model_type)(
|
301
|
+
model_name=name, **model_kwargs)
|
302
|
+
except Exception as e:
|
303
|
+
raise Exception(f"Unknown embedding model: {embedding_model_type}") from e
|
304
|
+
|
305
|
+
# Load text splitters
|
306
|
+
text_splitters = []
|
307
|
+
text_splitters_suffixes = []
|
308
|
+
for text_splitter_type in text_splitters_types:
|
309
|
+
try:
|
310
|
+
type_of_text_splitter = get_import(
|
311
|
+
text_splitter_type.get("type"))
|
312
|
+
kwargs = text_splitter_type.get("params")
|
313
|
+
suffix = text_splitter_type.get("suffix")
|
314
|
+
if kwargs:
|
315
|
+
text_splitter = type_of_text_splitter(**kwargs)
|
316
|
+
else:
|
317
|
+
text_splitter = type_of_text_splitter()
|
318
|
+
text_splitters.append(text_splitter)
|
319
|
+
text_splitters_suffixes.append(suffix)
|
320
|
+
except Exception as e:
|
321
|
+
raise Exception(f"Unknown text splitter: {text_splitter_type}") from e
|
322
|
+
|
323
|
+
for text_splitter, suffix in zip(text_splitters, text_splitters_suffixes):
|
324
|
+
print(f"{id}_{suffix}")
|
325
|
+
|
326
|
+
# Prepare manager parameters
|
327
|
+
manager_params = {
|
328
|
+
"collection_name": f"{id}_{suffix}",
|
329
|
+
"persist_directory": persist_dir,
|
330
|
+
"embeddings": model,
|
331
|
+
"text_splitter": text_splitter,
|
332
|
+
"batch_size": batch_size
|
333
|
+
}
|
334
|
+
|
335
|
+
# Instantiate the manager class
|
336
|
+
vector_store_manager = vector_store_manager_type(**manager_params)
|
337
|
+
|
338
|
+
# Prepare documents
|
339
|
+
# You may need to use a DataFrameLoader or similar to convert df to LangChain Documents
|
340
|
+
loader = DataFrameLoader(
|
341
|
+
data_frame=df, page_content_column=page_content_columns[0]
|
342
|
+
)
|
343
|
+
docs = loader.load()
|
344
|
+
|
345
|
+
# Generate vector store
|
346
|
+
vector_store_manager.generate_vector_store(docs)
|
347
|
+
|
348
|
+
# Optionally persist/save
|
349
|
+
vector_store_manager.save(persist_dir)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|