better example promise loading

This commit is contained in:
MaxRobinsonTheGreat 2024-11-25 13:05:02 -06:00
parent 68a2dccb76
commit 768d1da1bb
2 changed files with 21 additions and 20 deletions

View file

@ -141,24 +141,18 @@ export class Prompter {
} }
async initExamples() { async initExamples() {
console.log('initializing examples...');
try { try {
this.convo_examples = new Examples(this.embedding_model); this.convo_examples = new Examples(this.embedding_model);
this.coding_examples = new Examples(this.embedding_model); this.coding_examples = new Examples(this.embedding_model);
const [convoResult, codingResult] = await Promise.allSettled([ // Wait for both examples to load before proceeding
await Promise.all([
this.convo_examples.load(this.profile.conversation_examples), this.convo_examples.load(this.profile.conversation_examples),
this.coding_examples.load(this.profile.coding_examples) this.coding_examples.load(this.profile.coding_examples)
]); ]);
// Handle potential failures console.log('done initializing examples.');
if (convoResult.status === 'rejected') {
console.error('Failed to load conversation examples:', convoResult.reason);
throw convoResult.reason;
}
if (codingResult.status === 'rejected') {
console.error('Failed to load coding examples:', codingResult.reason);
throw codingResult.reason;
}
} catch (error) { } catch (error) {
console.error('Failed to initialize examples:', error); console.error('Failed to initialize examples:', error);
throw error; throw error;

View file

@ -31,19 +31,26 @@ export class Examples {
async load(examples) { async load(examples) {
this.examples = examples; this.examples = examples;
if (!this.model) return; // Early return if no embedding model
try { try {
if (this.model !== null) { console.log('embedding examples...');
const embeddingPromises = this.examples.map(async (example) => { // Create array of promises first
let turn_text = this.turnsToText(example); const embeddingPromises = examples.map(example => {
this.embeddings[turn_text] = await this.model.embed(turn_text); const turn_text = this.turnsToText(example);
return this.model.embed(turn_text)
.then(embedding => {
this.embeddings[turn_text] = embedding;
}); });
});
// Wait for all embeddings to complete
await Promise.all(embeddingPromises); await Promise.all(embeddingPromises);
} console.log('done embedding examples.');
} catch (err) { } catch (err) {
console.warn('Error with embedding model, using word overlap instead.'); console.warn('Error with embedding model, using word overlap instead:', err);
this.model = null; this.model = null;
} }
} }
async getRelevant(turns) { async getRelevant(turns) {