generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path03.3_Create_ML_CustomModel.py
287 lines (240 loc) · 11.4 KB
/
03.3_Create_ML_CustomModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
122
123
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# Databricks notebook source
# MAGIC %md You may find this notebook on https://github.com/databricks-industry-solutions/mfg-llm-qa-bot.
# COMMAND ----------
# MAGIC %md ##Create ML
# MAGIC
# MAGIC In this notebook, we create a custom MLflow pyfunc wrapper to store our langchain model in MLflow. We load and use an open source LLM model. We do this to follow MLOps best practices and simplify the deployment of our application.
# MAGIC
# MAGIC This continues from notebook 02.3
# MAGIC
# MAGIC
# MAGIC <p>
# MAGIC <img src="https://github.com/databricks-industry-solutions/mfg-llm-qa-bot/raw/main/images/MLflow-RAG.png" width="700" />
# MAGIC </p>
# MAGIC
# MAGIC This notebook was tested on the following infrastructure:
# MAGIC * DBR 13.3ML (GPU)
# MAGIC * g5.2xlarge (AWS) - however comparable infra on Azure should work (A10s)
# COMMAND ----------
# MAGIC %md
# MAGIC CUDA [memory management flag](https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
# COMMAND ----------
# MAGIC %sh export 'PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:512'
# COMMAND ----------
# MAGIC %md
# MAGIC Install Libraries
# COMMAND ----------
# MAGIC %pip install --upgrade langchain==0.1.6 SQLAlchemy==2.0.27 transformers==4.37.2 databricks-vectorsearch==0.22 mlflow[databricks] xformers==0.0.24 accelerate==0.27.0
# COMMAND ----------
dbutils.library.restartPython()
# COMMAND ----------
# MAGIC %run "./utils/configs"
# COMMAND ----------
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.prompts import PromptTemplate
from torch import cuda, bfloat16,float16
import transformers
from langchain import HuggingFacePipeline
from transformers import AutoTokenizer, pipeline
from langchain.chains import RetrievalQA
from databricks.vector_search.client import VectorSearchClient
from langchain.vectorstores import DatabricksVectorSearch
from langchain.embeddings import DatabricksEmbeddings
from mlflow.pyfunc import PythonModelContext
from utils.stoptoken import StopOnTokens
import json
import torch
import gc
import logging
import sys
# COMMAND ----------
# MAGIC %md
# MAGIC ##Create wrapper for LLM
# MAGIC
# MAGIC In the code below, we are using the [mlflow.pyfunc](https://mlflow.org/docs/latest/python_api/mlflow.pyfunc.html#creating-custom-pyfunc-models) to create a custom model type in MLflow. We will call this wrapper in the next notebook to save our model to MLflow.
# MAGIC
# MAGIC This approach allows us to customize input and return values to the LLM model
# MAGIC
# MAGIC If you get error messages like
# MAGIC * Some parameters are on the meta device device because they were offloaded to the cpu.
# MAGIC * You can't move a model that has some modules offloaded to cpu or disk.
# MAGIC
# MAGIC if you see above messages then GPU memory may be low
# COMMAND ----------
import json
class MLflowMfgBot(mlflow.pyfunc.PythonModel):
#constructor with args to pass in during model creation
def __init__(self):
pass
def _reconvertVals(self, autoconfig):
''' Convert string values in config to the right types'''
for key in autoconfig:
if key in ['trust_remote_code', 'return_full_text', 'low_cpu_mem_usage'] and autoconfig[key] is not None:
autoconfig[key] = bool(autoconfig[key])
if key in 'torch_dtype' and isinstance(autoconfig['torch_dtype'], str) and autoconfig['torch_dtype'] in 'torch.bfloat16':
autoconfig['torch_dtype'] = torch.bfloat16
if key in 'torch_dtype' and isinstance(autoconfig['torch_dtype'], str) and autoconfig['torch_dtype'] in 'torch.float16':
autoconfig['torch_dtype'] = torch.float16
def _get_retriever(self):
'''Get the langchain vector retriever from the Databricks object '''
logger = logging.getLogger('mlflow.store')
logger.info('_get_retriever')
vsc = VectorSearchClient(workspace_url=self._configs["DATABRICKS_URL"], personal_access_token=self._configs['DATABRICKS_TOKEN'])
index = vsc.get_index(endpoint_name=self._configs['vector_endpoint_name'],
index_name=f"{self._configs['source_catalog']}.{self._configs['source_schema']}.{self._configs['vector_index']}")
index.describe()
# Create the langchain retriever. text_columns-> chunks column
# return columns metadata_name and path along with results.
# embedding is None for Databricks managed embedding
vectorstore = DatabricksVectorSearch(
index, text_column="chunks", embedding=None, columns=['metadata_name', 'path']
)
#filter isnt working here at the moment
return vectorstore.as_retriever(search_kwargs={"k": self._configs["num_similar_docs"]}, search_type = "similarity")
#this is not to be confused with load_model. This is private to this class and called internally by load_context.
def _load_model(self):
logger = logging.getLogger('mlflow.store')
try:
device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
logger.info(device)
self._reconvertVals(self._automodelconfigs)
self._reconvertVals(self._pipelineconfigs)
print(f"{self._configs['model_name']} using configurations {self._automodelconfigs}")
#account for small variations in code for loading models between models
if 'mpt' in self._configs['model_name']:
modconfig = transformers.AutoConfig.from_pretrained(self._configs['model_name'] ,
trust_remote_code=True
)
#modconfig.attn_config['attn_impl'] = 'triton'
model = transformers.AutoModelForCausalLM.from_pretrained(
self._configs['model_name'],
config=modconfig,
**self._automodelconfigs
)
elif 'flan' in self._configs['model_name']:
model = transformers.AutoModelForSeq2SeqLM.from_pretrained(
self._configs['model_name'],
**self._automodelconfigs
)
else:
model = transformers.AutoModelForCausalLM.from_pretrained(
self._configs['model_name'],
**self._automodelconfigs
)
# model.to(device) -> `.to` is not supported for `4-bit` or `8-bit` models.
listmc = self._automodelconfigs.keys()
# if 'load_in_4bit' not in listmc and 'load_in_8bit' not in listmc:
# model.eval()
# model.to(device)
if 'RedPajama' in self._configs['model_name']:
model.tie_weights()
tokenizer = transformers.AutoTokenizer.from_pretrained(self._configs['tokenizer_name'])
if 'load_in_4bit' not in listmc and 'load_in_8bit' not in listmc:
generate_text = transformers.pipeline(
model=model, tokenizer=tokenizer,
#device=device, #latest accelerate lib doesnt like this.
pad_token_id=tokenizer.eos_token_id,
#stopping_criteria=stopping_criteria,
**self._pipelineconfigs
)
else:
generate_text = transformers.pipeline(
model=model, tokenizer=tokenizer,
pad_token_id=tokenizer.eos_token_id,
#stopping_criteria=stopping_criteria,
**self._pipelineconfigs
)
logger.info('Creating HF Pipeline')
llm = HuggingFacePipeline(pipeline=generate_text)
return llm
except Exception as e:
logger.info("ErrorDel")
logger.info(e)
_qa_chain=None
gc.collect()
torch.cuda.empty_cache()
def load_context(self, context):
"""This method is called when loading an MLflow model with pyfunc.load_model(), as soon as the Python Model is constructed.
Args:
context: MLflow context where the model artifact is stored.
"""
logger = logging.getLogger('mlflow.store')
logger.setLevel(logging.INFO)
#this is passed in
logger.info('-1-')
modconfig = context.model_config
logger.info('incoming model config ' + str(modconfig))
self._configs = json.loads(modconfig['configs'])
self._automodelconfigs = json.loads(modconfig['automodelconfigs'].replace("\'", "\""))
self._pipelineconfigs = json.loads(modconfig['pipelineconfigs'].replace("\'", "\""))
os.environ['HUGGINGFACEHUB_API_TOKEN'] = self._configs["HUGGINGFACEHUB_API_TOKEN"]
os.environ['DATABRICKS_HOST']=self._configs['DATABRICKS_URL']
os.environ['DATABRICKS_TOKEN']=self._configs['DATABRICKS_TOKEN']
logger.info('-2-')
retr = self._get_retriever()
if retr is None:
logger.info('Could not load context since Retriever failed')
return
self._retriever = retr
self._qa_chain = None
logger.info('-3-')
#have to do a bit of extra initialization with llama-2 models since its by invitation only
if 'lama-2-' in self._configs['model_name']:
import subprocess
retval = subprocess.call(f'huggingface-cli login --token {self._configs["HUGGINGFACEHUB_API_TOKEN"] }', shell=True)
logger.info(f"{self._configs['model_name']} limited auth is complete-{retval}")
logger.info('-4-')
llm = self._load_model()
if llm is None:
logger.info('cannot load context because model was not loaded')
return
logger.info('Getting RetrievalQA handle')
promptTemplate = PromptTemplate(
template=self._configs['prompt_template'], input_variables=["context", "question"])
chain_type_kwargs = {"prompt":promptTemplate, "verbose":False}
#qa chain is recreated. This is not stored with the model.
self._qa_chain = RetrievalQA.from_chain_type(llm=llm,
chain_type="stuff",
retriever=self._retriever,
return_source_documents=True,
chain_type_kwargs=chain_type_kwargs,
verbose=False)
logger.info('-5-')
def predict(self, context, inputs):
'''Evaluates a pyfunc-compatible input and produces a pyfunc-compatible output.
Only one question can be passed in.
Example of input in json split format thats passed to predict.
{"dataframe_split":{"columns":["question","filter"],"index":[0],"data":[["what are some properties of Acetaldehyde?",{"Name":"ACETALDEHYDE"}]]}}
'''
logger = logging.getLogger('mlflow.store')
result = {'answer':None, 'source':None, 'output_metadata':None}
resultQAErr = {'answer':'qa_chain is not initalized!', 'source':'MLFlow Model', 'output_metadata':None}
resultRetrErr = {'answer':'Retriever is not initalized!', 'source':'MLFlow Model', 'output_metadata':None}
if self._retriever is None:
logger.info('retriever is not initialized!')
return resultRetrErr
if self._qa_chain is None:
logger.info('qa_chain is not initialized!')
return resultQAErr
if isinstance(inputs, dict):
question = inputs['questions'][0]
filter = {}
if 'search_kwargs' in inputs:
filter = inputs['search_kwargs']
else:
question = inputs.iloc[0][0]
filter={}
filter['k']=6 #num documents to look at for response
if 'filter' in inputs:
filter['filter'] = inputs.iloc[0][1]
filter['fetch_k']=30 #num of documents to get before applying the filter.
logger.info(question)
logger.info(filter)
#get relevant documents
#inject the filter during every predict.
self._retriever.search_kwargs = filter #{"k": 6, "filter":filterdict, "fetch_k":20}
doc = self._qa_chain({'query':question})
logger.info(doc)
result['answer'] = doc['result']
result['source'] = ','.join([ src.metadata['path'] for src in doc['source_documents']])
return result