Phi-3 is a set of large language models published by Microsoft. Models are instruction tuned, and range in size from 3 billion to 14 billion parameters. See the model card below for benchmarks, data sources, and intended use cases.
pip install -U -q keras-hub
pip install -U -q keras
Jax, TensorFlow, and Torch come preinstalled in Kaggle Notebooks. For instruction on installing them in another environment see the Keras Getting Started page.
Presets
The following model checkpoints are provided by the Keras team. Full code examples for each are available below.
Preset name
Parameters
Description
phi3_mini_4k_instruct_en
3.82B
3B model with 4K max context
phi3_mini_128k_instruct_en
3.82B
3B model with 128K max context
Prompts
Phi-3 models are instruction tuned on turn by turn conversations and should be prompted with examples that precisely match the training data. Specifically, you must alternate user and assistant turns that begin and end with special tokens. New lines do matter. See the following for an example:
python
1prompt ="""<|user|>
2Hello!<|end|>
3<|assistant|>
4Hello! How are you?<|end|>
5<|user|>
6I'm great. Could you help me with a task?<|end|>
7"""
Example Usage
pip install -U -q keras-hub
python
1import keras
2import keras_hub
3import numpy as np
Use generate() to do text generation.
python
1phi3_lm = keras_hub.models.Phi3CausalLM.from_preset("phi3_mini_4k_instruct_en")2phi3_lm.generate("<|user|>\nHow to explain Internet for a medieval knight?<|end|>\n<|assistant|>", max_length=500)34# Generate with batched prompts.5phi3_lm.generate([6"<|user|>\nWhat is Keras?<|end|>\n<|assistant|>",7"<|user|>\nGive me your best brownie recipe.<|end|>\n<|assistant|>",8], max_length=500)
Compile the generate() function with a custom sampler.
python
1phi3_lm = keras_hub.models.Phi3CausalLM.from_preset("phi3_mini_4k_instruct_en")2phi3_lm.compile(sampler="greedy")3phi3_lm.generate("<|user|>\nWhat is Keras?<|end|>\n<|assistant|>", max_length=30)45phi3_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))6phi3_lm.generate("<|user|>\nWhat is Keras?<|end|>\n<|assistant|>", max_length=30)
Use generate() without preprocessing.
python
1prompt ={2"token_ids": np.array([[306,864,304,1827,0,0,0,0,0,0]]*2),3# Use `"padding_mask"` to indicate values that should not be overridden.4"padding_mask": np.array([[1,1,1,1,0,0,0,0,0,0]]*2),5}67phi3_lm = keras_hub.models.Phi3CausalLM.from_preset(8"phi3_mini_4k_instruct_en",9 preprocessor=None,10 dtype="bfloat16"11)12phi3_lm.generate(prompt)
Call fit() on a single batch.
python
1features =["The quick brown fox jumped.","I forgot my homework."]2phi3_lm = keras_hub.models.Phi3CausalLM.from_preset("phi3_mini_4k_instruct_en")3phi3_lm.fit(x=features, batch_size=2)
Example Usage with Hugging Face URI
pip install -U -q keras-hub
python
1import keras
2import keras_hub
3import numpy as np
Use generate() to do text generation.
python
1phi3_lm = keras_hub.models.Phi3CausalLM.from_preset("hf://keras/phi3_mini_4k_instruct_en")2phi3_lm.generate("<|user|>\nHow to explain Internet for a medieval knight?<|end|>\n<|assistant|>", max_length=500)34# Generate with batched prompts.5phi3_lm.generate([6"<|user|>\nWhat is Keras?<|end|>\n<|assistant|>",7"<|user|>\nGive me your best brownie recipe.<|end|>\n<|assistant|>",8], max_length=500)
Compile the generate() function with a custom sampler.
python
1phi3_lm = keras_hub.models.Phi3CausalLM.from_preset("hf://keras/phi3_mini_4k_instruct_en")2phi3_lm.compile(sampler="greedy")3phi3_lm.generate("<|user|>\nWhat is Keras?<|end|>\n<|assistant|>", max_length=30)45phi3_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))6phi3_lm.generate("<|user|>\nWhat is Keras?<|end|>\n<|assistant|>", max_length=30)
Use generate() without preprocessing.
python
1prompt ={2"token_ids": np.array([[306,864,304,1827,0,0,0,0,0,0]]*2),3# Use `"padding_mask"` to indicate values that should not be overridden.4"padding_mask": np.array([[1,1,1,1,0,0,0,0,0,0]]*2),5}67phi3_lm = keras_hub.models.Phi3CausalLM.from_preset(8"hf://keras/phi3_mini_4k_instruct_en",9 preprocessor=None,10 dtype="bfloat16"11)12phi3_lm.generate(prompt)
Call fit() on a single batch.
python
1features =["The quick brown fox jumped.","I forgot my homework."]2phi3_lm = keras_hub.models.Phi3CausalLM.from_preset("hf://keras/phi3_mini_4k_instruct_en")3phi3_lm.fit(x=features, batch_size=2)