forked from humanlayer/humanlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-human-review-sync.ts
More file actions
80 lines (72 loc) · 2.01 KB
/
02-human-review-sync.ts
File metadata and controls
80 lines (72 loc) · 2.01 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
import { HumanLayer, ResponseOption } from "@humanlayer/sdk";
import { config } from "dotenv";
import {
Classification,
classificationValues,
ClassifiedEmail,
classifyEmail,
logEmails,
twoEmailsShuffled,
} from "./common";
config(); // Load environment variables
const hl = new HumanLayer({
verbose: true,
runId: "email-classifier",
contactChannel: {
slack: {
channel_or_user_id: "",
context_about_channel_or_user: "",
experimental_slack_blocks: true,
},
},
});
async function main() {
try {
console.log("\nClassifying emails...\n");
const results: ClassifiedEmail[] = [];
for (const email of twoEmailsShuffled) {
const classification = await classifyEmail(email);
console.log(
`Classification for "${email.subject}" was ${classification}, checking with human`,
);
const { subject, body, to, from } = email;
const remainingOptions = classificationValues.filter(
(c) => c !== classification,
);
const responseOptions: ResponseOption[] = remainingOptions.map((c) => ({
name: c,
title: c,
description: `Classify as ${c}`,
prompt_fill: `manual classify: ${c}`,
interactive: false,
}));
// fetch human review as labels are processing
const humanReview = await hl.fetchHumanApproval({
spec: {
fn: "classifyEmail",
kwargs: { to, from, subject, body, classification },
reject_options: responseOptions,
},
});
const humanClassification = humanReview.approved
? classification
: (humanReview.reject_option_name as Classification | null | undefined);
results.push({
...email,
classification,
hasHumanReview: true,
humanComment: humanReview.comment,
humanClassification,
});
}
logEmails(results);
} catch (error) {
console.error("Error:", error);
}
}
main()
.then(console.log)
.catch((e) => {
console.error(e);
process.exit(1);
});