-
Notifications
You must be signed in to change notification settings - Fork 3
/
jury.ts
134 lines (125 loc) · 2.93 KB
/
jury.ts
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type { AgentOptions, ModelMessage } from "@hyv/core";
import { Agent } from "@hyv/core";
import { createInstruction, GPTModelAdapter } from "@hyv/openai";
import type { FileContentWithPath } from "@hyv/utils";
import { minify } from "@hyv/utils";
const authorInstruction = createInstruction(
"Author, Competitor",
minify`
Do tasks. Respect rules and rating criteria.
Think deeply, reason your thoughts, decide based on your reasoning.
`,
{
thoughts: "deep thoughts",
reason: "critical reasoning",
decision: "detailed decision",
story: {
name: "name of story",
content: "the story …",
},
}
);
const juryInstruction = createInstruction(
"Competition Jury",
minify`
Do tasks. Respect rules and rating criteria.
Think deeply, reason your thoughts, decide based on your reasoning.
`,
{
thoughts: "deep thoughts",
reason: "critical reasoning",
decision: "detailed decision",
winner: "name of story",
}
);
/**
* Gets the word count for a text
* @param text
*/
function getWordCount(text: string) {
return text.split(" ").filter(Boolean).length;
}
const finalJury = new Agent(
new GPTModelAdapter({
model: "gpt-4",
maxTokens: 1024,
systemInstruction: juryInstruction,
}),
{
verbosity: 1,
}
);
async function createAndAssign<T>(
task: ModelMessage,
systemInstruction: { systemInstruction: string; template: string; format: "markdown" | "json" },
options: AgentOptions = {}
) {
return (await new Agent(
new GPTModelAdapter({
model: "gpt-4",
maxTokens: 1024,
temperature: 0.9,
systemInstruction,
}),
{
verbosity: 1,
...options,
}
).assign(task)) as { id: string; message: ModelMessage & T };
}
const rules = ["minimum 300 words long", "must be unique and original"];
const ratingCriteria = ["unique story", "detail", "engaging"];
try {
const stories = (await Promise.all(
Array.from(
{ length: 3 },
async () =>
(
await createAndAssign(
{ task: "Write a UNIQUE story for a competition", rules, ratingCriteria },
authorInstruction,
{
async after(
message: ModelMessage & { story: { name: string; content: string } }
): Promise<
ModelMessage & {
story: { name: string; content: string; wordCount: number };
}
> {
return {
...message,
story: {
...message.story,
wordCount: getWordCount(message.story.content),
},
};
},
}
)
).message.story
)
)) as FileContentWithPath[];
const votes = (await Promise.all(
Array.from(
{ length: 3 },
async () =>
(
await createAndAssign(
{
task: "Read the stories and pick a winner",
rules,
ratingCriteria,
stories,
},
juryInstruction
)
).message.winner
)
)) as FileContentWithPath[];
await finalJury.assign({
task: "Count the votes and determine the winner",
votes,
});
} catch (error) {
console.error("Error:", error);
}