ModelTrainRegister
SemanticSearchModel
Bases: PythonModel
A class representing a semantic search model.
This class is used to perform semantic search over a corpus of sentences using a pre-trained model.
Attributes:
Name | Type | Description |
---|---|---|
model |
The pre-trained model used for encoding sentences. |
|
corpus |
The corpus of sentences used for semantic search. |
|
corpus_embeddings |
The embeddings of the sentences in the corpus. |
Methods:
Name | Description |
---|---|
load_context |
Load the model context for inference, including the corpus from a file. |
predict |
Perform semantic search over the corpus and return the most relevant results. |
Source code in src/fleming/discovery/model_train_register.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
load_context(context)
Load the model context for inference, including the corpus from a file.
Source code in src/fleming/discovery/model_train_register.py
predict(context, model_input, params=None)
Predict method to perform semantic search over the corpus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
The context object containing the model artifacts. |
required | |
model_input |
The input data for performing semantic search. |
required | |
params |
Optional parameters for controlling the search behavior. |
None
|
Returns:
Type | Description |
---|---|
A list of tuples containing the most relevant sentences from the corpus and their similarity scores. |
Source code in src/fleming/discovery/model_train_register.py
ModelTrainRegister
A class to train and register a semantic search model.
Example:
from fleming.discovery.model_train_register import ModelTrainRegister, SemanticSearchModel
from pyspark.sql import SparkSession
# Not required if using Databricks
spark = SparkSession.builder.appName("model_serving").getOrCreate()
model_directory = "/tmp/BERT_Semantic_Search_model"
corpus_file = "/tmp/search_corpus.txt"
corpus_embedding_file = '/tmp/corpus_embedding.pt'
model_developer = ModelTrainRegister(spark, model_directory, corpus_file, corpus_embedding_file)
# Register the model
semantic_search_model = "multi-qa-mpnet-base-dot-v1"
model_developer.register_model(semantic_search_model)
# Embed the corpus
model_developer.embed_corpus()
# Define parameters and artifacts
parameters = {"top_k": 50, "relevancy_score": 0.45}
input_example = ["Innersource best practices"]
test_output = ["match 1", "match 2"]
signature = infer_signature(input_example, test_output, params=parameters)
artifacts = {
"model_path": model_directory,
"corpus_file": corpus_file,
"corpus_embedding_file": corpus_embedding_file
}
unique_model_name = "semantic_search_model"
# Create and serve the model
experiment_location = "/path/to/experiment"
model_developer.create_registered_model(unique_model_name, input_example, signature, artifacts, experiment_location)
Parameters:
model_directory (str): The directory to save the trained model. corpus_file (str): The file containing the corpus of sentences. corpus_embedding_file (str): The file to save the embeddings of the corpus. semantic_search_model (str): The pre-trained model to use for semantic search.
Source code in src/fleming/discovery/model_train_register.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
|
__init__(spark, model_directory, corpus_file, corpus_embedding_file, semantic_search_model)
Initialize the ModelDeveloper class.
Parameters:
spark : SparkSession model_directory : str The directory to save the trained model. corpus_file : str The file containing the corpus of sentences. corpus_embedding_file : str The file to save the embeddings of the corpus. semantic_search_model : str The pre-trained model to use for semantic search.
Source code in src/fleming/discovery/model_train_register.py
register_model()
embed_corpus()
Embed the corpus of sentences using the pre-trained model.
Source code in src/fleming/discovery/model_train_register.py
create_registered_model(unique_model_name, input_example, signature, artifacts, experiment_location)
Create and serve the semantic search model.
Parameters:
unique_model_name : str The unique name for the model. input_example : list An example input for the model. signature : object The signature object for the model. artifacts : dict The artifacts required for the model. experiment_location : str The location to store the experiment.