Skip to content

Commit b330247

Browse files
Merge pull request meta-llama#16 from meta-llama/lg_inference_update
LG 1B and 11B vision inference update
2 parents f6d4910 + 51b6bc8 commit b330247

File tree

6 files changed

+582
-131
lines changed

6 files changed

+582
-131
lines changed

recipes/responsible_ai/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The [Purple Llama](https://github.com/meta-llama/PurpleLlama/) project provides
44

55
| Tool/Model | Description | Get Started
66
|---|---|---|
7-
[Llama Guard](https://llama.meta.com/docs/model-cards-and-prompt-formats/llama-guard-3) | Provide guardrailing on inputs and outputs | [Inference](./llama_guard/inference.py), [Finetuning](./llama_guard/llama_guard_customization_via_prompting_and_fine_tuning.ipynb)
7+
[Llama Guard](https://llama.meta.com/docs/model-cards-and-prompt-formats/llama-guard-3) | Provide guardrailing on inputs and outputs | [Inference](./llama_guard/llama_guard_text_and_vision_inference.ipynb), [Finetuning](./llama_guard/llama_guard_customization_via_prompting_and_fine_tuning.ipynb)
88
[Prompt Guard](https://llama.meta.com/docs/model-cards-and-prompt-formats/prompt-guard) | Model to safeguards against jailbreak attempts and embedded prompt injections | [Notebook](./prompt_guard/prompt_guard_tutorial.ipynb)
99
[Code Shield](https://github.com/meta-llama/PurpleLlama/tree/main/CodeShield) | Tool to safeguard against insecure code generated by the LLM | [Notebook](https://github.com/meta-llama/PurpleLlama/blob/main/CodeShield/notebook/CodeShieldUsageDemo.ipynb)
1010

recipes/responsible_ai/llama_guard/README.md

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,12 @@
22
<!-- markdown-link-check-disable -->
33
Meta Llama Guard is a language model that provides input and output guardrails for LLM inference. For more details and model cards, please visit the [PurpleLlama](https://github.com/meta-llama/PurpleLlama) repository.
44

5-
This folder contains an example file to run inference with a locally hosted model, either using the Hugging Face Hub or a local path.
5+
This [notebook](llama_guard_text_and_vision_inference.ipynb) shows how to load the models with the transformers library and how to customize the categories.
66

77
## Requirements
8-
1. Access to Llama guard model weights on Hugging Face. To get access, follow the steps described [here](https://github.com/facebookresearch/PurpleLlama/tree/main/Llama-Guard#download)
9-
2. Llama recipes package and it's dependencies [installed](https://github.com/meta-llama/llama-recipes?tab=readme-ov-file#installing)
10-
11-
12-
## Llama Guard inference script
13-
For testing, you can add User or User/Agent interactions into the prompts list and the run the script to verify the results. When the conversation has one or more Agent responses, it's considered of type agent.
14-
15-
16-
```
17-
prompts: List[Tuple[List[str], AgentType]] = [
18-
(["<Sample user prompt>"], AgentType.USER),
19-
20-
(["<Sample user prompt>",
21-
"<Sample agent response>"], AgentType.AGENT),
22-
23-
(["<Sample user prompt>",
24-
"<Sample agent response>",
25-
"<Sample user reply>",
26-
"<Sample agent response>",], AgentType.AGENT),
27-
28-
]
29-
```
30-
The complete prompt is built with the `build_custom_prompt` function, defined in [prompt_format.py](../../../src/llama_recipes/inference/prompt_format_utils.py). The file contains the default Meta Llama Guard categories. These categories can adjusted and new ones can be added, as described in the [research paper](https://ai.meta.com/research/publications/llama-guard-llm-based-input-output-safeguard-for-human-ai-conversations/), on section 4.5 Studying the adaptability of the model.
31-
<!-- markdown-link-check-enable -->
32-
33-
To run the samples, with all the dependencies installed, execute this command:
34-
35-
`python recipes/responsible_ai/llama_guard/inference.py`
36-
37-
This is the output:
38-
39-
```
40-
['<Sample user prompt>']
41-
> safe
42-
43-
==================================
44-
45-
['<Sample user prompt>', '<Sample agent response>']
46-
> safe
47-
48-
==================================
49-
50-
['<Sample user prompt>', '<Sample agent response>', '<Sample user reply>', '<Sample agent response>']
51-
> safe
52-
53-
==================================
54-
```
55-
56-
To run it with a local model, you can use the `model_id` param in the inference script:
57-
58-
`python recipes/responsible_ai/llama_guard/inference.py --model_id=/home/ubuntu/models/llama3/Llama-Guard-3-8B/ --llama_guard_version=LLAMA_GUARD_3`
59-
60-
Note: Make sure to also add the llama_guard_version; by default it uses LLAMA_GUARD_3
8+
1. Access to Llama guard model weights on Hugging Face. To get access, follow the steps described in the top of the model card in [Hugging Face](https://huggingface.co/meta-llama/Llama-Guard-3-1B)
9+
2. Llama recipes package and its dependencies [installed](https://github.com/meta-llama/llama-recipes?tab=readme-ov-file#installing)
10+
3. Pillow package installed
6111

6212
## Inference Safety Checker
6313
When running the regular inference script with prompts, Meta Llama Guard will be used as a safety checker on the user prompt and the model output. If both are safe, the result will be shown, else a message with the error will be shown, with the word unsafe and a comma separated list of categories infringed. Meta Llama Guard is always loaded quantized using Hugging Face Transformers library with bitsandbytes.
@@ -66,7 +16,7 @@ In this case, the default categories are applied by the tokenizer, using the `ap
6616

6717
Use this command for testing with a quantized Llama model, modifying the values accordingly:
6818

69-
`python examples/inference.py --model_name <path_to_regular_llama_model> --prompt_file <path_to_prompt_file> --quantization 8bit --enable_llamaguard_content_safety`
19+
`python inference.py --model_name <path_to_regular_llama_model> --prompt_file <path_to_prompt_file> --enable_llamaguard_content_safety`
7020

7121
## Llama Guard 3 Finetuning & Customization
7222
The safety categories in Llama Guard 3 can be tuned for specific application needs. Existing categories can be removed and new categories can be added to the taxonomy. The [Llama Guard Customization](./llama_guard_customization_via_prompting_and_fine_tuning.ipynb) notebook walks through the process.

recipes/responsible_ai/llama_guard/inference.py

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)