Skip to content

Llama Guard your files

Llama-guard3 is a LLM model that specializes in detecting harmful content in text. The script we’re discussing aims at batch applying llama-guard to your files.

By automating this process, you can save time and focus on addressing only the files that need attention.

guard.genai.mjs
// iterate over files and check if they are safe using llama-guard3:8b (https://ollama.com/library/llama-guard3)
for (const file of env.files) {
const { text } = await prompt`${file}`.options({
model: "ollama:llama-guard3:8b",
label: file.filename,
cache: "llama-guard3:8b",
system: [],
})
const safe = /safe/.test(text) && !/unsafe/.test(text)
if (!safe)
console.error(text)
}

Line-by-Line Explanation of the Script 📜

Let’s dive into the GenAI script and understand its components:

// Iterate over each file provided by the environment
for (const file of env.files) {

Here, we loop through each file available in the env.files array, which contains the files you want to check.

// Use a GenAI model to analyze each file for safety
const { text } = await prompt`${file}`.options({
model: "ollama:llama-guard3:8b",
label: file.filename,
cache: "llama-guard3:8b",
system: [],
})

This block uses the GenAI model ollama:llama-guard3:8b to analyze the contents of each file. The prompt function sends the file to the model, and various options are set to specify the model, label the file, and manage cache.

// Determine if the file is considered safe
const safe = /safe/.test(text) && !/unsafe/.test(text)

The script checks if the model’s analysis considers the file safe by searching the response text for the word “safe” and ensuring “unsafe” isn’t present.

// Log and store filenames of unsafe files
if (!safe) {
console.error(text)
}

If a file is found to be unsafe, its details are logged to the console.

Running the Script with GenAIScript CLI 🚀

To run this script, you’ll need to use the GenAIScript CLI. If you haven’t installed it yet, follow the installation guide.

Once installed, execute the script using the following command:

Terminal window
genaiscript run guard **/*.ts

This command will check all the files matching ”*/.ts” and let you know which ones are unsafe.

Happy coding and stay safe! 🛡️