Yes, the fetchEmbedding function does guarantee order preservation. When you pass an array of strings to the openai.embeddings.create function, it returns an array of embeddings in the same order as the input array. This means that the first embedding in the response corresponds to the first text in the input array, the second embedding corresponds to the second text, and so on.
In your code, you're correctly associating each embedding with its corresponding text chunk. You're iterating over the response.data array and the args.chunks array in parallel, so each embedding is associated with the text chunk at the same index.
Here's the relevant part of your code:
for (let i = 0; i < response.data.length; i++) {
await ctx.runMutation(internal.chatbook.addEmbedding, {
chatId: args.chatId,
content: args.chunks[i],
embedding: response.data[i].embedding,
metadata: args.metadata,
});
}
In this loop, args.chunks[i] is the i-th text chunk and response.data[i].embedding is the corresponding embedding. These are passed to the internal.chatbook.addEmbedding mutation, which presumably adds the embedding to the database.
So, as long as the openai.embeddings.create function is working correctly and the internal.chatbook.addEmbedding mutation is correctly adding the embeddings to the database, your code should ensure that the correct embedding is associated with each text chunk.