Skip to content

Commit

Permalink
Merge pull request elizaOS#799 from dievardump/main
Browse files Browse the repository at this point in the history
refactor: Improve actions samples random selection
  • Loading branch information
shakkernerd authored Dec 3, 2024
2 parents a0992ff + 712aebb commit 184e0bb
Showing 1 changed file with 21 additions and 7 deletions.
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 @@ import { Action, ActionExample } from "./types.ts";
* @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[][] = [];
let 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--;
}
}

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

0 comments on commit 184e0bb

Please sign in to comment.