-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeidentify-text.ts
More file actions
107 lines (91 loc) · 3.69 KB
/
deidentify-text.ts
File metadata and controls
107 lines (91 loc) · 3.69 KB
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
import {
Credentials,
Env,
LogLevel,
Skyflow,
SkyflowConfig,
VaultConfig,
SkyflowError,
DeidentifyTextRequest,
DeidentifyTextOptions,
TokenFormat,
TokenType,
Transformations,
DetectEntities,
DeidentifyTextResponse
} from 'skyflow-node';
/**
* Skyflow Deidentify Text Example
*
* This example demonstrates how to:
* 1. Configure credentials
* 2. Set up vault configuration
* 3. Create a deidentify text request
* 4. Use all available options for de-identification
* 5. Handle response and errors
*/
async function performDeidentifyText() {
try {
// Step 1: Configure Credentials
const credentials: Credentials = {
path: 'path-to-credentials-json', // Path to credentials file
};
// Step 2: Configure Vault
const primaryVaultConfig: VaultConfig = {
vaultId: '<VAULT_ID>', // Unique vault identifier
clusterId: '<CLUSTER_ID>', // From vault URL
env: Env.PROD, // Deployment environment
credentials: credentials // Authentication method
};
// Step 3: Configure Skyflow Client
const skyflowConfig: SkyflowConfig = {
vaultConfigs: [primaryVaultConfig],
logLevel: LogLevel.INFO, // Recommended to use LogLevel.ERROR in production environment.
};
// Initialize Skyflow Client
const skyflowClient: Skyflow = new Skyflow(skyflowConfig);
// Step 4: Prepare Deidentify Text Request
const deidentifyTextRequest = new DeidentifyTextRequest(
'My SSN is 123-45-6789 and my card is 4111 1111 1111 1111.', // Text to be deidentified
);
// Step 5: Configure DeidentifyTextOptions
const options = new DeidentifyTextOptions();
// setEntities: Specify which entities to deidentify
options.setEntities([DetectEntities.CREDIT_CARD, DetectEntities.SSN]);
// setAllowRegexList: Allowlist regex patterns (entities matching these will not be deidentified)
// optionsText.setAllowRegexList(['<YOUR_REGEX_PATTERN>']);
// setRestrictRegexList: Restrict de-identification to entities matching these regex patterns
// optionsText.setRestrictRegexList(['<YOUR_REGEX_PATTERN>']);
// setTokenFormat: Specify the token format for deidentified entities
const tokenFormat = new TokenFormat();
tokenFormat.setDefault(TokenType.VAULT_TOKEN);
options.setTokenFormat(tokenFormat);
// setTransformations: Specify custom transformations for entities
const transformations = new Transformations();
transformations.setShiftDays({
max: 30, // Maximum shift days
min: 30, // Minimum shift days
entities: [DetectEntities.DOB], // Entities to apply the shift
});
options.setTransformations(transformations);
// Step 6: Call deidentifyText API
const response: DeidentifyTextResponse = await skyflowClient
.detect(primaryVaultConfig.vaultId)
.deidentifyText(deidentifyTextRequest, options);
// Handle Successful Response
console.log('Deidentify Text Response:', response);
} catch (error) {
// Comprehensive Error Handling
if (error instanceof SkyflowError) {
console.error('Skyflow Specific Error:', {
code: error.error?.http_code,
message: error.message,
details: error.error?.details,
});
} else {
console.error('Unexpected Error:', JSON.stringify(error));
}
}
}
// Invoke the deidentify text function
performDeidentifyText();