microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/1873-devcontainer

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/evals/Modules/ModerationRunner.psm1

158lines · modecode

1# Copyright (c) Microsoft Corporation.
2# SPDX-License-Identifier: MIT
3# ModerationRunner.psm1
4# Purpose: Helpers for content moderation batch processing and orchestration
5#Requires -Version 7.0
6
7<#
8.SYNOPSIS
9 Builds a JSON-lines input file from a batch of records.
10
11.DESCRIPTION
12 Accepts an array of hashtables with 'id' and 'text' keys and writes them
13 as JSON-lines to a temporary file. Returns the file path.
14
15.PARAMETER Records
16 Array of hashtables, each with 'id' and 'text' keys.
17
18.PARAMETER OutFile
19 Path to the output JSON-lines file. Defaults to a temp file.
20
21.OUTPUTS
22 System.String - Path to the JSON-lines file.
23
24.EXAMPLE
25 $records = @(
26 @{ id = 'rec1'; text = 'Hello world' },
27 @{ id = 'rec2'; text = 'Test content' }
28 )
29 $inputFile = New-ModerationInputFile -Records $records
30#>
31function New-ModerationInputFile {
32 [CmdletBinding()]
33 [OutputType([string])]
34 param(
35 [Parameter(Mandatory = $true)]
36 [hashtable[]]$Records,
37
38 [Parameter(Mandatory = $false)]
39 [string]$OutFile
40 )
41
42 if (-not $OutFile) {
43 $OutFile = [System.IO.Path]::GetTempFileName()
44 }
45
46 $jsonLines = $Records | ForEach-Object {
47 ConvertTo-Json $_ -Compress -Depth 1
48 }
49 $jsonLines | Set-Content -Path $OutFile -Encoding utf8NoBOM
50
51 Write-Verbose "Wrote $($Records.Count) records to $OutFile"
52 return $OutFile
53}
54
55<#
56.SYNOPSIS
57 Reads files from a file list and builds moderation records.
58
59.DESCRIPTION
60 Accepts an array of file paths, reads each file, and constructs a hashtable
61 record with 'id' (relative path) and 'text' (file content).
62
63.PARAMETER FileList
64 Array of file paths to read.
65
66.PARAMETER RepoRoot
67 Repository root for relativizing file paths. Defaults to the current directory.
68
69.OUTPUTS
70 System.Collections.Hashtable[] - Array of records with id and text keys.
71
72.EXAMPLE
73 $files = Get-ChildItem *.md
74 $records = ConvertTo-ModerationRecords -FileList $files.FullName
75#>
76function ConvertTo-ModerationRecords {
77 [CmdletBinding()]
78 [OutputType([hashtable[]])]
79 param(
80 [Parameter(Mandatory = $true)]
81 [string[]]$FileList,
82
83 [Parameter(Mandatory = $false)]
84 [string]$RepoRoot = $PWD
85 )
86
87 $records = @()
88 foreach ($filePath in $FileList) {
89 if (-not (Test-Path -LiteralPath $filePath)) {
90 Write-Warning "File not found: $filePath"
91 continue
92 }
93 $relativePath = (Resolve-Path -LiteralPath $filePath -Relative -RelativeBasePath $RepoRoot).TrimStart('.', '\', '/')
94 $content = Get-Content -LiteralPath $filePath -Raw -Encoding utf8
95 $records += @{
96 id = $relativePath
97 text = $content
98 }
99 }
100 Write-Verbose "Built $($records.Count) records from $($FileList.Count) files"
101 return $records
102}
103
104<#
105.SYNOPSIS
106 Parses moderate.py JSON output and surfaces structured error messages.
107
108.DESCRIPTION
109 Reads the JSON output from moderate.py, extracts flagged records, and emits
110 GitHub Actions error annotations for each flagged item.
111
112.PARAMETER OutputPath
113 Path to the moderate.py JSON output file.
114
115.OUTPUTS
116 System.Boolean - Returns $true if any records were flagged, $false otherwise.
117
118.EXAMPLE
119 if (Test-ModerationOutput -OutputPath logs/moderation-corpus.json) {
120 Write-Error "Content moderation failed"
121 }
122#>
123function Test-ModerationOutput {
124 [CmdletBinding()]
125 [OutputType([bool])]
126 param(
127 [Parameter(Mandatory = $true)]
128 [string]$OutputPath
129 )
130
131 if (-not (Test-Path $OutputPath)) {
132 Write-Error "Moderation output file not found: $OutputPath"
133 return $true
134 }
135
136 $output = Get-Content -Path $OutputPath -Raw | ConvertFrom-Json
137 $flaggedCount = $output.summary.flaggedCount
138
139 if ($flaggedCount -eq 0) {
140 Write-Verbose "Content moderation passed: all $($output.summary.total) records clean"
141 return $false
142 }
143
144 Write-Warning "Content moderation failed: $flaggedCount/$($output.summary.total) records flagged"
145 foreach ($record in $output.records) {
146 if ($record.flagged) {
147 $labels = $record.flaggedLabels -join ', '
148 Write-Host "::error file=$($record.id)::Content moderation flag: $labels"
149 }
150 }
151 return $true
152}
153
154Export-ModuleMember -Function @(
155 'New-ModerationInputFile',
156 'ConvertTo-ModerationRecords',
157 'Test-ModerationOutput'
158)
159