From 8e808282c95abdc02d998e7d3139e16a9f6a9cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=8F=E4=BA=8E=E6=89=93=E7=90=86?= <1316001950@qq.com> Date: Mon, 26 Aug 2024 18:47:31 +0800 Subject: [PATCH] Faster loading:Load sample text in parallel --- src/agent/prompter.js | 11 +++++++---- src/utils/examples.js | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/agent/prompter.js b/src/agent/prompter.js index 0960ad0..6bc327c 100644 --- a/src/agent/prompter.js +++ b/src/agent/prompter.js @@ -93,12 +93,15 @@ export class Prompter { } async initExamples() { - console.log('Loading examples...') + // Using Promise.all to implement concurrent processing + // Create Examples instances this.convo_examples = new Examples(this.embedding_model); - await this.convo_examples.load(this.profile.conversation_examples); this.coding_examples = new Examples(this.embedding_model); - await this.coding_examples.load(this.profile.coding_examples); - console.log('Examples loaded.'); + // Use Promise.all to load examples concurrently + await Promise.all([ + this.convo_examples.load(this.profile.conversation_examples), + this.coding_examples.load(this.profile.coding_examples), + ]); } async replaceStrings(prompt, messages, examples=null, prev_memory=null, to_summarize=[], last_goals=null) { diff --git a/src/utils/examples.js b/src/utils/examples.js index 3bba0a0..5307642 100644 --- a/src/utils/examples.js +++ b/src/utils/examples.js @@ -32,10 +32,11 @@ export class Examples { async load(examples) { this.examples = examples; if (this.model !== null) { - for (let example of this.examples) { + const embeddingPromises = this.examples.map(async (example) => { let turn_text = this.turnsToText(example); this.embeddings[turn_text] = await this.model.embed(turn_text); - } + }); + await Promise.all(embeddingPromises); } }