Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve actions samples random selection #799

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@
* @returns A string containing formatted examples of conversations.
*/
export const composeActionExamples = (actionsData: Action[], count: number) => {
const actionExamples: ActionExample[][] = actionsData
.sort(() => 0.5 - Math.random())
.map((action: Action) =>
action.examples.sort(() => 0.5 - Math.random()).slice(0, 5)
)
.flat()
.slice(0, count);
const data: ActionExample[][][] = actionsData.map((action: Action) => [
...action.examples,
]);

const actionExamples: ActionExample[][] = [];
const length = data.length;
for (let i = 0; i < count && length; i++) {
const actionId = i % length;
const examples = data[actionId];
if (examples.length) {
const rand = ~~(Math.random() * examples.length);
actionExamples[i] = examples.splice(rand, 1)[0];
} else {
i--;
}

if (examples.length == 0) {
data.splice(actionId, 1);
length--;

Check failure on line 30 in packages/core/src/actions.ts

View workflow job for this annotation

GitHub Actions / check

'length' is constant
}
}

const formattedExamples = actionExamples.map((example) => {
const exampleNames = Array.from({ length: 5 }, () =>
Expand Down
Loading