File size: 12,419 Bytes
0d3ef1c fa1167f f3fb451 ecc6f18 10746fa f3fb451 10746fa fa1167f 0d3ef1c fa1167f 0d3ef1c fa1167f |
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import json
class EndpointHandler():
def __init__(self, path="ceiteach/chart-no-pretrain-llama31-unsloth"):
base_model_name = "unsloth/Meta-Llama-3.1-8B-bnb-4bit"
model_name = "ceiteach/chart-no-pretrain-llama31-unsloth"
model = AutoModelForCausalLM.from_pretrained(base_model_name, use_safetensors=True, load_in_4bit=True, device_map="auto")
model.load_adapter(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name, max_model_length=4096)
self.model = model
self.tokenizer = tokenizer
def __call__(self, data):
model = self.model
tokenizer = self.tokenizer
date = "2024-08-19"
previous_metric = "CPFORD"
previous_chart_type = "line"
instruction = """
You are responding to an athlete who wants to view their data in a chart.
Today's date is 2024-08-18 (YYYY-MM-DD).
You have access to the following functions. Your response must use at least one of these functions.
Functions: "
- {"name":"draw","description":"Use this tool to add or update a dataset in the chart.","parameters":{"type":"OBJECT","description":"The parameters for the draw function.","properties":{"chartType":{"type":"STRING","description":"The type of chart to draw. It can be either 'line' or 'bar'. If unspecified use the previous chart type which will be provided in the prompt."},"metric":{"type":"STRING","description":"The metric to be charted. If unspecified use the previous metric which will be provided in the prompt."}},"required":["chartType","metric"]}}
- {"name":"updateDateRange","description":"Use this tool to update the date range of the chart.","parameters":{"type":"OBJECT","description":"The parameters for the updateDateRange function.","properties":{"startDate":{"type":"STRING","description":"The start date of the chart. If unspecified use the previous start date which will be provided in the prompt."}},"required":["startDate"]}}
- {"name":"removeDataset","description":"Use this tool to remove a dataset from the chart.","parameters":{"type":"OBJECT","description":"The parameters for the removeDataset function.","properties":{"metric":{"type":"STRING","description":"The metric to be removed. If unspecified use the previous metric which will be provided in the prompt."}},"required":["metric"]}}
- {"name":"clearChart","description":"Use this tool to clear the chart.","parameters":{"type":"OBJECT","description":"The parameters for the clearChart function.","properties":{}}}
"
"Metrics" is a map of metric names to metric ids. The format is '<metric name>': '<metric id>'.
Metrics: "
'Accelerations': accelerationCount,
'Decelerations': decelerationCount,
'Total Distance': totalDistance,
'High Speed Distance': highSpeedDistance,
'Total Sprints': totalSprints,
'Max Heart Rate': maxHeartRate,
'Acceleration Distance': accelerationDistance,
'Deceleration Distance': decelerationDistance,
'Total Jumps': totalJumps,
'Muscle Soreness': muscleSoreness,
'Fatigue': fatigue,
'Stress': stress,
'Sleep Duration': sleepDuration,
'Deep Sleep Duration': deepSleepDuration,
'REM Sleep Duration': remSleepDuration,
'Light Sleep Duration': lightSleepDuration,
'Awake Duration': awakeDuration,
'Minutes Played': minutesPlayed,
'Goals': goals,
'Assists': assists,
'Shots': shots,
'Anti Mullerian Hormone': AMH2,
'Arachidic (20:0)': ARA20,
'Behenic (22:0)': BEHE,
'Total Carotene': CAROTENE,
'cis-Monounsaturated Fatty Acids': CISMONO,
'Cortisol': CORT,
'17 Hydroxyprogesterone': CP17HYD,
'a-Linolenic (ALA) 18:3 n3': CP183AL,
'Linoleic (LA) 18:2n6': CP186LI,
'Ferritin (HS)': CP1FERR,
'Omega-3 Index (HS)': CP1OMEG,
'Testosterone (HS)': CP1TEST,
'Vitamin A': CP1VITA,
'Vitamin E ': CP1VITAM,
'Eicosapentaenoic (EPA) 20:5 n3': CP203EPT,
'Arachidonic (AA) 20:4n6': CP206ARA,
'Docosahexaen (DHA) 22:6 n3': CP223DHC,
'25-Hydroxy Vitamin D3': CP25HDV3,
'25-Hydroxy Vitamin D': CP25HVD,
'25-Hydroxy Vitamin D2': CP25HVD2,
'Homocysteine v2': CP2HOMO,
'Vitamin B2 - SpectraCell': CP2VITA,
'Vitamin B3 - SpectraCell': CP3VITA,
'Vitamin B12 - SpectraCell': CP4VITA,
'Vitamin B6 - SpectraCell': CP6VITA,
'AA:EPA Ratio': CPAAEPA,
'Active B12': CPACTIV,
'Active Vitamin B12 (HS)': CPACTIVE,
'Albumin': CPALBUM,
'Albumin (SD1)': CPALBUMI,
'Alpha-Carotene': CPALCAR,
'Albumin/Globulin ratio': CPALGLR,
'Alkaline Phosphatase': CPALPHO,
'Alanine Transaminase': CPALTRA,
'Amylase': CPAMYLA,
'Anti Inflammatory Index': CPANTII,
'Anti-mullerian hormone': CPANTIM,
'Apollpoprotein B, P': CPAPOLL,
'Activated Partial Thromboplastin Time Ratio': CPAPTTR,
'Arginine': CPARGIN,
'Arginine (HS)': CPARGINI,
'Aspartate Aminotransferase': CPASAM,
'Asparagine - SpectraCell': CPASPAR,
'Asparagine (HS)': CPASPARA,
'AST (SD1)': CPASTSD,
'Atypical Lymphocyte': CPATLYMP,
'Alpha-Tocopherol (Vit. E)': CPATVTE,
'Avg Mins/Game': CPAVGMI,
'Active B12 (TH)': CPB12,
'Basophil, Absolute': CPBASOA,
'Basophil, %': CPBASOP,
'Basophil, % only': CPBASOPH,
'Bioavailable Testosterone': CPBATT,
'B Cells, Absolute': CPBCEAB,
'B-Cell %': CPBCELL,
'B-Cell Absolute Count US': CPBCELLA,
'Coenzyme Q10 - SpectraCell': CPBCOEN,
'Bermuda grass': CPBERGR,
'Beta Globulin': CPBETAG,
'Blood Glucose - Random': CPBGLR,
'Serum Bicarbonate': CPBICAR,
'Bilirubin, Indirect': CPBILIRU,
'Biotin - SpectraCell': CPBIOTI,
'Birch pollen': CPBIRPOL,
'Blood Glucose - Fasting': CPBLGF,
'Diastolic Blood Pressure': CPBPDIA,
'Systolic Blood Pressure': CPBPSYS,
'Branched-chain amino acids BCAA (HS)': CPBRANC,
'Beta-Carotene': CPBTCAR,
'Blood Urea Nitrogen/Creatinine Ratio': CPBUNCRR,
'BUN/Urea': CPBUREA,
'Calcium, ionized': CPCALCI,
'Calcium - SpectraCell': CPCALCIU,
'Calcium': CPCALCM,
'Calcium Osmolality': CPCALCO,
'Carnitine - SpectraCell': CPCARNI,
'Carnitine (HS)': CPCARNIT,
'Cat dander': CPCATDAN,
'Calcium for PTH, Intact': CPCCALC,
'Adjusted Calcium': CPCCALCM,
'Copper - SpectraCell': CPCCOPP,
'Choline - SpectraCell': CPCHOLI,
'Chloride': CPCHOLR,
'Chromium': CPCHROM,
'Citrulline (HS)': CPCITRU,
'CK (SD1)': CPCKSD1,
'Copper': CPCOPP,
'Copper (serum) (HS)': CPCOPPE,
'Cortisol (HS)': CPCORTI,
'Creatinine': CPCREAT,
'Creatine Kinase': CPCREATK,
'C Reactive Protein ': CPCRREPR,
'DHEA-Sulfate Serum': CPDHEAS,
'Direct Bilirubin': CPDIRBIL,
'Vitamin D3 - SpectraCell': CPDVITA,
'Vitamin E (Alpha Tocopherol)': CPEALPH,
'Vitamin E (Gamma Tocopherol)': CPEGAMM,
'Erythropoietin (EPO)': CPERYTH,
'Oestradiol': CPESTR,
'Vitamin C': CPEVITA,
'Folic Acid Red Blood Cell': CPFACRBC,
'Ferritin': CPFERRI,
'Fibrinogen': CPFIBRI,
'Follicle-Stimulating Hormone': CPFLSH,
'Folic Acid': CPFOLAC,
'Free Triiodothyronine': CPFRTRII,
'Free Testosterone': CPFRTTTE,
'Fructose Sensitivity': CPFRUCT,
'FSH': CPFSH,
'Free Thyroxine': CPFTHYR,
'Gamma Globulin': CPGAMMAG,
'EGFR Non-African American': CPGFRNAA,
'GRA': CPGGGGG,
'Gamma-Glutamyl Transpeptidase': CPGGLTRA,
'Glomerular Filtration Rate': CPGLFR,
'EGFR African American': CPGLFRAA,
'Globulins': CPGLOBU,
'Glutathione, Total': CPGLU,
'Glucose': CPGLUC,
'Glutathione - Red Cell': CPGLURC,
'Glutamine': CPGLUTA,
'Glutamic acid (HS)': CPHGLUT,
'Histidine (HS)': CPHISTI,
'Homocysteine': CPHOMOL,
'hs-CRP': CPHSCRP,
'HS-Omega 3': CPHSOM3,
'Omega-6 Fatty Acids': CPHSOME,
'Insulin': CPINSUL,
'Iodine': CPIODIN,
'Inorganic Phosphorus': CPIPHOSP,
'Iron (TH)': CPIRONT,
'Iron Saturation': CPIRSAT,
'Isoleucine': CPISOLE,
'Glutamine (HS)': CPKGLUT,
'Leucine': CPLEUCI,
'Magnesium': CPMAGM,
'Magnesium (erythrocytes) (HS)': CPMAGNE,
'Magnesium, RBC (BRF)': CPMAGNES,
'Manganese': CPMANGA,
'Manganese - SpectraCell': CPMANGAN,
'Mean Corpuscular Haemoglobin': CPMCH,
'Mercury': CPMERCU,
'Methionine': CPMETHI,
'Magnesium': CPMMAGN,
'Neutrophil': CPNEUTR,
'Non High-Density Lipoprotein Cholesterol': CPNHDLCH,
'NKCA %': CPNKCA,
'NKCA Per Cell': CPNKCAP,
'NK Cell %': CPNKCEL,
'NK Cell Absolute': CPNKCELL,
'Natural Killer Cells, Absolute': CPNKNCA,
'Nucleated RBC': CPNRBC,
'Zinc': CPNZINC,
'Oleic Acid': CPOLEICA,
'Omega 6:3': CPOMEGA,
'Omega6 : Omega3 ratio': CPOMG6_3,
'Prolactin': CPPROLN,
'Prostate Specific Antigen': CPPROSAG,
'Vitamin E': CPQQVIT,
'Vitamin B1': CPQVITA,
'Red Blood Cell Count': CPRBCC,
'Vitamin B5': CPRFVIT,
'Season Games Played': CPSEASO,
'SEGS': CPSEGS,
'Selenium': CPSELEN,
'Selenium (erythrocytes) (HS)': CPSELENI,
'Serine': CPSERI,
'Serine - SpectraCell': CPSERIN,
'Ferritin (TH)': CPSFERR,
'Serum Folate': CPSFOLA,
'Serum Glucose - Fasting': CPSGLF,
'Serum Glucose - Random': CPSGLR,
'Sex Hormone Binding Globulin': CPSHBG,
'Serum Inorganic Phosphate': CPSINP,
'Serum Iron': CPSIRON,
'Serum Lutein': CPSLUTE,
'Sodium': CPSODI,
'Vitamin B1': CPSPECT,
'Selenium': CPSSELE,
'Serum Testosterone': CPSTTTE,
'Vitamin A': CPSVITA,
'Vitamin D': CPSVITAM,
'T3 Uptake': CPT3UPT,
'Taurine': CPTAUR,
'Taurine (HS)': CPTAURI,
'T-Cells %': CPTCELL,
'T-Cell Absolute': CPTCELLA,
'T:C Ratio': CPTCRAT,
'T:C Ratio (HS)': CPTCRATI,
'Total Daily Cortisol': CPTDCORT,
'Testosterone Total Female': CPTESTO,
'Free Testosterone Female': CPTESTOS,
'Threonine': CPTHREO,
'Total Cholesterol': CPTOTCHO,
'Total Testosterone': CPTOTTTE,
'Transferrin': CPTRANSF,
'Triglycerides': CPTRYG,
'Tryptophan (HS)': CPTRYPT,
'Thyroid Stimulating Hormone': CPTSHBR,
'Testosterone': CPTTTE,
'Total Vitamin D': CPTVD,
'Typical Cycle Length': CPTYPIC,
'Tryptophan': CPTYPTO,
'Tyrosine (HS)': CPTYROS,
'Vitamin A': CPVITAM,
'Vitamin B1': CPVITB1,
'Vitamin B12': CPVITB12,
'Vitamin B2': CPVITB2,
'Vitamin B6': CPVITB6,
'Vitamin C': CPVITC,
'VLDL Cholesterol Cal': CPVLDLC,
'Vitamin E': CPVVITA,
'White Blood Cell Count': CPWBCC,
'Vitamin D 1,25': CPWVITA,
'Zinc': CPZINC,
'LDL/HDL Ratio': LDL - HDL,
'Omega 3 Fatty Acids': O3FA,
'Omega 3 Index': O3INDEX
"
Rules to follow: "
- Use the "draw" tool to add or update a dataset in the chart.
- Use the "updateDateRange" tool to update the date range of the chart.
- Use the "removeDataset" tool to remove a dataset from the chart.
- Use the "clearChart" tool to clear the chart.
- If the user wants you to perform multiple actions, you should return multiple function calls. For example, if the user says "Draw A and B", you should return two draw function calls.
- You can use the same tool multiple times in a response.
- The only values supported for "chartType" are "line" and "bar".
- If the user provides a metric name, you should return the metric id. If the user does not provide a metric name, you should return the previous metric id.
- Assume metric names if they are misspelled or shortened. For example, "fourth data" should be assumed to be "fort data".
- Never return a metric id that is not in "Metrics" and return the metric id exactly as it is formatted in "Metrics".
- If the user says something like "for the last 2 months", you must calculate the start date based on today's date.
- Only use the "removeDataset", "removeHighlight" or "clearChart" tools if the user has explicitly asked for it.
- "removeDataset" will remove a particular dataset. 'clearChart' will completely clear the chart. If the user mentions an athlete or metric, you should assume they want to remove a dataset and not clear the chart.
"
If a new chart type or metric is not provided, use the previous values as we can assume the user wants to continue with the same options.
- The previous chart type is line.
- The previous metric is CPFORD.
"""
user_input = data.pop("inputs", data)
alpaca_prompt = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
### Instruction:
{instruction}
### Input:
{user_input}
### Response:
"""
prompt_tokenized = tokenizer(alpaca_prompt, return_tensors="pt").to("cuda")
model.eval()
with torch.no_grad():
response = tokenizer.decode(model.generate(**prompt_tokenized, max_new_tokens=128, temperature=0.01)[0], skip_special_tokens=True)
response_content = response.split("### Response:")[1].strip()
try:
function_calls = json.loads(response_content);
return { "response": function_calls }
except json.JSONDecodeError as e:
print("Failed to parse function calls from response")
return { "response": "Unable to generate response"}
|