|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from dataclasses import dataclass |
| 16 | +from typing import Dict, List |
| 17 | + |
| 18 | +from nl_server import embeddings |
| 19 | +from nl_server import gcs |
| 20 | + |
| 21 | +# Index constants. Passed in `url=` |
| 22 | +CUSTOM_DC_INDEX = 'custom' |
| 23 | +DEFAULT_INDEX_TYPE = 'medium_ft' |
| 24 | + |
| 25 | +# The default base model we use. |
| 26 | +EMBEDDINGS_BASE_MODEL_NAME = 'all-MiniLM-L6-v2' |
| 27 | + |
| 28 | +# App Config constants. |
| 29 | +NL_MODEL_KEY = 'NL_MODEL' |
| 30 | +NL_EMBEDDINGS_KEY = 'NL_EMBEDDINGS' |
| 31 | +NL_EMBEDDINGS_VERSION_KEY = 'NL_EMBEDDINGS_VERSION_MAP' |
| 32 | + |
| 33 | + |
| 34 | +# Defines one embeddings index config. |
| 35 | +@dataclass |
| 36 | +class EmbeddingsIndex: |
| 37 | + # Name provided in the yaml file, and set in `idx=` URL param. |
| 38 | + name: str |
| 39 | + |
| 40 | + # File name provided in the yaml file. |
| 41 | + embeddings_file_name: str |
| 42 | + # Local path. |
| 43 | + embeddings_local_path: str = "" |
| 44 | + |
| 45 | + # Fine-tuned model name ("" if embeddings uses base model). |
| 46 | + tuned_model: str = "" |
| 47 | + # Fine-tuned model local path. |
| 48 | + tuned_model_local_path: str = "" |
| 49 | + |
| 50 | + |
| 51 | +# |
| 52 | +# Validates the config input, downloads all the files and returns a list of Indexes to load. |
| 53 | +# |
| 54 | +def load(embeddings_map: Dict[str, str], |
| 55 | + models_map: Dict[str, str]) -> List[EmbeddingsIndex]: |
| 56 | + # Create Index objects. |
| 57 | + indexes = _parse(embeddings_map) |
| 58 | + |
| 59 | + # This is just a sanity, we can soon deprecate models.yaml |
| 60 | + tuned_models_provided = list(set(models_map.values())) |
| 61 | + tuned_models_configured = list( |
| 62 | + set([i.tuned_model for i in indexes if i.tuned_model])) |
| 63 | + assert sorted(tuned_models_configured) == sorted(tuned_models_provided), \ |
| 64 | + f'{tuned_models_configured} vs. {tuned_models_provided}' |
| 65 | + |
| 66 | + # |
| 67 | + # Download all the models. |
| 68 | + # |
| 69 | + model2path = {d: gcs.download_model_folder(d) for d in tuned_models_configured} |
| 70 | + for idx in indexes: |
| 71 | + if idx.tuned_model: |
| 72 | + idx.tuned_model_local_path = model2path[idx.tuned_model] |
| 73 | + |
| 74 | + # |
| 75 | + # Download all the embeddings. |
| 76 | + # |
| 77 | + for idx in indexes: |
| 78 | + idx.embeddings_local_path = gcs.download_embeddings( |
| 79 | + idx.embeddings_file_name) |
| 80 | + |
| 81 | + return indexes |
| 82 | + |
| 83 | + |
| 84 | +def _parse(embeddings_map: Dict[str, str]) -> List[EmbeddingsIndex]: |
| 85 | + indexes: List[EmbeddingsIndex] = [] |
| 86 | + |
| 87 | + for key, value in embeddings_map.items(): |
| 88 | + idx = EmbeddingsIndex(name=key, embeddings_file_name=value) |
| 89 | + |
| 90 | + parts = value.split('.') |
| 91 | + assert parts[ |
| 92 | + -1] == 'csv', f'Embeddings file {value} name does not end with .csv!' |
| 93 | + |
| 94 | + if len(parts) == 4: |
| 95 | + # Expect: <embeddings_version>.<fine-tuned-model-version>.<base-model>.csv |
| 96 | + # Example: embeddings_sdg_2023_09_12_16_38_04.ft_final_v20230717230459.all-MiniLM-L6-v2.csv |
| 97 | + assert parts[ |
| 98 | + 2] == EMBEDDINGS_BASE_MODEL_NAME, f'Unexpected base model {parts[3]}' |
| 99 | + idx.tuned_model = f'{parts[1]}.{parts[2]}' |
| 100 | + else: |
| 101 | + # Expect: <embeddings_version>.csv |
| 102 | + # Example: embeddings_small_2023_05_24_23_17_03.csv |
| 103 | + assert len(parts) == 2, f'Unexpected file name format {value}' |
| 104 | + indexes.append(idx) |
| 105 | + |
| 106 | + return indexes |
0 commit comments