Prompt Title: Convert await.all into synchronous loop

Created 1 year ago
Votes 0
Views 213 times
0

Prompt Detail:

Sure, here's the code converted to process each chunk sequentially:

vbnet
let chunkProcessingErrorOccurred = false; for (let i = 0; i < fileChunks.length; i++) { const chunk = fileChunks[i]; try { const indexingMessageData = await _intelligenceOpenAIChatComplete({ temperature: 0, model: OpenAIEngine.ChatGpt, messages: [ { role: "system", content: "You are IndexingAssistantBot. Your job is to provide helpful messages to the user that gives them insight into the types of things that the indexing system is learning as it processes chunks of data.", }, { role: "user", content: `The system is currently processing the following chunk of information:\n\n<chunk begin>\nFile Name: ${ file.title }\nChunk: ${i + 1} of ${fileChunks.length}\n${ chunk.text }<chunk end>\n Provide up to 3 short (9 - 10 words max) and witty indexing message to help the user understand what you're learning. Each message should begin with "Learning about" or "Accumulating knowledge" or "Neural ridges forming" and should be as specific as possible. Respond in an array of strings (e.g. [message1, message2, message3])`, }, ], }); const indexingProgressMessagesArray = JSON.parse( indexingMessageData?.data.choices[0].message?.content as string ); for (const message of indexingProgressMessagesArray) { await pusherClient.trigger( channelKey, PusherEvent.WorkspaceNotification, { workspaceId, entityId, notification: message, } ); } const result = await intelligenceModule.createEmbedding({ text: metadataHeader + chunk.text, // header included to aid vector search embeddingsModel: EmbeddingsModel.AdaV2, context, }); if (!result?.data) { continue; } const searchDocumentChunk = await kg.searchDocumentChunk.upsert({ where: { id: uuidModule.generate(), }, create: { associatedSearchDocument: searchDocument.id, body: chunk.text, associatedWorkspace: workspaceId, endIndex: chunk.end.toString(), startIndex: chunk.start.toString(), metadataHeader, }, }); await pineconeModule.upsert({ pineconeIndex: PineconeIndex.ExperimentalAda2Embeddings, vectors: [ { id: searchDocumentChunk.id, values: result.data, metadata: { scopeIds }, // we emulate kg scope system here and use the $in operator in the pinecone query to filter }, ], context, }); } catch (unknownErr) { const err = unknownErr as Error; logger.error(`[makeChunksFromFileAttachment] Indexing chunk failed `, { workspaceId, chunk, originalError: objectModule.safeErrorAsJson(err), }); chunkProcessingErrorOccurred = true; } } if (chunkProcessingErrorOccurred) { throw new Error( `At least one chunk failed to index for file ${entityId} in workspace ${workspaceId}` ); }

In this version, the for...of loop is used to process each message in the indexingProgressMessagesArray array sequentially. Similarly, the for loop is used to process each chunk of the file sequentially. The rest of the code remains largely the same.

avatar
Dennis Xu
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

0
0
0
0
TypeScript Code Analysis.
1 year ago 2023-03-23 22:13:32 Haidar Chikh
0
0
C++ Syllabus.
1 year ago 2023-04-07 16:19:44 Mahesh Rawat
0
0
Access Modifiers in TypeScript
1 year ago 2023-04-14 09:54:51 Dilshod
0
0
Using RxJS in Angular
1 year ago 2023-04-25 01:57:06 Vijayakumar
0
0
0
0
global.d.ts
1 month ago 2024-03-15 07:55:08 Sam