Skip to content

Commit 184e0bb

Browse files
authored
Merge pull request #799 from dievardump/main
refactor: Improve actions samples random selection
2 parents a0992ff + 712aebb commit 184e0bb

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

packages/core/src/actions.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,27 @@ import { Action, ActionExample } from "./types.ts";
99
* @returns A string containing formatted examples of conversations.
1010
*/
1111
export const composeActionExamples = (actionsData: Action[], count: number) => {
12-
const actionExamples: ActionExample[][] = actionsData
13-
.sort(() => 0.5 - Math.random())
14-
.map((action: Action) =>
15-
action.examples.sort(() => 0.5 - Math.random()).slice(0, 5)
16-
)
17-
.flat()
18-
.slice(0, count);
12+
const data: ActionExample[][][] = actionsData.map((action: Action) => [
13+
...action.examples,
14+
]);
15+
16+
const actionExamples: ActionExample[][] = [];
17+
let length = data.length;
18+
for (let i = 0; i < count && length; i++) {
19+
const actionId = i % length;
20+
const examples = data[actionId];
21+
if (examples.length) {
22+
const rand = ~~(Math.random() * examples.length);
23+
actionExamples[i] = examples.splice(rand, 1)[0];
24+
} else {
25+
i--;
26+
}
27+
28+
if (examples.length == 0) {
29+
data.splice(actionId, 1);
30+
length--;
31+
}
32+
}
1933

2034
const formattedExamples = actionExamples.map((example) => {
2135
const exampleNames = Array.from({ length: 5 }, () =>

0 commit comments

Comments
 (0)